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,
51 int64_t UndoBufferSize;
52 int UseReadBehind = -4;
54 int AssertOnFailure = 1;
55 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
59 buffer_hash(hammer_off_t buf_offset)
63 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
67 static struct buffer_info*
68 find_buffer(struct volume_info *volume, hammer_off_t buf_offset)
71 struct buffer_info *buf;
73 hi = buffer_hash(buf_offset);
74 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
75 if (buf->buf_offset == buf_offset)
82 __alloc_volume(const char *volname, int oflags)
84 struct volume_info *vol;
87 vol = malloc(sizeof(*vol));
89 err(1, "alloc_volume");
90 bzero(vol, sizeof(*vol));
93 vol->name = strdup(volname);
94 vol->fd = open(vol->name, oflags);
96 err(1, "alloc_volume: Failed to open %s", vol->name);
99 vol->device_offset = 0;
102 vol->ondisk = malloc(HAMMER_BUFSIZE);
103 if (vol->ondisk == NULL)
104 err(1, "alloc_volume");
105 bzero(vol->ondisk, HAMMER_BUFSIZE);
107 for (i = 0; i < HAMMER_BUFLISTS; ++i)
108 TAILQ_INIT(&vol->buffer_lists[i]);
114 __add_volume(struct volume_info *vol)
116 struct volume_info *scan;
117 struct stat st1, st2;
119 if (fstat(vol->fd, &st1) != 0)
120 errx(1, "add_volume: %s: Failed to stat", vol->name);
122 TAILQ_FOREACH(scan, &VolList, entry) {
123 if (scan->vol_no == vol->vol_no) {
124 errx(1, "add_volume: %s: Duplicate volume number %d "
126 vol->name, vol->vol_no, scan->name);
128 if (fstat(scan->fd, &st2) != 0) {
129 errx(1, "add_volume: %s: Failed to stat %s",
130 vol->name, scan->name);
132 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
133 errx(1, "add_volume: %s: Specified more than once",
138 TAILQ_INSERT_TAIL(&VolList, vol, entry);
142 * Initialize a volume structure and ondisk vol_no field.
145 init_volume(int32_t vol_no, const char *filename, int oflags)
147 struct volume_info *vol;
149 vol = __alloc_volume(filename, oflags);
150 vol->vol_no = vol->ondisk->vol_no = vol_no;
151 vol->cache.modified = 1;
159 * Initialize a volume structure and read ondisk volume header.
162 load_volume(const char *filename, int oflags)
164 struct volume_info *vol;
165 struct hammer_volume_ondisk *ondisk;
168 vol = __alloc_volume(filename, oflags);
169 ondisk = vol->ondisk;
171 n = readhammerbuf(vol, ondisk, 0);
173 err(1, "load_volume: %s: Read failed at offset 0", vol->name);
175 vol->vol_no = ondisk->vol_no;
177 if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) {
178 errx(1, "load_volume: Invalid root volume# %d",
179 ondisk->vol_rootvol);
182 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType))) {
183 errx(1, "load_volume: %s: Header does not indicate "
184 "that this is a hammer volume", vol->name);
187 if (TAILQ_EMPTY(&VolList)) {
188 Hammer_FSId = ondisk->vol_fsid;
189 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId))) {
190 errx(1, "load_volume: %s: FSId does match other volumes!",
200 * Check basic volume characteristics.
203 check_volume(struct volume_info *vol)
205 struct partinfo pinfo;
209 * Get basic information about the volume
211 if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
213 * Allow the formatting of regular files as HAMMER volumes
215 if (fstat(vol->fd, &st) < 0)
216 err(1, "Unable to stat %s", vol->name);
217 vol->size = st.st_size;
218 vol->type = "REGFILE";
221 * When formatting a block device as a HAMMER volume the
222 * sector size must be compatible. HAMMER uses 16384 byte
223 * filesystem buffers.
225 if (pinfo.reserved_blocks) {
226 errx(1, "HAMMER cannot be placed in a partition "
227 "which overlaps the disklabel or MBR");
229 if (pinfo.media_blksize > HAMMER_BUFSIZE ||
230 HAMMER_BUFSIZE % pinfo.media_blksize) {
231 errx(1, "A media sector size of %d is not supported",
232 pinfo.media_blksize);
235 vol->size = pinfo.media_size;
236 vol->device_offset = pinfo.media_offset;
237 vol->type = "DEVICE";
242 get_volume(int32_t vol_no)
244 struct volume_info *vol;
246 TAILQ_FOREACH(vol, &VolList, entry) {
247 if (vol->vol_no == vol_no)
252 errx(1, "get_volume: Volume %d does not exist!",
257 /* not added to or removed from hammer cache */
262 get_root_volume(void)
264 return(get_volume(HAMMER_ROOT_VOLNO));
268 rel_volume(struct volume_info *volume)
272 /* not added to or removed from hammer cache */
273 --volume->cache.refs;
277 * Acquire the specified buffer. isnew is -1 only when called
278 * via get_buffer_readahead() to prevent another readahead.
281 get_buffer(hammer_off_t buf_offset, int isnew)
284 struct buffer_info *buf;
285 struct volume_info *volume;
286 hammer_off_t orig_offset = buf_offset;
292 zone = HAMMER_ZONE_DECODE(buf_offset);
293 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
294 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
296 if (buf_offset == HAMMER_OFF_BAD)
299 if (AssertOnFailure) {
300 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
301 HAMMER_ZONE_RAW_BUFFER);
303 vol_no = HAMMER_VOL_DECODE(buf_offset);
304 volume = get_volume(vol_no);
308 buf_offset &= ~HAMMER_BUFMASK64;
309 buf = find_buffer(volume, buf_offset);
312 buf = malloc(sizeof(*buf));
313 bzero(buf, sizeof(*buf));
315 fprintf(stderr, "get_buffer: %016llx %016llx at %p\n",
316 (long long)orig_offset, (long long)buf_offset,
319 buf->buf_offset = buf_offset;
320 buf->raw_offset = hammer_xlate_to_phys(volume->ondisk,
322 buf->volume = volume;
323 hi = buffer_hash(buf_offset);
324 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
325 ++volume->cache.refs;
326 buf->cache.u.buffer = buf;
327 hammer_cache_add(&buf->cache, ISBUFFER);
331 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
332 (long long)orig_offset, (long long)buf_offset,
335 hammer_cache_used(&buf->cache);
339 hammer_cache_flush();
340 if ((ondisk = buf->ondisk) == NULL) {
341 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
343 n = readhammerbuf(volume, ondisk, buf->raw_offset);
346 err(1, "get_buffer: %s:%016llx "
347 "Read failed at offset %016llx",
349 (long long)buf->buf_offset,
350 (long long)buf->raw_offset);
351 bzero(ondisk, HAMMER_BUFSIZE);
356 bzero(ondisk, HAMMER_BUFSIZE);
357 buf->cache.modified = 1;
360 get_buffer_readahead(buf);
365 get_buffer_readahead(struct buffer_info *base)
367 struct buffer_info *buf;
368 struct volume_info *vol;
369 hammer_off_t buf_offset;
371 int ri = UseReadBehind;
372 int re = UseReadAhead;
374 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
378 if (raw_offset >= vol->ondisk->vol_buf_end)
380 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
382 raw_offset += HAMMER_BUFSIZE;
385 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
386 raw_offset - vol->ondisk->vol_buf_beg);
387 buf = find_buffer(vol, buf_offset);
389 buf = get_buffer(buf_offset, -1);
393 raw_offset += HAMMER_BUFSIZE;
398 rel_buffer(struct buffer_info *buffer)
400 struct volume_info *volume;
405 assert(buffer->cache.refs > 0);
406 if (--buffer->cache.refs == 0) {
407 if (buffer->cache.delete) {
408 hi = buffer_hash(buffer->buf_offset);
409 volume = buffer->volume;
410 if (buffer->cache.modified)
411 flush_buffer(buffer);
412 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
413 hammer_cache_del(&buffer->cache);
414 free(buffer->ondisk);
422 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
423 * bufferp is freed if isnew or the offset is out of range of the cached data.
424 * If bufferp is freed a referenced buffer is loaded into it.
427 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
430 if (*bufferp != NULL) {
432 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
433 rel_buffer(*bufferp);
437 return(get_ondisk(buf_offset, bufferp, isnew));
441 * Retrieve a pointer to a B-Tree node given a zone offset. The underlying
442 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
445 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
447 if (*bufferp != NULL) {
448 rel_buffer(*bufferp);
451 return(get_ondisk(node_offset, bufferp, 0));
455 * Return a pointer to a buffer data given a buffer offset.
456 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
460 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew)
462 struct buffer_info *buffer;
465 if (buffer == NULL) {
466 buffer = *bufferp = get_buffer(buf_offset, isnew);
471 return((char *)buffer->ondisk +
472 ((int32_t)buf_offset & HAMMER_BUFMASK));
476 * Allocate HAMMER elements - btree nodes, meta data, data storage
479 alloc_btree_element(hammer_off_t *offp, struct buffer_info **data_bufferp)
481 hammer_node_ondisk_t node;
483 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
485 bzero(node, sizeof(*node));
490 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
491 struct buffer_info **data_bufferp)
495 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
497 bzero(data, data_len);
502 * The only data_len supported by HAMMER userspace for large data zone
503 * (zone 10) is HAMMER_BUFSIZE which is 16KB. >16KB data does not fit
504 * in a buffer allocated by get_buffer(). Also alloc_blockmap() does
505 * not consider >16KB buffer size.
508 alloc_data_element(hammer_off_t *offp, int32_t data_len,
509 struct buffer_info **data_bufferp)
517 zone = hammer_data_zone_index(data_len);
518 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
519 assert(zone == HAMMER_ZONE_LARGE_DATA_INDEX ||
520 zone == HAMMER_ZONE_SMALL_DATA_INDEX);
522 data = alloc_blockmap(zone, data_len, offp, data_bufferp);
523 bzero(data, data_len);
528 * Format a new blockmap. This is mostly a degenerate case because
529 * all allocations are now actually done from the freemap.
532 format_blockmap(struct volume_info *root_vol, int zone, hammer_off_t offset)
534 hammer_blockmap_t blockmap;
535 hammer_off_t zone_base;
537 /* Only root volume needs formatting */
538 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
540 assert(hammer_is_zone2_mapped_index(zone));
542 blockmap = &root_vol->ondisk->vol0_blockmap[zone];
543 zone_base = HAMMER_ZONE_ENCODE(zone, offset);
545 bzero(blockmap, sizeof(*blockmap));
546 blockmap->phys_offset = 0;
547 blockmap->first_offset = zone_base;
548 blockmap->next_offset = zone_base;
549 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
550 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
551 root_vol->cache.modified = 1;
555 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
556 * code will load each volume's freemap.
559 format_freemap(struct volume_info *root_vol)
561 struct buffer_info *buffer = NULL;
562 hammer_off_t layer1_offset;
563 hammer_blockmap_t blockmap;
564 struct hammer_blockmap_layer1 *layer1;
567 /* Only root volume needs formatting */
568 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
570 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
571 for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
572 isnew = ((i % HAMMER_BUFSIZE) == 0);
573 layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
574 bzero(layer1, sizeof(*layer1));
575 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
576 layer1->blocks_free = 0;
577 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
579 assert(i == HAMMER_BIGBLOCK_SIZE);
582 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
583 bzero(blockmap, sizeof(*blockmap));
584 blockmap->phys_offset = layer1_offset;
585 blockmap->first_offset = 0;
586 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
587 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
588 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
589 root_vol->cache.modified = 1;
593 * Load the volume's remaining free space into the freemap.
595 * Returns the number of big-blocks available.
598 initialize_freemap(struct volume_info *vol)
600 struct volume_info *root_vol;
601 struct buffer_info *buffer1 = NULL;
602 struct buffer_info *buffer2 = NULL;
603 struct hammer_blockmap_layer1 *layer1;
604 struct hammer_blockmap_layer2 *layer2;
605 hammer_off_t layer1_base;
606 hammer_off_t layer1_offset;
607 hammer_off_t layer2_offset;
608 hammer_off_t phys_offset;
609 hammer_off_t block_offset;
610 hammer_off_t aligned_vol_free_end;
611 hammer_blockmap_t freemap;
613 int64_t layer1_count = 0;
615 root_vol = get_root_volume();
616 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
617 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
619 printf("initialize freemap volume %d\n", vol->vol_no);
622 * Initialize the freemap. First preallocate the big-blocks required
623 * to implement layer2. This preallocation is a bootstrap allocation
624 * using blocks from the target volume.
626 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
627 layer1_base = freemap->phys_offset;
629 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
630 phys_offset < aligned_vol_free_end;
631 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
632 layer1_offset = layer1_base +
633 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
634 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
635 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
636 layer1->phys_offset = alloc_bigblock(vol,
637 HAMMER_ZONE_FREEMAP_INDEX);
638 layer1->blocks_free = 0;
639 buffer1->cache.modified = 1;
640 layer1->layer1_crc = crc32(layer1,
641 HAMMER_LAYER1_CRCSIZE);
646 * Now fill everything in.
648 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
649 phys_offset < aligned_vol_free_end;
650 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
652 layer1_offset = layer1_base +
653 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
654 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
655 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
657 for (block_offset = 0;
658 block_offset < HAMMER_BLOCKMAP_LAYER2;
659 block_offset += HAMMER_BIGBLOCK_SIZE) {
660 layer2_offset = layer1->phys_offset +
661 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
662 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
663 bzero(layer2, sizeof(*layer2));
665 if (phys_offset + block_offset < vol->vol_free_off) {
667 * Fixups XXX - big-blocks already allocated as part
668 * of the freemap bootstrap.
670 if (layer2->zone == 0) {
671 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
672 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
673 layer2->bytes_free = 0;
675 } else if (phys_offset + block_offset < vol->vol_free_end) {
677 layer2->append_off = 0;
678 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
682 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
683 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
684 layer2->bytes_free = 0;
686 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
687 buffer2->cache.modified = 1;
690 layer1->blocks_free += layer1_count;
691 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
692 buffer1->cache.modified = 1;
697 rel_volume(root_vol);
702 * Returns the number of big-blocks available for filesystem data and undos
703 * without formatting.
706 count_freemap(struct volume_info *vol)
708 hammer_off_t phys_offset;
709 hammer_off_t vol_free_off;
710 hammer_off_t aligned_vol_free_end;
713 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
714 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
715 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
717 if (vol->vol_no == HAMMER_ROOT_VOLNO)
718 vol_free_off += HAMMER_BIGBLOCK_SIZE;
720 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
721 phys_offset < aligned_vol_free_end;
722 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
723 vol_free_off += HAMMER_BIGBLOCK_SIZE;
726 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
727 phys_offset < aligned_vol_free_end;
728 phys_offset += HAMMER_BIGBLOCK_SIZE) {
729 if (phys_offset < vol_free_off) {
731 } else if (phys_offset < vol->vol_free_end) {
740 * Format the undomap for the root volume.
743 format_undomap(struct volume_info *root_vol)
745 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
746 hammer_off_t undo_limit;
747 hammer_blockmap_t blockmap;
748 struct hammer_volume_ondisk *ondisk;
749 struct buffer_info *buffer = NULL;
755 /* Only root volume needs formatting */
756 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
757 ondisk = root_vol->ondisk;
760 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
761 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
764 * The minimum UNDO fifo size is 500MB, or approximately 1% of
765 * the recommended 50G disk.
767 * Changing this minimum is rather dangerous as complex filesystem
768 * operations can cause the UNDO FIFO to fill up otherwise.
770 undo_limit = UndoBufferSize;
771 if (undo_limit == 0) {
772 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
773 if (undo_limit < 500*1024*1024)
774 undo_limit = 500*1024*1024;
776 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
777 ~HAMMER_BIGBLOCK_MASK64;
778 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
779 undo_limit = HAMMER_BIGBLOCK_SIZE;
780 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
781 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
782 UndoBufferSize = undo_limit;
784 blockmap = &ondisk->vol0_blockmap[undo_zone];
785 bzero(blockmap, sizeof(*blockmap));
786 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
787 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
788 blockmap->next_offset = blockmap->first_offset;
789 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
790 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
792 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
793 assert(limit_index <= HAMMER_UNDO_LAYER2);
795 for (n = 0; n < limit_index; ++n) {
796 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
797 HAMMER_ZONE_UNDO_INDEX);
799 while (n < HAMMER_UNDO_LAYER2) {
800 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
804 * Pre-initialize the UNDO blocks (HAMMER version 4+)
806 printf("initializing the undo map (%jd MB)\n",
807 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
810 scan = blockmap->first_offset;
813 while (scan < blockmap->alloc_offset) {
814 hammer_fifo_head_t head;
815 hammer_fifo_tail_t tail;
817 int bytes = HAMMER_UNDO_ALIGN;
819 isnew = ((scan & HAMMER_BUFMASK64) == 0);
820 head = get_buffer_data(scan, &buffer, isnew);
821 buffer->cache.modified = 1;
822 tail = (void *)((char *)head + bytes - sizeof(*tail));
825 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
826 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
827 head->hdr_size = bytes;
828 head->hdr_seq = seqno++;
830 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
831 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
832 tail->tail_size = bytes;
834 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
835 crc32(head + 1, bytes - sizeof(*head));
843 * Flush various tracking structures to disk
846 flush_all_volumes(void)
848 struct volume_info *vol;
850 TAILQ_FOREACH(vol, &VolList, entry)
855 flush_volume(struct volume_info *volume)
857 struct buffer_info *buffer;
860 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
861 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
862 flush_buffer(buffer);
864 if (writehammerbuf(volume, volume->ondisk, 0) == -1)
865 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
866 volume->cache.modified = 0;
870 flush_buffer(struct buffer_info *buffer)
872 struct volume_info *vol;
874 vol = buffer->volume;
875 if (writehammerbuf(vol, buffer->ondisk, buffer->raw_offset) == -1)
876 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
877 buffer->cache.modified = 0;
881 * Core I/O operations
884 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
888 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
889 if (n != HAMMER_BUFSIZE)
895 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
899 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
900 if (n != HAMMER_BUFSIZE)
905 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
908 value = HAMMER_BOOT_NOMBYTES;
909 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
911 if (value < HAMMER_BOOT_MINBYTES)
913 } else if (value < HAMMER_BOOT_MINBYTES) {
914 value = HAMMER_BOOT_MINBYTES;
920 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size)
923 value = HAMMER_MEM_NOMBYTES;
924 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
926 if (value < HAMMER_MEM_MINBYTES)
928 } else if (value < HAMMER_MEM_MINBYTES) {
929 value = HAMMER_MEM_MINBYTES;