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