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
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
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
34 * $DragonFly: src/sbin/hammer/cmd_blockmap.c,v 1.4 2008/07/19 18:48:14 dillon Exp $
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;
46 #define COLLECT_HSIZE 1024
47 #define COLLECT_HMASK (COLLECT_HSIZE - 1)
49 collect_t CollectHash[COLLECT_HSIZE];
51 static void dump_blockmap(const char *label, int zone);
52 static void check_btree_node(hammer_off_t node_offset, int depth);
53 static void collect_btree_elm(hammer_btree_elm_t elm);
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);
62 hammer_cmd_blockmap(void)
64 dump_blockmap("btree", HAMMER_ZONE_FREEMAP_INDEX);
69 dump_blockmap(const char *label, int zone)
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;
83 assert(RootVolNo >= 0);
84 root_volume = get_volume(RootVolNo);
85 rootmap = &root_volume->ondisk->vol0_blockmap[zone];
86 assert(rootmap->phys_offset != 0);
88 printf("zone %-16s next %016jx alloc %016jx\n",
90 (uintmax_t)rootmap->next_offset,
91 (uintmax_t)rootmap->alloc_offset);
93 for (scan1 = HAMMER_ZONE_ENCODE(zone, 0);
94 scan1 < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK);
95 scan1 += HAMMER_BLOCKMAP_LAYER2) {
99 layer1_offset = rootmap->phys_offset +
100 HAMMER_BLOCKMAP_LAYER1_OFFSET(scan1);
101 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
103 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE))
106 layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
109 printf("%c layer1 %016jx @%016jx blocks-free %jd\n",
112 (uintmax_t)layer1->phys_offset,
113 (intmax_t)layer1->blocks_free);
114 if (layer1->phys_offset == HAMMER_BLOCKMAP_FREE)
117 scan2 < scan1 + HAMMER_BLOCKMAP_LAYER2;
118 scan2 += HAMMER_LARGEBLOCK_SIZE
121 * Dive layer 2, each entry represents a large-block.
123 layer2_offset = layer1->phys_offset +
124 HAMMER_BLOCKMAP_LAYER2_OFFSET(scan2);
125 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
127 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE))
129 printf("%c %016jx zone=%d app=%-7d free=%-7d\n",
141 rel_volume(root_volume);
145 hammer_cmd_checkmap(void)
147 struct volume_info *volume;
148 hammer_off_t node_offset;
150 volume = get_volume(RootVolNo);
151 node_offset = volume->ondisk->vol0_btree_root;
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);
161 printf("Collecting allocation info from B-Tree: ");
163 check_btree_node(node_offset, 0);
165 dump_collect_table();
169 check_btree_node(hammer_off_t node_offset, int depth)
171 struct buffer_info *buffer = NULL;
172 hammer_node_ondisk_t node;
173 hammer_btree_elm_t elm;
177 node = get_node(node_offset, &buffer);
179 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
185 printf("B NODE %016jx cnt=%02d p=%016jx "
187 (uintmax_t)node_offset, node->count,
188 (uintmax_t)node->parent,
189 (node->type ? node->type : '?'), depth);
190 printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
194 for (i = 0; i < node->count; ++i) {
195 elm = &node->elms[i];
198 case HAMMER_BTREE_TYPE_INTERNAL:
199 if (elm->internal.subtree_offset) {
200 check_btree_node(elm->internal.subtree_offset,
204 case HAMMER_BTREE_TYPE_LEAF:
205 if (elm->leaf.data_offset)
206 collect_btree_elm(elm);
217 collect_btree_elm(hammer_btree_elm_t elm)
219 struct hammer_blockmap_layer1 layer1;
220 struct hammer_blockmap_layer2 layer2;
221 struct hammer_blockmap_layer2 *track2;
222 hammer_off_t offset = elm->leaf.data_offset;
226 blockmap_lookup(offset, &layer1, &layer2, &error);
227 collect = collect_get(layer1.phys_offset);
228 track2 = collect_get_track(collect, offset, &layer2);
229 track2->bytes_free -= (elm->leaf.data_len + 15) & ~15;
234 collect_get(hammer_off_t phys_offset)
236 int hv = crc32(&phys_offset, sizeof(phys_offset)) & COLLECT_HMASK;
239 for (collect = CollectHash[hv]; collect; collect = collect->hnext) {
240 if (collect->phys_offset == phys_offset)
243 collect = calloc(sizeof(*collect), 1);
244 collect->track2 = malloc(HAMMER_LARGEBLOCK_SIZE);
245 collect->layer2 = malloc(HAMMER_LARGEBLOCK_SIZE);
246 collect->phys_offset = phys_offset;
247 collect->hnext = CollectHash[hv];
248 CollectHash[hv] = collect;
249 bzero(collect->track2, HAMMER_LARGEBLOCK_SIZE);
250 bzero(collect->layer2, HAMMER_LARGEBLOCK_SIZE);
256 struct hammer_blockmap_layer2 *
257 collect_get_track(collect_t collect, hammer_off_t offset,
258 struct hammer_blockmap_layer2 *layer2)
260 struct hammer_blockmap_layer2 *track2;
263 i = HAMMER_BLOCKMAP_LAYER2_OFFSET(offset) / sizeof(*track2);
264 track2 = &collect->track2[i];
265 if (track2->entry_crc == 0) {
266 collect->layer2[i] = *layer2;
267 track2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
268 track2->entry_crc = 1; /* steal field to tag track load */
275 dump_collect_table(void)
280 for (i = 0; i < COLLECT_HSIZE; ++i) {
281 for (collect = CollectHash[i];
283 collect = collect->hnext) {
284 dump_collect(collect);
291 dump_collect(collect_t collect)
293 struct hammer_blockmap_layer2 *track2;
294 struct hammer_blockmap_layer2 *layer2;
297 for (i = 0; i < HAMMER_BLOCKMAP_RADIX2; ++i) {
298 track2 = &collect->track2[i];
299 layer2 = &collect->layer2[i];
302 * Currently just check bigblocks referenced by data
305 if (track2->entry_crc == 0)
308 if (track2->bytes_free != layer2->bytes_free) {
309 printf("BM\tblock=%016jx calc %d free, got %d\n",
310 (intmax_t)(collect->phys_offset +
311 i * HAMMER_LARGEBLOCK_SIZE),
314 } else if (VerboseOpt) {
315 printf("\tblock=%016jx %d free (correct)\n",
316 (intmax_t)(collect->phys_offset +
317 i * HAMMER_LARGEBLOCK_SIZE),