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
35 #include <sys/diskslice.h>
36 #include <sys/diskmbr.h>
38 #include "hammer_util.h"
40 static void get_buffer_readahead(struct buffer_info *base);
41 static __inline void *get_ondisk(hammer_off_t buf_offset,
42 struct buffer_info **bufferp, int isnew);
43 static int readhammerbuf(struct volume_info *vol, void *data, int64_t offset);
44 static int writehammerbuf(struct volume_info *vol, const void *data,
53 int64_t UndoBufferSize;
55 int UseReadBehind = -4;
57 int AssertOnFailure = 1;
58 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
62 buffer_hash(hammer_off_t buf_offset)
66 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
70 static struct buffer_info*
71 find_buffer(struct volume_info *volume, hammer_off_t buf_offset)
74 struct buffer_info *buf;
76 hi = buffer_hash(buf_offset);
77 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
78 if (buf->buf_offset == buf_offset)
84 * Lookup the requested information structure and related on-disk buffer.
85 * Missing structures are created.
88 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
90 struct volume_info *vol;
91 struct volume_info *scan;
92 struct hammer_volume_ondisk *ondisk;
97 * Allocate the volume structure
99 vol = malloc(sizeof(*vol));
100 bzero(vol, sizeof(*vol));
101 for (i = 0; i < HAMMER_BUFLISTS; ++i)
102 TAILQ_INIT(&vol->buffer_lists[i]);
103 vol->name = strdup(filename);
104 vol->fd = open(vol->name, oflags);
106 err(1, "setup_volume: %s: Open failed", vol->name);
110 * Read or initialize the volume header
112 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
114 bzero(ondisk, HAMMER_BUFSIZE);
116 n = readhammerbuf(vol, ondisk, 0);
118 err(1, "setup_volume: %s: Read failed at offset 0",
121 vol_no = ondisk->vol_no;
123 RootVolNo = ondisk->vol_rootvol;
124 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
125 errx(1, "setup_volume: %s: root volume disagreement: "
127 vol->name, RootVolNo, ondisk->vol_rootvol);
130 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
131 errx(1, "setup_volume: %s: Header does not indicate "
132 "that this is a hammer volume", vol->name);
134 if (TAILQ_EMPTY(&VolList)) {
135 Hammer_FSId = vol->ondisk->vol_fsid;
136 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
137 errx(1, "setup_volume: %s: FSId does match other "
138 "volumes!", vol->name);
141 vol->vol_no = vol_no;
144 vol->cache.modified = 1;
147 if (fstat(vol->fd, &st1) != 0){
148 errx(1, "setup_volume: %s: Failed to stat", vol->name);
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", vol->name, vol_no, scan->name);
159 if (fstat(scan->fd, &st2) != 0){
160 errx(1, "setup_volume: %s: Failed to stat %s",
161 vol->name, scan->name);
163 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
164 errx(1, "setup_volume: %s: Specified more than once",
168 TAILQ_INSERT_TAIL(&VolList, vol, entry);
173 * Check basic volume characteristics.
176 check_volume(struct volume_info *vol)
178 struct partinfo pinfo;
182 * Get basic information about the volume
184 if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
186 * Allow the formatting of regular files as HAMMER volumes
188 if (fstat(vol->fd, &st) < 0)
189 err(1, "Unable to stat %s", vol->name);
190 vol->size = st.st_size;
191 vol->type = "REGFILE";
194 * When formatting a block device as a HAMMER volume the
195 * sector size must be compatible. HAMMER uses 16384 byte
196 * filesystem buffers.
198 if (pinfo.reserved_blocks) {
199 errx(1, "HAMMER cannot be placed in a partition "
200 "which overlaps the disklabel or MBR");
202 if (pinfo.media_blksize > HAMMER_BUFSIZE ||
203 HAMMER_BUFSIZE % pinfo.media_blksize) {
204 errx(1, "A media sector size of %d is not supported",
205 pinfo.media_blksize);
208 vol->size = pinfo.media_size;
209 vol->device_offset = pinfo.media_offset;
210 vol->type = "DEVICE";
214 * Reserve space for (future) header junk, setup our poor-man's
215 * big-block allocator.
217 vol->vol_alloc = HAMMER_BUFSIZE * 16;
221 get_volume(int32_t vol_no)
223 struct volume_info *vol;
225 TAILQ_FOREACH(vol, &VolList, entry) {
226 if (vol->vol_no == vol_no)
231 errx(1, "get_volume: Volume %d does not exist!",
236 /* not added to or removed from hammer cache */
241 rel_volume(struct volume_info *volume)
245 /* not added to or removed from hammer cache */
246 --volume->cache.refs;
250 * Acquire the specified buffer. isnew is -1 only when called
251 * via get_buffer_readahead() to prevent another readahead.
254 get_buffer(hammer_off_t buf_offset, int isnew)
257 struct buffer_info *buf;
258 struct volume_info *volume;
259 hammer_off_t orig_offset = buf_offset;
265 zone = HAMMER_ZONE_DECODE(buf_offset);
266 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
267 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
269 if (buf_offset == HAMMER_OFF_BAD)
272 if (AssertOnFailure) {
273 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
274 HAMMER_ZONE_RAW_BUFFER);
276 vol_no = HAMMER_VOL_DECODE(buf_offset);
277 volume = get_volume(vol_no);
281 buf_offset &= ~HAMMER_BUFMASK64;
282 buf = find_buffer(volume, buf_offset);
285 buf = malloc(sizeof(*buf));
286 bzero(buf, sizeof(*buf));
288 fprintf(stderr, "get_buffer: %016llx %016llx at %p\n",
289 (long long)orig_offset, (long long)buf_offset,
292 buf->buf_offset = buf_offset;
293 buf->raw_offset = hammer_xlate_to_phys(volume->ondisk,
295 buf->volume = volume;
296 hi = buffer_hash(buf_offset);
297 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
298 ++volume->cache.refs;
299 buf->cache.u.buffer = buf;
300 hammer_cache_add(&buf->cache, ISBUFFER);
304 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
305 (long long)orig_offset, (long long)buf_offset,
308 hammer_cache_used(&buf->cache);
312 hammer_cache_flush();
313 if ((ondisk = buf->ondisk) == NULL) {
314 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
316 n = readhammerbuf(volume, ondisk, buf->raw_offset);
319 err(1, "get_buffer: %s:%016llx "
320 "Read failed at offset %016llx",
322 (long long)buf->buf_offset,
323 (long long)buf->raw_offset);
324 bzero(ondisk, HAMMER_BUFSIZE);
329 bzero(ondisk, HAMMER_BUFSIZE);
330 buf->cache.modified = 1;
333 get_buffer_readahead(buf);
338 get_buffer_readahead(struct buffer_info *base)
340 struct buffer_info *buf;
341 struct volume_info *vol;
342 hammer_off_t buf_offset;
344 int ri = UseReadBehind;
345 int re = UseReadAhead;
347 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
351 if (raw_offset >= vol->ondisk->vol_buf_end)
353 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
355 raw_offset += HAMMER_BUFSIZE;
358 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
359 raw_offset - vol->ondisk->vol_buf_beg);
360 buf = find_buffer(vol, buf_offset);
362 buf = get_buffer(buf_offset, -1);
366 raw_offset += HAMMER_BUFSIZE;
371 rel_buffer(struct buffer_info *buffer)
373 struct volume_info *volume;
378 assert(buffer->cache.refs > 0);
379 if (--buffer->cache.refs == 0) {
380 if (buffer->cache.delete) {
381 hi = buffer_hash(buffer->buf_offset);
382 volume = buffer->volume;
383 if (buffer->cache.modified)
384 flush_buffer(buffer);
385 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
386 hammer_cache_del(&buffer->cache);
387 free(buffer->ondisk);
395 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
396 * bufferp is freed if isnew or the offset is out of range of the cached data.
397 * If bufferp is freed a referenced buffer is loaded into it.
400 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
403 if (*bufferp != NULL) {
405 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
406 rel_buffer(*bufferp);
410 return(get_ondisk(buf_offset, bufferp, isnew));
414 * Retrieve a pointer to a B-Tree node given a zone offset. The underlying
415 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
418 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
420 if (*bufferp != NULL) {
421 rel_buffer(*bufferp);
424 return(get_ondisk(node_offset, bufferp, 0));
428 * Return a pointer to a buffer data given a buffer offset.
429 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
433 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew)
435 struct buffer_info *buffer;
438 if (buffer == NULL) {
439 buffer = *bufferp = get_buffer(buf_offset, isnew);
444 return((char *)buffer->ondisk +
445 ((int32_t)buf_offset & HAMMER_BUFMASK));
449 * Allocate HAMMER elements - btree nodes, meta data, data storage
452 alloc_btree_element(hammer_off_t *offp, struct buffer_info **data_bufferp)
454 hammer_node_ondisk_t node;
456 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
458 bzero(node, sizeof(*node));
463 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
464 struct buffer_info **data_bufferp)
468 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
470 bzero(data, data_len);
475 * The only data_len supported by HAMMER userspace for large data zone
476 * (zone 10) is HAMMER_BUFSIZE which is 16KB. >16KB data does not fit
477 * in a buffer allocated by get_buffer(). Also alloc_blockmap() does
478 * not consider >16KB buffer size.
481 alloc_data_element(hammer_off_t *offp, int32_t data_len,
482 struct buffer_info **data_bufferp)
490 zone = hammer_data_zone_index(data_len);
491 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
492 assert(zone == HAMMER_ZONE_LARGE_DATA_INDEX ||
493 zone == HAMMER_ZONE_SMALL_DATA_INDEX);
495 data = alloc_blockmap(zone, data_len, offp, data_bufferp);
496 bzero(data, data_len);
501 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
502 * code will load each volume's freemap.
505 format_freemap(struct volume_info *root_vol)
507 struct buffer_info *buffer = NULL;
508 hammer_off_t layer1_offset;
509 hammer_blockmap_t blockmap;
510 struct hammer_blockmap_layer1 *layer1;
513 /* Only root volume needs formatting */
514 assert(root_vol->vol_no == RootVolNo);
516 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
517 for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
518 isnew = ((i % HAMMER_BUFSIZE) == 0);
519 layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
520 bzero(layer1, sizeof(*layer1));
521 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
522 layer1->blocks_free = 0;
523 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
525 assert(i == HAMMER_BIGBLOCK_SIZE);
528 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
529 bzero(blockmap, sizeof(*blockmap));
530 blockmap->phys_offset = layer1_offset;
531 blockmap->first_offset = 0;
532 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
533 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
534 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
535 root_vol->cache.modified = 1;
539 * Load the volume's remaining free space into the freemap.
541 * Returns the number of big-blocks available.
544 initialize_freemap(struct volume_info *vol)
546 struct volume_info *root_vol;
547 struct buffer_info *buffer1 = NULL;
548 struct buffer_info *buffer2 = NULL;
549 struct hammer_blockmap_layer1 *layer1;
550 struct hammer_blockmap_layer2 *layer2;
551 hammer_off_t layer1_base;
552 hammer_off_t layer1_offset;
553 hammer_off_t layer2_offset;
554 hammer_off_t phys_offset;
555 hammer_off_t block_offset;
556 hammer_off_t aligned_vol_free_end;
557 hammer_blockmap_t freemap;
559 int64_t layer1_count = 0;
561 root_vol = get_volume(RootVolNo);
562 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
563 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
565 printf("initialize freemap volume %d\n", vol->vol_no);
568 * Initialize the freemap. First preallocate the big-blocks required
569 * to implement layer2. This preallocation is a bootstrap allocation
570 * using blocks from the target volume.
572 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
573 layer1_base = freemap->phys_offset;
575 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
576 phys_offset < aligned_vol_free_end;
577 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
578 layer1_offset = layer1_base +
579 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
580 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
581 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
582 layer1->phys_offset = alloc_bigblock(vol,
583 HAMMER_ZONE_FREEMAP_INDEX);
584 layer1->blocks_free = 0;
585 buffer1->cache.modified = 1;
586 layer1->layer1_crc = crc32(layer1,
587 HAMMER_LAYER1_CRCSIZE);
592 * Now fill everything in.
594 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
595 phys_offset < aligned_vol_free_end;
596 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
598 layer1_offset = layer1_base +
599 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
600 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
601 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
603 for (block_offset = 0;
604 block_offset < HAMMER_BLOCKMAP_LAYER2;
605 block_offset += HAMMER_BIGBLOCK_SIZE) {
606 layer2_offset = layer1->phys_offset +
607 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
608 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
609 bzero(layer2, sizeof(*layer2));
611 if (phys_offset + block_offset < vol->vol_free_off) {
613 * Fixups XXX - big-blocks already allocated as part
614 * of the freemap bootstrap.
616 if (layer2->zone == 0) {
617 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
618 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
619 layer2->bytes_free = 0;
621 } else if (phys_offset + block_offset < vol->vol_free_end) {
623 layer2->append_off = 0;
624 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
628 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
629 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
630 layer2->bytes_free = 0;
632 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
633 buffer2->cache.modified = 1;
636 layer1->blocks_free += layer1_count;
637 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
638 buffer1->cache.modified = 1;
643 rel_volume(root_vol);
648 * Returns the number of big-blocks available for filesystem data and undos
649 * without formatting.
652 count_freemap(struct volume_info *vol)
654 hammer_off_t phys_offset;
655 hammer_off_t vol_free_off;
656 hammer_off_t aligned_vol_free_end;
659 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
660 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
661 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
663 if (vol->vol_no == RootVolNo)
664 vol_free_off += HAMMER_BIGBLOCK_SIZE;
666 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
667 phys_offset < aligned_vol_free_end;
668 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
669 vol_free_off += HAMMER_BIGBLOCK_SIZE;
672 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
673 phys_offset < aligned_vol_free_end;
674 phys_offset += HAMMER_BIGBLOCK_SIZE) {
675 if (phys_offset < vol_free_off) {
677 } else if (phys_offset < vol->vol_free_end) {
686 * Format the undomap for the root volume.
689 format_undomap(struct volume_info *root_vol)
691 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
692 hammer_off_t undo_limit;
693 hammer_blockmap_t blockmap;
694 struct hammer_volume_ondisk *ondisk;
695 struct buffer_info *buffer = NULL;
701 /* Only root volume needs formatting */
702 assert(root_vol->vol_no == RootVolNo);
703 ondisk = root_vol->ondisk;
706 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
707 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
710 * The minimum UNDO fifo size is 500MB, or approximately 1% of
711 * the recommended 50G disk.
713 * Changing this minimum is rather dangerous as complex filesystem
714 * operations can cause the UNDO FIFO to fill up otherwise.
716 undo_limit = UndoBufferSize;
717 if (undo_limit == 0) {
718 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
719 if (undo_limit < 500*1024*1024)
720 undo_limit = 500*1024*1024;
722 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
723 ~HAMMER_BIGBLOCK_MASK64;
724 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
725 undo_limit = HAMMER_BIGBLOCK_SIZE;
726 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
727 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
728 UndoBufferSize = undo_limit;
730 blockmap = &ondisk->vol0_blockmap[undo_zone];
731 bzero(blockmap, sizeof(*blockmap));
732 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
733 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
734 blockmap->next_offset = blockmap->first_offset;
735 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
736 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
738 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
739 assert(limit_index <= HAMMER_UNDO_LAYER2);
741 for (n = 0; n < limit_index; ++n) {
742 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
743 HAMMER_ZONE_UNDO_INDEX);
745 while (n < HAMMER_UNDO_LAYER2) {
746 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
750 * Pre-initialize the UNDO blocks (HAMMER version 4+)
752 printf("initializing the undo map (%jd MB)\n",
753 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
756 scan = blockmap->first_offset;
759 while (scan < blockmap->alloc_offset) {
760 hammer_fifo_head_t head;
761 hammer_fifo_tail_t tail;
763 int bytes = HAMMER_UNDO_ALIGN;
765 isnew = ((scan & HAMMER_BUFMASK64) == 0);
766 head = get_buffer_data(scan, &buffer, isnew);
767 buffer->cache.modified = 1;
768 tail = (void *)((char *)head + bytes - sizeof(*tail));
771 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
772 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
773 head->hdr_size = bytes;
774 head->hdr_seq = seqno++;
776 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
777 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
778 tail->tail_size = bytes;
780 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
781 crc32(head + 1, bytes - sizeof(*head));
789 * Format a new blockmap. This is mostly a degenerate case because
790 * all allocations are now actually done from the freemap.
793 format_blockmap(hammer_blockmap_t blockmap, int zone, hammer_off_t offset)
795 hammer_off_t zone_base = HAMMER_ZONE_ENCODE(zone, offset);
797 bzero(blockmap, sizeof(*blockmap));
798 blockmap->phys_offset = 0;
799 blockmap->first_offset = zone_base;
800 blockmap->next_offset = zone_base;
801 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
802 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
806 * Flush various tracking structures to disk
809 flush_all_volumes(void)
811 struct volume_info *vol;
813 TAILQ_FOREACH(vol, &VolList, entry)
818 flush_volume(struct volume_info *volume)
820 struct buffer_info *buffer;
823 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
824 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
825 flush_buffer(buffer);
827 if (writehammerbuf(volume, volume->ondisk, 0) == -1)
828 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
829 volume->cache.modified = 0;
833 flush_buffer(struct buffer_info *buffer)
835 struct volume_info *vol;
837 vol = buffer->volume;
838 if (writehammerbuf(vol, buffer->ondisk, buffer->raw_offset) == -1)
839 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
840 buffer->cache.modified = 0;
844 * Core I/O operations
847 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
851 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
852 if (n != HAMMER_BUFSIZE)
858 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
862 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
863 if (n != HAMMER_BUFSIZE)
868 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
871 value = HAMMER_BOOT_NOMBYTES;
872 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
874 if (value < HAMMER_BOOT_MINBYTES)
876 } else if (value < HAMMER_BOOT_MINBYTES) {
877 value = HAMMER_BOOT_MINBYTES;
883 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size)
886 value = HAMMER_MEM_NOMBYTES;
887 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
889 if (value < HAMMER_MEM_MINBYTES)
891 } else if (value < HAMMER_MEM_MINBYTES) {
892 value = HAMMER_MEM_MINBYTES;