xref: /dragonfly/sbin/hammer/blockmap.c (revision a4fe36f1)
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.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 	struct hammer_blockmap_layer1 *layer1;
53 	struct hammer_blockmap_layer2 *layer2;
54 	hammer_off_t layer1_offset;
55 	hammer_off_t layer2_offset;
56 	hammer_off_t result_offset;
57 
58 	if (volume == NULL)
59 		volume = get_root_volume();
60 
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");
64 
65 	volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
66 
67 	/*
68 	 * Update the freemap if not zone4.
69 	 */
70 	if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
71 		root_vol = get_root_volume();
72 		freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
73 
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;
81 
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;
91 
92 		--root_vol->ondisk->vol0_stat_freebigblocks;
93 
94 		rel_buffer(buffer1);
95 		rel_buffer(buffer2);
96 		rel_volume(root_vol);
97 	}
98 
99 	rel_volume(volume);
100 	return(result_offset);
101 }
102 
103 /*
104  * Allocate a chunk of data out of a blockmap.  This is a simplified
105  * version which uses next_offset as a simple allocation iterator.
106  */
107 void *
108 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
109 	       struct buffer_info **bufferp)
110 {
111 	struct volume_info *volume;
112 	hammer_blockmap_t blockmap;
113 	hammer_blockmap_t freemap;
114 	struct buffer_info *buffer1 = NULL;
115 	struct buffer_info *buffer2 = NULL;
116 	struct hammer_blockmap_layer1 *layer1;
117 	struct hammer_blockmap_layer2 *layer2;
118 	hammer_off_t layer1_offset;
119 	hammer_off_t layer2_offset;
120 	hammer_off_t chunk_offset;
121 	void *ptr;
122 
123 	volume = get_root_volume();
124 
125 	blockmap = &volume->ondisk->vol0_blockmap[zone];
126 	freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
127 	assert(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
128 
129 	/*
130 	 * Alignment and buffer-boundary issues.  If the allocation would
131 	 * cross a buffer boundary we have to skip to the next buffer.
132 	 */
133 	bytes = (bytes + 15) & ~15;
134 	assert(bytes > 0 && bytes <= HAMMER_BUFSIZE);  /* not HAMMER_XBUFSIZE */
135 	assert(hammer_is_zone2_mapped_index(zone));
136 
137 again:
138 	assert(blockmap->next_offset != HAMMER_ZONE_ENCODE(zone + 1, 0));
139 
140 	if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
141 	    ~HAMMER_BUFMASK64) {
142 		blockmap->next_offset = (blockmap->next_offset + bytes - 1) &
143 				        ~HAMMER_BUFMASK64;
144 	}
145 	chunk_offset = blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
146 
147 	/*
148 	 * Dive layer 1.
149 	 */
150 	layer1_offset = freemap->phys_offset +
151 			HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
152 	layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
153 	assert(!(chunk_offset == 0 && layer1->blocks_free == 0));
154 
155 	if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
156 		fprintf(stderr, "alloc_blockmap: ran out of space!\n");
157 		exit(1);
158 	}
159 
160 	/*
161 	 * Dive layer 2, each entry represents a big-block.
162 	 */
163 	layer2_offset = layer1->phys_offset +
164 			HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
165 	layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
166 
167 	if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
168 		fprintf(stderr, "alloc_blockmap: ran out of space!\n");
169 		exit(1);
170 	}
171 
172 	/*
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.
175 	 */
176 	if (layer2->zone == 0) {
177 		--layer1->blocks_free;
178 		layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
179 		layer2->zone = zone;
180 		--volume->ondisk->vol0_stat_freebigblocks;
181 		assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
182 		assert(layer2->append_off == 0);
183 	}
184 	if (layer2->zone != zone) {
185 		blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
186 					~HAMMER_BIGBLOCK_MASK64;
187 		goto again;
188 	}
189 
190 	assert(layer2->append_off == chunk_offset);
191 	layer2->bytes_free -= bytes;
192 	*result_offp = blockmap->next_offset;
193 	blockmap->next_offset += bytes;
194 	layer2->append_off = (int)blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
195 	layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
196 
197 	ptr = get_buffer_data(*result_offp, bufferp, 0);
198 	(*bufferp)->cache.modified = 1;
199 
200 	buffer1->cache.modified = 1;
201 	buffer2->cache.modified = 1;
202 
203 	rel_buffer(buffer1);
204 	rel_buffer(buffer2);
205 	rel_volume(volume);
206 	return(ptr);
207 }
208 
209 hammer_off_t
210 blockmap_lookup(hammer_off_t zone_offset,
211 		struct hammer_blockmap_layer1 *save_layer1,
212 		struct hammer_blockmap_layer2 *save_layer2,
213 		int *errorp)
214 {
215 	struct volume_info *root_volume = NULL;
216 	hammer_blockmap_t blockmap;
217 	hammer_blockmap_t freemap;
218 	struct hammer_blockmap_layer1 *layer1;
219 	struct hammer_blockmap_layer2 *layer2;
220 	struct buffer_info *buffer1 = NULL;
221 	struct buffer_info *buffer2 = NULL;
222 	hammer_off_t layer1_offset;
223 	hammer_off_t layer2_offset;
224 	hammer_off_t result_offset;
225 	int zone;
226 	int i;
227 	int error = 0;
228 
229 	if (save_layer1)
230 		bzero(save_layer1, sizeof(*save_layer1));
231 	if (save_layer2)
232 		bzero(save_layer2, sizeof(*save_layer2));
233 
234 	zone = HAMMER_ZONE_DECODE(zone_offset);
235 
236 	if (zone <= HAMMER_ZONE_RAW_VOLUME_INDEX)
237 		error = -1;
238 	if (zone >= HAMMER_MAX_ZONES)
239 		error = -2;
240 	if (error) {
241 		result_offset = HAMMER_OFF_BAD;
242 		goto done;
243 	}
244 
245 	root_volume = get_root_volume();
246 	blockmap = &root_volume->ondisk->vol0_blockmap[zone];
247 
248 	if (zone == HAMMER_ZONE_RAW_BUFFER_INDEX) {
249 		result_offset = zone_offset;
250 	} else if (zone == HAMMER_ZONE_UNDO_INDEX) {
251 		i = (zone_offset & HAMMER_OFF_SHORT_MASK) /
252 		    HAMMER_BIGBLOCK_SIZE;
253 		if (zone_offset >= blockmap->alloc_offset) {
254 			error = -3;
255 			result_offset = HAMMER_OFF_BAD;
256 			goto done;
257 		}
258 		result_offset = root_volume->ondisk->vol0_undo_array[i] +
259 				(zone_offset & HAMMER_BIGBLOCK_MASK64);
260 	} else {
261 		result_offset = hammer_xlate_to_zone2(zone_offset);
262 	}
263 
264 	/*
265 	 * The blockmap should match the requested zone (else the volume
266 	 * header is mashed).
267 	 *
268 	 * Note that a valid offset can still be returned if AssertOnFailure
269 	 * is zero.
270 	 */
271 	if (HAMMER_ZONE_FREEMAP_INDEX != zone &&
272 	    HAMMER_ZONE_DECODE(blockmap->alloc_offset) != zone) {
273 		error = -4;
274 		goto done;
275 	}
276 
277 	/*
278 	 * Validate that the big-block is assigned to the zone.  Also
279 	 * assign save_layer{1,2}.
280 	 */
281 
282 	freemap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
283 	/*
284 	 * Dive layer 1.
285 	 */
286 	layer1_offset = freemap->phys_offset +
287 			HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
288 	layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
289 	if (layer1 == NULL) {
290 		error = -5;
291 		goto done;
292 	}
293 	if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
294 		error = -6;
295 		goto done;
296 	}
297 
298 	if (save_layer1)
299 		*save_layer1 = *layer1;
300 
301 	/*
302 	 * Dive layer 2, each entry represents a big-block.
303 	 */
304 	layer2_offset = layer1->phys_offset +
305 			HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
306 	layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
307 
308 	if (layer2 == NULL) {
309 		error = -7;
310 		goto done;
311 	}
312 	if (layer2->zone != zone) {
313 		error = -8;
314 		goto done;
315 	}
316 	if (save_layer2)
317 		*save_layer2 = *layer2;
318 
319 done:
320 	rel_buffer(buffer1);
321 	rel_buffer(buffer2);
322 	rel_volume(root_volume);
323 
324 	if (AssertOnFailure && error != 0)
325 		errx(1, "blockmap_lookup: error=%d\n", error);
326 	if (errorp)
327 		*errorp = error;
328 
329 	return(result_offset);
330 }
331 
332