xref: /dragonfly/sbin/hammer/blockmap.c (revision 6ab64ab6)
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/blockmap.c,v 1.2 2008/06/17 04:03:38 dillon Exp $
35  */
36 
37 #include "hammer_util.h"
38 
39 /*
40  * Allocate big-blocks using our poor-man's volume->vol_free_off.
41  *
42  * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
43  * itself and cannot update it yet.
44  */
45 hammer_off_t
46 alloc_bigblock(struct volume_info *volume, int zone)
47 {
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;
57 
58 	assert_volume_offset(volume);
59 	result_offset = volume->vol_free_off;
60 	if (result_offset >= volume->vol_free_end)
61 		errx(1, "alloc_bigblock: Ran out of room, filesystem too small");
62 
63 	volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
64 
65 	/*
66 	 * Update the freemap if not zone4.
67 	 */
68 	if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
69 		root_vol = get_root_volume();
70 		freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
71 
72 		layer1_offset = freemap->phys_offset +
73 			        HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
74 		layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
75 		assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
76 		--layer1->blocks_free;
77 		hammer_crc_set_layer1(layer1);
78 		buffer1->cache.modified = 1;
79 
80 		layer2_offset = layer1->phys_offset +
81 			        HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
82 		layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
83 		assert(layer2->zone == 0);
84 		layer2->zone = zone;
85 		layer2->append_off = HAMMER_BIGBLOCK_SIZE;
86 		layer2->bytes_free = 0;
87 		hammer_crc_set_layer2(layer2);
88 		buffer2->cache.modified = 1;
89 
90 		--root_vol->ondisk->vol0_stat_freebigblocks;
91 
92 		rel_buffer(buffer1);
93 		rel_buffer(buffer2);
94 	}
95 
96 	return(result_offset);
97 }
98 
99 /*
100  * Allocate a chunk of data out of a blockmap.  This is a simplified
101  * version which uses next_offset as a simple allocation iterator.
102  */
103 void *
104 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
105 	       struct buffer_info **bufferp)
106 {
107 	struct volume_info *volume;
108 	hammer_blockmap_t blockmap;
109 	hammer_blockmap_t freemap;
110 	struct buffer_info *buffer1 = NULL;
111 	struct buffer_info *buffer2 = NULL;
112 	hammer_blockmap_layer1_t layer1;
113 	hammer_blockmap_layer2_t layer2;
114 	hammer_off_t layer1_offset;
115 	hammer_off_t layer2_offset;
116 	hammer_off_t chunk_offset;
117 	void *ptr;
118 
119 	volume = get_root_volume();
120 
121 	blockmap = &volume->ondisk->vol0_blockmap[zone];
122 	freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
123 	assert(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
124 
125 	/*
126 	 * Alignment and buffer-boundary issues.  If the allocation would
127 	 * cross a buffer boundary we have to skip to the next buffer.
128 	 */
129 	bytes = (bytes + 15) & ~15;
130 	assert(bytes > 0 && bytes <= HAMMER_BUFSIZE);  /* not HAMMER_XBUFSIZE */
131 	assert(hammer_is_zone2_mapped_index(zone));
132 
133 again:
134 	assert(blockmap->next_offset != HAMMER_ZONE_ENCODE(zone + 1, 0));
135 
136 	if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
137 	    ~HAMMER_BUFMASK64) {
138 		blockmap->next_offset = (blockmap->next_offset + bytes - 1) &
139 				        ~HAMMER_BUFMASK64;
140 	}
141 	chunk_offset = blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
142 
143 	/*
144 	 * Dive layer 1.
145 	 */
146 	layer1_offset = freemap->phys_offset +
147 			HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
148 	layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
149 	assert(!(chunk_offset == 0 && layer1->blocks_free == 0));
150 
151 	if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
152 		fprintf(stderr, "alloc_blockmap: ran out of space!\n");
153 		exit(1);
154 	}
155 
156 	/*
157 	 * Dive layer 2, each entry represents a big-block.
158 	 */
159 	layer2_offset = layer1->phys_offset +
160 			HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
161 	layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
162 
163 	if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
164 		fprintf(stderr, "alloc_blockmap: ran out of space!\n");
165 		exit(1);
166 	}
167 
168 	/*
169 	 * If we are entering a new big-block assign ownership to our
170 	 * zone.  If the big-block is owned by another zone skip it.
171 	 */
172 	if (layer2->zone == 0) {
173 		--layer1->blocks_free;
174 		hammer_crc_set_layer1(layer1);
175 		layer2->zone = zone;
176 		--volume->ondisk->vol0_stat_freebigblocks;
177 		assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
178 		assert(layer2->append_off == 0);
179 	}
180 	if (layer2->zone != zone) {
181 		blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
182 					~HAMMER_BIGBLOCK_MASK64;
183 		goto again;
184 	}
185 
186 	assert(layer2->append_off == chunk_offset);
187 	layer2->bytes_free -= bytes;
188 	*result_offp = blockmap->next_offset;
189 	blockmap->next_offset += bytes;
190 	layer2->append_off = (int)blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
191 	hammer_crc_set_layer2(layer2);
192 
193 	ptr = get_buffer_data(*result_offp, bufferp, 0);
194 	(*bufferp)->cache.modified = 1;
195 
196 	buffer1->cache.modified = 1;
197 	buffer2->cache.modified = 1;
198 
199 	rel_buffer(buffer1);
200 	rel_buffer(buffer2);
201 	return(ptr);
202 }
203 
204 hammer_off_t
205 blockmap_lookup(hammer_off_t zone_offset,
206 		hammer_blockmap_layer1_t save_layer1,
207 		hammer_blockmap_layer2_t save_layer2,
208 		int *errorp)
209 {
210 	struct volume_info *root_volume = NULL;
211 	hammer_blockmap_t blockmap;
212 	hammer_blockmap_t freemap;
213 	hammer_blockmap_layer1_t layer1;
214 	hammer_blockmap_layer2_t layer2;
215 	struct buffer_info *buffer1 = NULL;
216 	struct buffer_info *buffer2 = NULL;
217 	hammer_off_t layer1_offset;
218 	hammer_off_t layer2_offset;
219 	hammer_off_t result_offset;
220 	int zone;
221 	int i;
222 	int error = 0;
223 
224 	if (save_layer1)
225 		bzero(save_layer1, sizeof(*save_layer1));
226 	if (save_layer2)
227 		bzero(save_layer2, sizeof(*save_layer2));
228 
229 	zone = HAMMER_ZONE_DECODE(zone_offset);
230 
231 	if (zone <= HAMMER_ZONE_RAW_VOLUME_INDEX)
232 		error = -1;
233 	if (zone >= HAMMER_MAX_ZONES)
234 		error = -2;
235 	if (error) {
236 		result_offset = HAMMER_OFF_BAD;
237 		goto done;
238 	}
239 
240 	root_volume = get_root_volume();
241 	blockmap = &root_volume->ondisk->vol0_blockmap[zone];
242 
243 	if (zone == HAMMER_ZONE_RAW_BUFFER_INDEX) {
244 		result_offset = zone_offset;
245 	} else if (zone == HAMMER_ZONE_UNDO_INDEX) {
246 		i = HAMMER_OFF_SHORT_ENCODE(zone_offset) / HAMMER_BIGBLOCK_SIZE;
247 		if (zone_offset >= blockmap->alloc_offset) {
248 			error = -3;
249 			result_offset = HAMMER_OFF_BAD;
250 			goto done;
251 		}
252 		result_offset = root_volume->ondisk->vol0_undo_array[i] +
253 				(zone_offset & HAMMER_BIGBLOCK_MASK64);
254 	} else {
255 		result_offset = hammer_xlate_to_zone2(zone_offset);
256 	}
257 
258 	/*
259 	 * The blockmap should match the requested zone (else the volume
260 	 * header is mashed).
261 	 */
262 	if (HAMMER_ZONE_FREEMAP_INDEX != zone &&
263 	    HAMMER_ZONE_DECODE(blockmap->alloc_offset) != zone) {
264 		error = -4;
265 		goto done;
266 	}
267 
268 	/*
269 	 * Validate that the big-block is assigned to the zone.  Also
270 	 * assign save_layer{1,2}.
271 	 */
272 
273 	freemap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
274 	/*
275 	 * Dive layer 1.
276 	 */
277 	layer1_offset = freemap->phys_offset +
278 			HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
279 	layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
280 	if (layer1 == NULL) {
281 		error = -5;
282 		goto done;
283 	}
284 	if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
285 		error = -6;
286 		goto done;
287 	}
288 
289 	if (save_layer1)
290 		*save_layer1 = *layer1;
291 
292 	/*
293 	 * Dive layer 2, each entry represents a big-block.
294 	 */
295 	layer2_offset = layer1->phys_offset +
296 			HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
297 	layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
298 
299 	if (layer2 == NULL) {
300 		error = -7;
301 		goto done;
302 	}
303 	if (layer2->zone != zone) {
304 		error = -8;
305 		goto done;
306 	}
307 	if (save_layer2)
308 		*save_layer2 = *layer2;
309 
310 done:
311 	rel_buffer(buffer1);
312 	rel_buffer(buffer2);
313 
314 	if (errorp)
315 		*errorp = error;
316 
317 	return(result_offset);
318 }
319 
320