sys/vfs/hammer: Use typedef'd for struct hammer_blockmap_xxx*
[dragonfly.git] / sbin / hammer / blockmap.c
blob7e4e33d680bcb1f0e4d6ac61cd1a8e2e9df58271
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/blockmap.c,v 1.2 2008/06/17 04:03:38 dillon Exp $
37 #include "hammer.h"
40 * Allocate big-blocks using our poor-man's volume->vol_free_off.
42 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
43 * itself and cannot update it yet.
45 hammer_off_t
46 alloc_bigblock(struct volume_info *volume, int zone)
48 struct volume_info *root_vol;
49 hammer_blockmap_t freemap;
50 struct buffer_info *buffer1 = NULL;
51 struct buffer_info *buffer2 = NULL;
52 hammer_blockmap_layer1_t layer1;
53 hammer_blockmap_layer2_t layer2;
54 hammer_off_t layer1_offset;
55 hammer_off_t layer2_offset;
56 hammer_off_t result_offset;
58 if (volume == NULL)
59 volume = get_root_volume();
61 result_offset = volume->vol_free_off;
62 if (result_offset >= volume->vol_free_end)
63 errx(1, "alloc_bigblock: Ran out of room, filesystem too small");
65 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
68 * Update the freemap if not zone4.
70 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
71 root_vol = get_root_volume();
72 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
74 layer1_offset = freemap->phys_offset +
75 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
76 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
77 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
78 --layer1->blocks_free;
79 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
80 buffer1->cache.modified = 1;
82 layer2_offset = layer1->phys_offset +
83 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
84 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
85 assert(layer2->zone == 0);
86 layer2->zone = zone;
87 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
88 layer2->bytes_free = 0;
89 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
90 buffer2->cache.modified = 1;
92 --root_vol->ondisk->vol0_stat_freebigblocks;
94 rel_buffer(buffer1);
95 rel_buffer(buffer2);
98 return(result_offset);
102 * Allocate a chunk of data out of a blockmap. This is a simplified
103 * version which uses next_offset as a simple allocation iterator.
105 void *
106 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
107 struct buffer_info **bufferp)
109 struct volume_info *volume;
110 hammer_blockmap_t blockmap;
111 hammer_blockmap_t freemap;
112 struct buffer_info *buffer1 = NULL;
113 struct buffer_info *buffer2 = NULL;
114 hammer_blockmap_layer1_t layer1;
115 hammer_blockmap_layer2_t layer2;
116 hammer_off_t layer1_offset;
117 hammer_off_t layer2_offset;
118 hammer_off_t chunk_offset;
119 void *ptr;
121 volume = get_root_volume();
123 blockmap = &volume->ondisk->vol0_blockmap[zone];
124 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
125 assert(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
128 * Alignment and buffer-boundary issues. If the allocation would
129 * cross a buffer boundary we have to skip to the next buffer.
131 bytes = (bytes + 15) & ~15;
132 assert(bytes > 0 && bytes <= HAMMER_BUFSIZE); /* not HAMMER_XBUFSIZE */
133 assert(hammer_is_zone2_mapped_index(zone));
135 again:
136 assert(blockmap->next_offset != HAMMER_ZONE_ENCODE(zone + 1, 0));
138 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
139 ~HAMMER_BUFMASK64) {
140 blockmap->next_offset = (blockmap->next_offset + bytes - 1) &
141 ~HAMMER_BUFMASK64;
143 chunk_offset = blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
146 * Dive layer 1.
148 layer1_offset = freemap->phys_offset +
149 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
150 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
151 assert(!(chunk_offset == 0 && layer1->blocks_free == 0));
153 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
154 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
155 exit(1);
159 * Dive layer 2, each entry represents a big-block.
161 layer2_offset = layer1->phys_offset +
162 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
163 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
165 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
166 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
167 exit(1);
171 * If we are entering a new big-block assign ownership to our
172 * zone. If the big-block is owned by another zone skip it.
174 if (layer2->zone == 0) {
175 --layer1->blocks_free;
176 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
177 layer2->zone = zone;
178 --volume->ondisk->vol0_stat_freebigblocks;
179 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
180 assert(layer2->append_off == 0);
182 if (layer2->zone != zone) {
183 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
184 ~HAMMER_BIGBLOCK_MASK64;
185 goto again;
188 assert(layer2->append_off == chunk_offset);
189 layer2->bytes_free -= bytes;
190 *result_offp = blockmap->next_offset;
191 blockmap->next_offset += bytes;
192 layer2->append_off = (int)blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
193 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
195 ptr = get_buffer_data(*result_offp, bufferp, 0);
196 (*bufferp)->cache.modified = 1;
198 buffer1->cache.modified = 1;
199 buffer2->cache.modified = 1;
201 rel_buffer(buffer1);
202 rel_buffer(buffer2);
203 return(ptr);
206 hammer_off_t
207 blockmap_lookup(hammer_off_t zone_offset,
208 hammer_blockmap_layer1_t save_layer1,
209 hammer_blockmap_layer2_t save_layer2,
210 int *errorp)
212 struct volume_info *root_volume = NULL;
213 hammer_blockmap_t blockmap;
214 hammer_blockmap_t freemap;
215 hammer_blockmap_layer1_t layer1;
216 hammer_blockmap_layer2_t layer2;
217 struct buffer_info *buffer1 = NULL;
218 struct buffer_info *buffer2 = NULL;
219 hammer_off_t layer1_offset;
220 hammer_off_t layer2_offset;
221 hammer_off_t result_offset;
222 int zone;
223 int i;
224 int error = 0;
226 if (save_layer1)
227 bzero(save_layer1, sizeof(*save_layer1));
228 if (save_layer2)
229 bzero(save_layer2, sizeof(*save_layer2));
231 zone = HAMMER_ZONE_DECODE(zone_offset);
233 if (zone <= HAMMER_ZONE_RAW_VOLUME_INDEX)
234 error = -1;
235 if (zone >= HAMMER_MAX_ZONES)
236 error = -2;
237 if (error) {
238 result_offset = HAMMER_OFF_BAD;
239 goto done;
242 root_volume = get_root_volume();
243 blockmap = &root_volume->ondisk->vol0_blockmap[zone];
245 if (zone == HAMMER_ZONE_RAW_BUFFER_INDEX) {
246 result_offset = zone_offset;
247 } else if (zone == HAMMER_ZONE_UNDO_INDEX) {
248 i = (zone_offset & HAMMER_OFF_SHORT_MASK) /
249 HAMMER_BIGBLOCK_SIZE;
250 if (zone_offset >= blockmap->alloc_offset) {
251 error = -3;
252 result_offset = HAMMER_OFF_BAD;
253 goto done;
255 result_offset = root_volume->ondisk->vol0_undo_array[i] +
256 (zone_offset & HAMMER_BIGBLOCK_MASK64);
257 } else {
258 result_offset = hammer_xlate_to_zone2(zone_offset);
262 * The blockmap should match the requested zone (else the volume
263 * header is mashed).
265 if (HAMMER_ZONE_FREEMAP_INDEX != zone &&
266 HAMMER_ZONE_DECODE(blockmap->alloc_offset) != zone) {
267 error = -4;
268 goto done;
272 * Validate that the big-block is assigned to the zone. Also
273 * assign save_layer{1,2}.
276 freemap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
278 * Dive layer 1.
280 layer1_offset = freemap->phys_offset +
281 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
282 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
283 if (layer1 == NULL) {
284 error = -5;
285 goto done;
287 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
288 error = -6;
289 goto done;
292 if (save_layer1)
293 *save_layer1 = *layer1;
296 * Dive layer 2, each entry represents a big-block.
298 layer2_offset = layer1->phys_offset +
299 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
300 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
302 if (layer2 == NULL) {
303 error = -7;
304 goto done;
306 if (layer2->zone != zone) {
307 error = -8;
308 goto done;
310 if (save_layer2)
311 *save_layer2 = *layer2;
313 done:
314 rel_buffer(buffer1);
315 rel_buffer(buffer2);
317 if (errorp)
318 *errorp = error;
320 return(result_offset);