Correct some casts in printf arguments in various utilities.
[dragonfly.git] / sbin / hammer / cmd_blockmap.c
CommitLineData
eb3f8f1f
MD
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
84e57c2c 3 *
eb3f8f1f
MD
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
84e57c2c 6 *
eb3f8f1f
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
84e57c2c 10 *
eb3f8f1f
MD
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
84e57c2c 20 *
eb3f8f1f
MD
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
eb3f8f1f
MD
33 */
34
35#include "hammer.h"
36
41ae0862
TK
37#include <sys/tree.h>
38
95c0f6b0
TK
39/*
40 * Each collect covers 1<<(19+23) bytes address space of layer 1.
41 * (plus a copy of 1<<23 bytes that holds layer2 entries in layer 1).
42 */
6ed4c886 43typedef struct collect {
0083f684 44 RB_ENTRY(collect) entry;
95c0f6b0 45 hammer_off_t phys_offset; /* layer2 address pointed by layer1 */
5aa4f01c 46 hammer_off_t *offsets; /* big-block offset for layer2[i] */
21e9e7d5
TK
47 hammer_blockmap_layer2_t track2; /* track of layer2 entries */
48 hammer_blockmap_layer2_t layer2; /* 1<<19 x 16 bytes entries */
95c0f6b0 49 int error; /* # of inconsistencies */
6ed4c886
MD
50} *collect_t;
51
0083f684
TK
52static int
53collect_compare(struct collect *c1, struct collect *c2)
54{
55 if (c1->phys_offset < c2->phys_offset)
56 return(-1);
57 if (c1->phys_offset > c2->phys_offset)
58 return(1);
59 return(0);
60}
61
62RB_HEAD(collect_rb_tree, collect) CollectTree = RB_INITIALIZER(&CollectTree);
63RB_PROTOTYPE2(collect_rb_tree, collect, entry, collect_compare, hammer_off_t);
64RB_GENERATE2(collect_rb_tree, collect, entry, collect_compare, hammer_off_t,
65 phys_offset);
6ed4c886 66
dce7ae2c 67static void dump_blockmap(int zone);
f8589256 68static void check_freemap(hammer_blockmap_t freemap);
6ed4c886 69static void check_btree_node(hammer_off_t node_offset, int depth);
53076cf0 70static void check_undo(hammer_blockmap_t undomap);
5f9a4634
TK
71static __inline void collect_btree_root(hammer_off_t node_offset);
72static __inline void collect_btree_internal(hammer_btree_elm_t elm);
73static __inline void collect_btree_leaf(hammer_btree_elm_t elm);
f8589256 74static __inline void collect_freemap_layer1(hammer_blockmap_t freemap);
21e9e7d5 75static __inline void collect_freemap_layer2(hammer_blockmap_layer1_t layer1);
5f9a4634
TK
76static __inline void collect_undo(hammer_off_t scan_offset,
77 hammer_fifo_head_t head);
901e04c7 78static void collect_blockmap(hammer_off_t offset, int32_t length, int zone);
21e9e7d5 79static hammer_blockmap_layer2_t collect_get_track(
901e04c7 80 collect_t collect, hammer_off_t offset, int zone,
21e9e7d5 81 hammer_blockmap_layer2_t layer2);
6ed4c886
MD
82static collect_t collect_get(hammer_off_t phys_offset);
83static void dump_collect_table(void);
64321baf 84static void dump_collect(collect_t collect, zone_stat_t stats);
eb3f8f1f 85
4a08dcf4
TK
86static int num_bad_layer1 = 0;
87static int num_bad_layer2 = 0;
88static int num_bad_node = 0;
89
eb3f8f1f
MD
90void
91hammer_cmd_blockmap(void)
92{
dce7ae2c 93 dump_blockmap(HAMMER_ZONE_FREEMAP_INDEX);
eb3f8f1f
MD
94}
95
96static
97void
dce7ae2c 98dump_blockmap(int zone)
eb3f8f1f 99{
2dba5fa7 100 volume_info_t root_volume;
eb3f8f1f 101 hammer_blockmap_t rootmap;
21e9e7d5
TK
102 hammer_blockmap_layer1_t layer1;
103 hammer_blockmap_layer2_t layer2;
c17fa600
TK
104 buffer_info_t buffer1 = NULL;
105 buffer_info_t buffer2 = NULL;
eb3f8f1f
MD
106 hammer_off_t layer1_offset;
107 hammer_off_t layer2_offset;
625b4957
TK
108 hammer_off_t phys_offset;
109 hammer_off_t block_offset;
64321baf 110 zone_stat_t stats = NULL;
1f4a137d 111 int xerr, aerr, ferr;
eb3f8f1f 112
42700c9d 113 root_volume = get_root_volume();
eb3f8f1f
MD
114 rootmap = &root_volume->ondisk->vol0_blockmap[zone];
115 assert(rootmap->phys_offset != 0);
116
dce7ae2c 117 print_blockmap(root_volume);
eb3f8f1f 118
284b6ac2
TK
119 if (VerboseOpt)
120 stats = hammer_init_zone_stat();
121
625b4957
TK
122 for (phys_offset = HAMMER_ZONE_ENCODE(zone, 0);
123 phys_offset < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK);
124 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
eb3f8f1f
MD
125 /*
126 * Dive layer 1.
127 */
128 layer1_offset = rootmap->phys_offset +
625b4957 129 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
eb3f8f1f 130 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
4a08dcf4
TK
131
132 xerr = ' '; /* good */
4c09d9c4 133 if (!hammer_crc_test_layer1(HammerVersion, layer1)) {
822e0d97 134 xerr = 'B';
4a08dcf4
TK
135 ++num_bad_layer1;
136 }
822e0d97 137 if (xerr == ' ' &&
f254e677 138 layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
da44aa75 139 continue;
f254e677 140 }
a276dc6b
MD
141 printf("%c layer1 %016jx @%016jx blocks-free %jd\n",
142 xerr,
625b4957 143 (uintmax_t)phys_offset,
a276dc6b
MD
144 (uintmax_t)layer1->phys_offset,
145 (intmax_t)layer1->blocks_free);
0c37fec9 146
625b4957
TK
147 for (block_offset = 0;
148 block_offset < HAMMER_BLOCKMAP_LAYER2;
149 block_offset += HAMMER_BIGBLOCK_SIZE) {
150 hammer_off_t zone_offset = phys_offset + block_offset;
eb3f8f1f 151 /*
e04ee2de 152 * Dive layer 2, each entry represents a big-block.
eb3f8f1f
MD
153 */
154 layer2_offset = layer1->phys_offset +
625b4957 155 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
eb3f8f1f 156 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
4a08dcf4 157
1f4a137d 158 xerr = aerr = ferr = ' '; /* good */
4c09d9c4 159 if (!hammer_crc_test_layer2(HammerVersion, layer2)) {
822e0d97 160 xerr = 'B';
4a08dcf4
TK
161 ++num_bad_layer2;
162 }
1f4a137d
TK
163 if (layer2->append_off > HAMMER_BIGBLOCK_SIZE) {
164 aerr = 'A';
165 ++num_bad_layer2;
166 }
167 if (layer2->bytes_free < 0 ||
168 layer2->bytes_free > HAMMER_BIGBLOCK_SIZE) {
169 ferr = 'F';
170 ++num_bad_layer2;
171 }
172
173 if (VerboseOpt < 2 &&
174 xerr == ' ' && aerr == ' ' && ferr == ' ' &&
f254e677 175 layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
320e3007 176 break;
f254e677 177 }
1f4a137d
TK
178 printf("%c%c%c %016jx zone=%-2d ",
179 xerr, aerr, ferr, (uintmax_t)zone_offset, layer2->zone);
f254e677 180 if (VerboseOpt) {
e7a33960 181 printf("vol=%-3d L1#=%-6d L2#=%-6d L1=%-7lu L2=%-7lu ",
625b4957
TK
182 HAMMER_VOL_DECODE(zone_offset),
183 HAMMER_BLOCKMAP_LAYER1_INDEX(zone_offset),
184 HAMMER_BLOCKMAP_LAYER2_INDEX(zone_offset),
185 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset),
186 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset));
f254e677 187 }
504ef445 188 printf("app=%-7d free=%-7d",
da44aa75
MD
189 layer2->append_off,
190 layer2->bytes_free);
5d96e8de 191 if (VerboseOpt) {
33614697
TK
192 double bytes_used = HAMMER_BIGBLOCK_SIZE -
193 layer2->bytes_free;
5d96e8de
TK
194 printf(" fill=%-5.1lf crc=%08x-%08x\n",
195 bytes_used * 100 / HAMMER_BIGBLOCK_SIZE,
504ef445
TK
196 layer1->layer1_crc,
197 layer2->entry_crc);
5d96e8de 198 } else {
504ef445 199 printf("\n");
5d96e8de 200 }
284b6ac2 201
dd96867b 202 if (stats)
284b6ac2 203 hammer_add_zone_stat_layer2(stats, layer2);
eb3f8f1f
MD
204 }
205 }
3c6df284
TK
206 rel_buffer(buffer1);
207 rel_buffer(buffer2);
284b6ac2 208
dd96867b 209 if (stats) {
284b6ac2
TK
210 hammer_print_zone_stat(stats);
211 hammer_cleanup_zone_stat(stats);
212 }
4a08dcf4 213
52e2f1b5 214 if (num_bad_layer1 || VerboseOpt)
4a08dcf4 215 printf("%d bad layer1\n", num_bad_layer1);
52e2f1b5 216 if (num_bad_layer2 || VerboseOpt)
4a08dcf4 217 printf("%d bad layer2\n", num_bad_layer1);
eb3f8f1f
MD
218}
219
6ed4c886
MD
220void
221hammer_cmd_checkmap(void)
222{
2dba5fa7 223 volume_info_t volume;
f8589256 224 hammer_blockmap_t freemap;
53076cf0 225 hammer_blockmap_t undomap;
6ed4c886
MD
226 hammer_off_t node_offset;
227
42700c9d 228 volume = get_root_volume();
6ed4c886 229 node_offset = volume->ondisk->vol0_btree_root;
f8589256 230 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
53076cf0 231 undomap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
d2bf3c30 232
dce7ae2c 233 print_blockmap(volume);
6ed4c886 234
f8589256
TK
235 printf("Collecting allocation info from freemap: ");
236 fflush(stdout);
237 check_freemap(freemap);
238 printf("done\n");
239
6ed4c886
MD
240 printf("Collecting allocation info from B-Tree: ");
241 fflush(stdout);
242 check_btree_node(node_offset, 0);
243 printf("done\n");
8fd2cc39 244
d2bf3c30
TK
245 printf("Collecting allocation info from UNDO: ");
246 fflush(stdout);
53076cf0 247 check_undo(undomap);
d2bf3c30
TK
248 printf("done\n");
249
250 dump_collect_table();
6ed4c886
MD
251}
252
005a4da7
TK
253static
254void
f8589256
TK
255check_freemap(hammer_blockmap_t freemap)
256{
257 hammer_off_t offset;
c17fa600 258 buffer_info_t buffer1 = NULL;
21e9e7d5 259 hammer_blockmap_layer1_t layer1;
f8589256
TK
260 int i;
261
262 collect_freemap_layer1(freemap);
263
37fa1bc7 264 for (i = 0; i < HAMMER_BLOCKMAP_RADIX1; ++i) {
f8589256
TK
265 offset = freemap->phys_offset + i * sizeof(*layer1);
266 layer1 = get_buffer_data(offset, &buffer1, 0);
267 if (layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL)
268 collect_freemap_layer2(layer1);
269 }
270 rel_buffer(buffer1);
271}
272
005a4da7
TK
273static
274void
6ed4c886
MD
275check_btree_node(hammer_off_t node_offset, int depth)
276{
c17fa600 277 buffer_info_t buffer = NULL;
6ed4c886
MD
278 hammer_node_ondisk_t node;
279 hammer_btree_elm_t elm;
280 int i;
4a08dcf4
TK
281 char badc = ' '; /* good */
282 char badm = ' '; /* good */
6ed4c886 283
53076cf0
TK
284 if (depth == 0)
285 collect_btree_root(node_offset);
1ba4249f 286 node = get_buffer_data(node_offset, &buffer, 0);
6ed4c886 287
4a08dcf4
TK
288 if (node == NULL) {
289 badc = 'B';
290 badm = 'I';
4c09d9c4 291 } else if (!hammer_crc_test_btree(HammerVersion, node)) {
6ed4c886 292 badc = 'B';
4a08dcf4 293 }
6ed4c886 294
4a08dcf4
TK
295 if (badm != ' ' || badc != ' ') { /* not good */
296 ++num_bad_node;
297 printf("%c%c NODE %016jx ",
298 badc, badm, (uintmax_t)node_offset);
299 if (node == NULL) {
300 printf("(IO ERROR)\n");
10de5026 301 rel_buffer(buffer);
4a08dcf4
TK
302 return;
303 } else {
304 printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx\n",
305 node->count,
306 (uintmax_t)node->parent,
307 (node->type ? node->type : '?'),
308 depth,
309 (uintmax_t)node->mirror_tid);
310 }
6ed4c886
MD
311 }
312
313 for (i = 0; i < node->count; ++i) {
314 elm = &node->elms[i];
315
316 switch(node->type) {
317 case HAMMER_BTREE_TYPE_INTERNAL:
318 if (elm->internal.subtree_offset) {
6cf258d9 319 collect_btree_internal(elm);
6ed4c886
MD
320 check_btree_node(elm->internal.subtree_offset,
321 depth + 1);
322 }
323 break;
324 case HAMMER_BTREE_TYPE_LEAF:
325 if (elm->leaf.data_offset)
6cf258d9 326 collect_btree_leaf(elm);
6ed4c886 327 break;
2222420c 328 default:
e0c64b7d 329 assert(!DebugOpt);
2222420c 330 break;
6ed4c886
MD
331 }
332 }
333 rel_buffer(buffer);
334}
335
005a4da7
TK
336static
337void
53076cf0 338check_undo(hammer_blockmap_t undomap)
d2bf3c30 339{
c17fa600 340 buffer_info_t buffer = NULL;
d2bf3c30
TK
341 hammer_off_t scan_offset;
342 hammer_fifo_head_t head;
343
459a6a40 344 scan_offset = HAMMER_ENCODE_UNDO(0);
53076cf0 345 while (scan_offset < undomap->alloc_offset) {
d2bf3c30
TK
346 head = get_buffer_data(scan_offset, &buffer, 0);
347 switch (head->hdr_type) {
348 case HAMMER_HEAD_TYPE_PAD:
349 case HAMMER_HEAD_TYPE_DUMMY:
350 case HAMMER_HEAD_TYPE_UNDO:
351 case HAMMER_HEAD_TYPE_REDO:
352 collect_undo(scan_offset, head);
353 break;
2222420c 354 default:
e0c64b7d 355 assert(!DebugOpt);
2222420c 356 break;
d2bf3c30
TK
357 }
358 if ((head->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
359 head->hdr_size == 0 ||
360 head->hdr_size > HAMMER_UNDO_ALIGN -
361 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
362 printf("Illegal size, skipping to next boundary\n");
19836c70 363 scan_offset = HAMMER_UNDO_DOALIGN(scan_offset);
d2bf3c30
TK
364 } else {
365 scan_offset += head->hdr_size;
366 }
367 }
368 rel_buffer(buffer);
369}
370
f8589256
TK
371static __inline
372void
373collect_freemap_layer1(hammer_blockmap_t freemap)
374{
375 /*
376 * This translation is necessary to do checkmap properly
377 * as zone4 is really just zone2 address space.
378 */
379 hammer_off_t zone4_offset = hammer_xlate_to_zoneX(
380 HAMMER_ZONE_FREEMAP_INDEX, freemap->phys_offset);
381 collect_blockmap(zone4_offset, HAMMER_BIGBLOCK_SIZE,
382 HAMMER_ZONE_FREEMAP_INDEX);
383}
384
385static __inline
386void
21e9e7d5 387collect_freemap_layer2(hammer_blockmap_layer1_t layer1)
f8589256
TK
388{
389 /*
390 * This translation is necessary to do checkmap properly
391 * as zone4 is really just zone2 address space.
392 */
393 hammer_off_t zone4_offset = hammer_xlate_to_zoneX(
394 HAMMER_ZONE_FREEMAP_INDEX, layer1->phys_offset);
395 collect_blockmap(zone4_offset, HAMMER_BIGBLOCK_SIZE,
396 HAMMER_ZONE_FREEMAP_INDEX);
397}
398
5f9a4634 399static __inline
6ed4c886 400void
6cf258d9
TK
401collect_btree_root(hammer_off_t node_offset)
402{
403 collect_blockmap(node_offset,
901e04c7
TK
404 sizeof(struct hammer_node_ondisk), /* 4KB */
405 HAMMER_ZONE_BTREE_INDEX);
6cf258d9
TK
406}
407
5f9a4634 408static __inline
6cf258d9
TK
409void
410collect_btree_internal(hammer_btree_elm_t elm)
411{
412 collect_blockmap(elm->internal.subtree_offset,
901e04c7
TK
413 sizeof(struct hammer_node_ondisk), /* 4KB */
414 HAMMER_ZONE_BTREE_INDEX);
6cf258d9
TK
415}
416
5f9a4634 417static __inline
6cf258d9
TK
418void
419collect_btree_leaf(hammer_btree_elm_t elm)
420{
901e04c7
TK
421 int zone;
422
423 switch (elm->base.rec_type) {
424 case HAMMER_RECTYPE_INODE:
425 case HAMMER_RECTYPE_DIRENTRY:
426 case HAMMER_RECTYPE_EXT:
427 case HAMMER_RECTYPE_FIX:
428 case HAMMER_RECTYPE_PFS:
429 case HAMMER_RECTYPE_SNAPSHOT:
430 case HAMMER_RECTYPE_CONFIG:
431 zone = HAMMER_ZONE_META_INDEX;
432 break;
433 case HAMMER_RECTYPE_DATA:
434 case HAMMER_RECTYPE_DB:
22c06dfe 435 zone = hammer_data_zone_index(elm->leaf.data_len);
901e04c7
TK
436 break;
437 default:
438 zone = HAMMER_ZONE_UNAVAIL_INDEX;
439 break;
440 }
6cf258d9 441 collect_blockmap(elm->leaf.data_offset,
f097bffe 442 HAMMER_DATA_DOALIGN(elm->leaf.data_len), zone);
6cf258d9
TK
443}
444
5f9a4634 445static __inline
d2bf3c30
TK
446void
447collect_undo(hammer_off_t scan_offset, hammer_fifo_head_t head)
448{
901e04c7
TK
449 collect_blockmap(scan_offset, head->hdr_size,
450 HAMMER_ZONE_UNDO_INDEX);
d2bf3c30
TK
451}
452
6cf258d9
TK
453static
454void
901e04c7 455collect_blockmap(hammer_off_t offset, int32_t length, int zone)
6ed4c886
MD
456{
457 struct hammer_blockmap_layer1 layer1;
458 struct hammer_blockmap_layer2 layer2;
21e9e7d5 459 hammer_blockmap_layer2_t track2;
6cf258d9 460 hammer_off_t result_offset;
6ed4c886
MD
461 collect_t collect;
462 int error;
463
8b916bc2 464 result_offset = blockmap_lookup_save(offset, &layer1, &layer2, &error);
e0c64b7d 465 if (DebugOpt) {
901e04c7 466 assert(HAMMER_ZONE_DECODE(offset) == zone);
9b6e7743 467 assert(hammer_is_zone_raw_buffer(result_offset));
6cf258d9
TK
468 assert(error == 0);
469 }
470 collect = collect_get(layer1.phys_offset); /* layer2 address */
5aa4f01c 471 track2 = collect_get_track(collect, result_offset, zone, &layer2);
6cf258d9 472 track2->bytes_free -= length;
6ed4c886
MD
473}
474
475static
476collect_t
477collect_get(hammer_off_t phys_offset)
478{
6ed4c886
MD
479 collect_t collect;
480
0083f684
TK
481 collect = RB_LOOKUP(collect_rb_tree, &CollectTree, phys_offset);
482 if (collect)
483 return(collect);
95c0f6b0 484
c8f165e2
TK
485 collect = calloc(1, sizeof(*collect));
486 collect->track2 = calloc(1, HAMMER_BIGBLOCK_SIZE); /* 1<<23 bytes */
487 collect->layer2 = calloc(1, HAMMER_BIGBLOCK_SIZE); /* 1<<23 bytes */
488 collect->offsets = calloc(HAMMER_BLOCKMAP_RADIX2, sizeof(hammer_off_t));
6ed4c886 489 collect->phys_offset = phys_offset;
0083f684 490 RB_INSERT(collect_rb_tree, &CollectTree, collect);
6ed4c886
MD
491
492 return (collect);
493}
494
488d97f5
TK
495static
496void
497collect_rel(collect_t collect)
498{
5aa4f01c 499 free(collect->offsets);
488d97f5
TK
500 free(collect->layer2);
501 free(collect->track2);
502 free(collect);
503}
504
6ed4c886 505static
21e9e7d5 506hammer_blockmap_layer2_t
901e04c7 507collect_get_track(collect_t collect, hammer_off_t offset, int zone,
21e9e7d5 508 hammer_blockmap_layer2_t layer2)
6ed4c886 509{
21e9e7d5 510 hammer_blockmap_layer2_t track2;
6ed4c886
MD
511 size_t i;
512
f5b0d00f 513 i = HAMMER_BLOCKMAP_LAYER2_INDEX(offset);
6ed4c886
MD
514 track2 = &collect->track2[i];
515 if (track2->entry_crc == 0) {
516 collect->layer2[i] = *layer2;
5aa4f01c 517 collect->offsets[i] = offset & ~HAMMER_BIGBLOCK_MASK64;
901e04c7 518 track2->zone = zone;
e04ee2de 519 track2->bytes_free = HAMMER_BIGBLOCK_SIZE;
6ed4c886
MD
520 track2->entry_crc = 1; /* steal field to tag track load */
521 }
522 return (track2);
523}
524
525static
526void
527dump_collect_table(void)
528{
91416fbd 529 collect_t collect;
18dbf12f 530 int error = 0;
64321baf 531 zone_stat_t stats = NULL;
020339d5
TK
532
533 if (VerboseOpt)
534 stats = hammer_init_zone_stat();
6ed4c886 535
0083f684
TK
536 RB_FOREACH(collect, collect_rb_tree, &CollectTree) {
537 dump_collect(collect, stats);
538 error += collect->error;
539 }
540
541 while ((collect = RB_ROOT(&CollectTree)) != NULL) {
542 RB_REMOVE(collect_rb_tree, &CollectTree, collect);
543 collect_rel(collect);
6ed4c886 544 }
0083f684 545 assert(RB_EMPTY(&CollectTree));
d42b45c3 546
dd96867b 547 if (stats) {
020339d5
TK
548 hammer_print_zone_stat(stats);
549 hammer_cleanup_zone_stat(stats);
d42b45c3 550 }
18dbf12f 551
52e2f1b5 552 if (num_bad_node || VerboseOpt)
4a08dcf4 553 printf("%d bad nodes\n", num_bad_node);
52e2f1b5 554 if (error || VerboseOpt)
18dbf12f 555 printf("%d errors\n", error);
6ed4c886
MD
556}
557
558static
559void
64321baf 560dump_collect(collect_t collect, zone_stat_t stats)
6ed4c886 561{
21e9e7d5
TK
562 hammer_blockmap_layer2_t track2;
563 hammer_blockmap_layer2_t layer2;
901e04c7 564 hammer_off_t offset;
f540a63d 565 int i;
6ed4c886
MD
566
567 for (i = 0; i < HAMMER_BLOCKMAP_RADIX2; ++i) {
568 track2 = &collect->track2[i];
569 layer2 = &collect->layer2[i];
5aa4f01c 570 offset = collect->offsets[i];
6ed4c886
MD
571
572 /*
f8589256
TK
573 * Check big-blocks referenced by freemap, data,
574 * B-Tree nodes and UNDO fifo.
6ed4c886
MD
575 */
576 if (track2->entry_crc == 0)
577 continue;
578
f254e677 579 if (DebugOpt) {
f540a63d
TK
580 assert((layer2->zone == HAMMER_ZONE_UNDO_INDEX) ||
581 (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX) ||
582 hammer_is_index_record(layer2->zone));
f254e677 583 }
dd96867b 584 if (stats)
020339d5 585 hammer_add_zone_stat_layer2(stats, layer2);
d42b45c3 586
901e04c7 587 if (track2->zone != layer2->zone) {
850f27ee 588 printf("BZ\tblock=%016jx calc zone=%-2d, got zone=%-2d\n",
1f5e0b99 589 (uintmax_t)offset,
901e04c7
TK
590 track2->zone,
591 layer2->zone);
592 collect->error++;
593 } else if (track2->bytes_free != layer2->bytes_free) {
850f27ee 594 printf("BM\tblock=%016jx zone=%-2d calc %d free, got %d\n",
1f5e0b99 595 (uintmax_t)offset,
d42b45c3 596 layer2->zone,
6ed4c886
MD
597 track2->bytes_free,
598 layer2->bytes_free);
18dbf12f 599 collect->error++;
6ed4c886 600 } else if (VerboseOpt) {
850f27ee 601 printf("\tblock=%016jx zone=%-2d %d free (correct)\n",
1f5e0b99 602 (uintmax_t)offset,
d42b45c3 603 layer2->zone,
6ed4c886
MD
604 track2->bytes_free);
605 }
606 }
607}