2 * Copyright (c) 2007 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/ondisk.c,v 1.10 2008/02/08 08:30:56 dillon Exp $
37 #include <sys/types.h>
46 #include "hammer_util.h"
48 static void init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type);
49 static hammer_off_t hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
50 struct buffer_info **bufp, u_int16_t hdr_type);
52 static void readhammerbuf(struct volume_info *vol, void *data,
55 static void writehammerbuf(struct volume_info *vol, const void *data,
63 int UsingSuperClusters;
66 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
69 * Lookup the requested information structure and related on-disk buffer.
70 * Missing structures are created.
73 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
75 struct volume_info *vol;
76 struct volume_info *scan;
77 struct hammer_volume_ondisk *ondisk;
81 * Allocate the volume structure
83 vol = malloc(sizeof(*vol));
84 bzero(vol, sizeof(*vol));
85 TAILQ_INIT(&vol->buffer_list);
86 vol->name = strdup(filename);
87 vol->fd = open(filename, oflags);
91 err(1, "setup_volume: %s: Open failed", filename);
95 * Read or initialize the volume header
97 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
99 bzero(ondisk, HAMMER_BUFSIZE);
101 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
102 if (n != HAMMER_BUFSIZE) {
103 err(1, "setup_volume: %s: Read failed at offset 0",
106 vol_no = ondisk->vol_no;
108 RootVolNo = ondisk->vol_rootvol;
109 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
110 errx(1, "setup_volume: %s: root volume disagreement: "
112 vol->name, RootVolNo, ondisk->vol_rootvol);
115 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
116 errx(1, "setup_volume: %s: Header does not indicate "
117 "that this is a hammer volume", vol->name);
119 if (TAILQ_EMPTY(&VolList)) {
120 Hammer_FSId = vol->ondisk->vol_fsid;
121 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
122 errx(1, "setup_volume: %s: FSId does match other "
123 "volumes!", vol->name);
126 vol->vol_no = vol_no;
129 init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);
130 vol->cache.modified = 1;
134 * Link the volume structure in
136 TAILQ_FOREACH(scan, &VolList, entry) {
137 if (scan->vol_no == vol_no) {
138 errx(1, "setup_volume %s: Duplicate volume number %d "
139 "against %s", filename, vol_no, scan->name);
142 TAILQ_INSERT_TAIL(&VolList, vol, entry);
147 get_volume(int32_t vol_no)
149 struct volume_info *vol;
151 TAILQ_FOREACH(vol, &VolList, entry) {
152 if (vol->vol_no == vol_no)
156 errx(1, "get_volume: Volume %d does not exist!", vol_no);
158 /* not added to or removed from hammer cache */
163 rel_volume(struct volume_info *volume)
165 /* not added to or removed from hammer cache */
166 --volume->cache.refs;
170 * Acquire the specified buffer.
173 get_buffer(hammer_off_t buf_offset, int isnew)
176 struct buffer_info *buf;
177 struct volume_info *volume;
181 assert((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
183 vol_no = HAMMER_VOL_DECODE(buf_offset);
184 volume = get_volume(vol_no);
185 buf_offset &= ~HAMMER_BUFMASK64;
187 TAILQ_FOREACH(buf, &volume->buffer_list, entry) {
188 if (buf->buf_offset == buf_offset)
192 buf = malloc(sizeof(*buf));
193 bzero(buf, sizeof(*buf));
194 buf->buf_offset = buf_offset;
195 buf->buf_disk_offset = volume->ondisk->vol_buf_beg +
196 (buf_offset & HAMMER_OFF_SHORT_MASK);
197 buf->volume = volume;
198 TAILQ_INSERT_TAIL(&volume->buffer_list, buf, entry);
199 ++volume->cache.refs;
200 buf->cache.u.buffer = buf;
201 hammer_cache_add(&buf->cache, ISBUFFER);
204 hammer_cache_flush();
205 if ((ondisk = buf->ondisk) == NULL) {
206 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
208 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
209 buf->buf_disk_offset);
210 if (n != HAMMER_BUFSIZE) {
211 err(1, "get_buffer: %s:%016llx Read failed at "
213 volume->name, buf->buf_offset,
214 buf->buf_disk_offset);
219 bzero(ondisk, HAMMER_BUFSIZE);
220 buf->cache.modified = 1;
226 rel_buffer(struct buffer_info *buffer)
228 struct volume_info *volume;
230 assert(buffer->cache.refs > 0);
231 if (--buffer->cache.refs == 0) {
232 if (buffer->cache.delete) {
233 volume = buffer->volume;
234 if (buffer->cache.modified)
235 flush_buffer(buffer);
236 TAILQ_REMOVE(&volume->buffer_list, buffer, entry);
237 hammer_cache_del(&buffer->cache);
238 free(buffer->ondisk);
246 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
247 * bufp is freed if non-NULL and a referenced buffer is loaded into it.
250 get_node(hammer_off_t node_offset, struct buffer_info **bufp)
252 struct buffer_info *buf;
256 *bufp = buf = get_buffer(node_offset, 0);
257 return((void *)((char *)buf->ondisk +
258 (int32_t)(node_offset & HAMMER_BUFMASK)));
262 * Allocate HAMMER elements - btree nodes, data storage, and record elements
264 * NOTE: hammer_alloc_fifo() initializes the fifo header for the returned
265 * item and zero's out the remainder, so don't bzero() it.
268 alloc_btree_element(hammer_off_t *offp)
270 struct buffer_info *buf;
273 *offp = hammer_alloc_fifo(sizeof(struct hammer_node_ondisk), 0,
274 &buf, HAMMER_HEAD_TYPE_BTREE);
275 item = (char *)buf->ondisk + ((int32_t)*offp & HAMMER_BUFMASK);
276 /* XXX buf not released, ptr remains valid */
280 hammer_record_ondisk_t
281 alloc_record_element(hammer_off_t *offp, u_int8_t rec_type,
282 int32_t rec_len, int32_t data_len, void **datap)
284 struct buffer_info *buf;
285 hammer_record_ondisk_t rec;
286 int32_t aligned_rec_len;
288 aligned_rec_len = (rec_len + HAMMER_HEAD_ALIGN_MASK) &
289 ~HAMMER_HEAD_ALIGN_MASK;
291 *offp = hammer_alloc_fifo(aligned_rec_len, data_len, &buf,
292 HAMMER_HEAD_TYPE_RECORD);
293 rec = (void *)((char *)buf->ondisk + ((int32_t)*offp & HAMMER_BUFMASK));
294 rec->base.base.rec_type = rec_type;
296 rec->base.data_off = *offp + aligned_rec_len;
297 rec->base.data_len = data_len;
298 *datap = (char *)rec + aligned_rec_len;
302 /* XXX buf not released, ptr remains valid */
307 * Reserve space from the FIFO. Make sure that bytes does not cross a
310 * Initialize the fifo header, keep track of the previous entry's size
311 * so the reverse poitner can be initialized (using lastBlk), and also
312 * store a terminator (used by the recovery code) which will be overwritten
313 * by the next allocation.
317 hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
318 struct buffer_info **bufp, u_int16_t hdr_type)
320 struct buffer_info *buf;
321 struct volume_info *volume;
322 hammer_fifo_head_t head;
324 int32_t aligned_bytes;
325 static u_int32_t lastBlk;
327 aligned_bytes = (base_bytes + ext_bytes + HAMMER_HEAD_ALIGN_MASK) &
328 ~HAMMER_HEAD_ALIGN_MASK;
330 volume = get_volume(RootVolNo);
331 off = volume->ondisk->vol0_fifo_end;
334 * For now don't deal with transitions across buffer boundaries,
335 * only newfs_hammer uses this function.
337 assert((off & ~HAMMER_BUFMASK64) ==
338 ((off + aligned_bytes + sizeof(*head)) & ~HAMMER_BUFMASK));
340 *bufp = buf = get_buffer(off, 0);
342 buf->cache.modified = 1;
343 volume->cache.modified = 1;
345 head = (void *)((char *)buf->ondisk + ((int32_t)off & HAMMER_BUFMASK));
346 bzero(head, base_bytes);
348 head->hdr_type = hdr_type;
349 head->hdr_rev_link = lastBlk;
350 head->hdr_fwd_link = aligned_bytes;
351 head->hdr_seq = volume->ondisk->vol0_next_seq++;
352 lastBlk = head->hdr_fwd_link;
354 volume->ondisk->vol0_fifo_end += aligned_bytes;
355 volume->cache.modified = 1;
356 head = (void *)((char *)head + aligned_bytes);
357 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
358 head->hdr_type = HAMMER_HEAD_TYPE_TERM;
359 head->hdr_rev_link = lastBlk;
360 head->hdr_fwd_link = 0;
362 head->hdr_seq = volume->ondisk->vol0_next_seq;
370 * Flush various tracking structures to disk
374 * Flush various tracking structures to disk
377 flush_all_volumes(void)
379 struct volume_info *vol;
381 TAILQ_FOREACH(vol, &VolList, entry)
386 flush_volume(struct volume_info *volume)
388 struct buffer_info *buffer;
390 TAILQ_FOREACH(buffer, &volume->buffer_list, entry)
391 flush_buffer(buffer);
392 writehammerbuf(volume, volume->ondisk, 0);
393 volume->cache.modified = 0;
397 flush_buffer(struct buffer_info *buffer)
399 writehammerbuf(buffer->volume, buffer->ondisk, buffer->buf_disk_offset);
400 buffer->cache.modified = 0;
404 * Generic buffer initialization
407 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
409 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
410 head->hdr_type = hdr_type;
411 head->hdr_rev_link = 0;
412 head->hdr_fwd_link = 0;
419 * Core I/O operations
422 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
426 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
427 if (n != HAMMER_BUFSIZE)
428 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
434 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
438 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
439 if (n != HAMMER_BUFSIZE)
440 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
444 panic(const char *ctl, ...)
449 vfprintf(stderr, ctl, va);
451 fprintf(stderr, "\n");