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