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