Merge illumos-gate
[unleashed.git] / usr / src / cmd / zdb / zdb.c
blobfcfd3345de5a718309a561c41db357ad244d8653
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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.
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdio_ext.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <sys/zfs_context.h>
35 #include <sys/spa.h>
36 #include <sys/spa_impl.h>
37 #include <sys/dmu.h>
38 #include <sys/zap.h>
39 #include <sys/fs/zfs.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/zfs_sa.h>
42 #include <sys/sa.h>
43 #include <sys/sa_impl.h>
44 #include <sys/vdev.h>
45 #include <sys/vdev_impl.h>
46 #include <sys/metaslab_impl.h>
47 #include <sys/dmu_objset.h>
48 #include <sys/dsl_dir.h>
49 #include <sys/dsl_dataset.h>
50 #include <sys/dsl_pool.h>
51 #include <sys/dbuf.h>
52 #include <sys/zil.h>
53 #include <sys/zil_impl.h>
54 #include <sys/stat.h>
55 #include <sys/resource.h>
56 #include <sys/dmu_traverse.h>
57 #include <sys/zio_checksum.h>
58 #include <sys/zio_compress.h>
59 #include <sys/zfs_fuid.h>
60 #include <sys/arc.h>
61 #include <sys/ddt.h>
62 #include <sys/zfeature.h>
63 #include <sys/abd.h>
64 #include <sys/blkptr.h>
65 #include <sys/dsl_scan.h>
66 #include <zfs_comutil.h>
67 #include <libcmdutils.h>
68 #undef verify
69 #include <libzfs.h>
71 #include "zdb.h"
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)
86 extern int reference_tracking_enable;
87 extern boolean_t zfs_recover;
88 extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
89 extern int zfs_vdev_async_read_max_active;
90 extern int aok;
91 extern boolean_t spa_load_verify_dryrun;
93 static const char cmdname[] = "zdb";
94 uint8_t dump_opt[256];
96 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
98 uint64_t *zopt_object = NULL;
99 static unsigned zopt_objects = 0;
100 libzfs_handle_t *g_zfs;
101 uint64_t max_inflight = 1000;
102 static int leaked_objects = 0;
104 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
105 static void mos_obj_refd(uint64_t);
108 * These libumem hooks provide a reasonable set of defaults for the allocator's
109 * debugging facilities.
111 const char *
112 _umem_debug_init()
114 return ("default,verbose"); /* $UMEM_DEBUG setting */
117 const char *
118 _umem_logging_init(void)
120 return ("fail,contents"); /* $UMEM_LOGGING setting */
123 static void
124 usage(void)
126 (void) fprintf(stderr,
127 "Usage:\t%s [-AbcdDFGhikLMPsvX] [-e [-V] [-p <path> ...]] "
128 "[-I <inflight I/Os>]\n"
129 "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
130 "\t\t[<poolname> [<object> ...]]\n"
131 "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
132 "[<object> ...]\n"
133 "\t%s -C [-A] [-U <cache>]\n"
134 "\t%s -l [-Aqu] <device>\n"
135 "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
136 "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
137 "\t%s -O <dataset> <path>\n"
138 "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
139 "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
140 "\t%s -E [-A] word0:word1:...:word15\n"
141 "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
142 "<poolname>\n\n",
143 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
144 cmdname, cmdname);
146 (void) fprintf(stderr, " Dataset name must include at least one "
147 "separator character '/' or '@'\n");
148 (void) fprintf(stderr, " If dataset name is specified, only that "
149 "dataset is dumped\n");
150 (void) fprintf(stderr, " If object numbers are specified, only "
151 "those objects are dumped\n\n");
152 (void) fprintf(stderr, " Options to control amount of output:\n");
153 (void) fprintf(stderr, " -b block statistics\n");
154 (void) fprintf(stderr, " -c checksum all metadata (twice for "
155 "all data) blocks\n");
156 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
157 (void) fprintf(stderr, " -d dataset(s)\n");
158 (void) fprintf(stderr, " -D dedup statistics\n");
159 (void) fprintf(stderr, " -E decode and display block from an "
160 "embedded block pointer\n");
161 (void) fprintf(stderr, " -h pool history\n");
162 (void) fprintf(stderr, " -i intent logs\n");
163 (void) fprintf(stderr, " -l read label contents\n");
164 (void) fprintf(stderr, " -k examine the checkpointed state "
165 "of the pool\n");
166 (void) fprintf(stderr, " -L disable leak tracking (do not "
167 "load spacemaps)\n");
168 (void) fprintf(stderr, " -m metaslabs\n");
169 (void) fprintf(stderr, " -M metaslab groups\n");
170 (void) fprintf(stderr, " -O perform object lookups by path\n");
171 (void) fprintf(stderr, " -R read and display block from a "
172 "device\n");
173 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
174 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
175 (void) fprintf(stderr, " -v verbose (applies to all "
176 "others)\n\n");
177 (void) fprintf(stderr, " Below options are intended for use "
178 "with other options:\n");
179 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
180 "panic recovery (-AA) or both (-AAA)\n");
181 (void) fprintf(stderr, " -e pool is exported/destroyed/"
182 "has altroot/not in a cachefile\n");
183 (void) fprintf(stderr, " -F attempt automatic rewind within "
184 "safe range of transaction groups\n");
185 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before "
186 "exiting\n");
187 (void) fprintf(stderr, " -I <number of inflight I/Os> -- "
188 "specify the maximum number of "
189 "checksumming I/Os [default is 200]\n");
190 (void) fprintf(stderr, " -o <variable>=<value> set global "
191 "variable to an unsigned 32-bit integer value\n");
192 (void) fprintf(stderr, " -p <path> -- use one or more with "
193 "-e to specify path to vdev dir\n");
194 (void) fprintf(stderr, " -P print numbers in parseable form\n");
195 (void) fprintf(stderr, " -q don't print label contents\n");
196 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
197 "searching for uberblocks\n");
198 (void) fprintf(stderr, " -u uberblock\n");
199 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
200 "cachefile\n");
201 (void) fprintf(stderr, " -V do verbatim import\n");
202 (void) fprintf(stderr, " -x <dumpdir> -- "
203 "dump all read blocks into specified directory\n");
204 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
205 "work with dataset)\n\n");
206 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
207 "to make only that option verbose\n");
208 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
209 exit(1);
212 static void
213 dump_debug_buffer()
215 if (dump_opt['G']) {
216 (void) printf("\n");
217 zfs_dbgmsg_print("zdb");
222 * Called for usage errors that are discovered after a call to spa_open(),
223 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
226 static void
227 fatal(const char *fmt, ...)
229 va_list ap;
231 va_start(ap, fmt);
232 (void) fprintf(stderr, "%s: ", cmdname);
233 (void) vfprintf(stderr, fmt, ap);
234 va_end(ap);
235 (void) fprintf(stderr, "\n");
237 dump_debug_buffer();
239 exit(1);
242 /* ARGSUSED */
243 static void
244 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
246 nvlist_t *nv;
247 size_t nvsize = *(uint64_t *)data;
248 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
250 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
252 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
254 umem_free(packed, nvsize);
256 dump_nvlist(nv, 8);
258 nvlist_free(nv);
261 /* ARGSUSED */
262 static void
263 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
265 spa_history_phys_t *shp = data;
267 if (shp == NULL)
268 return;
270 (void) printf("\t\tpool_create_len = %llu\n",
271 (u_longlong_t)shp->sh_pool_create_len);
272 (void) printf("\t\tphys_max_off = %llu\n",
273 (u_longlong_t)shp->sh_phys_max_off);
274 (void) printf("\t\tbof = %llu\n",
275 (u_longlong_t)shp->sh_bof);
276 (void) printf("\t\teof = %llu\n",
277 (u_longlong_t)shp->sh_eof);
278 (void) printf("\t\trecords_lost = %llu\n",
279 (u_longlong_t)shp->sh_records_lost);
282 static void
283 zdb_nicenum(uint64_t num, char *buf, size_t buflen)
285 if (dump_opt['P'])
286 (void) snprintf(buf, buflen, "%llu", (longlong_t)num);
287 else
288 nicenum(num, buf, sizeof (buf));
291 static const char histo_stars[] = "****************************************";
292 static const uint64_t histo_width = sizeof (histo_stars) - 1;
294 static void
295 dump_histogram(const uint64_t *histo, int size, int offset)
297 int i;
298 int minidx = size - 1;
299 int maxidx = 0;
300 uint64_t max = 0;
302 for (i = 0; i < size; i++) {
303 if (histo[i] > max)
304 max = histo[i];
305 if (histo[i] > 0 && i > maxidx)
306 maxidx = i;
307 if (histo[i] > 0 && i < minidx)
308 minidx = i;
311 if (max < histo_width)
312 max = histo_width;
314 for (i = minidx; i <= maxidx; i++) {
315 (void) printf("\t\t\t%3u: %6llu %s\n",
316 i + offset, (u_longlong_t)histo[i],
317 &histo_stars[(max - histo[i]) * histo_width / max]);
321 static void
322 dump_zap_stats(objset_t *os, uint64_t object)
324 int error;
325 zap_stats_t zs;
327 error = zap_get_stats(os, object, &zs);
328 if (error)
329 return;
331 if (zs.zs_ptrtbl_len == 0) {
332 ASSERT(zs.zs_num_blocks == 1);
333 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
334 (u_longlong_t)zs.zs_blocksize,
335 (u_longlong_t)zs.zs_num_entries);
336 return;
339 (void) printf("\tFat ZAP stats:\n");
341 (void) printf("\t\tPointer table:\n");
342 (void) printf("\t\t\t%llu elements\n",
343 (u_longlong_t)zs.zs_ptrtbl_len);
344 (void) printf("\t\t\tzt_blk: %llu\n",
345 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
346 (void) printf("\t\t\tzt_numblks: %llu\n",
347 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
348 (void) printf("\t\t\tzt_shift: %llu\n",
349 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
350 (void) printf("\t\t\tzt_blks_copied: %llu\n",
351 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
352 (void) printf("\t\t\tzt_nextblk: %llu\n",
353 (u_longlong_t)zs.zs_ptrtbl_nextblk);
355 (void) printf("\t\tZAP entries: %llu\n",
356 (u_longlong_t)zs.zs_num_entries);
357 (void) printf("\t\tLeaf blocks: %llu\n",
358 (u_longlong_t)zs.zs_num_leafs);
359 (void) printf("\t\tTotal blocks: %llu\n",
360 (u_longlong_t)zs.zs_num_blocks);
361 (void) printf("\t\tzap_block_type: 0x%llx\n",
362 (u_longlong_t)zs.zs_block_type);
363 (void) printf("\t\tzap_magic: 0x%llx\n",
364 (u_longlong_t)zs.zs_magic);
365 (void) printf("\t\tzap_salt: 0x%llx\n",
366 (u_longlong_t)zs.zs_salt);
368 (void) printf("\t\tLeafs with 2^n pointers:\n");
369 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
371 (void) printf("\t\tBlocks with n*5 entries:\n");
372 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
374 (void) printf("\t\tBlocks n/10 full:\n");
375 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
377 (void) printf("\t\tEntries with n chunks:\n");
378 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
380 (void) printf("\t\tBuckets with n entries:\n");
381 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
384 /*ARGSUSED*/
385 static void
386 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
390 /*ARGSUSED*/
391 static void
392 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
394 (void) printf("\tUNKNOWN OBJECT TYPE\n");
397 /*ARGSUSED*/
398 static void
399 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
403 /*ARGSUSED*/
404 static void
405 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
409 /*ARGSUSED*/
410 static void
411 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
413 zap_cursor_t zc;
414 zap_attribute_t attr;
415 void *prop;
416 unsigned i;
418 dump_zap_stats(os, object);
419 (void) printf("\n");
421 for (zap_cursor_init(&zc, os, object);
422 zap_cursor_retrieve(&zc, &attr) == 0;
423 zap_cursor_advance(&zc)) {
424 (void) printf("\t\t%s = ", attr.za_name);
425 if (attr.za_num_integers == 0) {
426 (void) printf("\n");
427 continue;
429 prop = umem_zalloc(attr.za_num_integers *
430 attr.za_integer_length, UMEM_NOFAIL);
431 (void) zap_lookup(os, object, attr.za_name,
432 attr.za_integer_length, attr.za_num_integers, prop);
433 if (attr.za_integer_length == 1) {
434 (void) printf("%s", (char *)prop);
435 } else {
436 for (i = 0; i < attr.za_num_integers; i++) {
437 switch (attr.za_integer_length) {
438 case 2:
439 (void) printf("%u ",
440 ((uint16_t *)prop)[i]);
441 break;
442 case 4:
443 (void) printf("%u ",
444 ((uint32_t *)prop)[i]);
445 break;
446 case 8:
447 (void) printf("%lld ",
448 (u_longlong_t)((int64_t *)prop)[i]);
449 break;
453 (void) printf("\n");
454 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
456 zap_cursor_fini(&zc);
459 static void
460 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
462 bpobj_phys_t *bpop = data;
463 char bytes[32], comp[32], uncomp[32];
465 /* make sure the output won't get truncated */
466 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
467 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
468 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
470 if (bpop == NULL)
471 return;
473 zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
474 zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
475 zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
477 (void) printf("\t\tnum_blkptrs = %llu\n",
478 (u_longlong_t)bpop->bpo_num_blkptrs);
479 (void) printf("\t\tbytes = %s\n", bytes);
480 if (size >= BPOBJ_SIZE_V1) {
481 (void) printf("\t\tcomp = %s\n", comp);
482 (void) printf("\t\tuncomp = %s\n", uncomp);
484 if (size >= sizeof (*bpop)) {
485 (void) printf("\t\tsubobjs = %llu\n",
486 (u_longlong_t)bpop->bpo_subobjs);
487 (void) printf("\t\tnum_subobjs = %llu\n",
488 (u_longlong_t)bpop->bpo_num_subobjs);
491 if (dump_opt['d'] < 5)
492 return;
494 for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
495 char blkbuf[BP_SPRINTF_LEN];
496 blkptr_t bp;
498 int err = dmu_read(os, object,
499 i * sizeof (bp), sizeof (bp), &bp, 0);
500 if (err != 0) {
501 (void) printf("got error %u from dmu_read\n", err);
502 break;
504 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
505 (void) printf("\t%s\n", blkbuf);
509 /* ARGSUSED */
510 static void
511 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
513 dmu_object_info_t doi;
515 VERIFY0(dmu_object_info(os, object, &doi));
516 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
518 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
519 if (err != 0) {
520 (void) printf("got error %u from dmu_read\n", err);
521 kmem_free(subobjs, doi.doi_max_offset);
522 return;
525 int64_t last_nonzero = -1;
526 for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
527 if (subobjs[i] != 0)
528 last_nonzero = i;
531 for (int64_t i = 0; i <= last_nonzero; i++) {
532 (void) printf("\t%llu\n", (longlong_t)subobjs[i]);
534 kmem_free(subobjs, doi.doi_max_offset);
537 /*ARGSUSED*/
538 static void
539 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
541 dump_zap_stats(os, object);
542 /* contents are printed elsewhere, properly decoded */
545 /*ARGSUSED*/
546 static void
547 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
549 zap_cursor_t zc;
550 zap_attribute_t attr;
552 dump_zap_stats(os, object);
553 (void) printf("\n");
555 for (zap_cursor_init(&zc, os, object);
556 zap_cursor_retrieve(&zc, &attr) == 0;
557 zap_cursor_advance(&zc)) {
558 (void) printf("\t\t%s = ", attr.za_name);
559 if (attr.za_num_integers == 0) {
560 (void) printf("\n");
561 continue;
563 (void) printf(" %llx : [%d:%d:%d]\n",
564 (u_longlong_t)attr.za_first_integer,
565 (int)ATTR_LENGTH(attr.za_first_integer),
566 (int)ATTR_BSWAP(attr.za_first_integer),
567 (int)ATTR_NUM(attr.za_first_integer));
569 zap_cursor_fini(&zc);
572 /*ARGSUSED*/
573 static void
574 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
576 zap_cursor_t zc;
577 zap_attribute_t attr;
578 uint16_t *layout_attrs;
579 unsigned i;
581 dump_zap_stats(os, object);
582 (void) printf("\n");
584 for (zap_cursor_init(&zc, os, object);
585 zap_cursor_retrieve(&zc, &attr) == 0;
586 zap_cursor_advance(&zc)) {
587 (void) printf("\t\t%s = [", attr.za_name);
588 if (attr.za_num_integers == 0) {
589 (void) printf("\n");
590 continue;
593 VERIFY(attr.za_integer_length == 2);
594 layout_attrs = umem_zalloc(attr.za_num_integers *
595 attr.za_integer_length, UMEM_NOFAIL);
597 VERIFY(zap_lookup(os, object, attr.za_name,
598 attr.za_integer_length,
599 attr.za_num_integers, layout_attrs) == 0);
601 for (i = 0; i != attr.za_num_integers; i++)
602 (void) printf(" %d ", (int)layout_attrs[i]);
603 (void) printf("]\n");
604 umem_free(layout_attrs,
605 attr.za_num_integers * attr.za_integer_length);
607 zap_cursor_fini(&zc);
610 /*ARGSUSED*/
611 static void
612 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
614 zap_cursor_t zc;
615 zap_attribute_t attr;
616 const char *typenames[] = {
617 /* 0 */ "not specified",
618 /* 1 */ "FIFO",
619 /* 2 */ "Character Device",
620 /* 3 */ "3 (invalid)",
621 /* 4 */ "Directory",
622 /* 5 */ "5 (invalid)",
623 /* 6 */ "Block Device",
624 /* 7 */ "7 (invalid)",
625 /* 8 */ "Regular File",
626 /* 9 */ "9 (invalid)",
627 /* 10 */ "Symbolic Link",
628 /* 11 */ "11 (invalid)",
629 /* 12 */ "Socket",
630 /* 13 */ "Door",
631 /* 14 */ "Event Port",
632 /* 15 */ "15 (invalid)",
635 dump_zap_stats(os, object);
636 (void) printf("\n");
638 for (zap_cursor_init(&zc, os, object);
639 zap_cursor_retrieve(&zc, &attr) == 0;
640 zap_cursor_advance(&zc)) {
641 (void) printf("\t\t%s = %lld (type: %s)\n",
642 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
643 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
645 zap_cursor_fini(&zc);
648 static int
649 get_dtl_refcount(vdev_t *vd)
651 int refcount = 0;
653 if (vd->vdev_ops->vdev_op_leaf) {
654 space_map_t *sm = vd->vdev_dtl_sm;
656 if (sm != NULL &&
657 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
658 return (1);
659 return (0);
662 for (unsigned c = 0; c < vd->vdev_children; c++)
663 refcount += get_dtl_refcount(vd->vdev_child[c]);
664 return (refcount);
667 static int
668 get_metaslab_refcount(vdev_t *vd)
670 int refcount = 0;
672 if (vd->vdev_top == vd) {
673 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
674 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
676 if (sm != NULL &&
677 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
678 refcount++;
681 for (unsigned c = 0; c < vd->vdev_children; c++)
682 refcount += get_metaslab_refcount(vd->vdev_child[c]);
684 return (refcount);
687 static int
688 get_obsolete_refcount(vdev_t *vd)
690 int refcount = 0;
692 uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd);
693 if (vd->vdev_top == vd && obsolete_sm_obj != 0) {
694 dmu_object_info_t doi;
695 VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
696 obsolete_sm_obj, &doi));
697 if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
698 refcount++;
700 } else {
701 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
702 ASSERT3U(obsolete_sm_obj, ==, 0);
704 for (unsigned c = 0; c < vd->vdev_children; c++) {
705 refcount += get_obsolete_refcount(vd->vdev_child[c]);
708 return (refcount);
711 static int
712 get_prev_obsolete_spacemap_refcount(spa_t *spa)
714 uint64_t prev_obj =
715 spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
716 if (prev_obj != 0) {
717 dmu_object_info_t doi;
718 VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
719 if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
720 return (1);
723 return (0);
726 static int
727 get_checkpoint_refcount(vdev_t *vd)
729 int refcount = 0;
731 if (vd->vdev_top == vd && vd->vdev_top_zap != 0 &&
732 zap_contains(spa_meta_objset(vd->vdev_spa),
733 vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0)
734 refcount++;
736 for (uint64_t c = 0; c < vd->vdev_children; c++)
737 refcount += get_checkpoint_refcount(vd->vdev_child[c]);
739 return (refcount);
742 static int
743 verify_spacemap_refcounts(spa_t *spa)
745 uint64_t expected_refcount = 0;
746 uint64_t actual_refcount;
748 (void) feature_get_refcount(spa,
749 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
750 &expected_refcount);
751 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
752 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
753 actual_refcount += get_obsolete_refcount(spa->spa_root_vdev);
754 actual_refcount += get_prev_obsolete_spacemap_refcount(spa);
755 actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev);
757 if (expected_refcount != actual_refcount) {
758 (void) printf("space map refcount mismatch: expected %lld != "
759 "actual %lld\n",
760 (longlong_t)expected_refcount,
761 (longlong_t)actual_refcount);
762 return (2);
764 return (0);
767 static void
768 dump_spacemap(objset_t *os, space_map_t *sm)
770 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
771 "INVALID", "INVALID", "INVALID", "INVALID" };
773 if (sm == NULL)
774 return;
776 (void) printf("space map object %llu:\n",
777 (longlong_t)sm->sm_phys->smp_object);
778 (void) printf(" smp_objsize = 0x%llx\n",
779 (longlong_t)sm->sm_phys->smp_objsize);
780 (void) printf(" smp_alloc = 0x%llx\n",
781 (longlong_t)sm->sm_phys->smp_alloc);
784 * Print out the freelist entries in both encoded and decoded form.
786 uint8_t mapshift = sm->sm_shift;
787 int64_t alloc = 0;
788 uint64_t word;
789 for (uint64_t offset = 0; offset < space_map_length(sm);
790 offset += sizeof (word)) {
792 VERIFY0(dmu_read(os, space_map_object(sm), offset,
793 sizeof (word), &word, DMU_READ_PREFETCH));
795 if (sm_entry_is_debug(word)) {
796 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
797 (u_longlong_t)(offset / sizeof (word)),
798 ddata[SM_DEBUG_ACTION_DECODE(word)],
799 (u_longlong_t)SM_DEBUG_TXG_DECODE(word),
800 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(word));
801 continue;
804 uint8_t words;
805 char entry_type;
806 uint64_t entry_off, entry_run, entry_vdev = SM_NO_VDEVID;
808 if (sm_entry_is_single_word(word)) {
809 entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ?
810 'A' : 'F';
811 entry_off = (SM_OFFSET_DECODE(word) << mapshift) +
812 sm->sm_start;
813 entry_run = SM_RUN_DECODE(word) << mapshift;
814 words = 1;
815 } else {
816 /* it is a two-word entry so we read another word */
817 ASSERT(sm_entry_is_double_word(word));
819 uint64_t extra_word;
820 offset += sizeof (extra_word);
821 VERIFY0(dmu_read(os, space_map_object(sm), offset,
822 sizeof (extra_word), &extra_word,
823 DMU_READ_PREFETCH));
825 ASSERT3U(offset, <=, space_map_length(sm));
827 entry_run = SM2_RUN_DECODE(word) << mapshift;
828 entry_vdev = SM2_VDEV_DECODE(word);
829 entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ?
830 'A' : 'F';
831 entry_off = (SM2_OFFSET_DECODE(extra_word) <<
832 mapshift) + sm->sm_start;
833 words = 2;
836 (void) printf("\t [%6llu] %c range:"
837 " %010llx-%010llx size: %06llx vdev: %06llu words: %u\n",
838 (u_longlong_t)(offset / sizeof (word)),
839 entry_type, (u_longlong_t)entry_off,
840 (u_longlong_t)(entry_off + entry_run),
841 (u_longlong_t)entry_run,
842 (u_longlong_t)entry_vdev, words);
844 if (entry_type == 'A')
845 alloc += entry_run;
846 else
847 alloc -= entry_run;
849 if ((uint64_t)alloc != space_map_allocated(sm)) {
850 (void) printf("space_map_object alloc (%lld) INCONSISTENT "
851 "with space map summary (%lld)\n",
852 (longlong_t)space_map_allocated(sm), (longlong_t)alloc);
856 static void
857 dump_metaslab_stats(metaslab_t *msp)
859 char maxbuf[32];
860 range_tree_t *rt = msp->ms_allocatable;
861 avl_tree_t *t = &msp->ms_allocatable_by_size;
862 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
864 /* max sure nicenum has enough space */
865 CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
867 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
869 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
870 "segments", avl_numnodes(t), "maxsize", maxbuf,
871 "freepct", free_pct);
872 (void) printf("\tIn-memory histogram:\n");
873 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
876 static void
877 dump_metaslab(metaslab_t *msp)
879 vdev_t *vd = msp->ms_group->mg_vd;
880 spa_t *spa = vd->vdev_spa;
881 space_map_t *sm = msp->ms_sm;
882 char freebuf[32];
884 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
885 sizeof (freebuf));
887 (void) printf(
888 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
889 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
890 (u_longlong_t)space_map_object(sm), freebuf);
892 if (dump_opt['m'] > 2 && !dump_opt['L']) {
893 mutex_enter(&msp->ms_lock);
894 metaslab_load_wait(msp);
895 if (!msp->ms_loaded) {
896 VERIFY0(metaslab_load(msp));
897 range_tree_stat_verify(msp->ms_allocatable);
899 dump_metaslab_stats(msp);
900 metaslab_unload(msp);
901 mutex_exit(&msp->ms_lock);
904 if (dump_opt['m'] > 1 && sm != NULL &&
905 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
907 * The space map histogram represents free space in chunks
908 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
910 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
911 (u_longlong_t)msp->ms_fragmentation);
912 dump_histogram(sm->sm_phys->smp_histogram,
913 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
916 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
917 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
919 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
923 static void
924 print_vdev_metaslab_header(vdev_t *vd)
926 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
927 (u_longlong_t)vd->vdev_id,
928 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
929 "offset", "spacemap", "free");
930 (void) printf("\t%15s %19s %15s %10s\n",
931 "---------------", "-------------------",
932 "---------------", "-------------");
935 static void
936 dump_metaslab_groups(spa_t *spa)
938 vdev_t *rvd = spa->spa_root_vdev;
939 metaslab_class_t *mc = spa_normal_class(spa);
940 uint64_t fragmentation;
942 metaslab_class_histogram_verify(mc);
944 for (unsigned c = 0; c < rvd->vdev_children; c++) {
945 vdev_t *tvd = rvd->vdev_child[c];
946 metaslab_group_t *mg = tvd->vdev_mg;
948 if (mg->mg_class != mc)
949 continue;
951 metaslab_group_histogram_verify(mg);
952 mg->mg_fragmentation = metaslab_group_fragmentation(mg);
954 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
955 "fragmentation",
956 (u_longlong_t)tvd->vdev_id,
957 (u_longlong_t)tvd->vdev_ms_count);
958 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
959 (void) printf("%3s\n", "-");
960 } else {
961 (void) printf("%3llu%%\n",
962 (u_longlong_t)mg->mg_fragmentation);
964 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
967 (void) printf("\tpool %s\tfragmentation", spa_name(spa));
968 fragmentation = metaslab_class_fragmentation(mc);
969 if (fragmentation == ZFS_FRAG_INVALID)
970 (void) printf("\t%3s\n", "-");
971 else
972 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
973 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
976 static void
977 print_vdev_indirect(vdev_t *vd)
979 vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
980 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
981 vdev_indirect_births_t *vib = vd->vdev_indirect_births;
983 if (vim == NULL) {
984 ASSERT3P(vib, ==, NULL);
985 return;
988 ASSERT3U(vdev_indirect_mapping_object(vim), ==,
989 vic->vic_mapping_object);
990 ASSERT3U(vdev_indirect_births_object(vib), ==,
991 vic->vic_births_object);
993 (void) printf("indirect births obj %llu:\n",
994 (longlong_t)vic->vic_births_object);
995 (void) printf(" vib_count = %llu\n",
996 (longlong_t)vdev_indirect_births_count(vib));
997 for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) {
998 vdev_indirect_birth_entry_phys_t *cur_vibe =
999 &vib->vib_entries[i];
1000 (void) printf("\toffset %llx -> txg %llu\n",
1001 (longlong_t)cur_vibe->vibe_offset,
1002 (longlong_t)cur_vibe->vibe_phys_birth_txg);
1004 (void) printf("\n");
1006 (void) printf("indirect mapping obj %llu:\n",
1007 (longlong_t)vic->vic_mapping_object);
1008 (void) printf(" vim_max_offset = 0x%llx\n",
1009 (longlong_t)vdev_indirect_mapping_max_offset(vim));
1010 (void) printf(" vim_bytes_mapped = 0x%llx\n",
1011 (longlong_t)vdev_indirect_mapping_bytes_mapped(vim));
1012 (void) printf(" vim_count = %llu\n",
1013 (longlong_t)vdev_indirect_mapping_num_entries(vim));
1015 if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3)
1016 return;
1018 uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim);
1020 for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
1021 vdev_indirect_mapping_entry_phys_t *vimep =
1022 &vim->vim_entries[i];
1023 (void) printf("\t<%llx:%llx:%llx> -> "
1024 "<%llx:%llx:%llx> (%x obsolete)\n",
1025 (longlong_t)vd->vdev_id,
1026 (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
1027 (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
1028 (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst),
1029 (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst),
1030 (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
1031 counts[i]);
1033 (void) printf("\n");
1035 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
1036 if (obsolete_sm_object != 0) {
1037 objset_t *mos = vd->vdev_spa->spa_meta_objset;
1038 (void) printf("obsolete space map object %llu:\n",
1039 (u_longlong_t)obsolete_sm_object);
1040 ASSERT(vd->vdev_obsolete_sm != NULL);
1041 ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==,
1042 obsolete_sm_object);
1043 dump_spacemap(mos, vd->vdev_obsolete_sm);
1044 (void) printf("\n");
1048 static void
1049 dump_metaslabs(spa_t *spa)
1051 vdev_t *vd, *rvd = spa->spa_root_vdev;
1052 uint64_t m, c = 0, children = rvd->vdev_children;
1054 (void) printf("\nMetaslabs:\n");
1056 if (!dump_opt['d'] && zopt_objects > 0) {
1057 c = zopt_object[0];
1059 if (c >= children)
1060 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
1062 if (zopt_objects > 1) {
1063 vd = rvd->vdev_child[c];
1064 print_vdev_metaslab_header(vd);
1066 for (m = 1; m < zopt_objects; m++) {
1067 if (zopt_object[m] < vd->vdev_ms_count)
1068 dump_metaslab(
1069 vd->vdev_ms[zopt_object[m]]);
1070 else
1071 (void) fprintf(stderr, "bad metaslab "
1072 "number %llu\n",
1073 (u_longlong_t)zopt_object[m]);
1075 (void) printf("\n");
1076 return;
1078 children = c + 1;
1080 for (; c < children; c++) {
1081 vd = rvd->vdev_child[c];
1082 print_vdev_metaslab_header(vd);
1084 print_vdev_indirect(vd);
1086 for (m = 0; m < vd->vdev_ms_count; m++)
1087 dump_metaslab(vd->vdev_ms[m]);
1088 (void) printf("\n");
1092 static void
1093 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
1095 const ddt_phys_t *ddp = dde->dde_phys;
1096 const ddt_key_t *ddk = &dde->dde_key;
1097 const char *types[4] = { "ditto", "single", "double", "triple" };
1098 char blkbuf[BP_SPRINTF_LEN];
1099 blkptr_t blk;
1101 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1102 if (ddp->ddp_phys_birth == 0)
1103 continue;
1104 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
1105 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
1106 (void) printf("index %llx refcnt %llu %s %s\n",
1107 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
1108 types[p], blkbuf);
1112 static void
1113 dump_dedup_ratio(const ddt_stat_t *dds)
1115 double rL, rP, rD, D, dedup, compress, copies;
1117 if (dds->dds_blocks == 0)
1118 return;
1120 rL = (double)dds->dds_ref_lsize;
1121 rP = (double)dds->dds_ref_psize;
1122 rD = (double)dds->dds_ref_dsize;
1123 D = (double)dds->dds_dsize;
1125 dedup = rD / D;
1126 compress = rL / rP;
1127 copies = rD / rP;
1129 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
1130 "dedup * compress / copies = %.2f\n\n",
1131 dedup, compress, copies, dedup * compress / copies);
1134 static void
1135 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
1137 char name[DDT_NAMELEN];
1138 ddt_entry_t dde;
1139 uint64_t walk = 0;
1140 dmu_object_info_t doi;
1141 uint64_t count, dspace, mspace;
1142 int error;
1144 error = ddt_object_info(ddt, type, class, &doi);
1146 if (error == ENOENT)
1147 return;
1148 ASSERT(error == 0);
1150 if ((count = ddt_object_count(ddt, type, class)) == 0)
1151 return;
1153 dspace = doi.doi_physical_blocks_512 << 9;
1154 mspace = doi.doi_fill_count * doi.doi_data_block_size;
1156 ddt_object_name(ddt, type, class, name);
1158 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
1159 name,
1160 (u_longlong_t)count,
1161 (u_longlong_t)(dspace / count),
1162 (u_longlong_t)(mspace / count));
1164 if (dump_opt['D'] < 3)
1165 return;
1167 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
1169 if (dump_opt['D'] < 4)
1170 return;
1172 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1173 return;
1175 (void) printf("%s contents:\n\n", name);
1177 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1178 dump_dde(ddt, &dde, walk);
1180 ASSERT3U(error, ==, ENOENT);
1182 (void) printf("\n");
1185 static void
1186 dump_all_ddts(spa_t *spa)
1188 ddt_histogram_t ddh_total;
1189 ddt_stat_t dds_total;
1191 bzero(&ddh_total, sizeof (ddh_total));
1192 bzero(&dds_total, sizeof (dds_total));
1194 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1195 ddt_t *ddt = spa->spa_ddt[c];
1196 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1197 for (enum ddt_class class = 0; class < DDT_CLASSES;
1198 class++) {
1199 dump_ddt(ddt, type, class);
1204 ddt_get_dedup_stats(spa, &dds_total);
1206 if (dds_total.dds_blocks == 0) {
1207 (void) printf("All DDTs are empty\n");
1208 return;
1211 (void) printf("\n");
1213 if (dump_opt['D'] > 1) {
1214 (void) printf("DDT histogram (aggregated over all DDTs):\n");
1215 ddt_get_dedup_histogram(spa, &ddh_total);
1216 zpool_dump_ddt(&dds_total, &ddh_total);
1219 dump_dedup_ratio(&dds_total);
1222 static void
1223 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
1225 char *prefix = arg;
1227 (void) printf("%s [%llu,%llu) length %llu\n",
1228 prefix,
1229 (u_longlong_t)start,
1230 (u_longlong_t)(start + size),
1231 (u_longlong_t)(size));
1234 static void
1235 dump_dtl(vdev_t *vd, int indent)
1237 spa_t *spa = vd->vdev_spa;
1238 boolean_t required;
1239 const char *name[DTL_TYPES] = { "missing", "partial", "scrub",
1240 "outage" };
1241 char prefix[256];
1243 spa_vdev_state_enter(spa, SCL_NONE);
1244 required = vdev_dtl_required(vd);
1245 (void) spa_vdev_state_exit(spa, NULL, 0);
1247 if (indent == 0)
1248 (void) printf("\nDirty time logs:\n\n");
1250 (void) printf("\t%*s%s [%s]\n", indent, "",
1251 vd->vdev_path ? vd->vdev_path :
1252 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
1253 required ? "DTL-required" : "DTL-expendable");
1255 for (int t = 0; t < DTL_TYPES; t++) {
1256 range_tree_t *rt = vd->vdev_dtl[t];
1257 if (range_tree_space(rt) == 0)
1258 continue;
1259 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
1260 indent + 2, "", name[t]);
1261 range_tree_walk(rt, dump_dtl_seg, prefix);
1262 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
1263 dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1266 for (unsigned c = 0; c < vd->vdev_children; c++)
1267 dump_dtl(vd->vdev_child[c], indent + 4);
1270 static void
1271 dump_history(spa_t *spa)
1273 nvlist_t **events = NULL;
1274 uint64_t resid, len, off = 0;
1275 uint_t num = 0;
1276 int error;
1277 time_t tsec;
1278 struct tm t;
1279 char tbuf[30];
1280 char internalstr[MAXPATHLEN];
1282 char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
1283 do {
1284 len = SPA_MAXBLOCKSIZE;
1286 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
1287 (void) fprintf(stderr, "Unable to read history: "
1288 "error %d\n", error);
1289 umem_free(buf, SPA_MAXBLOCKSIZE);
1290 return;
1293 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
1294 break;
1296 off -= resid;
1297 } while (len != 0);
1298 umem_free(buf, SPA_MAXBLOCKSIZE);
1300 (void) printf("\nHistory:\n");
1301 for (unsigned i = 0; i < num; i++) {
1302 uint64_t time, txg, ievent;
1303 char *cmd, *intstr;
1304 boolean_t printed = B_FALSE;
1306 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
1307 &time) != 0)
1308 goto next;
1309 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
1310 &cmd) != 0) {
1311 if (nvlist_lookup_uint64(events[i],
1312 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
1313 goto next;
1314 verify(nvlist_lookup_uint64(events[i],
1315 ZPOOL_HIST_TXG, &txg) == 0);
1316 verify(nvlist_lookup_string(events[i],
1317 ZPOOL_HIST_INT_STR, &intstr) == 0);
1318 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1319 goto next;
1321 (void) snprintf(internalstr,
1322 sizeof (internalstr),
1323 "[internal %s txg:%ju] %s",
1324 zfs_history_event_names[ievent], (uintmax_t)txg,
1325 intstr);
1326 cmd = internalstr;
1328 tsec = time;
1329 (void) localtime_r(&tsec, &t);
1330 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1331 (void) printf("%s %s\n", tbuf, cmd);
1332 printed = B_TRUE;
1334 next:
1335 if (dump_opt['h'] > 1) {
1336 if (!printed)
1337 (void) printf("unrecognized record:\n");
1338 dump_nvlist(events[i], 2);
1343 /*ARGSUSED*/
1344 static void
1345 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1349 static uint64_t
1350 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
1351 const zbookmark_phys_t *zb)
1353 if (dnp == NULL) {
1354 ASSERT(zb->zb_level < 0);
1355 if (zb->zb_object == 0)
1356 return (zb->zb_blkid);
1357 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1360 ASSERT(zb->zb_level >= 0);
1362 return ((zb->zb_blkid <<
1363 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1364 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1367 static void
1368 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1370 const dva_t *dva = bp->blk_dva;
1371 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1373 if (dump_opt['b'] >= 6) {
1374 snprintf_blkptr(blkbuf, buflen, bp);
1375 return;
1378 if (BP_IS_EMBEDDED(bp)) {
1379 (void) sprintf(blkbuf,
1380 "EMBEDDED et=%u %llxL/%llxP B=%llu",
1381 (int)BPE_GET_ETYPE(bp),
1382 (u_longlong_t)BPE_GET_LSIZE(bp),
1383 (u_longlong_t)BPE_GET_PSIZE(bp),
1384 (u_longlong_t)bp->blk_birth);
1385 return;
1388 blkbuf[0] = '\0';
1389 for (int i = 0; i < ndvas; i++)
1390 (void) snprintf(blkbuf + strlen(blkbuf),
1391 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1392 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1393 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1394 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1396 if (BP_IS_HOLE(bp)) {
1397 (void) snprintf(blkbuf + strlen(blkbuf),
1398 buflen - strlen(blkbuf),
1399 "%llxL B=%llu",
1400 (u_longlong_t)BP_GET_LSIZE(bp),
1401 (u_longlong_t)bp->blk_birth);
1402 } else {
1403 (void) snprintf(blkbuf + strlen(blkbuf),
1404 buflen - strlen(blkbuf),
1405 "%llxL/%llxP F=%llu B=%llu/%llu",
1406 (u_longlong_t)BP_GET_LSIZE(bp),
1407 (u_longlong_t)BP_GET_PSIZE(bp),
1408 (u_longlong_t)BP_GET_FILL(bp),
1409 (u_longlong_t)bp->blk_birth,
1410 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1414 static void
1415 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
1416 const dnode_phys_t *dnp)
1418 char blkbuf[BP_SPRINTF_LEN];
1419 int l;
1421 if (!BP_IS_EMBEDDED(bp)) {
1422 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1423 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1426 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1428 ASSERT(zb->zb_level >= 0);
1430 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1431 if (l == zb->zb_level) {
1432 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1433 } else {
1434 (void) printf(" ");
1438 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1439 (void) printf("%s\n", blkbuf);
1442 static int
1443 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1444 blkptr_t *bp, const zbookmark_phys_t *zb)
1446 int err = 0;
1448 if (bp->blk_birth == 0)
1449 return (0);
1451 print_indirect(bp, zb, dnp);
1453 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1454 arc_flags_t flags = ARC_FLAG_WAIT;
1455 int i;
1456 blkptr_t *cbp;
1457 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1458 arc_buf_t *buf;
1459 uint64_t fill = 0;
1461 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1462 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1463 if (err)
1464 return (err);
1465 ASSERT(buf->b_data);
1467 /* recursively visit blocks below this */
1468 cbp = buf->b_data;
1469 for (i = 0; i < epb; i++, cbp++) {
1470 zbookmark_phys_t czb;
1472 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1473 zb->zb_level - 1,
1474 zb->zb_blkid * epb + i);
1475 err = visit_indirect(spa, dnp, cbp, &czb);
1476 if (err)
1477 break;
1478 fill += BP_GET_FILL(cbp);
1480 if (!err)
1481 ASSERT3U(fill, ==, BP_GET_FILL(bp));
1482 arc_buf_destroy(buf, &buf);
1485 return (err);
1488 /*ARGSUSED*/
1489 static void
1490 dump_indirect(dnode_t *dn)
1492 dnode_phys_t *dnp = dn->dn_phys;
1493 int j;
1494 zbookmark_phys_t czb;
1496 (void) printf("Indirect blocks:\n");
1498 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1499 dn->dn_object, dnp->dn_nlevels - 1, 0);
1500 for (j = 0; j < dnp->dn_nblkptr; j++) {
1501 czb.zb_blkid = j;
1502 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1503 &dnp->dn_blkptr[j], &czb);
1506 (void) printf("\n");
1509 /*ARGSUSED*/
1510 static void
1511 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1513 dsl_dir_phys_t *dd = data;
1514 time_t crtime;
1515 char nice[32];
1517 /* make sure nicenum has enough space */
1518 CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
1520 if (dd == NULL)
1521 return;
1523 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1525 crtime = dd->dd_creation_time;
1526 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1527 (void) printf("\t\thead_dataset_obj = %llu\n",
1528 (u_longlong_t)dd->dd_head_dataset_obj);
1529 (void) printf("\t\tparent_dir_obj = %llu\n",
1530 (u_longlong_t)dd->dd_parent_obj);
1531 (void) printf("\t\torigin_obj = %llu\n",
1532 (u_longlong_t)dd->dd_origin_obj);
1533 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1534 (u_longlong_t)dd->dd_child_dir_zapobj);
1535 zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
1536 (void) printf("\t\tused_bytes = %s\n", nice);
1537 zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
1538 (void) printf("\t\tcompressed_bytes = %s\n", nice);
1539 zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
1540 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
1541 zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
1542 (void) printf("\t\tquota = %s\n", nice);
1543 zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
1544 (void) printf("\t\treserved = %s\n", nice);
1545 (void) printf("\t\tprops_zapobj = %llu\n",
1546 (u_longlong_t)dd->dd_props_zapobj);
1547 (void) printf("\t\tdeleg_zapobj = %llu\n",
1548 (u_longlong_t)dd->dd_deleg_zapobj);
1549 (void) printf("\t\tflags = %llx\n",
1550 (u_longlong_t)dd->dd_flags);
1552 #define DO(which) \
1553 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
1554 sizeof (nice)); \
1555 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1556 DO(HEAD);
1557 DO(SNAP);
1558 DO(CHILD);
1559 DO(CHILD_RSRV);
1560 DO(REFRSRV);
1561 #undef DO
1562 (void) printf("\t\tclones = %llu\n",
1563 (u_longlong_t)dd->dd_clones);
1566 /*ARGSUSED*/
1567 static void
1568 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1570 dsl_dataset_phys_t *ds = data;
1571 time_t crtime;
1572 char used[32], compressed[32], uncompressed[32], unique[32];
1573 char blkbuf[BP_SPRINTF_LEN];
1575 /* make sure nicenum has enough space */
1576 CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
1577 CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
1578 CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
1579 CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
1581 if (ds == NULL)
1582 return;
1584 ASSERT(size == sizeof (*ds));
1585 crtime = ds->ds_creation_time;
1586 zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
1587 zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
1588 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
1589 sizeof (uncompressed));
1590 zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
1591 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1593 (void) printf("\t\tdir_obj = %llu\n",
1594 (u_longlong_t)ds->ds_dir_obj);
1595 (void) printf("\t\tprev_snap_obj = %llu\n",
1596 (u_longlong_t)ds->ds_prev_snap_obj);
1597 (void) printf("\t\tprev_snap_txg = %llu\n",
1598 (u_longlong_t)ds->ds_prev_snap_txg);
1599 (void) printf("\t\tnext_snap_obj = %llu\n",
1600 (u_longlong_t)ds->ds_next_snap_obj);
1601 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1602 (u_longlong_t)ds->ds_snapnames_zapobj);
1603 (void) printf("\t\tnum_children = %llu\n",
1604 (u_longlong_t)ds->ds_num_children);
1605 (void) printf("\t\tuserrefs_obj = %llu\n",
1606 (u_longlong_t)ds->ds_userrefs_obj);
1607 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1608 (void) printf("\t\tcreation_txg = %llu\n",
1609 (u_longlong_t)ds->ds_creation_txg);
1610 (void) printf("\t\tdeadlist_obj = %llu\n",
1611 (u_longlong_t)ds->ds_deadlist_obj);
1612 (void) printf("\t\tused_bytes = %s\n", used);
1613 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1614 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1615 (void) printf("\t\tunique = %s\n", unique);
1616 (void) printf("\t\tfsid_guid = %llu\n",
1617 (u_longlong_t)ds->ds_fsid_guid);
1618 (void) printf("\t\tguid = %llu\n",
1619 (u_longlong_t)ds->ds_guid);
1620 (void) printf("\t\tflags = %llx\n",
1621 (u_longlong_t)ds->ds_flags);
1622 (void) printf("\t\tnext_clones_obj = %llu\n",
1623 (u_longlong_t)ds->ds_next_clones_obj);
1624 (void) printf("\t\tprops_obj = %llu\n",
1625 (u_longlong_t)ds->ds_props_obj);
1626 (void) printf("\t\tbp = %s\n", blkbuf);
1629 /* ARGSUSED */
1630 static int
1631 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1633 char blkbuf[BP_SPRINTF_LEN];
1635 if (bp->blk_birth != 0) {
1636 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1637 (void) printf("\t%s\n", blkbuf);
1639 return (0);
1642 static void
1643 dump_bptree(objset_t *os, uint64_t obj, const char *name)
1645 char bytes[32];
1646 bptree_phys_t *bt;
1647 dmu_buf_t *db;
1649 /* make sure nicenum has enough space */
1650 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1652 if (dump_opt['d'] < 3)
1653 return;
1655 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1656 bt = db->db_data;
1657 zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1658 (void) printf("\n %s: %llu datasets, %s\n",
1659 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1660 dmu_buf_rele(db, FTAG);
1662 if (dump_opt['d'] < 5)
1663 return;
1665 (void) printf("\n");
1667 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1670 /* ARGSUSED */
1671 static int
1672 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1674 char blkbuf[BP_SPRINTF_LEN];
1676 ASSERT(bp->blk_birth != 0);
1677 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1678 (void) printf("\t%s\n", blkbuf);
1679 return (0);
1682 static void
1683 dump_full_bpobj(bpobj_t *bpo, const char *name, int indent)
1685 char bytes[32];
1686 char comp[32];
1687 char uncomp[32];
1689 /* make sure nicenum has enough space */
1690 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1691 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1692 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1694 if (dump_opt['d'] < 3)
1695 return;
1697 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1698 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1699 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
1700 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1701 (void) printf(" %*s: object %llu, %llu local blkptrs, "
1702 "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1703 indent * 8, name,
1704 (u_longlong_t)bpo->bpo_object,
1705 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1706 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1707 (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
1708 bytes, comp, uncomp);
1710 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1711 uint64_t subobj;
1712 bpobj_t subbpo;
1713 int error;
1714 VERIFY0(dmu_read(bpo->bpo_os,
1715 bpo->bpo_phys->bpo_subobjs,
1716 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1717 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1718 if (error != 0) {
1719 (void) printf("ERROR %u while trying to open "
1720 "subobj id %llu\n",
1721 error, (u_longlong_t)subobj);
1722 continue;
1724 dump_full_bpobj(&subbpo, "subobj", indent + 1);
1725 bpobj_close(&subbpo);
1727 } else {
1728 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1729 indent * 8, name,
1730 (u_longlong_t)bpo->bpo_object,
1731 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1732 bytes);
1735 if (dump_opt['d'] < 5)
1736 return;
1739 if (indent == 0) {
1740 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1741 (void) printf("\n");
1745 static void
1746 bpobj_count_refd(bpobj_t *bpo)
1748 mos_obj_refd(bpo->bpo_object);
1750 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1751 mos_obj_refd(bpo->bpo_phys->bpo_subobjs);
1752 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1753 uint64_t subobj;
1754 bpobj_t subbpo;
1755 int error;
1756 VERIFY0(dmu_read(bpo->bpo_os,
1757 bpo->bpo_phys->bpo_subobjs,
1758 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1759 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1760 if (error != 0) {
1761 (void) printf("ERROR %u while trying to open "
1762 "subobj id %llu\n",
1763 error, (u_longlong_t)subobj);
1764 continue;
1766 bpobj_count_refd(&subbpo);
1767 bpobj_close(&subbpo);
1772 static void
1773 dump_deadlist(dsl_deadlist_t *dl)
1775 dsl_deadlist_entry_t *dle;
1776 uint64_t unused;
1777 char bytes[32];
1778 char comp[32];
1779 char uncomp[32];
1780 uint64_t empty_bpobj =
1781 dmu_objset_spa(dl->dl_os)->spa_dsl_pool->dp_empty_bpobj;
1783 /* force the tree to be loaded */
1784 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1786 if (dl->dl_oldfmt) {
1787 if (dl->dl_bpobj.bpo_object != empty_bpobj)
1788 bpobj_count_refd(&dl->dl_bpobj);
1789 } else {
1790 mos_obj_refd(dl->dl_object);
1791 for (dle = avl_first(&dl->dl_tree); dle;
1792 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1793 if (dle->dle_bpobj.bpo_object != empty_bpobj)
1794 bpobj_count_refd(&dle->dle_bpobj);
1798 /* make sure nicenum has enough space */
1799 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1800 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1801 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1803 if (dump_opt['d'] < 3)
1804 return;
1806 if (dl->dl_oldfmt) {
1807 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1808 return;
1811 zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
1812 zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
1813 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1814 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1815 bytes, comp, uncomp);
1817 if (dump_opt['d'] < 4)
1818 return;
1820 (void) printf("\n");
1822 for (dle = avl_first(&dl->dl_tree); dle;
1823 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1824 if (dump_opt['d'] >= 5) {
1825 char buf[128];
1826 (void) snprintf(buf, sizeof (buf),
1827 "mintxg %llu -> obj %llu",
1828 (longlong_t)dle->dle_mintxg,
1829 (longlong_t)dle->dle_bpobj.bpo_object);
1831 dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1832 } else {
1833 (void) printf("mintxg %llu -> obj %llu\n",
1834 (longlong_t)dle->dle_mintxg,
1835 (longlong_t)dle->dle_bpobj.bpo_object);
1840 static avl_tree_t idx_tree;
1841 static avl_tree_t domain_tree;
1842 static boolean_t fuid_table_loaded;
1843 static objset_t *sa_os = NULL;
1844 static sa_attr_type_t *sa_attr_table = NULL;
1846 static int
1847 open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp)
1849 int err;
1850 uint64_t sa_attrs = 0;
1851 uint64_t version = 0;
1853 VERIFY3P(sa_os, ==, NULL);
1854 err = dmu_objset_own(path, type, B_TRUE, tag, osp);
1855 if (err != 0) {
1856 (void) fprintf(stderr, "failed to own dataset '%s': %s\n", path,
1857 strerror(err));
1858 return (err);
1861 if (dmu_objset_type(*osp) == DMU_OST_ZFS) {
1862 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1863 8, 1, &version);
1864 if (version >= ZPL_VERSION_SA) {
1865 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1866 8, 1, &sa_attrs);
1868 err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
1869 &sa_attr_table);
1870 if (err != 0) {
1871 (void) fprintf(stderr, "sa_setup failed: %s\n",
1872 strerror(err));
1873 dmu_objset_disown(*osp, tag);
1874 *osp = NULL;
1877 sa_os = *osp;
1879 return (0);
1882 static void
1883 close_objset(objset_t *os, void *tag)
1885 VERIFY3P(os, ==, sa_os);
1886 if (os->os_sa != NULL)
1887 sa_tear_down(os);
1888 dmu_objset_disown(os, tag);
1889 sa_attr_table = NULL;
1890 sa_os = NULL;
1893 static void
1894 fuid_table_destroy()
1896 if (fuid_table_loaded) {
1897 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1898 fuid_table_loaded = B_FALSE;
1903 * print uid or gid information.
1904 * For normal POSIX id just the id is printed in decimal format.
1905 * For CIFS files with FUID the fuid is printed in hex followed by
1906 * the domain-rid string.
1908 static void
1909 print_idstr(uint64_t id, const char *id_type)
1911 if (FUID_INDEX(id)) {
1912 char *domain;
1914 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1915 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1916 (u_longlong_t)id, domain, (int)FUID_RID(id));
1917 } else {
1918 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1923 static void
1924 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1926 uint32_t uid_idx, gid_idx;
1928 uid_idx = FUID_INDEX(uid);
1929 gid_idx = FUID_INDEX(gid);
1931 /* Load domain table, if not already loaded */
1932 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1933 uint64_t fuid_obj;
1935 /* first find the fuid object. It lives in the master node */
1936 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1937 8, 1, &fuid_obj) == 0);
1938 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1939 (void) zfs_fuid_table_load(os, fuid_obj,
1940 &idx_tree, &domain_tree);
1941 fuid_table_loaded = B_TRUE;
1944 print_idstr(uid, "uid");
1945 print_idstr(gid, "gid");
1948 /*ARGSUSED*/
1949 static void
1950 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1952 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
1953 sa_handle_t *hdl;
1954 uint64_t xattr, rdev, gen;
1955 uint64_t uid, gid, mode, fsize, parent, links;
1956 uint64_t pflags;
1957 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1958 time_t z_crtime, z_atime, z_mtime, z_ctime;
1959 sa_bulk_attr_t bulk[12];
1960 int idx = 0;
1961 int error;
1963 VERIFY3P(os, ==, sa_os);
1964 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1965 (void) printf("Failed to get handle for SA znode\n");
1966 return;
1969 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1970 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1971 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1972 &links, 8);
1973 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1974 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1975 &mode, 8);
1976 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1977 NULL, &parent, 8);
1978 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1979 &fsize, 8);
1980 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1981 acctm, 16);
1982 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1983 modtm, 16);
1984 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1985 crtm, 16);
1986 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1987 chgtm, 16);
1988 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1989 &pflags, 8);
1991 if (sa_bulk_lookup(hdl, bulk, idx)) {
1992 (void) sa_handle_destroy(hdl);
1993 return;
1996 z_crtime = (time_t)crtm[0];
1997 z_atime = (time_t)acctm[0];
1998 z_mtime = (time_t)modtm[0];
1999 z_ctime = (time_t)chgtm[0];
2001 if (dump_opt['d'] > 4) {
2002 error = zfs_obj_to_path(os, object, path, sizeof (path));
2003 if (error == ESTALE) {
2004 (void) snprintf(path, sizeof (path), "on delete queue");
2005 } else if (error != 0) {
2006 leaked_objects++;
2007 (void) snprintf(path, sizeof (path),
2008 "path not found, possibly leaked");
2010 (void) printf("\tpath %s\n", path);
2012 dump_uidgid(os, uid, gid);
2013 (void) printf("\tatime %s", ctime(&z_atime));
2014 (void) printf("\tmtime %s", ctime(&z_mtime));
2015 (void) printf("\tctime %s", ctime(&z_ctime));
2016 (void) printf("\tcrtime %s", ctime(&z_crtime));
2017 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
2018 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
2019 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
2020 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
2021 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
2022 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
2023 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
2024 sizeof (uint64_t)) == 0)
2025 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
2026 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
2027 sizeof (uint64_t)) == 0)
2028 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
2029 sa_handle_destroy(hdl);
2032 /*ARGSUSED*/
2033 static void
2034 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
2038 /*ARGSUSED*/
2039 static void
2040 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
2044 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
2045 dump_none, /* unallocated */
2046 dump_zap, /* object directory */
2047 dump_uint64, /* object array */
2048 dump_none, /* packed nvlist */
2049 dump_packed_nvlist, /* packed nvlist size */
2050 dump_none, /* bpobj */
2051 dump_bpobj, /* bpobj header */
2052 dump_none, /* SPA space map header */
2053 dump_none, /* SPA space map */
2054 dump_none, /* ZIL intent log */
2055 dump_dnode, /* DMU dnode */
2056 dump_dmu_objset, /* DMU objset */
2057 dump_dsl_dir, /* DSL directory */
2058 dump_zap, /* DSL directory child map */
2059 dump_zap, /* DSL dataset snap map */
2060 dump_zap, /* DSL props */
2061 dump_dsl_dataset, /* DSL dataset */
2062 dump_znode, /* ZFS znode */
2063 dump_acl, /* ZFS V0 ACL */
2064 dump_uint8, /* ZFS plain file */
2065 dump_zpldir, /* ZFS directory */
2066 dump_zap, /* ZFS master node */
2067 dump_zap, /* ZFS delete queue */
2068 dump_uint8, /* zvol object */
2069 dump_zap, /* zvol prop */
2070 dump_uint8, /* other uint8[] */
2071 dump_uint64, /* other uint64[] */
2072 dump_zap, /* other ZAP */
2073 dump_zap, /* persistent error log */
2074 dump_uint8, /* SPA history */
2075 dump_history_offsets, /* SPA history offsets */
2076 dump_zap, /* Pool properties */
2077 dump_zap, /* DSL permissions */
2078 dump_acl, /* ZFS ACL */
2079 dump_uint8, /* ZFS SYSACL */
2080 dump_none, /* FUID nvlist */
2081 dump_packed_nvlist, /* FUID nvlist size */
2082 dump_zap, /* DSL dataset next clones */
2083 dump_zap, /* DSL scrub queue */
2084 dump_zap, /* ZFS user/group used */
2085 dump_zap, /* ZFS user/group quota */
2086 dump_zap, /* snapshot refcount tags */
2087 dump_ddt_zap, /* DDT ZAP object */
2088 dump_zap, /* DDT statistics */
2089 dump_znode, /* SA object */
2090 dump_zap, /* SA Master Node */
2091 dump_sa_attrs, /* SA attribute registration */
2092 dump_sa_layouts, /* SA attribute layouts */
2093 dump_zap, /* DSL scrub translations */
2094 dump_none, /* fake dedup BP */
2095 dump_zap, /* deadlist */
2096 dump_none, /* deadlist hdr */
2097 dump_zap, /* dsl clones */
2098 dump_bpobj_subobjs, /* bpobj subobjs */
2099 dump_unknown, /* Unknown type, must be last */
2102 static void
2103 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
2105 dmu_buf_t *db = NULL;
2106 dmu_object_info_t doi;
2107 dnode_t *dn;
2108 void *bonus = NULL;
2109 size_t bsize = 0;
2110 char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
2111 char bonus_size[32];
2112 char aux[50];
2113 int error;
2115 /* make sure nicenum has enough space */
2116 CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
2117 CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
2118 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
2119 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
2120 CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
2122 if (*print_header) {
2123 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n",
2124 "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
2125 "%full", "type");
2126 *print_header = 0;
2129 if (object == 0) {
2130 dn = DMU_META_DNODE(os);
2131 } else {
2132 error = dmu_bonus_hold(os, object, FTAG, &db);
2133 if (error)
2134 fatal("dmu_bonus_hold(%llu) failed, errno %u",
2135 object, error);
2136 bonus = db->db_data;
2137 bsize = db->db_size;
2138 dn = DB_DNODE((dmu_buf_impl_t *)db);
2140 dmu_object_info_from_dnode(dn, &doi);
2142 zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
2143 zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
2144 zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
2145 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
2146 zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
2147 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
2148 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
2149 doi.doi_max_offset);
2151 aux[0] = '\0';
2153 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
2154 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
2155 ZDB_CHECKSUM_NAME(doi.doi_checksum));
2158 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
2159 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
2160 ZDB_COMPRESS_NAME(doi.doi_compress));
2163 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n",
2164 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
2165 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
2167 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
2168 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n",
2169 "", "", "", "", "", bonus_size, "bonus",
2170 ZDB_OT_NAME(doi.doi_bonus_type));
2173 if (verbosity >= 4) {
2174 (void) printf("\tdnode flags: %s%s%s\n",
2175 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
2176 "USED_BYTES " : "",
2177 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
2178 "USERUSED_ACCOUNTED " : "",
2179 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
2180 "SPILL_BLKPTR" : "");
2181 (void) printf("\tdnode maxblkid: %llu\n",
2182 (longlong_t)dn->dn_phys->dn_maxblkid);
2184 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
2185 bonus, bsize);
2186 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
2187 *print_header = 1;
2190 if (verbosity >= 5)
2191 dump_indirect(dn);
2193 if (verbosity >= 5) {
2195 * Report the list of segments that comprise the object.
2197 uint64_t start = 0;
2198 uint64_t end;
2199 uint64_t blkfill = 1;
2200 int minlvl = 1;
2202 if (dn->dn_type == DMU_OT_DNODE) {
2203 minlvl = 0;
2204 blkfill = DNODES_PER_BLOCK;
2207 for (;;) {
2208 char segsize[32];
2209 /* make sure nicenum has enough space */
2210 CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
2211 error = dnode_next_offset(dn,
2212 0, &start, minlvl, blkfill, 0);
2213 if (error)
2214 break;
2215 end = start;
2216 error = dnode_next_offset(dn,
2217 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
2218 zdb_nicenum(end - start, segsize, sizeof (segsize));
2219 (void) printf("\t\tsegment [%016llx, %016llx)"
2220 " size %5s\n", (u_longlong_t)start,
2221 (u_longlong_t)end, segsize);
2222 if (error)
2223 break;
2224 start = end;
2228 if (db != NULL)
2229 dmu_buf_rele(db, FTAG);
2232 static void
2233 count_dir_mos_objects(dsl_dir_t *dd)
2235 mos_obj_refd(dd->dd_object);
2236 mos_obj_refd(dsl_dir_phys(dd)->dd_child_dir_zapobj);
2237 mos_obj_refd(dsl_dir_phys(dd)->dd_deleg_zapobj);
2238 mos_obj_refd(dsl_dir_phys(dd)->dd_props_zapobj);
2239 mos_obj_refd(dsl_dir_phys(dd)->dd_clones);
2242 static void
2243 count_ds_mos_objects(dsl_dataset_t *ds)
2245 mos_obj_refd(ds->ds_object);
2246 mos_obj_refd(dsl_dataset_phys(ds)->ds_next_clones_obj);
2247 mos_obj_refd(dsl_dataset_phys(ds)->ds_props_obj);
2248 mos_obj_refd(dsl_dataset_phys(ds)->ds_userrefs_obj);
2249 mos_obj_refd(dsl_dataset_phys(ds)->ds_snapnames_zapobj);
2251 if (!dsl_dataset_is_snapshot(ds)) {
2252 count_dir_mos_objects(ds->ds_dir);
2256 static const char *objset_types[DMU_OST_NUMTYPES] = {
2257 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
2259 static void
2260 dump_dir(objset_t *os)
2262 dmu_objset_stats_t dds;
2263 uint64_t object, object_count;
2264 uint64_t refdbytes, usedobjs, scratch;
2265 char numbuf[32];
2266 char blkbuf[BP_SPRINTF_LEN + 20];
2267 char osname[ZFS_MAX_DATASET_NAME_LEN];
2268 const char *type = "UNKNOWN";
2269 int verbosity = dump_opt['d'];
2270 int print_header = 1;
2271 unsigned i;
2272 int error;
2274 /* make sure nicenum has enough space */
2275 CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
2277 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2278 dmu_objset_fast_stat(os, &dds);
2279 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2281 if (dds.dds_type < DMU_OST_NUMTYPES)
2282 type = objset_types[dds.dds_type];
2284 if (dds.dds_type == DMU_OST_META) {
2285 dds.dds_creation_txg = TXG_INITIAL;
2286 usedobjs = BP_GET_FILL(os->os_rootbp);
2287 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2288 dd_used_bytes;
2289 } else {
2290 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2293 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2295 zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
2297 if (verbosity >= 4) {
2298 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
2299 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
2300 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2301 } else {
2302 blkbuf[0] = '\0';
2305 dmu_objset_name(os, osname);
2307 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2308 "%s, %llu objects%s%s\n",
2309 osname, type, (u_longlong_t)dmu_objset_id(os),
2310 (u_longlong_t)dds.dds_creation_txg,
2311 numbuf, (u_longlong_t)usedobjs, blkbuf,
2312 (dds.dds_inconsistent) ? " (inconsistent)" : "");
2314 if (zopt_objects != 0) {
2315 for (i = 0; i < zopt_objects; i++)
2316 dump_object(os, zopt_object[i], verbosity,
2317 &print_header);
2318 (void) printf("\n");
2319 return;
2322 if (dump_opt['i'] != 0 || verbosity >= 2)
2323 dump_intent_log(dmu_objset_zil(os));
2325 if (dmu_objset_ds(os) != NULL) {
2326 dsl_dataset_t *ds = dmu_objset_ds(os);
2327 dump_deadlist(&ds->ds_deadlist);
2329 if (dsl_dataset_remap_deadlist_exists(ds)) {
2330 (void) printf("ds_remap_deadlist:\n");
2331 dump_deadlist(&ds->ds_remap_deadlist);
2333 count_ds_mos_objects(ds);
2336 if (verbosity < 2)
2337 return;
2339 if (BP_IS_HOLE(os->os_rootbp))
2340 return;
2342 dump_object(os, 0, verbosity, &print_header);
2343 object_count = 0;
2344 if (DMU_USERUSED_DNODE(os) != NULL &&
2345 DMU_USERUSED_DNODE(os)->dn_type != 0) {
2346 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
2347 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
2350 object = 0;
2351 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2352 dump_object(os, object, verbosity, &print_header);
2353 object_count++;
2356 ASSERT3U(object_count, ==, usedobjs);
2358 (void) printf("\n");
2360 if (error != ESRCH) {
2361 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2362 abort();
2364 if (leaked_objects != 0) {
2365 (void) printf("%d potentially leaked objects detected\n",
2366 leaked_objects);
2367 leaked_objects = 0;
2371 static void
2372 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2374 time_t timestamp = ub->ub_timestamp;
2376 (void) printf("%s", header ? header : "");
2377 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2378 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2379 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2380 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2381 (void) printf("\ttimestamp = %llu UTC = %s",
2382 (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2383 if (dump_opt['u'] >= 3) {
2384 char blkbuf[BP_SPRINTF_LEN];
2385 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2386 (void) printf("\trootbp = %s\n", blkbuf);
2388 (void) printf("\tcheckpoint_txg = %llu\n",
2389 (u_longlong_t)ub->ub_checkpoint_txg);
2390 (void) printf("%s", footer ? footer : "");
2393 static void
2394 dump_config(spa_t *spa)
2396 dmu_buf_t *db;
2397 size_t nvsize = 0;
2398 int error = 0;
2401 error = dmu_bonus_hold(spa->spa_meta_objset,
2402 spa->spa_config_object, FTAG, &db);
2404 if (error == 0) {
2405 nvsize = *(uint64_t *)db->db_data;
2406 dmu_buf_rele(db, FTAG);
2408 (void) printf("\nMOS Configuration:\n");
2409 dump_packed_nvlist(spa->spa_meta_objset,
2410 spa->spa_config_object, (void *)&nvsize, 1);
2411 } else {
2412 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
2413 (u_longlong_t)spa->spa_config_object, error);
2417 static void
2418 dump_cachefile(const char *cachefile)
2420 int fd;
2421 struct stat64 statbuf;
2422 char *buf;
2423 nvlist_t *config;
2425 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2426 (void) printf("cannot open '%s': %s\n", cachefile,
2427 strerror(errno));
2428 exit(1);
2431 if (fstat64(fd, &statbuf) != 0) {
2432 (void) printf("failed to stat '%s': %s\n", cachefile,
2433 strerror(errno));
2434 exit(1);
2437 if ((buf = malloc(statbuf.st_size)) == NULL) {
2438 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
2439 (u_longlong_t)statbuf.st_size);
2440 exit(1);
2443 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2444 (void) fprintf(stderr, "failed to read %llu bytes\n",
2445 (u_longlong_t)statbuf.st_size);
2446 exit(1);
2449 (void) close(fd);
2451 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2452 (void) fprintf(stderr, "failed to unpack nvlist\n");
2453 exit(1);
2456 free(buf);
2458 dump_nvlist(config, 0);
2460 nvlist_free(config);
2463 #define ZDB_MAX_UB_HEADER_SIZE 32
2465 static void
2466 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
2468 vdev_t vd;
2469 vdev_t *vdp = &vd;
2470 char header[ZDB_MAX_UB_HEADER_SIZE];
2472 vd.vdev_ashift = ashift;
2473 vdp->vdev_top = vdp;
2475 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
2476 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
2477 uberblock_t *ub = (void *)((char *)lbl + uoff);
2479 if (uberblock_verify(ub))
2480 continue;
2481 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
2482 "Uberblock[%d]\n", i);
2483 dump_uberblock(ub, header, "");
2487 static char curpath[PATH_MAX];
2490 * Iterate through the path components, recursively passing
2491 * current one's obj and remaining path until we find the obj
2492 * for the last one.
2494 static int
2495 dump_path_impl(objset_t *os, uint64_t obj, char *name)
2497 int err;
2498 int header = 1;
2499 uint64_t child_obj;
2500 char *s;
2501 dmu_buf_t *db;
2502 dmu_object_info_t doi;
2504 if ((s = strchr(name, '/')) != NULL)
2505 *s = '\0';
2506 err = zap_lookup(os, obj, name, 8, 1, &child_obj);
2508 (void) strlcat(curpath, name, sizeof (curpath));
2510 if (err != 0) {
2511 (void) fprintf(stderr, "failed to lookup %s: %s\n",
2512 curpath, strerror(err));
2513 return (err);
2516 child_obj = ZFS_DIRENT_OBJ(child_obj);
2517 err = sa_buf_hold(os, child_obj, FTAG, &db);
2518 if (err != 0) {
2519 (void) fprintf(stderr,
2520 "failed to get SA dbuf for obj %llu: %s\n",
2521 (u_longlong_t)child_obj, strerror(err));
2522 return (EINVAL);
2524 dmu_object_info_from_db(db, &doi);
2525 sa_buf_rele(db, FTAG);
2527 if (doi.doi_bonus_type != DMU_OT_SA &&
2528 doi.doi_bonus_type != DMU_OT_ZNODE) {
2529 (void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
2530 doi.doi_bonus_type, (u_longlong_t)child_obj);
2531 return (EINVAL);
2534 if (dump_opt['v'] > 6) {
2535 (void) printf("obj=%llu %s type=%d bonustype=%d\n",
2536 (u_longlong_t)child_obj, curpath, doi.doi_type,
2537 doi.doi_bonus_type);
2540 (void) strlcat(curpath, "/", sizeof (curpath));
2542 switch (doi.doi_type) {
2543 case DMU_OT_DIRECTORY_CONTENTS:
2544 if (s != NULL && *(s + 1) != '\0')
2545 return (dump_path_impl(os, child_obj, s + 1));
2546 /*FALLTHROUGH*/
2547 case DMU_OT_PLAIN_FILE_CONTENTS:
2548 dump_object(os, child_obj, dump_opt['v'], &header);
2549 return (0);
2550 default:
2551 (void) fprintf(stderr, "object %llu has non-file/directory "
2552 "type %d\n", (u_longlong_t)obj, doi.doi_type);
2553 break;
2556 return (EINVAL);
2560 * Dump the blocks for the object specified by path inside the dataset.
2562 static int
2563 dump_path(char *ds, char *path)
2565 int err;
2566 objset_t *os;
2567 uint64_t root_obj;
2569 err = open_objset(ds, DMU_OST_ZFS, FTAG, &os);
2570 if (err != 0)
2571 return (err);
2573 err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
2574 if (err != 0) {
2575 (void) fprintf(stderr, "can't lookup root znode: %s\n",
2576 strerror(err));
2577 dmu_objset_disown(os, FTAG);
2578 return (EINVAL);
2581 (void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
2583 err = dump_path_impl(os, root_obj, path);
2585 close_objset(os, FTAG);
2586 return (err);
2589 static int
2590 dump_label(const char *dev)
2592 int fd;
2593 vdev_label_t label;
2594 char path[MAXPATHLEN];
2595 char *buf = label.vl_vdev_phys.vp_nvlist;
2596 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2597 struct stat64 statbuf;
2598 uint64_t psize, ashift;
2599 boolean_t label_found = B_FALSE;
2601 (void) strlcpy(path, dev, sizeof (path));
2602 if (dev[0] == '/') {
2603 if (strncmp(dev, ZFS_DISK_ROOTD,
2604 strlen(ZFS_DISK_ROOTD)) == 0) {
2605 (void) snprintf(path, sizeof (path), "%s%s",
2606 ZFS_RDISK_ROOTD, dev + strlen(ZFS_DISK_ROOTD));
2608 } else if (stat64(path, &statbuf) != 0) {
2609 char *s;
2611 (void) snprintf(path, sizeof (path), "%s%s", ZFS_RDISK_ROOTD,
2612 dev);
2613 if (((s = strrchr(dev, 's')) == NULL &&
2614 (s = strchr(dev, 'p')) == NULL) ||
2615 !isdigit(*(s + 1)))
2616 (void) strlcat(path, "s0", sizeof (path));
2619 if ((fd = open64(path, O_RDONLY)) < 0) {
2620 (void) fprintf(stderr, "cannot open '%s': %s\n", path,
2621 strerror(errno));
2622 exit(1);
2625 if (fstat64(fd, &statbuf) != 0) {
2626 (void) fprintf(stderr, "failed to stat '%s': %s\n", path,
2627 strerror(errno));
2628 (void) close(fd);
2629 exit(1);
2632 if (S_ISBLK(statbuf.st_mode)) {
2633 (void) fprintf(stderr,
2634 "cannot use '%s': character device required\n", path);
2635 (void) close(fd);
2636 exit(1);
2639 psize = statbuf.st_size;
2640 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2642 for (int l = 0; l < VDEV_LABELS; l++) {
2643 nvlist_t *config = NULL;
2645 if (!dump_opt['q']) {
2646 (void) printf("------------------------------------\n");
2647 (void) printf("LABEL %d\n", l);
2648 (void) printf("------------------------------------\n");
2651 if (pread64(fd, &label, sizeof (label),
2652 vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2653 if (!dump_opt['q'])
2654 (void) printf("failed to read label %d\n", l);
2655 continue;
2658 if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2659 if (!dump_opt['q'])
2660 (void) printf("failed to unpack label %d\n", l);
2661 ashift = SPA_MINBLOCKSHIFT;
2662 } else {
2663 nvlist_t *vdev_tree = NULL;
2665 if (!dump_opt['q'])
2666 dump_nvlist(config, 4);
2667 if ((nvlist_lookup_nvlist(config,
2668 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2669 (nvlist_lookup_uint64(vdev_tree,
2670 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2671 ashift = SPA_MINBLOCKSHIFT;
2672 nvlist_free(config);
2673 label_found = B_TRUE;
2675 if (dump_opt['u'])
2676 dump_label_uberblocks(&label, ashift);
2679 (void) close(fd);
2681 return (label_found ? 0 : 2);
2684 static uint64_t dataset_feature_count[SPA_FEATURES];
2685 static uint64_t remap_deadlist_count = 0;
2687 /*ARGSUSED*/
2688 static int
2689 dump_one_dir(const char *dsname, void *arg)
2691 int error;
2692 objset_t *os;
2694 error = open_objset(dsname, DMU_OST_ANY, FTAG, &os);
2695 if (error != 0)
2696 return (0);
2698 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2699 if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2700 continue;
2701 ASSERT(spa_feature_table[f].fi_flags &
2702 ZFEATURE_FLAG_PER_DATASET);
2703 dataset_feature_count[f]++;
2706 if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) {
2707 remap_deadlist_count++;
2710 dump_dir(os);
2711 close_objset(os, FTAG);
2712 fuid_table_destroy();
2713 return (0);
2717 * Block statistics.
2719 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2720 typedef struct zdb_blkstats {
2721 uint64_t zb_asize;
2722 uint64_t zb_lsize;
2723 uint64_t zb_psize;
2724 uint64_t zb_count;
2725 uint64_t zb_gangs;
2726 uint64_t zb_ditto_samevdev;
2727 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2728 } zdb_blkstats_t;
2731 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2733 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2734 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2735 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2736 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2738 static const char *zdb_ot_extname[] = {
2739 "deferred free",
2740 "dedup ditto",
2741 "other",
2742 "Total",
2745 #define ZB_TOTAL DN_MAX_LEVELS
2747 typedef struct zdb_cb {
2748 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2749 uint64_t zcb_removing_size;
2750 uint64_t zcb_checkpoint_size;
2751 uint64_t zcb_dedup_asize;
2752 uint64_t zcb_dedup_blocks;
2753 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2754 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
2755 [BPE_PAYLOAD_SIZE];
2756 uint64_t zcb_start;
2757 hrtime_t zcb_lastprint;
2758 uint64_t zcb_totalasize;
2759 uint64_t zcb_errors[256];
2760 int zcb_readfails;
2761 int zcb_haderrors;
2762 spa_t *zcb_spa;
2763 uint32_t **zcb_vd_obsolete_counts;
2764 } zdb_cb_t;
2766 static void
2767 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2768 dmu_object_type_t type)
2770 uint64_t refcnt = 0;
2772 ASSERT(type < ZDB_OT_TOTAL);
2774 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2775 return;
2777 for (int i = 0; i < 4; i++) {
2778 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2779 int t = (i & 1) ? type : ZDB_OT_TOTAL;
2780 int equal;
2781 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2783 zb->zb_asize += BP_GET_ASIZE(bp);
2784 zb->zb_lsize += BP_GET_LSIZE(bp);
2785 zb->zb_psize += BP_GET_PSIZE(bp);
2786 zb->zb_count++;
2789 * The histogram is only big enough to record blocks up to
2790 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2791 * "other", bucket.
2793 unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2794 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2795 zb->zb_psize_histogram[idx]++;
2797 zb->zb_gangs += BP_COUNT_GANG(bp);
2799 switch (BP_GET_NDVAS(bp)) {
2800 case 2:
2801 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2802 DVA_GET_VDEV(&bp->blk_dva[1]))
2803 zb->zb_ditto_samevdev++;
2804 break;
2805 case 3:
2806 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2807 DVA_GET_VDEV(&bp->blk_dva[1])) +
2808 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2809 DVA_GET_VDEV(&bp->blk_dva[2])) +
2810 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2811 DVA_GET_VDEV(&bp->blk_dva[2]));
2812 if (equal != 0)
2813 zb->zb_ditto_samevdev++;
2814 break;
2819 if (BP_IS_EMBEDDED(bp)) {
2820 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
2821 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
2822 [BPE_GET_PSIZE(bp)]++;
2823 return;
2826 if (dump_opt['L'])
2827 return;
2829 if (BP_GET_DEDUP(bp)) {
2830 ddt_t *ddt;
2831 ddt_entry_t *dde;
2833 ddt = ddt_select(zcb->zcb_spa, bp);
2834 ddt_enter(ddt);
2835 dde = ddt_lookup(ddt, bp, B_FALSE);
2837 if (dde == NULL) {
2838 refcnt = 0;
2839 } else {
2840 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2841 ddt_phys_decref(ddp);
2842 refcnt = ddp->ddp_refcnt;
2843 if (ddt_phys_total_refcnt(dde) == 0)
2844 ddt_remove(ddt, dde);
2846 ddt_exit(ddt);
2849 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2850 refcnt ? 0 : spa_min_claim_txg(zcb->zcb_spa),
2851 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2854 static void
2855 zdb_blkptr_done(zio_t *zio)
2857 spa_t *spa = zio->io_spa;
2858 blkptr_t *bp = zio->io_bp;
2859 int ioerr = zio->io_error;
2860 zdb_cb_t *zcb = zio->io_private;
2861 zbookmark_phys_t *zb = &zio->io_bookmark;
2863 abd_free(zio->io_abd);
2865 mutex_enter(&spa->spa_scrub_lock);
2866 spa->spa_scrub_inflight--;
2867 cv_broadcast(&spa->spa_scrub_io_cv);
2869 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2870 char blkbuf[BP_SPRINTF_LEN];
2872 zcb->zcb_haderrors = 1;
2873 zcb->zcb_errors[ioerr]++;
2875 if (dump_opt['b'] >= 2)
2876 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2877 else
2878 blkbuf[0] = '\0';
2880 (void) printf("zdb_blkptr_cb: "
2881 "Got error %d reading "
2882 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
2883 ioerr,
2884 (u_longlong_t)zb->zb_objset,
2885 (u_longlong_t)zb->zb_object,
2886 (u_longlong_t)zb->zb_level,
2887 (u_longlong_t)zb->zb_blkid,
2888 blkbuf);
2890 mutex_exit(&spa->spa_scrub_lock);
2893 static int
2894 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2895 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2897 zdb_cb_t *zcb = arg;
2898 dmu_object_type_t type;
2899 boolean_t is_metadata;
2901 if (bp == NULL)
2902 return (0);
2904 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
2905 char blkbuf[BP_SPRINTF_LEN];
2906 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2907 (void) printf("objset %llu object %llu "
2908 "level %lld offset 0x%llx %s\n",
2909 (u_longlong_t)zb->zb_objset,
2910 (u_longlong_t)zb->zb_object,
2911 (longlong_t)zb->zb_level,
2912 (u_longlong_t)blkid2offset(dnp, bp, zb),
2913 blkbuf);
2916 if (BP_IS_HOLE(bp))
2917 return (0);
2919 type = BP_GET_TYPE(bp);
2921 zdb_count_block(zcb, zilog, bp,
2922 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2924 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2926 if (!BP_IS_EMBEDDED(bp) &&
2927 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2928 size_t size = BP_GET_PSIZE(bp);
2929 abd_t *abd = abd_alloc(size, B_FALSE);
2930 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2932 /* If it's an intent log block, failure is expected. */
2933 if (zb->zb_level == ZB_ZIL_LEVEL)
2934 flags |= ZIO_FLAG_SPECULATIVE;
2936 mutex_enter(&spa->spa_scrub_lock);
2937 while (spa->spa_scrub_inflight > max_inflight)
2938 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2939 spa->spa_scrub_inflight++;
2940 mutex_exit(&spa->spa_scrub_lock);
2942 zio_nowait(zio_read(NULL, spa, bp, abd, size,
2943 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2946 zcb->zcb_readfails = 0;
2948 /* only call gethrtime() every 100 blocks */
2949 static int iters;
2950 if (++iters > 100)
2951 iters = 0;
2952 else
2953 return (0);
2955 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2956 uint64_t now = gethrtime();
2957 char buf[10];
2958 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2959 int kb_per_sec =
2960 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2961 int sec_remaining =
2962 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2964 /* make sure nicenum has enough space */
2965 CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
2967 zfs_nicenum(bytes, buf, sizeof (buf));
2968 (void) fprintf(stderr,
2969 "\r%5s completed (%4dMB/s) "
2970 "estimated time remaining: %uhr %02umin %02usec ",
2971 buf, kb_per_sec / 1024,
2972 sec_remaining / 60 / 60,
2973 sec_remaining / 60 % 60,
2974 sec_remaining % 60);
2976 zcb->zcb_lastprint = now;
2979 return (0);
2982 static void
2983 zdb_leak(void *arg, uint64_t start, uint64_t size)
2985 vdev_t *vd = arg;
2987 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2988 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2991 static metaslab_ops_t zdb_metaslab_ops = {
2992 NULL /* alloc */
2995 static void
2996 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2998 ddt_bookmark_t ddb;
2999 ddt_entry_t dde;
3000 int error;
3002 bzero(&ddb, sizeof (ddb));
3003 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
3004 blkptr_t blk;
3005 ddt_phys_t *ddp = dde.dde_phys;
3007 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
3008 return;
3010 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
3012 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
3013 if (ddp->ddp_phys_birth == 0)
3014 continue;
3015 ddt_bp_create(ddb.ddb_checksum,
3016 &dde.dde_key, ddp, &blk);
3017 if (p == DDT_PHYS_DITTO) {
3018 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
3019 } else {
3020 zcb->zcb_dedup_asize +=
3021 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
3022 zcb->zcb_dedup_blocks++;
3025 if (!dump_opt['L']) {
3026 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
3027 ddt_enter(ddt);
3028 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
3029 ddt_exit(ddt);
3033 ASSERT(error == ENOENT);
3036 /* ARGSUSED */
3037 static void
3038 claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
3039 uint64_t size, void *arg)
3042 * This callback was called through a remap from
3043 * a device being removed. Therefore, the vdev that
3044 * this callback is applied to is a concrete
3045 * vdev.
3047 ASSERT(vdev_is_concrete(vd));
3049 VERIFY0(metaslab_claim_impl(vd, offset, size,
3050 spa_min_claim_txg(vd->vdev_spa)));
3053 static void
3054 claim_segment_cb(void *arg, uint64_t offset, uint64_t size)
3056 vdev_t *vd = arg;
3058 vdev_indirect_ops.vdev_op_remap(vd, offset, size,
3059 claim_segment_impl_cb, NULL);
3063 * After accounting for all allocated blocks that are directly referenced,
3064 * we might have missed a reference to a block from a partially complete
3065 * (and thus unused) indirect mapping object. We perform a secondary pass
3066 * through the metaslabs we have already mapped and claim the destination
3067 * blocks.
3069 static void
3070 zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb)
3072 if (spa->spa_vdev_removal == NULL)
3073 return;
3075 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3077 spa_vdev_removal_t *svr = spa->spa_vdev_removal;
3078 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
3079 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3081 for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
3082 metaslab_t *msp = vd->vdev_ms[msi];
3084 if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim))
3085 break;
3087 ASSERT0(range_tree_space(svr->svr_allocd_segs));
3089 if (msp->ms_sm != NULL) {
3090 VERIFY0(space_map_load(msp->ms_sm,
3091 svr->svr_allocd_segs, SM_ALLOC));
3094 * Clear everything past what has been synced unless
3095 * it's past the spacemap, because we have not allocated
3096 * mappings for it yet.
3098 uint64_t vim_max_offset =
3099 vdev_indirect_mapping_max_offset(vim);
3100 uint64_t sm_end = msp->ms_sm->sm_start +
3101 msp->ms_sm->sm_size;
3102 if (sm_end > vim_max_offset)
3103 range_tree_clear(svr->svr_allocd_segs,
3104 vim_max_offset, sm_end - vim_max_offset);
3107 zcb->zcb_removing_size +=
3108 range_tree_space(svr->svr_allocd_segs);
3109 range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd);
3112 spa_config_exit(spa, SCL_CONFIG, FTAG);
3115 /* ARGSUSED */
3116 static int
3117 increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3119 zdb_cb_t *zcb = arg;
3120 spa_t *spa = zcb->zcb_spa;
3121 vdev_t *vd;
3122 const dva_t *dva = &bp->blk_dva[0];
3124 ASSERT(!dump_opt['L']);
3125 ASSERT3U(BP_GET_NDVAS(bp), ==, 1);
3127 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3128 vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva));
3129 ASSERT3P(vd, !=, NULL);
3130 spa_config_exit(spa, SCL_VDEV, FTAG);
3132 ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
3133 ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL);
3135 vdev_indirect_mapping_increment_obsolete_count(
3136 vd->vdev_indirect_mapping,
3137 DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva),
3138 zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
3140 return (0);
3143 static uint32_t *
3144 zdb_load_obsolete_counts(vdev_t *vd)
3146 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3147 spa_t *spa = vd->vdev_spa;
3148 spa_condensing_indirect_phys_t *scip =
3149 &spa->spa_condensing_indirect_phys;
3150 uint32_t *counts;
3152 EQUIV(vdev_obsolete_sm_object(vd) != 0, vd->vdev_obsolete_sm != NULL);
3153 counts = vdev_indirect_mapping_load_obsolete_counts(vim);
3154 if (vd->vdev_obsolete_sm != NULL) {
3155 vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
3156 vd->vdev_obsolete_sm);
3158 if (scip->scip_vdev == vd->vdev_id &&
3159 scip->scip_prev_obsolete_sm_object != 0) {
3160 space_map_t *prev_obsolete_sm = NULL;
3161 VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset,
3162 scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0));
3163 space_map_update(prev_obsolete_sm);
3164 vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
3165 prev_obsolete_sm);
3166 space_map_close(prev_obsolete_sm);
3168 return (counts);
3171 typedef struct checkpoint_sm_exclude_entry_arg {
3172 vdev_t *cseea_vd;
3173 uint64_t cseea_checkpoint_size;
3174 } checkpoint_sm_exclude_entry_arg_t;
3176 static int
3177 checkpoint_sm_exclude_entry_cb(space_map_entry_t *sme, void *arg)
3179 checkpoint_sm_exclude_entry_arg_t *cseea = arg;
3180 vdev_t *vd = cseea->cseea_vd;
3181 metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
3182 uint64_t end = sme->sme_offset + sme->sme_run;
3184 ASSERT(sme->sme_type == SM_FREE);
3187 * Since the vdev_checkpoint_sm exists in the vdev level
3188 * and the ms_sm space maps exist in the metaslab level,
3189 * an entry in the checkpoint space map could theoretically
3190 * cross the boundaries of the metaslab that it belongs.
3192 * In reality, because of the way that we populate and
3193 * manipulate the checkpoint's space maps currently,
3194 * there shouldn't be any entries that cross metaslabs.
3195 * Hence the assertion below.
3197 * That said, there is no fundamental requirement that
3198 * the checkpoint's space map entries should not cross
3199 * metaslab boundaries. So if needed we could add code
3200 * that handles metaslab-crossing segments in the future.
3202 VERIFY3U(sme->sme_offset, >=, ms->ms_start);
3203 VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
3206 * By removing the entry from the allocated segments we
3207 * also verify that the entry is there to begin with.
3209 mutex_enter(&ms->ms_lock);
3210 range_tree_remove(ms->ms_allocatable, sme->sme_offset, sme->sme_run);
3211 mutex_exit(&ms->ms_lock);
3213 cseea->cseea_checkpoint_size += sme->sme_run;
3214 return (0);
3217 static void
3218 zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb)
3220 spa_t *spa = vd->vdev_spa;
3221 space_map_t *checkpoint_sm = NULL;
3222 uint64_t checkpoint_sm_obj;
3225 * If there is no vdev_top_zap, we are in a pool whose
3226 * version predates the pool checkpoint feature.
3228 if (vd->vdev_top_zap == 0)
3229 return;
3232 * If there is no reference of the vdev_checkpoint_sm in
3233 * the vdev_top_zap, then one of the following scenarios
3234 * is true:
3236 * 1] There is no checkpoint
3237 * 2] There is a checkpoint, but no checkpointed blocks
3238 * have been freed yet
3239 * 3] The current vdev is indirect
3241 * In these cases we return immediately.
3243 if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
3244 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
3245 return;
3247 VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
3248 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1,
3249 &checkpoint_sm_obj));
3251 checkpoint_sm_exclude_entry_arg_t cseea;
3252 cseea.cseea_vd = vd;
3253 cseea.cseea_checkpoint_size = 0;
3255 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
3256 checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
3257 space_map_update(checkpoint_sm);
3259 VERIFY0(space_map_iterate(checkpoint_sm,
3260 checkpoint_sm_exclude_entry_cb, &cseea));
3261 space_map_close(checkpoint_sm);
3263 zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size;
3266 static void
3267 zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb)
3269 vdev_t *rvd = spa->spa_root_vdev;
3270 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
3271 ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id);
3272 zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb);
3276 static void
3277 load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype)
3279 vdev_t *rvd = spa->spa_root_vdev;
3280 for (uint64_t i = 0; i < rvd->vdev_children; i++) {
3281 vdev_t *vd = rvd->vdev_child[i];
3283 ASSERT3U(i, ==, vd->vdev_id);
3285 if (vd->vdev_ops == &vdev_indirect_ops)
3286 continue;
3288 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3289 metaslab_t *msp = vd->vdev_ms[m];
3291 (void) fprintf(stderr,
3292 "\rloading concrete vdev %llu, "
3293 "metaslab %llu of %llu ...",
3294 (longlong_t)vd->vdev_id,
3295 (longlong_t)msp->ms_id,
3296 (longlong_t)vd->vdev_ms_count);
3298 mutex_enter(&msp->ms_lock);
3299 metaslab_unload(msp);
3302 * We don't want to spend the CPU manipulating the
3303 * size-ordered tree, so clear the range_tree ops.
3305 msp->ms_allocatable->rt_ops = NULL;
3307 if (msp->ms_sm != NULL) {
3308 VERIFY0(space_map_load(msp->ms_sm,
3309 msp->ms_allocatable, maptype));
3311 if (!msp->ms_loaded)
3312 msp->ms_loaded = B_TRUE;
3313 mutex_exit(&msp->ms_lock);
3319 * vm_idxp is an in-out parameter which (for indirect vdevs) is the
3320 * index in vim_entries that has the first entry in this metaslab.
3321 * On return, it will be set to the first entry after this metaslab.
3323 static void
3324 load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp,
3325 uint64_t *vim_idxp)
3327 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3329 mutex_enter(&msp->ms_lock);
3330 metaslab_unload(msp);
3333 * We don't want to spend the CPU manipulating the
3334 * size-ordered tree, so clear the range_tree ops.
3336 msp->ms_allocatable->rt_ops = NULL;
3338 for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim);
3339 (*vim_idxp)++) {
3340 vdev_indirect_mapping_entry_phys_t *vimep =
3341 &vim->vim_entries[*vim_idxp];
3342 uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
3343 uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst);
3344 ASSERT3U(ent_offset, >=, msp->ms_start);
3345 if (ent_offset >= msp->ms_start + msp->ms_size)
3346 break;
3349 * Mappings do not cross metaslab boundaries,
3350 * because we create them by walking the metaslabs.
3352 ASSERT3U(ent_offset + ent_len, <=,
3353 msp->ms_start + msp->ms_size);
3354 range_tree_add(msp->ms_allocatable, ent_offset, ent_len);
3357 if (!msp->ms_loaded)
3358 msp->ms_loaded = B_TRUE;
3359 mutex_exit(&msp->ms_lock);
3362 static void
3363 zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb)
3365 vdev_t *rvd = spa->spa_root_vdev;
3366 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
3367 vdev_t *vd = rvd->vdev_child[c];
3369 ASSERT3U(c, ==, vd->vdev_id);
3371 if (vd->vdev_ops != &vdev_indirect_ops)
3372 continue;
3375 * Note: we don't check for mapping leaks on
3376 * removing vdevs because their ms_allocatable's
3377 * are used to look for leaks in allocated space.
3379 zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd);
3382 * Normally, indirect vdevs don't have any
3383 * metaslabs. We want to set them up for
3384 * zio_claim().
3386 VERIFY0(vdev_metaslab_init(vd, 0));
3388 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3389 uint64_t vim_idx = 0;
3390 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3392 (void) fprintf(stderr,
3393 "\rloading indirect vdev %llu, "
3394 "metaslab %llu of %llu ...",
3395 (longlong_t)vd->vdev_id,
3396 (longlong_t)vd->vdev_ms[m]->ms_id,
3397 (longlong_t)vd->vdev_ms_count);
3399 load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m],
3400 &vim_idx);
3402 ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim));
3406 static void
3407 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
3409 zcb->zcb_spa = spa;
3411 if (!dump_opt['L']) {
3412 dsl_pool_t *dp = spa->spa_dsl_pool;
3413 vdev_t *rvd = spa->spa_root_vdev;
3416 * We are going to be changing the meaning of the metaslab's
3417 * ms_allocatable. Ensure that the allocator doesn't try to
3418 * use the tree.
3420 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
3421 spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
3423 zcb->zcb_vd_obsolete_counts =
3424 umem_zalloc(rvd->vdev_children * sizeof (uint32_t *),
3425 UMEM_NOFAIL);
3428 * For leak detection, we overload the ms_allocatable trees
3429 * to contain allocated segments instead of free segments.
3430 * As a result, we can't use the normal metaslab_load/unload
3431 * interfaces.
3433 zdb_leak_init_prepare_indirect_vdevs(spa, zcb);
3434 load_concrete_ms_allocatable_trees(spa, SM_ALLOC);
3437 * On load_concrete_ms_allocatable_trees() we loaded all the
3438 * allocated entries from the ms_sm to the ms_allocatable for
3439 * each metaslab. If the pool has a checkpoint or is in the
3440 * middle of discarding a checkpoint, some of these blocks
3441 * may have been freed but their ms_sm may not have been
3442 * updated because they are referenced by the checkpoint. In
3443 * order to avoid false-positives during leak-detection, we
3444 * go through the vdev's checkpoint space map and exclude all
3445 * its entries from their relevant ms_allocatable.
3447 * We also aggregate the space held by the checkpoint and add
3448 * it to zcb_checkpoint_size.
3450 * Note that at this point we are also verifying that all the
3451 * entries on the checkpoint_sm are marked as allocated in
3452 * the ms_sm of their relevant metaslab.
3453 * [see comment in checkpoint_sm_exclude_entry_cb()]
3455 zdb_leak_init_exclude_checkpoint(spa, zcb);
3457 /* for cleaner progress output */
3458 (void) fprintf(stderr, "\n");
3460 if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
3461 ASSERT(spa_feature_is_enabled(spa,
3462 SPA_FEATURE_DEVICE_REMOVAL));
3463 (void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj,
3464 increment_indirect_mapping_cb, zcb, NULL);
3466 } else {
3468 * If leak tracing is disabled, we still need to consider
3469 * any checkpointed space in our space verification.
3471 zcb->zcb_checkpoint_size += spa_get_checkpoint_space(spa);
3474 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3475 zdb_ddt_leak_init(spa, zcb);
3476 spa_config_exit(spa, SCL_CONFIG, FTAG);
3479 static boolean_t
3480 zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb)
3482 boolean_t leaks = B_FALSE;
3483 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3484 uint64_t total_leaked = 0;
3486 ASSERT(vim != NULL);
3488 for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
3489 vdev_indirect_mapping_entry_phys_t *vimep =
3490 &vim->vim_entries[i];
3491 uint64_t obsolete_bytes = 0;
3492 uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
3493 metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
3496 * This is not very efficient but it's easy to
3497 * verify correctness.
3499 for (uint64_t inner_offset = 0;
3500 inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst);
3501 inner_offset += 1 << vd->vdev_ashift) {
3502 if (range_tree_contains(msp->ms_allocatable,
3503 offset + inner_offset, 1 << vd->vdev_ashift)) {
3504 obsolete_bytes += 1 << vd->vdev_ashift;
3508 int64_t bytes_leaked = obsolete_bytes -
3509 zcb->zcb_vd_obsolete_counts[vd->vdev_id][i];
3510 ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=,
3511 zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]);
3512 if (bytes_leaked != 0 &&
3513 (vdev_obsolete_counts_are_precise(vd) ||
3514 dump_opt['d'] >= 5)) {
3515 (void) printf("obsolete indirect mapping count "
3516 "mismatch on %llu:%llx:%llx : %llx bytes leaked\n",
3517 (u_longlong_t)vd->vdev_id,
3518 (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
3519 (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
3520 (u_longlong_t)bytes_leaked);
3522 total_leaked += ABS(bytes_leaked);
3525 if (!vdev_obsolete_counts_are_precise(vd) && total_leaked > 0) {
3526 int pct_leaked = total_leaked * 100 /
3527 vdev_indirect_mapping_bytes_mapped(vim);
3528 (void) printf("cannot verify obsolete indirect mapping "
3529 "counts of vdev %llu because precise feature was not "
3530 "enabled when it was removed: %d%% (%llx bytes) of mapping"
3531 "unreferenced\n",
3532 (u_longlong_t)vd->vdev_id, pct_leaked,
3533 (u_longlong_t)total_leaked);
3534 } else if (total_leaked > 0) {
3535 (void) printf("obsolete indirect mapping count mismatch "
3536 "for vdev %llu -- %llx total bytes mismatched\n",
3537 (u_longlong_t)vd->vdev_id,
3538 (u_longlong_t)total_leaked);
3539 leaks |= B_TRUE;
3542 vdev_indirect_mapping_free_obsolete_counts(vim,
3543 zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
3544 zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL;
3546 return (leaks);
3549 static boolean_t
3550 zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb)
3552 boolean_t leaks = B_FALSE;
3553 if (!dump_opt['L']) {
3554 vdev_t *rvd = spa->spa_root_vdev;
3555 for (unsigned c = 0; c < rvd->vdev_children; c++) {
3556 vdev_t *vd = rvd->vdev_child[c];
3557 metaslab_group_t *mg = vd->vdev_mg;
3559 if (zcb->zcb_vd_obsolete_counts[c] != NULL) {
3560 leaks |= zdb_check_for_obsolete_leaks(vd, zcb);
3563 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
3564 metaslab_t *msp = vd->vdev_ms[m];
3565 ASSERT3P(mg, ==, msp->ms_group);
3568 * ms_allocatable has been overloaded
3569 * to contain allocated segments. Now that
3570 * we finished traversing all blocks, any
3571 * block that remains in the ms_allocatable
3572 * represents an allocated block that we
3573 * did not claim during the traversal.
3574 * Claimed blocks would have been removed
3575 * from the ms_allocatable. For indirect
3576 * vdevs, space remaining in the tree
3577 * represents parts of the mapping that are
3578 * not referenced, which is not a bug.
3580 if (vd->vdev_ops == &vdev_indirect_ops) {
3581 range_tree_vacate(msp->ms_allocatable,
3582 NULL, NULL);
3583 } else {
3584 range_tree_vacate(msp->ms_allocatable,
3585 zdb_leak, vd);
3588 if (msp->ms_loaded) {
3589 msp->ms_loaded = B_FALSE;
3594 umem_free(zcb->zcb_vd_obsolete_counts,
3595 rvd->vdev_children * sizeof (uint32_t *));
3596 zcb->zcb_vd_obsolete_counts = NULL;
3598 return (leaks);
3601 /* ARGSUSED */
3602 static int
3603 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3605 zdb_cb_t *zcb = arg;
3607 if (dump_opt['b'] >= 5) {
3608 char blkbuf[BP_SPRINTF_LEN];
3609 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3610 (void) printf("[%s] %s\n",
3611 "deferred free", blkbuf);
3613 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
3614 return (0);
3617 static int
3618 dump_block_stats(spa_t *spa)
3620 zdb_cb_t zcb;
3621 zdb_blkstats_t *zb, *tzb;
3622 uint64_t norm_alloc, norm_space, total_alloc, total_found;
3623 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
3624 boolean_t leaks = B_FALSE;
3626 bzero(&zcb, sizeof (zcb));
3627 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
3628 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
3629 (dump_opt['c'] == 1) ? "metadata " : "",
3630 dump_opt['c'] ? "checksums " : "",
3631 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
3632 !dump_opt['L'] ? "nothing leaked " : "");
3635 * Load all space maps as SM_ALLOC maps, then traverse the pool
3636 * claiming each block we discover. If the pool is perfectly
3637 * consistent, the space maps will be empty when we're done.
3638 * Anything left over is a leak; any block we can't claim (because
3639 * it's not part of any space map) is a double allocation,
3640 * reference to a freed block, or an unclaimed log block.
3642 zdb_leak_init(spa, &zcb);
3645 * If there's a deferred-free bplist, process that first.
3647 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
3648 count_block_cb, &zcb, NULL);
3650 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3651 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
3652 count_block_cb, &zcb, NULL);
3655 zdb_claim_removing(spa, &zcb);
3657 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
3658 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
3659 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
3660 &zcb, NULL));
3663 if (dump_opt['c'] > 1)
3664 flags |= TRAVERSE_PREFETCH_DATA;
3666 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
3667 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
3668 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
3671 * If we've traversed the data blocks then we need to wait for those
3672 * I/Os to complete. We leverage "The Godfather" zio to wait on
3673 * all async I/Os to complete.
3675 if (dump_opt['c']) {
3676 for (int i = 0; i < max_ncpus; i++) {
3677 (void) zio_wait(spa->spa_async_zio_root[i]);
3678 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3679 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3680 ZIO_FLAG_GODFATHER);
3684 if (zcb.zcb_haderrors) {
3685 (void) printf("\nError counts:\n\n");
3686 (void) printf("\t%5s %s\n", "errno", "count");
3687 for (int e = 0; e < 256; e++) {
3688 if (zcb.zcb_errors[e] != 0) {
3689 (void) printf("\t%5d %llu\n",
3690 e, (u_longlong_t)zcb.zcb_errors[e]);
3696 * Report any leaked segments.
3698 leaks |= zdb_leak_fini(spa, &zcb);
3700 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
3702 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
3703 norm_space = metaslab_class_get_space(spa_normal_class(spa));
3705 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
3706 total_found = tzb->zb_asize - zcb.zcb_dedup_asize +
3707 zcb.zcb_removing_size + zcb.zcb_checkpoint_size;
3709 if (total_found == total_alloc) {
3710 if (!dump_opt['L'])
3711 (void) printf("\n\tNo leaks (block sum matches space"
3712 " maps exactly)\n");
3713 } else {
3714 (void) printf("block traversal size %llu != alloc %llu "
3715 "(%s %lld)\n",
3716 (u_longlong_t)total_found,
3717 (u_longlong_t)total_alloc,
3718 (dump_opt['L']) ? "unreachable" : "leaked",
3719 (longlong_t)(total_alloc - total_found));
3720 leaks = B_TRUE;
3723 if (tzb->zb_count == 0)
3724 return (2);
3726 (void) printf("\n");
3727 (void) printf("\tbp count: %10llu\n",
3728 (u_longlong_t)tzb->zb_count);
3729 (void) printf("\tganged count: %10llu\n",
3730 (longlong_t)tzb->zb_gangs);
3731 (void) printf("\tbp logical: %10llu avg: %6llu\n",
3732 (u_longlong_t)tzb->zb_lsize,
3733 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
3734 (void) printf("\tbp physical: %10llu avg:"
3735 " %6llu compression: %6.2f\n",
3736 (u_longlong_t)tzb->zb_psize,
3737 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
3738 (double)tzb->zb_lsize / tzb->zb_psize);
3739 (void) printf("\tbp allocated: %10llu avg:"
3740 " %6llu compression: %6.2f\n",
3741 (u_longlong_t)tzb->zb_asize,
3742 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
3743 (double)tzb->zb_lsize / tzb->zb_asize);
3744 (void) printf("\tbp deduped: %10llu ref>1:"
3745 " %6llu deduplication: %6.2f\n",
3746 (u_longlong_t)zcb.zcb_dedup_asize,
3747 (u_longlong_t)zcb.zcb_dedup_blocks,
3748 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
3749 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
3750 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
3752 for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
3753 if (zcb.zcb_embedded_blocks[i] == 0)
3754 continue;
3755 (void) printf("\n");
3756 (void) printf("\tadditional, non-pointer bps of type %u: "
3757 "%10llu\n",
3758 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
3760 if (dump_opt['b'] >= 3) {
3761 (void) printf("\t number of (compressed) bytes: "
3762 "number of bps\n");
3763 dump_histogram(zcb.zcb_embedded_histogram[i],
3764 sizeof (zcb.zcb_embedded_histogram[i]) /
3765 sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
3769 if (tzb->zb_ditto_samevdev != 0) {
3770 (void) printf("\tDittoed blocks on same vdev: %llu\n",
3771 (longlong_t)tzb->zb_ditto_samevdev);
3774 for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) {
3775 vdev_t *vd = spa->spa_root_vdev->vdev_child[v];
3776 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3778 if (vim == NULL) {
3779 continue;
3782 char mem[32];
3783 zdb_nicenum(vdev_indirect_mapping_num_entries(vim),
3784 mem, vdev_indirect_mapping_size(vim));
3786 (void) printf("\tindirect vdev id %llu has %llu segments "
3787 "(%s in memory)\n",
3788 (longlong_t)vd->vdev_id,
3789 (longlong_t)vdev_indirect_mapping_num_entries(vim), mem);
3792 if (dump_opt['b'] >= 2) {
3793 int l, t, level;
3794 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
3795 "\t avg\t comp\t%%Total\tType\n");
3797 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
3798 char csize[32], lsize[32], psize[32], asize[32];
3799 char avg[32], gang[32];
3800 const char *typename;
3802 /* make sure nicenum has enough space */
3803 CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
3804 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
3805 CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
3806 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
3807 CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
3808 CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
3810 if (t < DMU_OT_NUMTYPES)
3811 typename = dmu_ot[t].ot_name;
3812 else
3813 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
3815 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
3816 (void) printf("%6s\t%5s\t%5s\t%5s"
3817 "\t%5s\t%5s\t%6s\t%s\n",
3818 "-",
3819 "-",
3820 "-",
3821 "-",
3822 "-",
3823 "-",
3824 "-",
3825 typename);
3826 continue;
3829 for (l = ZB_TOTAL - 1; l >= -1; l--) {
3830 level = (l == -1 ? ZB_TOTAL : l);
3831 zb = &zcb.zcb_type[level][t];
3833 if (zb->zb_asize == 0)
3834 continue;
3836 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
3837 continue;
3839 if (level == 0 && zb->zb_asize ==
3840 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
3841 continue;
3843 zdb_nicenum(zb->zb_count, csize,
3844 sizeof (csize));
3845 zdb_nicenum(zb->zb_lsize, lsize,
3846 sizeof (lsize));
3847 zdb_nicenum(zb->zb_psize, psize,
3848 sizeof (psize));
3849 zdb_nicenum(zb->zb_asize, asize,
3850 sizeof (asize));
3851 zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
3852 sizeof (avg));
3853 zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
3855 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
3856 "\t%5.2f\t%6.2f\t",
3857 csize, lsize, psize, asize, avg,
3858 (double)zb->zb_lsize / zb->zb_psize,
3859 100.0 * zb->zb_asize / tzb->zb_asize);
3861 if (level == ZB_TOTAL)
3862 (void) printf("%s\n", typename);
3863 else
3864 (void) printf(" L%d %s\n",
3865 level, typename);
3867 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
3868 (void) printf("\t number of ganged "
3869 "blocks: %s\n", gang);
3872 if (dump_opt['b'] >= 4) {
3873 (void) printf("psize "
3874 "(in 512-byte sectors): "
3875 "number of blocks\n");
3876 dump_histogram(zb->zb_psize_histogram,
3877 PSIZE_HISTO_SIZE, 0);
3883 (void) printf("\n");
3885 if (leaks)
3886 return (2);
3888 if (zcb.zcb_haderrors)
3889 return (3);
3891 return (0);
3894 typedef struct zdb_ddt_entry {
3895 ddt_key_t zdde_key;
3896 uint64_t zdde_ref_blocks;
3897 uint64_t zdde_ref_lsize;
3898 uint64_t zdde_ref_psize;
3899 uint64_t zdde_ref_dsize;
3900 avl_node_t zdde_node;
3901 } zdb_ddt_entry_t;
3903 /* ARGSUSED */
3904 static int
3905 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
3906 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3908 avl_tree_t *t = arg;
3909 avl_index_t where;
3910 zdb_ddt_entry_t *zdde, zdde_search;
3912 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
3913 return (0);
3915 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
3916 (void) printf("traversing objset %llu, %llu objects, "
3917 "%lu blocks so far\n",
3918 (u_longlong_t)zb->zb_objset,
3919 (u_longlong_t)BP_GET_FILL(bp),
3920 avl_numnodes(t));
3923 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
3924 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
3925 return (0);
3927 ddt_key_fill(&zdde_search.zdde_key, bp);
3929 zdde = avl_find(t, &zdde_search, &where);
3931 if (zdde == NULL) {
3932 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
3933 zdde->zdde_key = zdde_search.zdde_key;
3934 avl_insert(t, zdde, where);
3937 zdde->zdde_ref_blocks += 1;
3938 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
3939 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
3940 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
3942 return (0);
3945 static void
3946 dump_simulated_ddt(spa_t *spa)
3948 avl_tree_t t;
3949 void *cookie = NULL;
3950 zdb_ddt_entry_t *zdde;
3951 ddt_histogram_t ddh_total;
3952 ddt_stat_t dds_total;
3954 bzero(&ddh_total, sizeof (ddh_total));
3955 bzero(&dds_total, sizeof (dds_total));
3956 avl_create(&t, ddt_entry_compare,
3957 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3959 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3961 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
3962 zdb_ddt_add_cb, &t);
3964 spa_config_exit(spa, SCL_CONFIG, FTAG);
3966 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3967 ddt_stat_t dds;
3968 uint64_t refcnt = zdde->zdde_ref_blocks;
3969 ASSERT(refcnt != 0);
3971 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3972 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3973 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3974 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3976 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3977 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3978 dds.dds_ref_psize = zdde->zdde_ref_psize;
3979 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3981 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3982 &dds, 0);
3984 umem_free(zdde, sizeof (*zdde));
3987 avl_destroy(&t);
3989 ddt_histogram_stat(&dds_total, &ddh_total);
3991 (void) printf("Simulated DDT histogram:\n");
3993 zpool_dump_ddt(&dds_total, &ddh_total);
3995 dump_dedup_ratio(&dds_total);
3998 static int
3999 verify_device_removal_feature_counts(spa_t *spa)
4001 uint64_t dr_feature_refcount = 0;
4002 uint64_t oc_feature_refcount = 0;
4003 uint64_t indirect_vdev_count = 0;
4004 uint64_t precise_vdev_count = 0;
4005 uint64_t obsolete_counts_object_count = 0;
4006 uint64_t obsolete_sm_count = 0;
4007 uint64_t obsolete_counts_count = 0;
4008 uint64_t scip_count = 0;
4009 uint64_t obsolete_bpobj_count = 0;
4010 int ret = 0;
4012 spa_condensing_indirect_phys_t *scip =
4013 &spa->spa_condensing_indirect_phys;
4014 if (scip->scip_next_mapping_object != 0) {
4015 vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev];
4016 ASSERT(scip->scip_prev_obsolete_sm_object != 0);
4017 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
4019 (void) printf("Condensing indirect vdev %llu: new mapping "
4020 "object %llu, prev obsolete sm %llu\n",
4021 (u_longlong_t)scip->scip_vdev,
4022 (u_longlong_t)scip->scip_next_mapping_object,
4023 (u_longlong_t)scip->scip_prev_obsolete_sm_object);
4024 if (scip->scip_prev_obsolete_sm_object != 0) {
4025 space_map_t *prev_obsolete_sm = NULL;
4026 VERIFY0(space_map_open(&prev_obsolete_sm,
4027 spa->spa_meta_objset,
4028 scip->scip_prev_obsolete_sm_object,
4029 0, vd->vdev_asize, 0));
4030 space_map_update(prev_obsolete_sm);
4031 dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm);
4032 (void) printf("\n");
4033 space_map_close(prev_obsolete_sm);
4036 scip_count += 2;
4039 for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
4040 vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
4041 vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
4043 if (vic->vic_mapping_object != 0) {
4044 ASSERT(vd->vdev_ops == &vdev_indirect_ops ||
4045 vd->vdev_removing);
4046 indirect_vdev_count++;
4048 if (vd->vdev_indirect_mapping->vim_havecounts) {
4049 obsolete_counts_count++;
4052 if (vdev_obsolete_counts_are_precise(vd)) {
4053 ASSERT(vic->vic_mapping_object != 0);
4054 precise_vdev_count++;
4056 if (vdev_obsolete_sm_object(vd) != 0) {
4057 ASSERT(vic->vic_mapping_object != 0);
4058 obsolete_sm_count++;
4062 (void) feature_get_refcount(spa,
4063 &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL],
4064 &dr_feature_refcount);
4065 (void) feature_get_refcount(spa,
4066 &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS],
4067 &oc_feature_refcount);
4069 if (dr_feature_refcount != indirect_vdev_count) {
4070 ret = 1;
4071 (void) printf("Number of indirect vdevs (%llu) " \
4072 "does not match feature count (%llu)\n",
4073 (u_longlong_t)indirect_vdev_count,
4074 (u_longlong_t)dr_feature_refcount);
4075 } else {
4076 (void) printf("Verified device_removal feature refcount " \
4077 "of %llu is correct\n",
4078 (u_longlong_t)dr_feature_refcount);
4081 if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
4082 DMU_POOL_OBSOLETE_BPOBJ) == 0) {
4083 obsolete_bpobj_count++;
4087 obsolete_counts_object_count = precise_vdev_count;
4088 obsolete_counts_object_count += obsolete_sm_count;
4089 obsolete_counts_object_count += obsolete_counts_count;
4090 obsolete_counts_object_count += scip_count;
4091 obsolete_counts_object_count += obsolete_bpobj_count;
4092 obsolete_counts_object_count += remap_deadlist_count;
4094 if (oc_feature_refcount != obsolete_counts_object_count) {
4095 ret = 1;
4096 (void) printf("Number of obsolete counts objects (%llu) " \
4097 "does not match feature count (%llu)\n",
4098 (u_longlong_t)obsolete_counts_object_count,
4099 (u_longlong_t)oc_feature_refcount);
4100 (void) printf("pv:%llu os:%llu oc:%llu sc:%llu "
4101 "ob:%llu rd:%llu\n",
4102 (u_longlong_t)precise_vdev_count,
4103 (u_longlong_t)obsolete_sm_count,
4104 (u_longlong_t)obsolete_counts_count,
4105 (u_longlong_t)scip_count,
4106 (u_longlong_t)obsolete_bpobj_count,
4107 (u_longlong_t)remap_deadlist_count);
4108 } else {
4109 (void) printf("Verified indirect_refcount feature refcount " \
4110 "of %llu is correct\n",
4111 (u_longlong_t)oc_feature_refcount);
4113 return (ret);
4116 #define BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE"
4118 * Import the checkpointed state of the pool specified by the target
4119 * parameter as readonly. The function also accepts a pool config
4120 * as an optional parameter, else it attempts to infer the config by
4121 * the name of the target pool.
4123 * Note that the checkpointed state's pool name will be the name of
4124 * the original pool with the above suffix appened to it. In addition,
4125 * if the target is not a pool name (e.g. a path to a dataset) then
4126 * the new_path parameter is populated with the updated path to
4127 * reflect the fact that we are looking into the checkpointed state.
4129 * The function returns a newly-allocated copy of the name of the
4130 * pool containing the checkpointed state. When this copy is no
4131 * longer needed it should be freed with free(3C). Same thing
4132 * applies to the new_path parameter if allocated.
4134 static char *
4135 import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
4137 int error = 0;
4138 char *poolname, *bogus_name;
4140 /* If the target is not a pool, the extract the pool name */
4141 char *path_start = strchr(target, '/');
4142 if (path_start != NULL) {
4143 size_t poolname_len = path_start - target;
4144 poolname = strndup(target, poolname_len);
4145 } else {
4146 poolname = target;
4149 if (cfg == NULL) {
4150 error = spa_get_stats(poolname, &cfg, NULL, 0);
4151 if (error != 0) {
4152 fatal("Tried to read config of pool \"%s\" but "
4153 "spa_get_stats() failed with error %d\n",
4154 poolname, error);
4158 (void) asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX);
4159 fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
4161 error = spa_import(bogus_name, cfg, NULL,
4162 ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT);
4163 if (error != 0) {
4164 fatal("Tried to import pool \"%s\" but spa_import() failed "
4165 "with error %d\n", bogus_name, error);
4168 if (new_path != NULL && path_start != NULL)
4169 (void) asprintf(new_path, "%s%s", bogus_name, path_start);
4171 if (target != poolname)
4172 free(poolname);
4174 return (bogus_name);
4177 typedef struct verify_checkpoint_sm_entry_cb_arg {
4178 vdev_t *vcsec_vd;
4180 /* the following fields are only used for printing progress */
4181 uint64_t vcsec_entryid;
4182 uint64_t vcsec_num_entries;
4183 } verify_checkpoint_sm_entry_cb_arg_t;
4185 #define ENTRIES_PER_PROGRESS_UPDATE 10000
4187 static int
4188 verify_checkpoint_sm_entry_cb(space_map_entry_t *sme, void *arg)
4190 verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg;
4191 vdev_t *vd = vcsec->vcsec_vd;
4192 metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
4193 uint64_t end = sme->sme_offset + sme->sme_run;
4195 ASSERT(sme->sme_type == SM_FREE);
4197 if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) {
4198 (void) fprintf(stderr,
4199 "\rverifying vdev %llu, space map entry %llu of %llu ...",
4200 (longlong_t)vd->vdev_id,
4201 (longlong_t)vcsec->vcsec_entryid,
4202 (longlong_t)vcsec->vcsec_num_entries);
4204 vcsec->vcsec_entryid++;
4207 * See comment in checkpoint_sm_exclude_entry_cb()
4209 VERIFY3U(sme->sme_offset, >=, ms->ms_start);
4210 VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
4213 * The entries in the vdev_checkpoint_sm should be marked as
4214 * allocated in the checkpointed state of the pool, therefore
4215 * their respective ms_allocateable trees should not contain them.
4217 mutex_enter(&ms->ms_lock);
4218 range_tree_verify(ms->ms_allocatable, sme->sme_offset, sme->sme_run);
4219 mutex_exit(&ms->ms_lock);
4221 return (0);
4225 * Verify that all segments in the vdev_checkpoint_sm are allocated
4226 * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's
4227 * ms_allocatable).
4229 * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of
4230 * each vdev in the current state of the pool to the metaslab space maps
4231 * (ms_sm) of the checkpointed state of the pool.
4233 * Note that the function changes the state of the ms_allocatable
4234 * trees of the current spa_t. The entries of these ms_allocatable
4235 * trees are cleared out and then repopulated from with the free
4236 * entries of their respective ms_sm space maps.
4238 static void
4239 verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current)
4241 vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
4242 vdev_t *current_rvd = current->spa_root_vdev;
4244 load_concrete_ms_allocatable_trees(checkpoint, SM_FREE);
4246 for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) {
4247 vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c];
4248 vdev_t *current_vd = current_rvd->vdev_child[c];
4250 space_map_t *checkpoint_sm = NULL;
4251 uint64_t checkpoint_sm_obj;
4253 if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
4255 * Since we don't allow device removal in a pool
4256 * that has a checkpoint, we expect that all removed
4257 * vdevs were removed from the pool before the
4258 * checkpoint.
4260 ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
4261 continue;
4265 * If the checkpoint space map doesn't exist, then nothing
4266 * here is checkpointed so there's nothing to verify.
4268 if (current_vd->vdev_top_zap == 0 ||
4269 zap_contains(spa_meta_objset(current),
4270 current_vd->vdev_top_zap,
4271 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
4272 continue;
4274 VERIFY0(zap_lookup(spa_meta_objset(current),
4275 current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
4276 sizeof (uint64_t), 1, &checkpoint_sm_obj));
4278 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current),
4279 checkpoint_sm_obj, 0, current_vd->vdev_asize,
4280 current_vd->vdev_ashift));
4281 space_map_update(checkpoint_sm);
4283 verify_checkpoint_sm_entry_cb_arg_t vcsec;
4284 vcsec.vcsec_vd = ckpoint_vd;
4285 vcsec.vcsec_entryid = 0;
4286 vcsec.vcsec_num_entries =
4287 space_map_length(checkpoint_sm) / sizeof (uint64_t);
4288 VERIFY0(space_map_iterate(checkpoint_sm,
4289 verify_checkpoint_sm_entry_cb, &vcsec));
4290 dump_spacemap(current->spa_meta_objset, checkpoint_sm);
4291 space_map_close(checkpoint_sm);
4295 * If we've added vdevs since we took the checkpoint, ensure
4296 * that their checkpoint space maps are empty.
4298 if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) {
4299 for (uint64_t c = ckpoint_rvd->vdev_children;
4300 c < current_rvd->vdev_children; c++) {
4301 vdev_t *current_vd = current_rvd->vdev_child[c];
4302 ASSERT3P(current_vd->vdev_checkpoint_sm, ==, NULL);
4306 /* for cleaner progress output */
4307 (void) fprintf(stderr, "\n");
4311 * Verifies that all space that's allocated in the checkpoint is
4312 * still allocated in the current version, by checking that everything
4313 * in checkpoint's ms_allocatable (which is actually allocated, not
4314 * allocatable/free) is not present in current's ms_allocatable.
4316 * Note that the function changes the state of the ms_allocatable
4317 * trees of both spas when called. The entries of all ms_allocatable
4318 * trees are cleared out and then repopulated from their respective
4319 * ms_sm space maps. In the checkpointed state we load the allocated
4320 * entries, and in the current state we load the free entries.
4322 static void
4323 verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current)
4325 vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
4326 vdev_t *current_rvd = current->spa_root_vdev;
4328 load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC);
4329 load_concrete_ms_allocatable_trees(current, SM_FREE);
4331 for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) {
4332 vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i];
4333 vdev_t *current_vd = current_rvd->vdev_child[i];
4335 if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
4337 * See comment in verify_checkpoint_vdev_spacemaps()
4339 ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
4340 continue;
4343 for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) {
4344 metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m];
4345 metaslab_t *current_msp = current_vd->vdev_ms[m];
4347 (void) fprintf(stderr,
4348 "\rverifying vdev %llu of %llu, "
4349 "metaslab %llu of %llu ...",
4350 (longlong_t)current_vd->vdev_id,
4351 (longlong_t)current_rvd->vdev_children,
4352 (longlong_t)current_vd->vdev_ms[m]->ms_id,
4353 (longlong_t)current_vd->vdev_ms_count);
4356 * We walk through the ms_allocatable trees that
4357 * are loaded with the allocated blocks from the
4358 * ms_sm spacemaps of the checkpoint. For each
4359 * one of these ranges we ensure that none of them
4360 * exists in the ms_allocatable trees of the
4361 * current state which are loaded with the ranges
4362 * that are currently free.
4364 * This way we ensure that none of the blocks that
4365 * are part of the checkpoint were freed by mistake.
4367 range_tree_walk(ckpoint_msp->ms_allocatable,
4368 (range_tree_func_t *)range_tree_verify,
4369 current_msp->ms_allocatable);
4373 /* for cleaner progress output */
4374 (void) fprintf(stderr, "\n");
4377 static void
4378 verify_checkpoint_blocks(spa_t *spa)
4380 spa_t *checkpoint_spa;
4381 char *checkpoint_pool;
4382 nvlist_t *config = NULL;
4383 int error = 0;
4386 * We import the checkpointed state of the pool (under a different
4387 * name) so we can do verification on it against the current state
4388 * of the pool.
4390 checkpoint_pool = import_checkpointed_state(spa->spa_name, config,
4391 NULL);
4392 ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0);
4394 error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG);
4395 if (error != 0) {
4396 fatal("Tried to open pool \"%s\" but spa_open() failed with "
4397 "error %d\n", checkpoint_pool, error);
4401 * Ensure that ranges in the checkpoint space maps of each vdev
4402 * are allocated according to the checkpointed state's metaslab
4403 * space maps.
4405 verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa);
4408 * Ensure that allocated ranges in the checkpoint's metaslab
4409 * space maps remain allocated in the metaslab space maps of
4410 * the current state.
4412 verify_checkpoint_ms_spacemaps(checkpoint_spa, spa);
4415 * Once we are done, we get rid of the checkpointed state.
4417 spa_close(checkpoint_spa, FTAG);
4418 free(checkpoint_pool);
4421 static void
4422 dump_leftover_checkpoint_blocks(spa_t *spa)
4424 vdev_t *rvd = spa->spa_root_vdev;
4426 for (uint64_t i = 0; i < rvd->vdev_children; i++) {
4427 vdev_t *vd = rvd->vdev_child[i];
4429 space_map_t *checkpoint_sm = NULL;
4430 uint64_t checkpoint_sm_obj;
4432 if (vd->vdev_top_zap == 0)
4433 continue;
4435 if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
4436 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
4437 continue;
4439 VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
4440 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
4441 sizeof (uint64_t), 1, &checkpoint_sm_obj));
4443 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
4444 checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
4445 space_map_update(checkpoint_sm);
4446 dump_spacemap(spa->spa_meta_objset, checkpoint_sm);
4447 space_map_close(checkpoint_sm);
4451 static int
4452 verify_checkpoint(spa_t *spa)
4454 uberblock_t checkpoint;
4455 int error;
4457 if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT))
4458 return (0);
4460 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
4461 DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
4462 sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
4464 if (error == ENOENT && !dump_opt['L']) {
4466 * If the feature is active but the uberblock is missing
4467 * then we must be in the middle of discarding the
4468 * checkpoint.
4470 (void) printf("\nPartially discarded checkpoint "
4471 "state found:\n");
4472 dump_leftover_checkpoint_blocks(spa);
4473 return (0);
4474 } else if (error != 0) {
4475 (void) printf("lookup error %d when looking for "
4476 "checkpointed uberblock in MOS\n", error);
4477 return (error);
4479 dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n");
4481 if (checkpoint.ub_checkpoint_txg == 0) {
4482 (void) printf("\nub_checkpoint_txg not set in checkpointed "
4483 "uberblock\n");
4484 error = 3;
4487 if (error == 0 && !dump_opt['L'])
4488 verify_checkpoint_blocks(spa);
4490 return (error);
4493 /* ARGSUSED */
4494 static void
4495 mos_leaks_cb(void *arg, uint64_t start, uint64_t size)
4497 for (uint64_t i = start; i < size; i++) {
4498 (void) printf("MOS object %llu referenced but not allocated\n",
4499 (u_longlong_t)i);
4503 static range_tree_t *mos_refd_objs;
4505 static void
4506 mos_obj_refd(uint64_t obj)
4508 if (obj != 0 && mos_refd_objs != NULL)
4509 range_tree_add(mos_refd_objs, obj, 1);
4512 static void
4513 mos_leak_vdev(vdev_t *vd)
4515 mos_obj_refd(vd->vdev_dtl_object);
4516 mos_obj_refd(vd->vdev_ms_array);
4517 mos_obj_refd(vd->vdev_top_zap);
4518 mos_obj_refd(vd->vdev_indirect_config.vic_births_object);
4519 mos_obj_refd(vd->vdev_indirect_config.vic_mapping_object);
4520 mos_obj_refd(vd->vdev_leaf_zap);
4521 if (vd->vdev_checkpoint_sm != NULL)
4522 mos_obj_refd(vd->vdev_checkpoint_sm->sm_object);
4523 if (vd->vdev_indirect_mapping != NULL) {
4524 mos_obj_refd(vd->vdev_indirect_mapping->
4525 vim_phys->vimp_counts_object);
4527 if (vd->vdev_obsolete_sm != NULL)
4528 mos_obj_refd(vd->vdev_obsolete_sm->sm_object);
4530 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
4531 metaslab_t *ms = vd->vdev_ms[m];
4532 mos_obj_refd(space_map_object(ms->ms_sm));
4535 for (uint64_t c = 0; c < vd->vdev_children; c++) {
4536 mos_leak_vdev(vd->vdev_child[c]);
4540 static int
4541 dump_mos_leaks(spa_t *spa)
4543 int rv = 0;
4544 objset_t *mos = spa->spa_meta_objset;
4545 dsl_pool_t *dp = spa->spa_dsl_pool;
4547 /* Visit and mark all referenced objects in the MOS */
4549 mos_obj_refd(DMU_POOL_DIRECTORY_OBJECT);
4550 mos_obj_refd(spa->spa_pool_props_object);
4551 mos_obj_refd(spa->spa_config_object);
4552 mos_obj_refd(spa->spa_ddt_stat_object);
4553 mos_obj_refd(spa->spa_feat_desc_obj);
4554 mos_obj_refd(spa->spa_feat_enabled_txg_obj);
4555 mos_obj_refd(spa->spa_feat_for_read_obj);
4556 mos_obj_refd(spa->spa_feat_for_write_obj);
4557 mos_obj_refd(spa->spa_history);
4558 mos_obj_refd(spa->spa_errlog_last);
4559 mos_obj_refd(spa->spa_errlog_scrub);
4560 mos_obj_refd(spa->spa_all_vdev_zaps);
4561 mos_obj_refd(spa->spa_dsl_pool->dp_bptree_obj);
4562 mos_obj_refd(spa->spa_dsl_pool->dp_tmp_userrefs_obj);
4563 mos_obj_refd(spa->spa_dsl_pool->dp_scan->scn_phys.scn_queue_obj);
4564 bpobj_count_refd(&spa->spa_deferred_bpobj);
4565 mos_obj_refd(dp->dp_empty_bpobj);
4566 bpobj_count_refd(&dp->dp_obsolete_bpobj);
4567 bpobj_count_refd(&dp->dp_free_bpobj);
4568 mos_obj_refd(spa->spa_l2cache.sav_object);
4569 mos_obj_refd(spa->spa_spares.sav_object);
4571 mos_obj_refd(spa->spa_condensing_indirect_phys.
4572 scip_next_mapping_object);
4573 mos_obj_refd(spa->spa_condensing_indirect_phys.
4574 scip_prev_obsolete_sm_object);
4575 if (spa->spa_condensing_indirect_phys.scip_next_mapping_object != 0) {
4576 vdev_indirect_mapping_t *vim =
4577 vdev_indirect_mapping_open(mos,
4578 spa->spa_condensing_indirect_phys.scip_next_mapping_object);
4579 mos_obj_refd(vim->vim_phys->vimp_counts_object);
4580 vdev_indirect_mapping_close(vim);
4583 if (dp->dp_origin_snap != NULL) {
4584 dsl_dataset_t *ds;
4586 dsl_pool_config_enter(dp, FTAG);
4587 VERIFY0(dsl_dataset_hold_obj(dp,
4588 dsl_dataset_phys(dp->dp_origin_snap)->ds_next_snap_obj,
4589 FTAG, &ds));
4590 count_ds_mos_objects(ds);
4591 dump_deadlist(&ds->ds_deadlist);
4592 dsl_dataset_rele(ds, FTAG);
4593 dsl_pool_config_exit(dp, FTAG);
4595 count_ds_mos_objects(dp->dp_origin_snap);
4596 dump_deadlist(&dp->dp_origin_snap->ds_deadlist);
4598 count_dir_mos_objects(dp->dp_mos_dir);
4599 if (dp->dp_free_dir != NULL)
4600 count_dir_mos_objects(dp->dp_free_dir);
4601 if (dp->dp_leak_dir != NULL)
4602 count_dir_mos_objects(dp->dp_leak_dir);
4604 mos_leak_vdev(spa->spa_root_vdev);
4606 for (uint64_t class = 0; class < DDT_CLASSES; class++) {
4607 for (uint64_t type = 0; type < DDT_TYPES; type++) {
4608 for (uint64_t cksum = 0;
4609 cksum < ZIO_CHECKSUM_FUNCTIONS; cksum++) {
4610 ddt_t *ddt = spa->spa_ddt[cksum];
4611 mos_obj_refd(ddt->ddt_object[type][class]);
4617 * Visit all allocated objects and make sure they are referenced.
4619 uint64_t object = 0;
4620 while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) {
4621 if (range_tree_contains(mos_refd_objs, object, 1)) {
4622 range_tree_remove(mos_refd_objs, object, 1);
4623 } else {
4624 dmu_object_info_t doi;
4625 const char *name;
4626 dmu_object_info(mos, object, &doi);
4627 if (doi.doi_type & DMU_OT_NEWTYPE) {
4628 dmu_object_byteswap_t bswap =
4629 DMU_OT_BYTESWAP(doi.doi_type);
4630 name = dmu_ot_byteswap[bswap].ob_name;
4631 } else {
4632 name = dmu_ot[doi.doi_type].ot_name;
4635 (void) printf("MOS object %llu (%s) leaked\n",
4636 (u_longlong_t)object, name);
4637 rv = 2;
4640 (void) range_tree_walk(mos_refd_objs, mos_leaks_cb, NULL);
4641 if (!range_tree_is_empty(mos_refd_objs))
4642 rv = 2;
4643 range_tree_vacate(mos_refd_objs, NULL, NULL);
4644 range_tree_destroy(mos_refd_objs);
4645 return (rv);
4648 static void
4649 dump_zpool(spa_t *spa)
4651 dsl_pool_t *dp = spa_get_dsl(spa);
4652 int rc = 0;
4654 if (dump_opt['S']) {
4655 dump_simulated_ddt(spa);
4656 return;
4659 if (!dump_opt['e'] && dump_opt['C'] > 1) {
4660 (void) printf("\nCached configuration:\n");
4661 dump_nvlist(spa->spa_config, 8);
4664 if (dump_opt['C'])
4665 dump_config(spa);
4667 if (dump_opt['u'])
4668 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
4670 if (dump_opt['D'])
4671 dump_all_ddts(spa);
4673 if (dump_opt['d'] > 2 || dump_opt['m'])
4674 dump_metaslabs(spa);
4675 if (dump_opt['M'])
4676 dump_metaslab_groups(spa);
4678 if (dump_opt['d'] || dump_opt['i']) {
4679 mos_refd_objs = range_tree_create(NULL, NULL);
4680 dump_dir(dp->dp_meta_objset);
4682 if (dump_opt['d'] >= 3) {
4683 dsl_pool_t *dp = spa->spa_dsl_pool;
4684 dump_full_bpobj(&spa->spa_deferred_bpobj,
4685 "Deferred frees", 0);
4686 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
4687 dump_full_bpobj(&dp->dp_free_bpobj,
4688 "Pool snapshot frees", 0);
4690 if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
4691 ASSERT(spa_feature_is_enabled(spa,
4692 SPA_FEATURE_DEVICE_REMOVAL));
4693 dump_full_bpobj(&dp->dp_obsolete_bpobj,
4694 "Pool obsolete blocks", 0);
4697 if (spa_feature_is_active(spa,
4698 SPA_FEATURE_ASYNC_DESTROY)) {
4699 dump_bptree(spa->spa_meta_objset,
4700 dp->dp_bptree_obj,
4701 "Pool dataset frees");
4703 dump_dtl(spa->spa_root_vdev, 0);
4705 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
4706 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
4708 if (rc == 0 && !dump_opt['L'])
4709 rc = dump_mos_leaks(spa);
4711 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
4712 uint64_t refcount;
4714 if (!(spa_feature_table[f].fi_flags &
4715 ZFEATURE_FLAG_PER_DATASET) ||
4716 !spa_feature_is_enabled(spa, f)) {
4717 ASSERT0(dataset_feature_count[f]);
4718 continue;
4720 (void) feature_get_refcount(spa,
4721 &spa_feature_table[f], &refcount);
4722 if (dataset_feature_count[f] != refcount) {
4723 (void) printf("%s feature refcount mismatch: "
4724 "%lld datasets != %lld refcount\n",
4725 spa_feature_table[f].fi_uname,
4726 (longlong_t)dataset_feature_count[f],
4727 (longlong_t)refcount);
4728 rc = 2;
4729 } else {
4730 (void) printf("Verified %s feature refcount "
4731 "of %llu is correct\n",
4732 spa_feature_table[f].fi_uname,
4733 (longlong_t)refcount);
4737 if (rc == 0) {
4738 rc = verify_device_removal_feature_counts(spa);
4742 if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
4743 rc = dump_block_stats(spa);
4745 if (rc == 0)
4746 rc = verify_spacemap_refcounts(spa);
4748 if (dump_opt['s'])
4749 show_pool_stats(spa);
4751 if (dump_opt['h'])
4752 dump_history(spa);
4754 if (rc == 0)
4755 rc = verify_checkpoint(spa);
4757 if (rc != 0) {
4758 dump_debug_buffer();
4759 exit(rc);
4763 #define ZDB_FLAG_CHECKSUM 0x0001
4764 #define ZDB_FLAG_DECOMPRESS 0x0002
4765 #define ZDB_FLAG_BSWAP 0x0004
4766 #define ZDB_FLAG_GBH 0x0008
4767 #define ZDB_FLAG_INDIRECT 0x0010
4768 #define ZDB_FLAG_PHYS 0x0020
4769 #define ZDB_FLAG_RAW 0x0040
4770 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
4772 static int flagbits[256];
4774 static void
4775 zdb_print_blkptr(blkptr_t *bp, int flags)
4777 char blkbuf[BP_SPRINTF_LEN];
4779 if (flags & ZDB_FLAG_BSWAP)
4780 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
4782 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
4783 (void) printf("%s\n", blkbuf);
4786 static void
4787 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
4789 int i;
4791 for (i = 0; i < nbps; i++)
4792 zdb_print_blkptr(&bp[i], flags);
4795 static void
4796 zdb_dump_gbh(void *buf, int flags)
4798 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
4801 static void
4802 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
4804 if (flags & ZDB_FLAG_BSWAP)
4805 byteswap_uint64_array(buf, size);
4806 (void) write(1, buf, size);
4809 static void
4810 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
4812 uint64_t *d = (uint64_t *)buf;
4813 unsigned nwords = size / sizeof (uint64_t);
4814 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
4815 unsigned i, j;
4816 const char *hdr;
4817 char *c;
4820 if (do_bswap)
4821 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
4822 else
4823 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
4825 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
4827 for (i = 0; i < nwords; i += 2) {
4828 (void) printf("%06llx: %016llx %016llx ",
4829 (u_longlong_t)(i * sizeof (uint64_t)),
4830 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
4831 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
4833 c = (char *)&d[i];
4834 for (j = 0; j < 2 * sizeof (uint64_t); j++)
4835 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
4836 (void) printf("\n");
4841 * There are two acceptable formats:
4842 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
4843 * child[.child]* - For example: 0.1.1
4845 * The second form can be used to specify arbitrary vdevs anywhere
4846 * in the heirarchy. For example, in a pool with a mirror of
4847 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
4849 static vdev_t *
4850 zdb_vdev_lookup(vdev_t *vdev, const char *path)
4852 char *s, *p, *q;
4853 unsigned i;
4855 if (vdev == NULL)
4856 return (NULL);
4858 /* First, assume the x.x.x.x format */
4859 i = strtoul(path, &s, 10);
4860 if (s == path || (s && *s != '.' && *s != '\0'))
4861 goto name;
4862 if (i >= vdev->vdev_children)
4863 return (NULL);
4865 vdev = vdev->vdev_child[i];
4866 if (*s == '\0')
4867 return (vdev);
4868 return (zdb_vdev_lookup(vdev, s+1));
4870 name:
4871 for (i = 0; i < vdev->vdev_children; i++) {
4872 vdev_t *vc = vdev->vdev_child[i];
4874 if (vc->vdev_path == NULL) {
4875 vc = zdb_vdev_lookup(vc, path);
4876 if (vc == NULL)
4877 continue;
4878 else
4879 return (vc);
4882 p = strrchr(vc->vdev_path, '/');
4883 p = p ? p + 1 : vc->vdev_path;
4884 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
4886 if (strcmp(vc->vdev_path, path) == 0)
4887 return (vc);
4888 if (strcmp(p, path) == 0)
4889 return (vc);
4890 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
4891 return (vc);
4894 return (NULL);
4897 /* ARGSUSED */
4898 static int
4899 random_get_pseudo_bytes_cb(void *buf, size_t len, void *unused)
4901 return (random_get_pseudo_bytes(buf, len));
4905 * Read a block from a pool and print it out. The syntax of the
4906 * block descriptor is:
4908 * pool:vdev_specifier:offset:size[:flags]
4910 * pool - The name of the pool you wish to read from
4911 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
4912 * offset - offset, in hex, in bytes
4913 * size - Amount of data to read, in hex, in bytes
4914 * flags - A string of characters specifying options
4915 * b: Decode a blkptr at given offset within block
4916 * *c: Calculate and display checksums
4917 * d: Decompress data before dumping
4918 * e: Byteswap data before dumping
4919 * g: Display data as a gang block header
4920 * i: Display as an indirect block
4921 * p: Do I/O to physical offset
4922 * r: Dump raw data to stdout
4924 * * = not yet implemented
4926 static void
4927 zdb_read_block(char *thing, spa_t *spa)
4929 blkptr_t blk, *bp = &blk;
4930 dva_t *dva = bp->blk_dva;
4931 int flags = 0;
4932 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
4933 zio_t *zio;
4934 vdev_t *vd;
4935 abd_t *pabd;
4936 void *lbuf, *buf;
4937 const char *s, *vdev;
4938 char *p, *dup, *flagstr;
4939 int i, error;
4941 dup = strdup(thing);
4942 s = strtok(dup, ":");
4943 vdev = s ? s : "";
4944 s = strtok(NULL, ":");
4945 offset = strtoull(s ? s : "", NULL, 16);
4946 s = strtok(NULL, ":");
4947 size = strtoull(s ? s : "", NULL, 16);
4948 s = strtok(NULL, ":");
4949 if (s)
4950 flagstr = strdup(s);
4951 else
4952 flagstr = strdup("");
4954 s = NULL;
4955 if (size == 0)
4956 s = "size must not be zero";
4957 if (!IS_P2ALIGNED(size, DEV_BSIZE))
4958 s = "size must be a multiple of sector size";
4959 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
4960 s = "offset must be a multiple of sector size";
4961 if (s) {
4962 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
4963 free(dup);
4964 return;
4967 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
4968 for (i = 0; flagstr[i]; i++) {
4969 int bit = flagbits[(uchar_t)flagstr[i]];
4971 if (bit == 0) {
4972 (void) printf("***Invalid flag: %c\n",
4973 flagstr[i]);
4974 continue;
4976 flags |= bit;
4978 /* If it's not something with an argument, keep going */
4979 if ((bit & (ZDB_FLAG_CHECKSUM |
4980 ZDB_FLAG_PRINT_BLKPTR)) == 0)
4981 continue;
4983 p = &flagstr[i + 1];
4984 if (bit == ZDB_FLAG_PRINT_BLKPTR)
4985 blkptr_offset = strtoull(p, &p, 16);
4986 if (*p != ':' && *p != '\0') {
4987 (void) printf("***Invalid flag arg: '%s'\n", s);
4988 free(dup);
4989 return;
4993 free(flagstr);
4995 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
4996 if (vd == NULL) {
4997 (void) printf("***Invalid vdev: %s\n", vdev);
4998 free(dup);
4999 return;
5000 } else {
5001 if (vd->vdev_path)
5002 (void) fprintf(stderr, "Found vdev: %s\n",
5003 vd->vdev_path);
5004 else
5005 (void) fprintf(stderr, "Found vdev type: %s\n",
5006 vd->vdev_ops->vdev_op_type);
5009 psize = size;
5010 lsize = size;
5012 pabd = abd_alloc_linear(SPA_MAXBLOCKSIZE, B_FALSE);
5013 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5015 BP_ZERO(bp);
5017 DVA_SET_VDEV(&dva[0], vd->vdev_id);
5018 DVA_SET_OFFSET(&dva[0], offset);
5019 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
5020 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
5022 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
5024 BP_SET_LSIZE(bp, lsize);
5025 BP_SET_PSIZE(bp, psize);
5026 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
5027 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
5028 BP_SET_TYPE(bp, DMU_OT_NONE);
5029 BP_SET_LEVEL(bp, 0);
5030 BP_SET_DEDUP(bp, 0);
5031 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
5033 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5034 zio = zio_root(spa, NULL, NULL, 0);
5036 if (vd == vd->vdev_top) {
5038 * Treat this as a normal block read.
5040 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
5041 ZIO_PRIORITY_SYNC_READ,
5042 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
5043 } else {
5045 * Treat this as a vdev child I/O.
5047 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
5048 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
5049 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
5050 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
5051 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | ZIO_FLAG_OPTIONAL,
5052 NULL, NULL));
5055 error = zio_wait(zio);
5056 spa_config_exit(spa, SCL_STATE, FTAG);
5058 if (error) {
5059 (void) printf("Read of %s failed, error: %d\n", thing, error);
5060 goto out;
5063 if (flags & ZDB_FLAG_DECOMPRESS) {
5065 * We don't know how the data was compressed, so just try
5066 * every decompress function at every inflated blocksize.
5068 enum zio_compress c;
5069 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5070 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
5072 abd_copy_to_buf(pbuf2, pabd, psize);
5074 VERIFY0(abd_iterate_func(pabd, psize, SPA_MAXBLOCKSIZE - psize,
5075 random_get_pseudo_bytes_cb, NULL));
5077 VERIFY0(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
5078 SPA_MAXBLOCKSIZE - psize));
5080 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
5081 lsize -= SPA_MINBLOCKSIZE) {
5082 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
5083 if (zio_decompress_data(c, pabd,
5084 lbuf, psize, lsize) == 0 &&
5085 zio_decompress_data_buf(c, pbuf2,
5086 lbuf2, psize, lsize) == 0 &&
5087 bcmp(lbuf, lbuf2, lsize) == 0)
5088 break;
5090 if (c != ZIO_COMPRESS_FUNCTIONS)
5091 break;
5092 lsize -= SPA_MINBLOCKSIZE;
5095 umem_free(pbuf2, SPA_MAXBLOCKSIZE);
5096 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
5098 if (lsize <= psize) {
5099 (void) printf("Decompress of %s failed\n", thing);
5100 goto out;
5102 buf = lbuf;
5103 size = lsize;
5104 } else {
5105 buf = abd_to_buf(pabd);
5106 size = psize;
5109 if (flags & ZDB_FLAG_PRINT_BLKPTR)
5110 zdb_print_blkptr((blkptr_t *)(void *)
5111 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
5112 else if (flags & ZDB_FLAG_RAW)
5113 zdb_dump_block_raw(buf, size, flags);
5114 else if (flags & ZDB_FLAG_INDIRECT)
5115 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
5116 flags);
5117 else if (flags & ZDB_FLAG_GBH)
5118 zdb_dump_gbh(buf, flags);
5119 else
5120 zdb_dump_block(thing, buf, size, flags);
5122 out:
5123 abd_free(pabd);
5124 umem_free(lbuf, SPA_MAXBLOCKSIZE);
5125 free(dup);
5128 static void
5129 zdb_embedded_block(char *thing)
5131 blkptr_t bp;
5132 unsigned long long *words = (void *)&bp;
5133 char *buf;
5134 int err;
5136 bzero(&bp, sizeof (bp));
5137 err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
5138 "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
5139 words + 0, words + 1, words + 2, words + 3,
5140 words + 4, words + 5, words + 6, words + 7,
5141 words + 8, words + 9, words + 10, words + 11,
5142 words + 12, words + 13, words + 14, words + 15);
5143 if (err != 16) {
5144 (void) fprintf(stderr, "invalid input format\n");
5145 exit(1);
5147 ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
5148 buf = malloc(SPA_MAXBLOCKSIZE);
5149 if (buf == NULL) {
5150 (void) fprintf(stderr, "out of memory\n");
5151 exit(1);
5153 err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
5154 if (err != 0) {
5155 (void) fprintf(stderr, "decode failed: %u\n", err);
5156 exit(1);
5158 zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
5159 free(buf);
5162 static boolean_t
5163 pool_match(nvlist_t *cfg, char *tgt)
5165 uint64_t v, guid = strtoull(tgt, NULL, 0);
5166 char *s;
5168 if (guid != 0) {
5169 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
5170 return (v == guid);
5171 } else {
5172 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
5173 return (strcmp(s, tgt) == 0);
5175 return (B_FALSE);
5178 static char *
5179 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
5181 nvlist_t *pools;
5182 nvlist_t *match = NULL;
5183 char *name = NULL;
5184 char *sepp = NULL;
5185 char sep = '\0';
5186 int count = 0;
5187 importargs_t args;
5189 bzero(&args, sizeof (args));
5190 args.paths = dirc;
5191 args.path = dirv;
5192 args.can_be_active = B_TRUE;
5194 if ((sepp = strpbrk(*target, "/@")) != NULL) {
5195 sep = *sepp;
5196 *sepp = '\0';
5199 pools = zpool_search_import(g_zfs, &args);
5201 if (pools != NULL) {
5202 nvpair_t *elem = NULL;
5203 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
5204 verify(nvpair_value_nvlist(elem, configp) == 0);
5205 if (pool_match(*configp, *target)) {
5206 count++;
5207 if (match != NULL) {
5208 /* print previously found config */
5209 if (name != NULL) {
5210 (void) printf("%s\n", name);
5211 dump_nvlist(match, 8);
5212 name = NULL;
5214 (void) printf("%s\n",
5215 nvpair_name(elem));
5216 dump_nvlist(*configp, 8);
5217 } else {
5218 match = *configp;
5219 name = nvpair_name(elem);
5224 if (count > 1)
5225 (void) fatal("\tMatched %d pools - use pool GUID "
5226 "instead of pool name or \n"
5227 "\tpool name part of a dataset name to select pool", count);
5229 if (sepp)
5230 *sepp = sep;
5232 * If pool GUID was specified for pool id, replace it with pool name
5234 if (name && (strstr(*target, name) != *target)) {
5235 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
5237 *target = umem_alloc(sz, UMEM_NOFAIL);
5238 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
5241 *configp = name ? match : NULL;
5243 return (name);
5247 main(int argc, char **argv)
5249 int c;
5250 struct rlimit rl = { 1024, 1024 };
5251 spa_t *spa = NULL;
5252 objset_t *os = NULL;
5253 int dump_all = 1;
5254 int verbose = 0;
5255 int error = 0;
5256 char **searchdirs = NULL;
5257 int nsearch = 0;
5258 char *target;
5259 nvlist_t *policy = NULL;
5260 uint64_t max_txg = UINT64_MAX;
5261 int flags = ZFS_IMPORT_MISSING_LOG;
5262 int rewind = ZPOOL_NEVER_REWIND;
5263 char *spa_config_path_env;
5264 boolean_t target_is_spa = B_TRUE;
5265 nvlist_t *cfg = NULL;
5267 (void) setrlimit(RLIMIT_NOFILE, &rl);
5268 (void) enable_extended_FILE_stdio(-1, -1);
5270 dprintf_setup(&argc, argv);
5273 * If there is an environment variable SPA_CONFIG_PATH it overrides
5274 * default spa_config_path setting. If -U flag is specified it will
5275 * override this environment variable settings once again.
5277 spa_config_path_env = getenv("SPA_CONFIG_PATH");
5278 if (spa_config_path_env != NULL)
5279 spa_config_path = spa_config_path_env;
5281 while ((c = getopt(argc, argv,
5282 "AbcCdDeEFGhiI:klLmMo:Op:PqRsSt:uU:vVx:X")) != -1) {
5283 switch (c) {
5284 case 'b':
5285 case 'c':
5286 case 'C':
5287 case 'd':
5288 case 'D':
5289 case 'E':
5290 case 'G':
5291 case 'h':
5292 case 'i':
5293 case 'l':
5294 case 'm':
5295 case 'M':
5296 case 'O':
5297 case 'R':
5298 case 's':
5299 case 'S':
5300 case 'u':
5301 dump_opt[c]++;
5302 dump_all = 0;
5303 break;
5304 case 'A':
5305 case 'e':
5306 case 'F':
5307 case 'k':
5308 case 'L':
5309 case 'P':
5310 case 'q':
5311 case 'X':
5312 dump_opt[c]++;
5313 break;
5314 /* NB: Sort single match options below. */
5315 case 'I':
5316 max_inflight = strtoull(optarg, NULL, 0);
5317 if (max_inflight == 0) {
5318 (void) fprintf(stderr, "maximum number "
5319 "of inflight I/Os must be greater "
5320 "than 0\n");
5321 usage();
5323 break;
5324 case 'o':
5325 error = set_global_var(optarg);
5326 if (error != 0)
5327 usage();
5328 break;
5329 case 'p':
5330 if (searchdirs == NULL) {
5331 searchdirs = umem_alloc(sizeof (char *),
5332 UMEM_NOFAIL);
5333 } else {
5334 char **tmp = umem_alloc((nsearch + 1) *
5335 sizeof (char *), UMEM_NOFAIL);
5336 bcopy(searchdirs, tmp, nsearch *
5337 sizeof (char *));
5338 umem_free(searchdirs,
5339 nsearch * sizeof (char *));
5340 searchdirs = tmp;
5342 searchdirs[nsearch++] = optarg;
5343 break;
5344 case 't':
5345 max_txg = strtoull(optarg, NULL, 0);
5346 if (max_txg < TXG_INITIAL) {
5347 (void) fprintf(stderr, "incorrect txg "
5348 "specified: %s\n", optarg);
5349 usage();
5351 break;
5352 case 'U':
5353 spa_config_path = optarg;
5354 if (spa_config_path[0] != '/') {
5355 (void) fprintf(stderr,
5356 "cachefile must be an absolute path "
5357 "(i.e. start with a slash)\n");
5358 usage();
5360 break;
5361 case 'v':
5362 verbose++;
5363 break;
5364 case 'V':
5365 flags = ZFS_IMPORT_VERBATIM;
5366 break;
5367 case 'x':
5368 vn_dumpdir = optarg;
5369 break;
5370 default:
5371 usage();
5372 break;
5376 if (!dump_opt['e'] && searchdirs != NULL) {
5377 (void) fprintf(stderr, "-p option requires use of -e\n");
5378 usage();
5382 * ZDB does not typically re-read blocks; therefore limit the ARC
5383 * to 256 MB, which can be used entirely for metadata.
5385 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
5388 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
5389 * "zdb -b" uses traversal prefetch which uses async reads.
5390 * For good performance, let several of them be active at once.
5392 zfs_vdev_async_read_max_active = 10;
5395 * Disable reference tracking for better performance.
5397 reference_tracking_enable = B_FALSE;
5400 * Do not fail spa_load when spa_load_verify fails. This is needed
5401 * to load non-idle pools.
5403 spa_load_verify_dryrun = B_TRUE;
5405 kernel_init(FREAD);
5406 g_zfs = libzfs_init();
5407 ASSERT(g_zfs != NULL);
5409 if (dump_all)
5410 verbose = MAX(verbose, 1);
5412 for (c = 0; c < 256; c++) {
5413 if (dump_all && strchr("AeEFklLOPRSX", c) == NULL)
5414 dump_opt[c] = 1;
5415 if (dump_opt[c])
5416 dump_opt[c] += verbose;
5419 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
5420 zfs_recover = (dump_opt['A'] > 1);
5422 argc -= optind;
5423 argv += optind;
5425 if (argc < 2 && dump_opt['R'])
5426 usage();
5428 if (dump_opt['E']) {
5429 if (argc != 1)
5430 usage();
5431 zdb_embedded_block(argv[0]);
5432 return (0);
5435 if (argc < 1) {
5436 if (!dump_opt['e'] && dump_opt['C']) {
5437 dump_cachefile(spa_config_path);
5438 return (0);
5440 usage();
5443 if (dump_opt['l'])
5444 return (dump_label(argv[0]));
5446 if (dump_opt['O']) {
5447 if (argc != 2)
5448 usage();
5449 dump_opt['v'] = verbose + 3;
5450 return (dump_path(argv[0], argv[1]));
5453 if (dump_opt['X'] || dump_opt['F'])
5454 rewind = ZPOOL_DO_REWIND |
5455 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
5457 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
5458 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, max_txg) != 0 ||
5459 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind) != 0)
5460 fatal("internal error: %s", strerror(ENOMEM));
5462 error = 0;
5463 target = argv[0];
5465 if (dump_opt['e']) {
5466 char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
5468 error = ENOENT;
5469 if (name) {
5470 if (dump_opt['C'] > 1) {
5471 (void) printf("\nConfiguration for import:\n");
5472 dump_nvlist(cfg, 8);
5475 if (nvlist_add_nvlist(cfg,
5476 ZPOOL_LOAD_POLICY, policy) != 0) {
5477 fatal("can't open '%s': %s",
5478 target, strerror(ENOMEM));
5480 error = spa_import(name, cfg, NULL, flags);
5484 char *checkpoint_pool = NULL;
5485 char *checkpoint_target = NULL;
5486 if (dump_opt['k']) {
5487 checkpoint_pool = import_checkpointed_state(target, cfg,
5488 &checkpoint_target);
5490 if (checkpoint_target != NULL)
5491 target = checkpoint_target;
5495 if (strpbrk(target, "/@") != NULL) {
5496 size_t targetlen;
5498 target_is_spa = B_FALSE;
5500 * Remove any trailing slash. Later code would get confused
5501 * by it, but we want to allow it so that "pool/" can
5502 * indicate that we want to dump the topmost filesystem,
5503 * rather than the whole pool.
5505 targetlen = strlen(target);
5506 if (targetlen != 0 && target[targetlen - 1] == '/')
5507 target[targetlen - 1] = '\0';
5510 if (error == 0) {
5511 if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) {
5512 ASSERT(checkpoint_pool != NULL);
5513 ASSERT(checkpoint_target == NULL);
5515 error = spa_open(checkpoint_pool, &spa, FTAG);
5516 if (error != 0) {
5517 fatal("Tried to open pool \"%s\" but "
5518 "spa_open() failed with error %d\n",
5519 checkpoint_pool, error);
5522 } else if (target_is_spa || dump_opt['R']) {
5523 error = spa_open_rewind(target, &spa, FTAG, policy,
5524 NULL);
5525 if (error) {
5527 * If we're missing the log device then
5528 * try opening the pool after clearing the
5529 * log state.
5531 mutex_enter(&spa_namespace_lock);
5532 if ((spa = spa_lookup(target)) != NULL &&
5533 spa->spa_log_state == SPA_LOG_MISSING) {
5534 spa->spa_log_state = SPA_LOG_CLEAR;
5535 error = 0;
5537 mutex_exit(&spa_namespace_lock);
5539 if (!error) {
5540 error = spa_open_rewind(target, &spa,
5541 FTAG, policy, NULL);
5544 } else {
5545 error = open_objset(target, DMU_OST_ANY, FTAG, &os);
5548 nvlist_free(policy);
5550 if (error)
5551 fatal("can't open '%s': %s", target, strerror(error));
5553 argv++;
5554 argc--;
5555 if (!dump_opt['R']) {
5556 if (argc > 0) {
5557 zopt_objects = argc;
5558 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
5559 for (unsigned i = 0; i < zopt_objects; i++) {
5560 errno = 0;
5561 zopt_object[i] = strtoull(argv[i], NULL, 0);
5562 if (zopt_object[i] == 0 && errno != 0)
5563 fatal("bad number %s: %s",
5564 argv[i], strerror(errno));
5567 if (os != NULL) {
5568 dump_dir(os);
5569 } else if (zopt_objects > 0 && !dump_opt['m']) {
5570 dump_dir(spa->spa_meta_objset);
5571 } else {
5572 dump_zpool(spa);
5574 } else {
5575 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
5576 flagbits['c'] = ZDB_FLAG_CHECKSUM;
5577 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
5578 flagbits['e'] = ZDB_FLAG_BSWAP;
5579 flagbits['g'] = ZDB_FLAG_GBH;
5580 flagbits['i'] = ZDB_FLAG_INDIRECT;
5581 flagbits['p'] = ZDB_FLAG_PHYS;
5582 flagbits['r'] = ZDB_FLAG_RAW;
5584 for (int i = 0; i < argc; i++)
5585 zdb_read_block(argv[i], spa);
5588 if (dump_opt['k']) {
5589 free(checkpoint_pool);
5590 if (!target_is_spa)
5591 free(checkpoint_target);
5594 if (os != NULL)
5595 close_objset(os, FTAG);
5596 else
5597 spa_close(spa, FTAG);
5599 fuid_table_destroy();
5601 dump_debug_buffer();
5603 libzfs_fini(g_zfs);
5604 kernel_fini();
5606 return (error);