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.16 2008/05/05 20:34:52 dillon Exp $
37 #include <sys/types.h>
47 #include "hammer_util.h"
49 static void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
50 struct buffer_info **bufferp);
51 static hammer_off_t alloc_bigblock(struct volume_info *volume,
54 static void init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type);
55 static hammer_off_t hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
56 struct buffer_info **bufp, u_int16_t hdr_type);
57 static void readhammerbuf(struct volume_info *vol, void *data,
60 static void writehammerbuf(struct volume_info *vol, const void *data,
68 int64_t UndoBufferSize;
69 int UsingSuperClusters;
72 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
75 * Lookup the requested information structure and related on-disk buffer.
76 * Missing structures are created.
79 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
81 struct volume_info *vol;
82 struct volume_info *scan;
83 struct hammer_volume_ondisk *ondisk;
87 * Allocate the volume structure
89 vol = malloc(sizeof(*vol));
90 bzero(vol, sizeof(*vol));
91 TAILQ_INIT(&vol->buffer_list);
92 vol->name = strdup(filename);
93 vol->fd = open(filename, oflags);
97 err(1, "setup_volume: %s: Open failed", filename);
101 * Read or initialize the volume header
103 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
105 bzero(ondisk, HAMMER_BUFSIZE);
107 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
108 if (n != HAMMER_BUFSIZE) {
109 err(1, "setup_volume: %s: Read failed at offset 0",
112 vol_no = ondisk->vol_no;
114 RootVolNo = ondisk->vol_rootvol;
115 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
116 errx(1, "setup_volume: %s: root volume disagreement: "
118 vol->name, RootVolNo, ondisk->vol_rootvol);
121 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
122 errx(1, "setup_volume: %s: Header does not indicate "
123 "that this is a hammer volume", vol->name);
125 if (TAILQ_EMPTY(&VolList)) {
126 Hammer_FSId = vol->ondisk->vol_fsid;
127 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
128 errx(1, "setup_volume: %s: FSId does match other "
129 "volumes!", vol->name);
132 vol->vol_no = vol_no;
135 /*init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);*/
136 vol->cache.modified = 1;
140 * Link the volume structure in
142 TAILQ_FOREACH(scan, &VolList, entry) {
143 if (scan->vol_no == vol_no) {
144 errx(1, "setup_volume %s: Duplicate volume number %d "
145 "against %s", filename, vol_no, scan->name);
148 TAILQ_INSERT_TAIL(&VolList, vol, entry);
153 get_volume(int32_t vol_no)
155 struct volume_info *vol;
157 TAILQ_FOREACH(vol, &VolList, entry) {
158 if (vol->vol_no == vol_no)
162 errx(1, "get_volume: Volume %d does not exist!", vol_no);
164 /* not added to or removed from hammer cache */
169 rel_volume(struct volume_info *volume)
171 /* not added to or removed from hammer cache */
172 --volume->cache.refs;
176 * Acquire the specified buffer.
179 get_buffer(hammer_off_t buf_offset, int isnew)
182 struct buffer_info *buf;
183 struct volume_info *volume;
188 zone = HAMMER_ZONE_DECODE(buf_offset);
189 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
190 buf_offset = blockmap_lookup(buf_offset, NULL, NULL);
192 assert((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
193 vol_no = HAMMER_VOL_DECODE(buf_offset);
194 volume = get_volume(vol_no);
195 buf_offset &= ~HAMMER_BUFMASK64;
197 TAILQ_FOREACH(buf, &volume->buffer_list, entry) {
198 if (buf->buf_offset == buf_offset)
202 buf = malloc(sizeof(*buf));
203 bzero(buf, sizeof(*buf));
204 buf->buf_offset = buf_offset;
205 buf->buf_disk_offset = volume->ondisk->vol_buf_beg +
206 (buf_offset & HAMMER_OFF_SHORT_MASK);
207 buf->volume = volume;
208 TAILQ_INSERT_TAIL(&volume->buffer_list, buf, entry);
209 ++volume->cache.refs;
210 buf->cache.u.buffer = buf;
211 hammer_cache_add(&buf->cache, ISBUFFER);
214 hammer_cache_flush();
215 if ((ondisk = buf->ondisk) == NULL) {
216 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
218 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
219 buf->buf_disk_offset);
220 if (n != HAMMER_BUFSIZE) {
221 err(1, "get_buffer: %s:%016llx Read failed at "
223 volume->name, buf->buf_offset,
224 buf->buf_disk_offset);
229 bzero(ondisk, HAMMER_BUFSIZE);
230 buf->cache.modified = 1;
236 rel_buffer(struct buffer_info *buffer)
238 struct volume_info *volume;
240 assert(buffer->cache.refs > 0);
241 if (--buffer->cache.refs == 0) {
242 if (buffer->cache.delete) {
243 volume = buffer->volume;
244 if (buffer->cache.modified)
245 flush_buffer(buffer);
246 TAILQ_REMOVE(&volume->buffer_list, buffer, entry);
247 hammer_cache_del(&buffer->cache);
248 free(buffer->ondisk);
256 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
259 struct buffer_info *buffer;
261 if ((buffer = *bufferp) != NULL) {
263 ((buffer->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
265 buffer = *bufferp = NULL;
269 buffer = *bufferp = get_buffer(buf_offset, isnew);
270 return((char *)buffer->ondisk + ((int32_t)buf_offset & HAMMER_BUFMASK));
274 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
275 * bufp is freed if non-NULL and a referenced buffer is loaded into it.
278 get_node(hammer_off_t node_offset, struct buffer_info **bufp)
280 struct buffer_info *buf;
284 *bufp = buf = get_buffer(node_offset, 0);
285 return((void *)((char *)buf->ondisk +
286 (int32_t)(node_offset & HAMMER_BUFMASK)));
290 * Allocate HAMMER elements - btree nodes, data storage, and record elements
292 * NOTE: hammer_alloc_fifo() initializes the fifo header for the returned
293 * item and zero's out the remainder, so don't bzero() it.
296 alloc_btree_element(hammer_off_t *offp)
298 struct buffer_info *buffer = NULL;
299 hammer_node_ondisk_t node;
301 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
303 bzero(node, sizeof(*node));
304 /* XXX buffer not released, pointer remains valid */
308 hammer_record_ondisk_t
309 alloc_record_element(hammer_off_t *offp, int32_t data_len, void **datap)
311 struct buffer_info *record_buffer = NULL;
312 struct buffer_info *data_buffer = NULL;
313 hammer_record_ondisk_t rec;
315 rec = alloc_blockmap(HAMMER_ZONE_RECORD_INDEX, sizeof(*rec),
316 offp, &record_buffer);
317 bzero(rec, sizeof(*rec));
319 if (data_len >= HAMMER_BUFSIZE) {
320 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
321 *datap = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
322 &rec->base.data_off, &data_buffer);
323 rec->base.data_len = data_len;
324 bzero(*datap, data_len);
325 } else if (data_len) {
326 *datap = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
327 &rec->base.data_off, &data_buffer);
328 rec->base.data_len = data_len;
329 bzero(*datap, data_len);
333 /* XXX buf not released, ptr remains valid */
338 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
339 * code will load each volume's freemap.
342 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
344 struct buffer_info *buffer = NULL;
345 hammer_off_t layer1_offset;
346 struct hammer_blockmap_layer1 *layer1;
349 layer1_offset = alloc_bigblock(root_vol, 0);
350 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
351 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
352 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
354 bzero(layer1, sizeof(*layer1));
355 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
356 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
360 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
361 blockmap->phys_offset = layer1_offset;
362 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
363 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
364 blockmap->reserved01 = 0;
365 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
366 root_vol->cache.modified = 1;
370 * Load the volume's remaining free space into the freemap. If this is
371 * the root volume, initialize the freemap owner for the layer1 bigblock.
373 * Returns the number of bigblocks available.
376 initialize_freemap(struct volume_info *vol)
378 struct volume_info *root_vol;
379 struct buffer_info *buffer1 = NULL;
380 struct buffer_info *buffer2 = NULL;
381 struct hammer_blockmap_layer1 *layer1;
382 struct hammer_blockmap_layer2 *layer2;
383 hammer_off_t layer1_base;
384 hammer_off_t layer1_offset;
385 hammer_off_t layer2_offset;
386 hammer_off_t phys_offset;
387 hammer_off_t aligned_vol_free_end;
391 root_vol = get_volume(RootVolNo);
392 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
393 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
395 printf("initialize freemap volume %d\n", vol->vol_no);
398 * Initialize the freemap. First preallocate the bigblocks required
399 * to implement layer2. This preallocation is a bootstrap allocation
400 * using blocks from the target volume.
402 layer1_base = root_vol->ondisk->vol0_blockmap[
403 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
404 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
405 phys_offset < aligned_vol_free_end;
406 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
407 layer1_offset = layer1_base +
408 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
409 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
410 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
411 layer1->phys_offset = alloc_bigblock(vol, 0);
412 layer1->blocks_free = 0;
413 buffer1->cache.modified = 1;
414 layer1->layer1_crc = crc32(layer1,
415 HAMMER_LAYER1_CRCSIZE);
420 * Now fill everything in.
422 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
423 phys_offset < aligned_vol_free_end;
424 phys_offset += HAMMER_LARGEBLOCK_SIZE) {
426 layer1_offset = layer1_base +
427 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
428 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
430 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
431 layer2_offset = layer1->phys_offset +
432 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
434 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
435 if (phys_offset < vol->vol_free_off) {
437 * Fixups XXX - bigblocks already allocated as part
438 * of the freemap bootstrap.
440 layer2->u.owner = HAMMER_ENCODE_FREEMAP(0, 0); /* XXX */
441 } else if (phys_offset < vol->vol_free_end) {
442 ++layer1->blocks_free;
443 buffer1->cache.modified = 1;
444 layer2->u.owner = HAMMER_BLOCKMAP_FREE;
448 layer2->u.owner = HAMMER_BLOCKMAP_UNAVAIL;
450 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
451 buffer2->cache.modified = 1;
457 layer1->layer1_crc = crc32(layer1,
458 HAMMER_LAYER1_CRCSIZE);
459 buffer1->cache.modified = 1;
464 rel_volume(root_vol);
469 * Allocate big-blocks using our poor-man's volume->vol_free_off and
470 * update the freemap if owner != 0.
473 alloc_bigblock(struct volume_info *volume, hammer_off_t owner)
475 struct buffer_info *buffer = NULL;
476 struct volume_info *root_vol;
477 hammer_off_t result_offset;
478 hammer_off_t layer_offset;
479 struct hammer_blockmap_layer1 *layer1;
480 struct hammer_blockmap_layer2 *layer2;
483 if (volume == NULL) {
484 volume = get_volume(RootVolNo);
489 result_offset = volume->vol_free_off;
490 if (result_offset >= volume->vol_free_end)
491 panic("alloc_bigblock: Ran out of room, filesystem too small");
492 volume->vol_free_off += HAMMER_LARGEBLOCK_SIZE;
498 root_vol = get_volume(RootVolNo);
499 layer_offset = root_vol->ondisk->vol0_blockmap[
500 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
501 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
502 layer1 = get_buffer_data(layer_offset, &buffer, 0);
503 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
504 --layer1->blocks_free;
505 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
506 buffer->cache.modified = 1;
507 layer_offset = layer1->phys_offset +
508 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
509 layer2 = get_buffer_data(layer_offset, &buffer, 0);
510 assert(layer2->u.owner == HAMMER_BLOCKMAP_FREE);
511 layer2->u.owner = owner;
512 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
513 buffer->cache.modified = 1;
516 rel_volume(root_vol);
521 return(result_offset);
525 * Format the undo-map for the root volume.
528 format_undomap(hammer_volume_ondisk_t ondisk)
530 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
531 hammer_off_t undo_limit;
532 hammer_blockmap_t blockmap;
534 struct hammer_blockmap_layer2 *layer2;
539 * Size the undo buffer in multiples of HAMMER_LARGEBLOCK_SIZE,
540 * up to HAMMER_UNDO_LAYER2 large blocks. Size to approximately
543 undo_limit = UndoBufferSize;
545 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
546 undo_limit = (undo_limit + HAMMER_LARGEBLOCK_MASK64) &
547 ~HAMMER_LARGEBLOCK_MASK64;
548 if (undo_limit < HAMMER_LARGEBLOCK_SIZE)
549 undo_limit = HAMMER_LARGEBLOCK_SIZE;
550 if (undo_limit > HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2)
551 undo_limit = HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2;
552 UndoBufferSize = undo_limit;
554 blockmap = &ondisk->vol0_blockmap[undo_zone];
555 bzero(blockmap, sizeof(*blockmap));
556 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
557 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
558 blockmap->next_offset = blockmap->first_offset;
559 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
560 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
562 layer2 = &ondisk->vol0_undo_array[0];
564 scan = blockmap->next_offset;
565 limit_index = undo_limit / HAMMER_LARGEBLOCK_SIZE;
567 assert(limit_index < HAMMER_UNDO_LAYER2);
569 for (n = 0; n < limit_index; ++n) {
570 layer2->u.phys_offset = alloc_bigblock(NULL, scan);
571 layer2->bytes_free = -1; /* not used */
572 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
574 scan += HAMMER_LARGEBLOCK_SIZE;
577 while (n < HAMMER_UNDO_LAYER2) {
578 layer2->u.phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
579 layer2->bytes_free = -1;
580 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
587 * Format a new blockmap. Set the owner to the base of the blockmap
588 * (meaning either the blockmap layer1 bigblock, layer2 bigblock, or
592 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_off)
594 blockmap->phys_offset = alloc_bigblock(NULL, zone_off);
595 blockmap->alloc_offset = zone_off;
596 blockmap->first_offset = zone_off;
597 blockmap->next_offset = zone_off;
598 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
603 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
604 struct buffer_info **bufferp)
606 struct buffer_info *buffer1 = NULL;
607 struct buffer_info *buffer2 = NULL;
608 struct volume_info *volume;
609 hammer_blockmap_t rootmap;
610 struct hammer_blockmap_layer1 *layer1;
611 struct hammer_blockmap_layer2 *layer2;
612 hammer_off_t layer1_offset;
613 hammer_off_t layer2_offset;
614 hammer_off_t bigblock_offset;
617 volume = get_volume(RootVolNo);
619 rootmap = &volume->ondisk->vol0_blockmap[zone];
622 * Alignment and buffer-boundary issues
624 bytes = (bytes + 7) & ~7;
625 if ((rootmap->phys_offset ^ (rootmap->phys_offset + bytes - 1)) &
627 volume->cache.modified = 1;
628 rootmap->phys_offset = (rootmap->phys_offset + bytes) &
635 layer1_offset = rootmap->phys_offset +
636 HAMMER_BLOCKMAP_LAYER1_OFFSET(rootmap->alloc_offset);
638 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
639 if ((rootmap->alloc_offset & HAMMER_BLOCKMAP_LAYER2_MASK) == 0) {
640 buffer1->cache.modified = 1;
641 bzero(layer1, sizeof(*layer1));
642 layer1->blocks_free = HAMMER_BLOCKMAP_RADIX2;
643 layer1->phys_offset = alloc_bigblock(NULL,
644 rootmap->alloc_offset);
650 layer2_offset = layer1->phys_offset +
651 HAMMER_BLOCKMAP_LAYER2_OFFSET(rootmap->alloc_offset);
653 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
655 if ((rootmap->alloc_offset & HAMMER_LARGEBLOCK_MASK64) == 0) {
656 buffer2->cache.modified = 1;
657 bzero(layer2, sizeof(*layer2));
658 layer2->u.phys_offset = alloc_bigblock(NULL,
659 rootmap->alloc_offset);
660 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
661 --layer1->blocks_free;
664 buffer1->cache.modified = 1;
665 buffer2->cache.modified = 1;
666 volume->cache.modified = 1;
667 layer2->bytes_free -= bytes;
668 *result_offp = rootmap->alloc_offset;
669 rootmap->alloc_offset += bytes;
670 rootmap->next_offset = rootmap->alloc_offset;
672 bigblock_offset = layer2->u.phys_offset +
673 (*result_offp & HAMMER_LARGEBLOCK_MASK);
675 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
676 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
678 ptr = get_buffer_data(bigblock_offset, bufferp, 0);
679 (*bufferp)->cache.modified = 1;
692 * Reserve space from the FIFO. Make sure that bytes does not cross a
695 * Zero out base_bytes and initialize the fifo head and tail. The
696 * data area is not zerod.
700 hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
701 struct buffer_info **bufp, u_int16_t hdr_type)
703 struct buffer_info *buf;
704 struct volume_info *volume;
705 hammer_fifo_head_t head;
706 hammer_fifo_tail_t tail;
708 int32_t aligned_bytes;
710 aligned_bytes = (base_bytes + ext_bytes + HAMMER_TAIL_ONDISK_SIZE +
711 HAMMER_HEAD_ALIGN_MASK) & ~HAMMER_HEAD_ALIGN_MASK;
713 volume = get_volume(RootVolNo);
714 off = volume->ondisk->vol0_fifo_end;
717 * For now don't deal with transitions across buffer boundaries,
718 * only newfs_hammer uses this function.
720 assert((off & ~HAMMER_BUFMASK64) ==
721 ((off + aligned_bytes) & ~HAMMER_BUFMASK));
723 *bufp = buf = get_buffer(off, 0);
725 buf->cache.modified = 1;
726 volume->cache.modified = 1;
728 head = (void *)((char *)buf->ondisk + ((int32_t)off & HAMMER_BUFMASK));
729 bzero(head, base_bytes);
731 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
732 head->hdr_type = hdr_type;
733 head->hdr_size = aligned_bytes;
734 head->hdr_seq = volume->ondisk->vol0_next_seq++;
736 tail = (void*)((char *)head + aligned_bytes - HAMMER_TAIL_ONDISK_SIZE);
737 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
738 tail->tail_type = hdr_type;
739 tail->tail_size = aligned_bytes;
741 volume->ondisk->vol0_fifo_end += aligned_bytes;
742 volume->cache.modified = 1;
752 * Flush various tracking structures to disk
756 * Flush various tracking structures to disk
759 flush_all_volumes(void)
761 struct volume_info *vol;
763 TAILQ_FOREACH(vol, &VolList, entry)
768 flush_volume(struct volume_info *volume)
770 struct buffer_info *buffer;
772 TAILQ_FOREACH(buffer, &volume->buffer_list, entry)
773 flush_buffer(buffer);
774 writehammerbuf(volume, volume->ondisk, 0);
775 volume->cache.modified = 0;
779 flush_buffer(struct buffer_info *buffer)
781 writehammerbuf(buffer->volume, buffer->ondisk, buffer->buf_disk_offset);
782 buffer->cache.modified = 0;
787 * Generic buffer initialization
790 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
792 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
793 head->hdr_type = hdr_type;
803 * Core I/O operations
806 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
810 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
811 if (n != HAMMER_BUFSIZE)
812 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
818 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
822 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
823 if (n != HAMMER_BUFSIZE)
824 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
828 panic(const char *ctl, ...)
833 vfprintf(stderr, ctl, va);
835 fprintf(stderr, "\n");