usched: Allow process to change self cpu affinity
[dragonfly.git] / sbin / hammer / blockmap.c
blob7f4ccf4dd796b6c0304aef4cd808cec5b22bfa7b
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_util.h"
40 * Allocate big-blocks using our poor-man's volume->vol_free_off.
41 * We are bootstrapping the freemap itself and cannot update it yet.
43 hammer_off_t
44 bootstrap_bigblock(volume_info_t volume)
46 hammer_off_t result_offset;
48 assert_volume_offset(volume);
49 result_offset = volume->vol_free_off;
51 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
53 return(result_offset);
57 * Allocate a big-block for zone-3 for UNDO/REDO FIFO.
59 hammer_off_t
60 alloc_undo_bigblock(volume_info_t volume)
62 hammer_blockmap_t freemap;
63 buffer_info_t buffer1 = NULL;
64 buffer_info_t buffer2 = NULL;
65 hammer_blockmap_layer1_t layer1;
66 hammer_blockmap_layer2_t layer2;
67 hammer_off_t layer1_offset;
68 hammer_off_t layer2_offset;
69 hammer_off_t result_offset;
71 /* Only root volume needs formatting */
72 assert(volume->vol_no == HAMMER_ROOT_VOLNO);
74 result_offset = bootstrap_bigblock(volume);
75 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
78 * Dive layer 1.
80 layer1_offset = freemap->phys_offset +
81 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
82 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
83 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
84 --layer1->blocks_free;
85 hammer_crc_set_layer1(HammerVersion, layer1);
86 buffer1->cache.modified = 1;
89 * Dive layer 2, each entry represents a big-block.
91 layer2_offset = layer1->phys_offset +
92 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
93 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
94 assert(layer2->zone == 0);
95 layer2->zone = HAMMER_ZONE_UNDO_INDEX;
96 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
97 layer2->bytes_free = 0;
98 hammer_crc_set_layer2(HammerVersion, layer2);
99 buffer2->cache.modified = 1;
101 --volume->ondisk->vol0_stat_freebigblocks;
103 rel_buffer(buffer1);
104 rel_buffer(buffer2);
106 return(result_offset);
110 * Allocate a chunk of data out of a blockmap. This is a simplified
111 * version which uses next_offset as a simple allocation iterator.
113 void *
114 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
115 buffer_info_t *bufferp)
117 volume_info_t volume;
118 hammer_blockmap_t blockmap;
119 hammer_blockmap_t freemap;
120 buffer_info_t buffer1 = NULL;
121 buffer_info_t buffer2 = NULL;
122 hammer_blockmap_layer1_t layer1;
123 hammer_blockmap_layer2_t layer2;
124 hammer_off_t tmp_offset;
125 hammer_off_t layer1_offset;
126 hammer_off_t layer2_offset;
127 void *ptr;
129 volume = get_root_volume();
131 blockmap = &volume->ondisk->vol0_blockmap[zone];
132 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
133 assert(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
136 * Alignment and buffer-boundary issues. If the allocation would
137 * cross a buffer boundary we have to skip to the next buffer.
139 bytes = HAMMER_DATA_DOALIGN(bytes);
140 assert(bytes > 0 && bytes <= HAMMER_BUFSIZE); /* not HAMMER_XBUFSIZE */
141 assert(hammer_is_index_record(zone));
143 again:
144 assert(blockmap->next_offset != HAMMER_ZONE_ENCODE(zone + 1, 0));
146 tmp_offset = blockmap->next_offset + bytes - 1;
147 if ((blockmap->next_offset ^ tmp_offset) & ~HAMMER_BUFMASK64)
148 blockmap->next_offset = tmp_offset & ~HAMMER_BUFMASK64;
151 * Dive layer 1.
153 layer1_offset = freemap->phys_offset +
154 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
155 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
156 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
157 assert(!((blockmap->next_offset & HAMMER_BIGBLOCK_MASK) == 0 &&
158 layer1->blocks_free == 0));
161 * Dive layer 2, each entry represents a big-block.
163 layer2_offset = layer1->phys_offset +
164 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
165 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
167 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
168 errx(1, "alloc_blockmap: layer2 ran out of space!");
169 /* not reached */
173 * If we are entering a new big-block assign ownership to our
174 * zone. If the big-block is owned by another zone skip it.
176 if (layer2->zone == 0) {
177 --layer1->blocks_free;
178 hammer_crc_set_layer1(HammerVersion, layer1);
179 layer2->zone = zone;
180 --volume->ondisk->vol0_stat_freebigblocks;
181 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
182 assert(layer2->append_off == 0);
184 if (layer2->zone != zone) {
185 blockmap->next_offset =
186 HAMMER_ZONE_LAYER2_NEXT_OFFSET(blockmap->next_offset);
187 goto again;
190 assert(layer2->append_off ==
191 (blockmap->next_offset & HAMMER_BIGBLOCK_MASK));
192 layer2->bytes_free -= bytes;
193 *result_offp = blockmap->next_offset;
194 blockmap->next_offset += bytes;
195 layer2->append_off = (int)blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
196 hammer_crc_set_layer2(HammerVersion, layer2);
198 ptr = get_buffer_data(*result_offp, bufferp, 0);
199 (*bufferp)->cache.modified = 1;
201 buffer1->cache.modified = 1;
202 buffer2->cache.modified = 1;
204 rel_buffer(buffer1);
205 rel_buffer(buffer2);
206 return(ptr);
209 hammer_off_t
210 blockmap_lookup(hammer_off_t zone_offset, int *errorp)
212 return(blockmap_lookup_save(zone_offset, NULL, NULL, errorp));
215 hammer_off_t
216 blockmap_lookup_save(hammer_off_t zone_offset,
217 hammer_blockmap_layer1_t save_layer1,
218 hammer_blockmap_layer2_t save_layer2,
219 int *errorp)
221 volume_info_t root_volume = NULL;
222 hammer_volume_ondisk_t ondisk;
223 hammer_blockmap_t blockmap;
224 hammer_blockmap_t freemap;
225 hammer_blockmap_layer1_t layer1;
226 hammer_blockmap_layer2_t layer2;
227 buffer_info_t buffer1 = NULL;
228 buffer_info_t buffer2 = NULL;
229 hammer_off_t layer1_offset;
230 hammer_off_t layer2_offset;
231 hammer_off_t result_offset = HAMMER_OFF_BAD;;
232 int zone;
233 int error = 0;
235 if (save_layer1)
236 bzero(save_layer1, sizeof(*save_layer1));
237 if (save_layer2)
238 bzero(save_layer2, sizeof(*save_layer2));
240 zone = HAMMER_ZONE_DECODE(zone_offset);
242 if (zone <= HAMMER_ZONE_RAW_VOLUME_INDEX) {
243 error = -1;
244 goto done;
246 if (zone >= HAMMER_MAX_ZONES) {
247 error = -2;
248 goto done;
251 root_volume = get_root_volume();
252 ondisk = root_volume->ondisk;
253 blockmap = &ondisk->vol0_blockmap[zone];
256 * Handle blockmap offset translations.
258 if (hammer_is_index_record(zone)) {
259 result_offset = hammer_xlate_to_zone2(zone_offset);
260 } else if (zone == HAMMER_ZONE_UNDO_INDEX) {
261 if (zone_offset >= blockmap->alloc_offset) {
262 error = -3;
263 goto done;
265 result_offset = hammer_xlate_to_undo(ondisk, zone_offset);
266 } else {
267 /* assert(zone == HAMMER_ZONE_RAW_BUFFER_INDEX); */
268 result_offset = zone_offset;
272 * The blockmap should match the requested zone (else the volume
273 * header is mashed).
275 if (hammer_is_index_record(zone) &&
276 HAMMER_ZONE_DECODE(blockmap->alloc_offset) != zone) {
277 error = -4;
278 goto done;
282 * Validate that the big-block is assigned to the zone. Also
283 * assign save_layer{1,2} if not NULL.
285 freemap = &ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
288 * Dive layer 1.
290 layer1_offset = freemap->phys_offset +
291 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
292 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
294 if (layer1 == NULL) {
295 error = -5;
296 goto done;
298 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
299 error = -6;
300 goto done;
302 if (save_layer1)
303 *save_layer1 = *layer1;
306 * Dive layer 2, each entry represents a big-block.
308 layer2_offset = layer1->phys_offset +
309 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
310 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
312 if (layer2 == NULL) {
313 error = -7;
314 goto done;
316 if (layer2->zone != zone) {
317 error = -8;
318 goto done;
320 if (save_layer2)
321 *save_layer2 = *layer2;
323 done:
324 rel_buffer(buffer1);
325 rel_buffer(buffer2);
327 if (errorp)
328 *errorp = error;
330 return(result_offset);