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