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