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