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