Clean up some more after the sys/emulation/linux removal.
[dragonfly.git] / sbin / hammer / cmd_blockmap.c
blob743f45417d5df74bb768fa3bf496c8eab588624e
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
34 * $DragonFly: src/sbin/hammer/cmd_blockmap.c,v 1.4 2008/07/19 18:48:14 dillon Exp $
37 #include "hammer.h"
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).
43 typedef struct collect {
44 RB_ENTRY(collect) entry;
45 hammer_off_t phys_offset; /* layer2 address pointed by layer1 */
46 hammer_off_t *offsets; /* big-block offset for layer2[i] */
47 struct hammer_blockmap_layer2 *track2; /* track of layer2 entries */
48 struct hammer_blockmap_layer2 *layer2; /* 1<<19 x 16 bytes entries */
49 int error; /* # of inconsistencies */
50 } *collect_t;
52 static int
53 collect_compare(struct collect *c1, struct collect *c2)
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);
62 RB_HEAD(collect_rb_tree, collect) CollectTree = RB_INITIALIZER(&CollectTree);
63 RB_PROTOTYPE2(collect_rb_tree, collect, entry, collect_compare, hammer_off_t);
64 RB_GENERATE2(collect_rb_tree, collect, entry, collect_compare, hammer_off_t,
65 phys_offset);
67 static void dump_blockmap(const char *label, int zone);
68 static void check_freemap(hammer_blockmap_t freemap);
69 static void check_btree_node(hammer_off_t node_offset, int depth);
70 static void check_undo(hammer_blockmap_t undomap);
71 static __inline void collect_btree_root(hammer_off_t node_offset);
72 static __inline void collect_btree_internal(hammer_btree_elm_t elm);
73 static __inline void collect_btree_leaf(hammer_btree_elm_t elm);
74 static __inline void collect_freemap_layer1(hammer_blockmap_t freemap);
75 static __inline void collect_freemap_layer2(struct hammer_blockmap_layer1 *layer1);
76 static __inline void collect_undo(hammer_off_t scan_offset,
77 hammer_fifo_head_t head);
78 static void collect_blockmap(hammer_off_t offset, int32_t length, int zone);
79 static struct hammer_blockmap_layer2 *collect_get_track(
80 collect_t collect, hammer_off_t offset, int zone,
81 struct hammer_blockmap_layer2 *layer2);
82 static collect_t collect_get(hammer_off_t phys_offset);
83 static void dump_collect_table(void);
84 static void dump_collect(collect_t collect, struct zone_stat *stats);
86 static int num_bad_layer1 = 0;
87 static int num_bad_layer2 = 0;
88 static int num_bad_node = 0;
90 void
91 hammer_cmd_blockmap(void)
93 dump_blockmap("freemap", HAMMER_ZONE_FREEMAP_INDEX);
96 static
97 void
98 dump_blockmap(const char *label, int zone)
100 struct volume_info *root_volume;
101 hammer_blockmap_t rootmap;
102 hammer_blockmap_t blockmap;
103 struct hammer_blockmap_layer1 *layer1;
104 struct hammer_blockmap_layer2 *layer2;
105 struct buffer_info *buffer1 = NULL;
106 struct buffer_info *buffer2 = NULL;
107 hammer_off_t layer1_offset;
108 hammer_off_t layer2_offset;
109 hammer_off_t phys_offset;
110 hammer_off_t block_offset;
111 struct zone_stat *stats = NULL;
112 int xerr, aerr, ferr;
113 int i;
115 root_volume = get_root_volume();
116 rootmap = &root_volume->ondisk->vol0_blockmap[zone];
117 assert(rootmap->phys_offset != 0);
119 printf(" "
120 "phys first next alloc\n");
121 for (i = 0; i < HAMMER_MAX_ZONES; i++) {
122 blockmap = &root_volume->ondisk->vol0_blockmap[i];
123 if (VerboseOpt || i == zone) {
124 printf("zone %-2d %-10s %016jx %016jx %016jx %016jx\n",
125 i, (i == zone ? label : ""),
126 (uintmax_t)blockmap->phys_offset,
127 (uintmax_t)blockmap->first_offset,
128 (uintmax_t)blockmap->next_offset,
129 (uintmax_t)blockmap->alloc_offset);
133 if (VerboseOpt)
134 stats = hammer_init_zone_stat();
136 for (phys_offset = HAMMER_ZONE_ENCODE(zone, 0);
137 phys_offset < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK);
138 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
140 * Dive layer 1.
142 layer1_offset = rootmap->phys_offset +
143 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
144 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
146 xerr = ' '; /* good */
147 if (layer1->layer1_crc !=
148 crc32(layer1, HAMMER_LAYER1_CRCSIZE)) {
149 xerr = 'B';
150 ++num_bad_layer1;
152 if (xerr == ' ' &&
153 layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
154 continue;
156 printf("%c layer1 %016jx @%016jx blocks-free %jd\n",
157 xerr,
158 (uintmax_t)phys_offset,
159 (uintmax_t)layer1->phys_offset,
160 (intmax_t)layer1->blocks_free);
162 for (block_offset = 0;
163 block_offset < HAMMER_BLOCKMAP_LAYER2;
164 block_offset += HAMMER_BIGBLOCK_SIZE) {
165 hammer_off_t zone_offset = phys_offset + block_offset;
167 * Dive layer 2, each entry represents a big-block.
169 layer2_offset = layer1->phys_offset +
170 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
171 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
173 xerr = aerr = ferr = ' '; /* good */
174 if (layer2->entry_crc !=
175 crc32(layer2, HAMMER_LAYER2_CRCSIZE)) {
176 xerr = 'B';
177 ++num_bad_layer2;
179 if (layer2->append_off > HAMMER_BIGBLOCK_SIZE) {
180 aerr = 'A';
181 ++num_bad_layer2;
183 if (layer2->bytes_free < 0 ||
184 layer2->bytes_free > HAMMER_BIGBLOCK_SIZE) {
185 ferr = 'F';
186 ++num_bad_layer2;
189 if (VerboseOpt < 2 &&
190 xerr == ' ' && aerr == ' ' && ferr == ' ' &&
191 layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
192 break;
194 printf("%c%c%c %016jx zone=%-2d ",
195 xerr, aerr, ferr, (uintmax_t)zone_offset, layer2->zone);
196 if (VerboseOpt) {
197 printf("vol=%-3d L1#=%-6d L2#=%-6d L1=%-7lu L2=%-7lu ",
198 HAMMER_VOL_DECODE(zone_offset),
199 HAMMER_BLOCKMAP_LAYER1_INDEX(zone_offset),
200 HAMMER_BLOCKMAP_LAYER2_INDEX(zone_offset),
201 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_offset),
202 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_offset));
204 printf("app=%-7d free=%-7d",
205 layer2->append_off,
206 layer2->bytes_free);
207 if (VerboseOpt) {
208 double bytes_used = HAMMER_BIGBLOCK_SIZE -
209 layer2->bytes_free;
210 printf(" fill=%-5.1lf crc=%08x-%08x\n",
211 bytes_used * 100 / HAMMER_BIGBLOCK_SIZE,
212 layer1->layer1_crc,
213 layer2->entry_crc);
214 } else {
215 printf("\n");
218 if (VerboseOpt)
219 hammer_add_zone_stat_layer2(stats, layer2);
222 rel_buffer(buffer1);
223 rel_buffer(buffer2);
225 if (VerboseOpt) {
226 hammer_print_zone_stat(stats);
227 hammer_cleanup_zone_stat(stats);
230 if (num_bad_layer1 || VerboseOpt) {
231 printf("%d bad layer1\n", num_bad_layer1);
233 if (num_bad_layer2 || VerboseOpt) {
234 printf("%d bad layer2\n", num_bad_layer1);
238 void
239 hammer_cmd_checkmap(void)
241 struct volume_info *volume;
242 hammer_blockmap_t freemap;
243 hammer_blockmap_t undomap;
244 hammer_off_t node_offset;
246 volume = get_root_volume();
247 node_offset = volume->ondisk->vol0_btree_root;
248 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
249 undomap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
251 if (QuietOpt < 3) {
252 printf("Volume header\tnext_tid=%016jx\n",
253 (uintmax_t)volume->ondisk->vol0_next_tid);
254 printf("\t\tbufoffset=%016jx\n",
255 (uintmax_t)volume->ondisk->vol_buf_beg);
256 printf("\t\tundosize=%jdMB\n",
257 (intmax_t)((undomap->alloc_offset & HAMMER_OFF_LONG_MASK)
258 / (1024 * 1024)));
261 AssertOnFailure = (DebugOpt != 0);
263 printf("Collecting allocation info from freemap: ");
264 fflush(stdout);
265 check_freemap(freemap);
266 printf("done\n");
268 printf("Collecting allocation info from B-Tree: ");
269 fflush(stdout);
270 check_btree_node(node_offset, 0);
271 printf("done\n");
273 printf("Collecting allocation info from UNDO: ");
274 fflush(stdout);
275 check_undo(undomap);
276 printf("done\n");
278 dump_collect_table();
281 static void
282 check_freemap(hammer_blockmap_t freemap)
284 hammer_off_t offset;
285 struct buffer_info *buffer1 = NULL;
286 struct hammer_blockmap_layer1 *layer1;
287 int i;
289 collect_freemap_layer1(freemap);
291 for (i = 0; i < HAMMER_BLOCKMAP_RADIX1; ++i) {
292 offset = freemap->phys_offset + i * sizeof(*layer1);
293 layer1 = get_buffer_data(offset, &buffer1, 0);
294 if (layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL)
295 collect_freemap_layer2(layer1);
297 rel_buffer(buffer1);
300 static void
301 check_btree_node(hammer_off_t node_offset, int depth)
303 struct buffer_info *buffer = NULL;
304 hammer_node_ondisk_t node;
305 hammer_btree_elm_t elm;
306 int i;
307 char badc = ' '; /* good */
308 char badm = ' '; /* good */
310 if (depth == 0)
311 collect_btree_root(node_offset);
312 node = get_node(node_offset, &buffer);
314 if (node == NULL) {
315 badc = 'B';
316 badm = 'I';
317 } else if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) != node->crc) {
318 badc = 'B';
321 if (badm != ' ' || badc != ' ') { /* not good */
322 ++num_bad_node;
323 printf("%c%c NODE %016jx ",
324 badc, badm, (uintmax_t)node_offset);
325 if (node == NULL) {
326 printf("(IO ERROR)\n");
327 rel_buffer(buffer);
328 return;
329 } else {
330 printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx\n",
331 node->count,
332 (uintmax_t)node->parent,
333 (node->type ? node->type : '?'),
334 depth,
335 (uintmax_t)node->mirror_tid);
339 for (i = 0; i < node->count; ++i) {
340 elm = &node->elms[i];
342 switch(node->type) {
343 case HAMMER_BTREE_TYPE_INTERNAL:
344 if (elm->internal.subtree_offset) {
345 collect_btree_internal(elm);
346 check_btree_node(elm->internal.subtree_offset,
347 depth + 1);
349 break;
350 case HAMMER_BTREE_TYPE_LEAF:
351 if (elm->leaf.data_offset)
352 collect_btree_leaf(elm);
353 break;
354 default:
355 assert(!AssertOnFailure);
356 break;
359 rel_buffer(buffer);
362 static void
363 check_undo(hammer_blockmap_t undomap)
365 struct buffer_info *buffer = NULL;
366 hammer_off_t scan_offset;
367 hammer_fifo_head_t head;
369 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
370 while (scan_offset < undomap->alloc_offset) {
371 head = get_buffer_data(scan_offset, &buffer, 0);
372 switch (head->hdr_type) {
373 case HAMMER_HEAD_TYPE_PAD:
374 case HAMMER_HEAD_TYPE_DUMMY:
375 case HAMMER_HEAD_TYPE_UNDO:
376 case HAMMER_HEAD_TYPE_REDO:
377 collect_undo(scan_offset, head);
378 break;
379 default:
380 assert(!AssertOnFailure);
381 break;
383 if ((head->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
384 head->hdr_size == 0 ||
385 head->hdr_size > HAMMER_UNDO_ALIGN -
386 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
387 printf("Illegal size, skipping to next boundary\n");
388 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
389 ~HAMMER_UNDO_MASK64;
390 } else {
391 scan_offset += head->hdr_size;
394 rel_buffer(buffer);
397 static __inline
398 void
399 collect_freemap_layer1(hammer_blockmap_t freemap)
402 * This translation is necessary to do checkmap properly
403 * as zone4 is really just zone2 address space.
405 hammer_off_t zone4_offset = hammer_xlate_to_zoneX(
406 HAMMER_ZONE_FREEMAP_INDEX, freemap->phys_offset);
407 collect_blockmap(zone4_offset, HAMMER_BIGBLOCK_SIZE,
408 HAMMER_ZONE_FREEMAP_INDEX);
411 static __inline
412 void
413 collect_freemap_layer2(struct hammer_blockmap_layer1 *layer1)
416 * This translation is necessary to do checkmap properly
417 * as zone4 is really just zone2 address space.
419 hammer_off_t zone4_offset = hammer_xlate_to_zoneX(
420 HAMMER_ZONE_FREEMAP_INDEX, layer1->phys_offset);
421 collect_blockmap(zone4_offset, HAMMER_BIGBLOCK_SIZE,
422 HAMMER_ZONE_FREEMAP_INDEX);
425 static __inline
426 void
427 collect_btree_root(hammer_off_t node_offset)
429 collect_blockmap(node_offset,
430 sizeof(struct hammer_node_ondisk), /* 4KB */
431 HAMMER_ZONE_BTREE_INDEX);
434 static __inline
435 void
436 collect_btree_internal(hammer_btree_elm_t elm)
438 collect_blockmap(elm->internal.subtree_offset,
439 sizeof(struct hammer_node_ondisk), /* 4KB */
440 HAMMER_ZONE_BTREE_INDEX);
443 static __inline
444 void
445 collect_btree_leaf(hammer_btree_elm_t elm)
447 int zone;
449 switch (elm->base.rec_type) {
450 case HAMMER_RECTYPE_INODE:
451 case HAMMER_RECTYPE_DIRENTRY:
452 case HAMMER_RECTYPE_EXT:
453 case HAMMER_RECTYPE_FIX:
454 case HAMMER_RECTYPE_PFS:
455 case HAMMER_RECTYPE_SNAPSHOT:
456 case HAMMER_RECTYPE_CONFIG:
457 zone = HAMMER_ZONE_META_INDEX;
458 break;
459 case HAMMER_RECTYPE_DATA:
460 case HAMMER_RECTYPE_DB:
461 zone = hammer_data_zone_index(elm->leaf.data_len);
462 break;
463 default:
464 zone = HAMMER_ZONE_UNAVAIL_INDEX;
465 break;
467 collect_blockmap(elm->leaf.data_offset,
468 (elm->leaf.data_len + 15) & ~15, zone);
471 static __inline
472 void
473 collect_undo(hammer_off_t scan_offset, hammer_fifo_head_t head)
475 collect_blockmap(scan_offset, head->hdr_size,
476 HAMMER_ZONE_UNDO_INDEX);
479 static
480 void
481 collect_blockmap(hammer_off_t offset, int32_t length, int zone)
483 struct hammer_blockmap_layer1 layer1;
484 struct hammer_blockmap_layer2 layer2;
485 struct hammer_blockmap_layer2 *track2;
486 hammer_off_t result_offset;
487 collect_t collect;
488 int error;
490 result_offset = blockmap_lookup(offset, &layer1, &layer2, &error);
491 if (AssertOnFailure) {
492 assert(HAMMER_ZONE_DECODE(offset) == zone);
493 assert(HAMMER_ZONE_DECODE(result_offset) ==
494 HAMMER_ZONE_RAW_BUFFER_INDEX);
495 assert(error == 0);
497 collect = collect_get(layer1.phys_offset); /* layer2 address */
498 track2 = collect_get_track(collect, result_offset, zone, &layer2);
499 track2->bytes_free -= length;
502 static
503 collect_t
504 collect_get(hammer_off_t phys_offset)
506 collect_t collect;
508 collect = RB_LOOKUP(collect_rb_tree, &CollectTree, phys_offset);
509 if (collect)
510 return(collect);
512 collect = calloc(sizeof(*collect), 1);
513 collect->track2 = malloc(HAMMER_BIGBLOCK_SIZE); /* 1<<23 bytes */
514 collect->layer2 = malloc(HAMMER_BIGBLOCK_SIZE); /* 1<<23 bytes */
515 collect->offsets = malloc(sizeof(hammer_off_t) * HAMMER_BLOCKMAP_RADIX2);
516 collect->phys_offset = phys_offset;
517 RB_INSERT(collect_rb_tree, &CollectTree, collect);
518 bzero(collect->track2, HAMMER_BIGBLOCK_SIZE);
519 bzero(collect->layer2, HAMMER_BIGBLOCK_SIZE);
521 return (collect);
524 static
525 void
526 collect_rel(collect_t collect)
528 free(collect->offsets);
529 free(collect->layer2);
530 free(collect->track2);
531 free(collect);
534 static
535 struct hammer_blockmap_layer2 *
536 collect_get_track(collect_t collect, hammer_off_t offset, int zone,
537 struct hammer_blockmap_layer2 *layer2)
539 struct hammer_blockmap_layer2 *track2;
540 size_t i;
542 i = HAMMER_BLOCKMAP_LAYER2_INDEX(offset);
543 track2 = &collect->track2[i];
544 if (track2->entry_crc == 0) {
545 collect->layer2[i] = *layer2;
546 collect->offsets[i] = offset & ~HAMMER_BIGBLOCK_MASK64;
547 track2->zone = zone;
548 track2->bytes_free = HAMMER_BIGBLOCK_SIZE;
549 track2->entry_crc = 1; /* steal field to tag track load */
551 return (track2);
554 static
555 void
556 dump_collect_table(void)
558 collect_t collect;
559 int error = 0;
560 struct zone_stat *stats = NULL;
562 if (VerboseOpt)
563 stats = hammer_init_zone_stat();
565 RB_FOREACH(collect, collect_rb_tree, &CollectTree) {
566 dump_collect(collect, stats);
567 error += collect->error;
570 while ((collect = RB_ROOT(&CollectTree)) != NULL) {
571 RB_REMOVE(collect_rb_tree, &CollectTree, collect);
572 collect_rel(collect);
574 assert(RB_EMPTY(&CollectTree));
576 if (VerboseOpt) {
577 hammer_print_zone_stat(stats);
578 hammer_cleanup_zone_stat(stats);
581 if (num_bad_node || VerboseOpt) {
582 printf("%d bad nodes\n", num_bad_node);
584 if (error || VerboseOpt) {
585 printf("%d errors\n", error);
589 static
590 void
591 dump_collect(collect_t collect, struct zone_stat *stats)
593 struct hammer_blockmap_layer2 *track2;
594 struct hammer_blockmap_layer2 *layer2;
595 hammer_off_t offset;
596 int i, zone;
598 for (i = 0; i < HAMMER_BLOCKMAP_RADIX2; ++i) {
599 track2 = &collect->track2[i];
600 layer2 = &collect->layer2[i];
601 offset = collect->offsets[i];
604 * Check big-blocks referenced by freemap, data,
605 * B-Tree nodes and UNDO fifo.
607 if (track2->entry_crc == 0)
608 continue;
610 zone = layer2->zone;
611 if (AssertOnFailure) {
612 assert((zone == HAMMER_ZONE_UNDO_INDEX) ||
613 (zone == HAMMER_ZONE_FREEMAP_INDEX) ||
614 hammer_is_zone2_mapped_index(zone));
616 if (VerboseOpt)
617 hammer_add_zone_stat_layer2(stats, layer2);
619 if (track2->zone != layer2->zone) {
620 printf("BZ\tblock=%016jx calc zone=%-2d, got zone=%-2d\n",
621 (intmax_t)offset,
622 track2->zone,
623 layer2->zone);
624 collect->error++;
625 } else if (track2->bytes_free != layer2->bytes_free) {
626 printf("BM\tblock=%016jx zone=%-2d calc %d free, got %d\n",
627 (intmax_t)offset,
628 layer2->zone,
629 track2->bytes_free,
630 layer2->bytes_free);
631 collect->error++;
632 } else if (VerboseOpt) {
633 printf("\tblock=%016jx zone=%-2d %d free (correct)\n",
634 (intmax_t)offset,
635 layer2->zone,
636 track2->bytes_free);