sbin/hammer: Make hammer checkmap command support btree zone
[dragonfly.git] / sbin / hammer / cmd_blockmap.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  *
34  * $DragonFly: src/sbin/hammer/cmd_blockmap.c,v 1.4 2008/07/19 18:48:14 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 typedef struct collect {
40         struct collect  *hnext;
41         hammer_off_t    phys_offset;
42         struct hammer_blockmap_layer2 *track2;
43         struct hammer_blockmap_layer2 *layer2;
44 } *collect_t;
45
46 collect_t CollectHash[COLLECT_HSIZE];
47
48 static void dump_blockmap(const char *label, int zone);
49 static void check_btree_node(hammer_off_t node_offset, int depth);
50 static void collect_btree_root(hammer_off_t node_offset);
51 static void collect_btree_internal(hammer_btree_elm_t elm);
52 static void collect_btree_leaf(hammer_btree_elm_t elm);
53 static void collect_blockmap(hammer_off_t offset, int32_t length);
54 static struct hammer_blockmap_layer2 *collect_get_track(
55         collect_t collect, hammer_off_t offset,
56         struct hammer_blockmap_layer2 *layer2);
57 static collect_t collect_get(hammer_off_t phys_offset);
58 static void dump_collect_table(void);
59 static void dump_collect(collect_t collect);
60
61 void
62 hammer_cmd_blockmap(void)
63 {
64         dump_blockmap("freemap", HAMMER_ZONE_FREEMAP_INDEX);
65 }
66
67 static
68 void
69 dump_blockmap(const char *label, int zone)
70 {
71         struct volume_info *root_volume;
72         hammer_blockmap_t rootmap;
73         struct hammer_blockmap_layer1 *layer1;
74         struct hammer_blockmap_layer2 *layer2;
75         struct buffer_info *buffer1 = NULL;
76         struct buffer_info *buffer2 = NULL;
77         hammer_off_t layer1_offset;
78         hammer_off_t layer2_offset;
79         hammer_off_t scan1;
80         hammer_off_t scan2;
81         int xerr;
82
83         assert(RootVolNo >= 0);
84         root_volume = get_volume(RootVolNo);
85         rootmap = &root_volume->ondisk->vol0_blockmap[zone];
86         assert(rootmap->phys_offset != 0);
87
88         printf("zone %-16s next %016jx alloc %016jx\n",
89                 label,
90                 (uintmax_t)rootmap->next_offset,
91                 (uintmax_t)rootmap->alloc_offset);
92
93         for (scan1 = HAMMER_ZONE_ENCODE(zone, 0);
94              scan1 < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK);
95              scan1 += HAMMER_BLOCKMAP_LAYER2) {
96                 /*
97                  * Dive layer 1.
98                  */
99                 layer1_offset = rootmap->phys_offset +
100                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(scan1);
101                 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
102                 xerr = ' ';
103                 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
104                         xerr = 'B';
105                 if (xerr == ' ' &&
106                     layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
107                         continue;
108                 }
109                 printf("%c layer1 %016jx @%016jx blocks-free %jd\n",
110                         xerr,
111                         (uintmax_t)scan1,
112                         (uintmax_t)layer1->phys_offset,
113                         (intmax_t)layer1->blocks_free);
114                 if (layer1->phys_offset == HAMMER_BLOCKMAP_FREE)
115                         continue;
116                 for (scan2 = scan1;
117                      scan2 < scan1 + HAMMER_BLOCKMAP_LAYER2;
118                      scan2 += HAMMER_BIGBLOCK_SIZE
119                 ) {
120                         /*
121                          * Dive layer 2, each entry represents a big-block.
122                          */
123                         layer2_offset = layer1->phys_offset +
124                                         HAMMER_BLOCKMAP_LAYER2_OFFSET(scan2);
125                         layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
126                         xerr = ' ';
127                         if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
128                                 xerr = 'B';
129                         printf("%c       %016jx zone=%d app=%-7d free=%-7d\n",
130                                 xerr,
131                                 (uintmax_t)scan2,
132                                 layer2->zone,
133                                 layer2->append_off,
134                                 layer2->bytes_free);
135                 }
136         }
137         if (buffer1)
138                 rel_buffer(buffer1);
139         if (buffer2)
140                 rel_buffer(buffer2);
141         rel_volume(root_volume);
142 }
143
144 void
145 hammer_cmd_checkmap(void)
146 {
147         struct volume_info *volume;
148         hammer_off_t node_offset;
149
150         volume = get_volume(RootVolNo);
151         node_offset = volume->ondisk->vol0_btree_root;
152         if (QuietOpt < 3) {
153                 printf("Volume header\trecords=%jd next_tid=%016jx\n",
154                        (intmax_t)volume->ondisk->vol0_stat_records,
155                        (uintmax_t)volume->ondisk->vol0_next_tid);
156                 printf("\t\tbufoffset=%016jx\n",
157                        (uintmax_t)volume->ondisk->vol_buf_beg);
158         }
159         rel_volume(volume);
160
161         printf("Collecting allocation info from B-Tree: ");
162         fflush(stdout);
163         collect_btree_root(node_offset);
164         check_btree_node(node_offset, 0);
165         printf("done\n");
166         dump_collect_table();
167 }
168
169 static void
170 check_btree_node(hammer_off_t node_offset, int depth)
171 {
172         struct buffer_info *buffer = NULL;
173         hammer_node_ondisk_t node;
174         hammer_btree_elm_t elm;
175         int i;
176         char badc;
177
178         node = get_node(node_offset, &buffer);
179
180         if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
181                 badc = ' ';
182         else
183                 badc = 'B';
184
185         if (badc != ' ') {
186                 printf("B    NODE %016jx cnt=%02d p=%016jx "
187                        "type=%c depth=%d",
188                        (uintmax_t)node_offset, node->count,
189                        (uintmax_t)node->parent,
190                        (node->type ? node->type : '?'), depth);
191                 printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
192                 printf(" {\n");
193         }
194
195         for (i = 0; i < node->count; ++i) {
196                 elm = &node->elms[i];
197
198                 switch(node->type) {
199                 case HAMMER_BTREE_TYPE_INTERNAL:
200                         if (elm->internal.subtree_offset) {
201                                 collect_btree_internal(elm);
202                                 check_btree_node(elm->internal.subtree_offset,
203                                                  depth + 1);
204                         }
205                         break;
206                 case HAMMER_BTREE_TYPE_LEAF:
207                         if (elm->leaf.data_offset)
208                                 collect_btree_leaf(elm);
209                         break;
210                 default:
211                         assert(0);
212                 }
213         }
214         rel_buffer(buffer);
215 }
216
217 static
218 void
219 collect_btree_root(hammer_off_t node_offset)
220 {
221         collect_blockmap(node_offset,
222                 sizeof(struct hammer_node_ondisk)); /* 4KB */
223 }
224
225 static
226 void
227 collect_btree_internal(hammer_btree_elm_t elm)
228 {
229         collect_blockmap(elm->internal.subtree_offset,
230                 sizeof(struct hammer_node_ondisk)); /* 4KB */
231 }
232
233 static
234 void
235 collect_btree_leaf(hammer_btree_elm_t elm)
236 {
237         collect_blockmap(elm->leaf.data_offset,
238                 (elm->leaf.data_len + 15) & ~15);
239 }
240
241 static
242 void
243 collect_blockmap(hammer_off_t offset, int32_t length)
244 {
245         struct hammer_blockmap_layer1 layer1;
246         struct hammer_blockmap_layer2 layer2;
247         struct hammer_blockmap_layer2 *track2;
248         hammer_off_t result_offset;
249         collect_t collect;
250         int error;
251
252         result_offset = blockmap_lookup(offset, &layer1, &layer2, &error);
253         if (AssertOnFailure) {
254                 assert(HAMMER_ZONE_DECODE(result_offset) ==
255                         HAMMER_ZONE_RAW_BUFFER_INDEX);
256                 assert(error == 0);
257         }
258         collect = collect_get(layer1.phys_offset); /* layer2 address */
259         track2 = collect_get_track(collect, offset, &layer2);
260         track2->bytes_free -= length;
261 }
262
263 static
264 collect_t
265 collect_get(hammer_off_t phys_offset)
266 {
267         int hv = crc32(&phys_offset, sizeof(phys_offset)) & COLLECT_HMASK;
268         collect_t collect;
269
270         for (collect = CollectHash[hv]; collect; collect = collect->hnext) {
271                 if (collect->phys_offset == phys_offset)
272                         return(collect);
273         }
274         collect = calloc(sizeof(*collect), 1);
275         collect->track2 = malloc(HAMMER_BIGBLOCK_SIZE);
276         collect->layer2 = malloc(HAMMER_BIGBLOCK_SIZE);
277         collect->phys_offset = phys_offset;
278         collect->hnext = CollectHash[hv];
279         CollectHash[hv] = collect;
280         bzero(collect->track2, HAMMER_BIGBLOCK_SIZE);
281         bzero(collect->layer2, HAMMER_BIGBLOCK_SIZE);
282
283         return (collect);
284 }
285
286 static
287 void
288 collect_rel(collect_t collect)
289 {
290         free(collect->layer2);
291         free(collect->track2);
292         free(collect);
293 }
294
295 static
296 struct hammer_blockmap_layer2 *
297 collect_get_track(collect_t collect, hammer_off_t offset,
298                   struct hammer_blockmap_layer2 *layer2)
299 {
300         struct hammer_blockmap_layer2 *track2;
301         size_t i;
302
303         i = HAMMER_BLOCKMAP_LAYER2_OFFSET(offset) / sizeof(*track2);
304         track2 = &collect->track2[i];
305         if (track2->entry_crc == 0) {
306                 collect->layer2[i] = *layer2;
307                 track2->bytes_free = HAMMER_BIGBLOCK_SIZE;
308                 track2->entry_crc = 1;  /* steal field to tag track load */
309         }
310         return (track2);
311 }
312
313 static
314 void
315 dump_collect_table(void)
316 {
317         collect_t collect, tmp;
318         int i;
319
320         for (i = 0; i < COLLECT_HSIZE; ++i) {
321                 for (collect = CollectHash[i]; collect; ) {
322                         dump_collect(collect);
323                         tmp = collect;
324                         collect = collect->hnext;
325                         collect_rel(tmp);
326                 }
327         }
328 }
329
330 static
331 void
332 dump_collect(collect_t collect)
333 {
334         struct hammer_blockmap_layer2 *track2;
335         struct hammer_blockmap_layer2 *layer2;
336         size_t i;
337
338         for (i = 0; i < HAMMER_BLOCKMAP_RADIX2; ++i) {
339                 track2 = &collect->track2[i];
340                 layer2 = &collect->layer2[i];
341
342                 /*
343                  * Currently just check bigblocks referenced by data
344                  * or B-Tree nodes.
345                  */
346                 if (track2->entry_crc == 0)
347                         continue;
348
349                 if (track2->bytes_free != layer2->bytes_free) {
350                         printf("BM\tblock=%016jx calc %d free, got %d\n",
351                                 (intmax_t)(collect->phys_offset +
352                                            i * HAMMER_BIGBLOCK_SIZE),
353                                 track2->bytes_free,
354                                 layer2->bytes_free);
355                 } else if (VerboseOpt) {
356                         printf("\tblock=%016jx %d free (correct)\n",
357                                 (intmax_t)(collect->phys_offset +
358                                            i * HAMMER_BIGBLOCK_SIZE),
359                                 track2->bytes_free);
360                 }
361         }
362 }