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