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