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