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.19 2008/05/12 21:17:16 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,
69 int64_t UndoBufferSize;
70 int UsingSuperClusters;
73 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
77 buffer_hash(hammer_off_t buf_offset)
81 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
86 * Lookup the requested information structure and related on-disk buffer.
87 * Missing structures are created.
90 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
92 struct volume_info *vol;
93 struct volume_info *scan;
94 struct hammer_volume_ondisk *ondisk;
98 * Allocate the volume structure
100 vol = malloc(sizeof(*vol));
101 bzero(vol, sizeof(*vol));
102 for (i = 0; i < HAMMER_BUFLISTS; ++i)
103 TAILQ_INIT(&vol->buffer_lists[i]);
104 vol->name = strdup(filename);
105 vol->fd = open(filename, oflags);
109 err(1, "setup_volume: %s: Open failed", filename);
113 * Read or initialize the volume header
115 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
117 bzero(ondisk, HAMMER_BUFSIZE);
119 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
120 if (n != HAMMER_BUFSIZE) {
121 err(1, "setup_volume: %s: Read failed at offset 0",
124 vol_no = ondisk->vol_no;
126 RootVolNo = ondisk->vol_rootvol;
127 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
128 errx(1, "setup_volume: %s: root volume disagreement: "
130 vol->name, RootVolNo, ondisk->vol_rootvol);
133 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
134 errx(1, "setup_volume: %s: Header does not indicate "
135 "that this is a hammer volume", vol->name);
137 if (TAILQ_EMPTY(&VolList)) {
138 Hammer_FSId = vol->ondisk->vol_fsid;
139 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
140 errx(1, "setup_volume: %s: FSId does match other "
141 "volumes!", vol->name);
144 vol->vol_no = vol_no;
147 /*init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);*/
148 vol->cache.modified = 1;
152 * Link the volume structure in
154 TAILQ_FOREACH(scan, &VolList, entry) {
155 if (scan->vol_no == vol_no) {
156 errx(1, "setup_volume %s: Duplicate volume number %d "
157 "against %s", filename, vol_no, scan->name);
160 TAILQ_INSERT_TAIL(&VolList, vol, entry);
165 get_volume(int32_t vol_no)
167 struct volume_info *vol;
169 TAILQ_FOREACH(vol, &VolList, entry) {
170 if (vol->vol_no == vol_no)
174 errx(1, "get_volume: Volume %d does not exist!", vol_no);
176 /* not added to or removed from hammer cache */
181 rel_volume(struct volume_info *volume)
183 /* not added to or removed from hammer cache */
184 --volume->cache.refs;
188 * Acquire the specified buffer.
191 get_buffer(hammer_off_t buf_offset, int isnew)
194 struct buffer_info *buf;
195 struct volume_info *volume;
196 hammer_off_t orig_offset = buf_offset;
201 zone = HAMMER_ZONE_DECODE(buf_offset);
202 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
203 buf_offset = blockmap_lookup(buf_offset, NULL, NULL);
205 assert((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
206 vol_no = HAMMER_VOL_DECODE(buf_offset);
207 volume = get_volume(vol_no);
208 buf_offset &= ~HAMMER_BUFMASK64;
210 hi = buffer_hash(buf_offset);
212 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry) {
213 if (buf->buf_offset == buf_offset)
217 buf = malloc(sizeof(*buf));
218 bzero(buf, sizeof(*buf));
220 fprintf(stderr, "get_buffer %016llx %016llx\n",
221 orig_offset, buf_offset);
223 buf->buf_offset = buf_offset;
224 buf->buf_disk_offset = volume->ondisk->vol_buf_beg +
225 (buf_offset & HAMMER_OFF_SHORT_MASK);
226 buf->volume = volume;
227 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
228 ++volume->cache.refs;
229 buf->cache.u.buffer = buf;
230 hammer_cache_add(&buf->cache, ISBUFFER);
233 hammer_cache_flush();
234 if ((ondisk = buf->ondisk) == NULL) {
235 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
237 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
238 buf->buf_disk_offset);
239 if (n != HAMMER_BUFSIZE) {
240 err(1, "get_buffer: %s:%016llx Read failed at "
242 volume->name, buf->buf_offset,
243 buf->buf_disk_offset);
248 bzero(ondisk, HAMMER_BUFSIZE);
249 buf->cache.modified = 1;
255 rel_buffer(struct buffer_info *buffer)
257 struct volume_info *volume;
260 assert(buffer->cache.refs > 0);
261 if (--buffer->cache.refs == 0) {
262 if (buffer->cache.delete) {
263 hi = buffer_hash(buffer->buf_offset);
264 volume = buffer->volume;
265 if (buffer->cache.modified)
266 flush_buffer(buffer);
267 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
268 hammer_cache_del(&buffer->cache);
269 free(buffer->ondisk);
277 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
280 struct buffer_info *buffer;
282 if ((buffer = *bufferp) != NULL) {
284 ((buffer->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
286 buffer = *bufferp = NULL;
290 buffer = *bufferp = get_buffer(buf_offset, isnew);
291 return((char *)buffer->ondisk + ((int32_t)buf_offset & HAMMER_BUFMASK));
295 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
296 * bufp is freed if non-NULL and a referenced buffer is loaded into it.
299 get_node(hammer_off_t node_offset, struct buffer_info **bufp)
301 struct buffer_info *buf;
305 *bufp = buf = get_buffer(node_offset, 0);
306 return((void *)((char *)buf->ondisk +
307 (int32_t)(node_offset & HAMMER_BUFMASK)));
311 * Allocate HAMMER elements - btree nodes, data storage, and record elements
313 * NOTE: hammer_alloc_fifo() initializes the fifo header for the returned
314 * item and zero's out the remainder, so don't bzero() it.
317 alloc_btree_element(hammer_off_t *offp)
319 struct buffer_info *buffer = NULL;
320 hammer_node_ondisk_t node;
322 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
324 bzero(node, sizeof(*node));
325 /* XXX buffer not released, pointer remains valid */
330 alloc_data_element(hammer_off_t *offp, int32_t data_len,
331 struct buffer_info **data_bufferp)
335 if (data_len >= HAMMER_BUFSIZE) {
336 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
337 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
339 bzero(data, data_len);
340 } else if (data_len) {
341 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
343 bzero(data, data_len);
351 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
352 * code will load each volume's freemap.
355 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
357 struct buffer_info *buffer = NULL;
358 hammer_off_t layer1_offset;
359 struct hammer_blockmap_layer1 *layer1;
362 layer1_offset = alloc_bigblock(root_vol, 0);
363 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
364 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
365 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
367 bzero(layer1, sizeof(*layer1));
368 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
369 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
373 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
374 blockmap->phys_offset = layer1_offset;
375 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
376 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
377 blockmap->reserved01 = 0;
378 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
379 root_vol->cache.modified = 1;
383 * Load the volume's remaining free space into the freemap. If this is
384 * the root volume, initialize the freemap owner for the layer1 bigblock.
386 * Returns the number of bigblocks available.
389 initialize_freemap(struct volume_info *vol)
391 struct volume_info *root_vol;
392 struct buffer_info *buffer1 = NULL;
393 struct buffer_info *buffer2 = NULL;
394 struct hammer_blockmap_layer1 *layer1;
395 struct hammer_blockmap_layer2 *layer2;
396 hammer_off_t layer1_base;
397 hammer_off_t layer1_offset;
398 hammer_off_t layer2_offset;
399 hammer_off_t phys_offset;
400 hammer_off_t aligned_vol_free_end;
404 root_vol = get_volume(RootVolNo);
405 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
406 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
408 printf("initialize freemap volume %d\n", vol->vol_no);
411 * Initialize the freemap. First preallocate the bigblocks required
412 * to implement layer2. This preallocation is a bootstrap allocation
413 * using blocks from the target volume.
415 layer1_base = root_vol->ondisk->vol0_blockmap[
416 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
417 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
418 phys_offset < aligned_vol_free_end;
419 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
420 layer1_offset = layer1_base +
421 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
422 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
423 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
424 layer1->phys_offset = alloc_bigblock(vol, 0);
425 layer1->blocks_free = 0;
426 buffer1->cache.modified = 1;
427 layer1->layer1_crc = crc32(layer1,
428 HAMMER_LAYER1_CRCSIZE);
433 * Now fill everything in.
435 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
436 phys_offset < aligned_vol_free_end;
437 phys_offset += HAMMER_LARGEBLOCK_SIZE) {
439 layer1_offset = layer1_base +
440 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
441 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
443 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
444 layer2_offset = layer1->phys_offset +
445 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
447 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
448 if (phys_offset < vol->vol_free_off) {
450 * Fixups XXX - bigblocks already allocated as part
451 * of the freemap bootstrap.
453 layer2->u.owner = HAMMER_ENCODE_FREEMAP(0, 0); /* XXX */
454 } else if (phys_offset < vol->vol_free_end) {
455 ++layer1->blocks_free;
456 buffer1->cache.modified = 1;
457 layer2->u.owner = HAMMER_BLOCKMAP_FREE;
461 layer2->u.owner = HAMMER_BLOCKMAP_UNAVAIL;
463 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
464 buffer2->cache.modified = 1;
470 layer1->layer1_crc = crc32(layer1,
471 HAMMER_LAYER1_CRCSIZE);
472 buffer1->cache.modified = 1;
477 rel_volume(root_vol);
482 * Allocate big-blocks using our poor-man's volume->vol_free_off and
483 * update the freemap if owner != 0.
486 alloc_bigblock(struct volume_info *volume, hammer_off_t owner)
488 struct buffer_info *buffer = NULL;
489 struct volume_info *root_vol;
490 hammer_off_t result_offset;
491 hammer_off_t layer_offset;
492 struct hammer_blockmap_layer1 *layer1;
493 struct hammer_blockmap_layer2 *layer2;
496 if (volume == NULL) {
497 volume = get_volume(RootVolNo);
502 result_offset = volume->vol_free_off;
503 if (result_offset >= volume->vol_free_end)
504 panic("alloc_bigblock: Ran out of room, filesystem too small");
505 volume->vol_free_off += HAMMER_LARGEBLOCK_SIZE;
511 root_vol = get_volume(RootVolNo);
512 layer_offset = root_vol->ondisk->vol0_blockmap[
513 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
514 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
515 layer1 = get_buffer_data(layer_offset, &buffer, 0);
516 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
517 --layer1->blocks_free;
518 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
519 buffer->cache.modified = 1;
520 layer_offset = layer1->phys_offset +
521 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
522 layer2 = get_buffer_data(layer_offset, &buffer, 0);
523 assert(layer2->u.owner == HAMMER_BLOCKMAP_FREE);
524 layer2->u.owner = owner;
525 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
526 buffer->cache.modified = 1;
529 rel_volume(root_vol);
534 return(result_offset);
538 * Format the undo-map for the root volume.
541 format_undomap(hammer_volume_ondisk_t ondisk)
543 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
544 hammer_off_t undo_limit;
545 hammer_blockmap_t blockmap;
547 struct hammer_blockmap_layer2 *layer2;
552 * Size the undo buffer in multiples of HAMMER_LARGEBLOCK_SIZE,
553 * up to HAMMER_UNDO_LAYER2 large blocks. Size to approximately
556 undo_limit = UndoBufferSize;
558 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
559 undo_limit = (undo_limit + HAMMER_LARGEBLOCK_MASK64) &
560 ~HAMMER_LARGEBLOCK_MASK64;
561 if (undo_limit < HAMMER_LARGEBLOCK_SIZE)
562 undo_limit = HAMMER_LARGEBLOCK_SIZE;
563 if (undo_limit > HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2)
564 undo_limit = HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2;
565 UndoBufferSize = undo_limit;
567 blockmap = &ondisk->vol0_blockmap[undo_zone];
568 bzero(blockmap, sizeof(*blockmap));
569 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
570 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
571 blockmap->next_offset = blockmap->first_offset;
572 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
573 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
575 layer2 = &ondisk->vol0_undo_array[0];
577 scan = blockmap->next_offset;
578 limit_index = undo_limit / HAMMER_LARGEBLOCK_SIZE;
580 assert(limit_index <= HAMMER_UNDO_LAYER2);
582 for (n = 0; n < limit_index; ++n) {
583 layer2->u.phys_offset = alloc_bigblock(NULL, scan);
584 layer2->bytes_free = -1; /* not used */
585 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
587 scan += HAMMER_LARGEBLOCK_SIZE;
590 while (n < HAMMER_UNDO_LAYER2) {
591 layer2->u.phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
592 layer2->bytes_free = -1;
593 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
600 * Format a new blockmap. Set the owner to the base of the blockmap
601 * (meaning either the blockmap layer1 bigblock, layer2 bigblock, or
605 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_off)
607 blockmap->phys_offset = alloc_bigblock(NULL, zone_off);
608 blockmap->alloc_offset = zone_off;
609 blockmap->first_offset = zone_off;
610 blockmap->next_offset = zone_off;
611 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
616 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
617 struct buffer_info **bufferp)
619 struct buffer_info *buffer1 = NULL;
620 struct buffer_info *buffer2 = NULL;
621 struct volume_info *volume;
622 hammer_blockmap_t rootmap;
623 struct hammer_blockmap_layer1 *layer1;
624 struct hammer_blockmap_layer2 *layer2;
625 hammer_off_t layer1_offset;
626 hammer_off_t layer2_offset;
627 hammer_off_t bigblock_offset;
630 volume = get_volume(RootVolNo);
632 rootmap = &volume->ondisk->vol0_blockmap[zone];
635 * Alignment and buffer-boundary issues
637 bytes = (bytes + 7) & ~7;
638 if ((rootmap->phys_offset ^ (rootmap->phys_offset + bytes - 1)) &
640 volume->cache.modified = 1;
641 rootmap->phys_offset = (rootmap->phys_offset + bytes) &
648 layer1_offset = rootmap->phys_offset +
649 HAMMER_BLOCKMAP_LAYER1_OFFSET(rootmap->alloc_offset);
651 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
652 if ((rootmap->alloc_offset & HAMMER_BLOCKMAP_LAYER2_MASK) == 0) {
653 buffer1->cache.modified = 1;
654 bzero(layer1, sizeof(*layer1));
655 layer1->blocks_free = HAMMER_BLOCKMAP_RADIX2;
656 layer1->phys_offset = alloc_bigblock(NULL,
657 rootmap->alloc_offset);
663 layer2_offset = layer1->phys_offset +
664 HAMMER_BLOCKMAP_LAYER2_OFFSET(rootmap->alloc_offset);
666 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
668 if ((rootmap->alloc_offset & HAMMER_LARGEBLOCK_MASK64) == 0) {
669 buffer2->cache.modified = 1;
670 bzero(layer2, sizeof(*layer2));
671 layer2->u.phys_offset = alloc_bigblock(NULL,
672 rootmap->alloc_offset);
673 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
674 --layer1->blocks_free;
677 buffer1->cache.modified = 1;
678 buffer2->cache.modified = 1;
679 volume->cache.modified = 1;
680 layer2->bytes_free -= bytes;
681 *result_offp = rootmap->alloc_offset;
682 rootmap->alloc_offset += bytes;
683 rootmap->next_offset = rootmap->alloc_offset;
685 bigblock_offset = layer2->u.phys_offset +
686 (*result_offp & HAMMER_LARGEBLOCK_MASK);
688 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
689 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
691 ptr = get_buffer_data(bigblock_offset, bufferp, 0);
692 (*bufferp)->cache.modified = 1;
705 * Reserve space from the FIFO. Make sure that bytes does not cross a
708 * Zero out base_bytes and initialize the fifo head and tail. The
709 * data area is not zerod.
713 hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
714 struct buffer_info **bufp, u_int16_t hdr_type)
716 struct buffer_info *buf;
717 struct volume_info *volume;
718 hammer_fifo_head_t head;
719 hammer_fifo_tail_t tail;
721 int32_t aligned_bytes;
723 aligned_bytes = (base_bytes + ext_bytes + HAMMER_TAIL_ONDISK_SIZE +
724 HAMMER_HEAD_ALIGN_MASK) & ~HAMMER_HEAD_ALIGN_MASK;
726 volume = get_volume(RootVolNo);
727 off = volume->ondisk->vol0_fifo_end;
730 * For now don't deal with transitions across buffer boundaries,
731 * only newfs_hammer uses this function.
733 assert((off & ~HAMMER_BUFMASK64) ==
734 ((off + aligned_bytes) & ~HAMMER_BUFMASK));
736 *bufp = buf = get_buffer(off, 0);
738 buf->cache.modified = 1;
739 volume->cache.modified = 1;
741 head = (void *)((char *)buf->ondisk + ((int32_t)off & HAMMER_BUFMASK));
742 bzero(head, base_bytes);
744 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
745 head->hdr_type = hdr_type;
746 head->hdr_size = aligned_bytes;
747 head->hdr_seq = volume->ondisk->vol0_next_seq++;
749 tail = (void*)((char *)head + aligned_bytes - HAMMER_TAIL_ONDISK_SIZE);
750 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
751 tail->tail_type = hdr_type;
752 tail->tail_size = aligned_bytes;
754 volume->ondisk->vol0_fifo_end += aligned_bytes;
755 volume->cache.modified = 1;
765 * Flush various tracking structures to disk
769 * Flush various tracking structures to disk
772 flush_all_volumes(void)
774 struct volume_info *vol;
776 TAILQ_FOREACH(vol, &VolList, entry)
781 flush_volume(struct volume_info *volume)
783 struct buffer_info *buffer;
786 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
787 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
788 flush_buffer(buffer);
790 writehammerbuf(volume, volume->ondisk, 0);
791 volume->cache.modified = 0;
795 flush_buffer(struct buffer_info *buffer)
797 writehammerbuf(buffer->volume, buffer->ondisk, buffer->buf_disk_offset);
798 buffer->cache.modified = 0;
803 * Generic buffer initialization
806 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
808 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
809 head->hdr_type = hdr_type;
819 * Core I/O operations
822 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
826 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
827 if (n != HAMMER_BUFSIZE)
828 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
834 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
838 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
839 if (n != HAMMER_BUFSIZE)
840 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
844 panic(const char *ctl, ...)
849 vfprintf(stderr, ctl, va);
851 fprintf(stderr, "\n");