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 int UseReadBehind = -4;
53 int AssertOnFailure = 1;
54 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
55 static int valid_hammer_volumes;
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->rdonly = (oflags == O_RDONLY);
94 vol->name = strdup(volname);
95 vol->fd = open(vol->name, oflags);
97 err(1, "alloc_volume: Failed to open %s", vol->name);
100 vol->device_offset = 0;
103 vol->ondisk = malloc(HAMMER_BUFSIZE);
104 if (vol->ondisk == NULL)
105 err(1, "alloc_volume");
106 bzero(vol->ondisk, HAMMER_BUFSIZE);
108 for (i = 0; i < HAMMER_BUFLISTS; ++i)
109 TAILQ_INIT(&vol->buffer_lists[i]);
115 __add_volume(struct volume_info *vol)
117 struct volume_info *scan;
118 struct stat st1, st2;
120 if (fstat(vol->fd, &st1) != 0)
121 errx(1, "add_volume: %s: Failed to stat", vol->name);
123 TAILQ_FOREACH(scan, &VolList, entry) {
124 if (scan->vol_no == vol->vol_no) {
125 errx(1, "add_volume: %s: Duplicate volume number %d "
127 vol->name, vol->vol_no, scan->name);
129 if (fstat(scan->fd, &st2) != 0) {
130 errx(1, "add_volume: %s: Failed to stat %s",
131 vol->name, scan->name);
133 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
134 errx(1, "add_volume: %s: Specified more than once",
139 TAILQ_INSERT_TAIL(&VolList, vol, entry);
143 * Initialize a volume structure and ondisk vol_no field.
146 init_volume(int32_t vol_no, const char *filename, int oflags)
148 struct volume_info *vol;
150 vol = __alloc_volume(filename, oflags);
151 vol->vol_no = vol->ondisk->vol_no = vol_no;
152 vol->cache.modified = 1;
160 * Initialize a volume structure and read ondisk volume header.
163 load_volume(const char *filename, int oflags)
165 struct volume_info *vol;
166 struct hammer_volume_ondisk *ondisk;
169 vol = __alloc_volume(filename, oflags);
170 ondisk = vol->ondisk;
172 n = readhammerbuf(vol, ondisk, 0);
174 err(1, "load_volume: %s: Read failed at offset 0", vol->name);
176 vol->vol_no = ondisk->vol_no;
178 if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) {
179 errx(1, "load_volume: Invalid root volume# %d",
180 ondisk->vol_rootvol);
183 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType))) {
184 errx(1, "load_volume: %s: Header does not indicate "
185 "that this is a hammer volume", vol->name);
188 if (valid_hammer_volumes++ == 0) {
189 Hammer_FSId = ondisk->vol_fsid;
190 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId))) {
191 errx(1, "load_volume: %s: FSId does match other volumes!",
201 * Check basic volume characteristics.
204 check_volume(struct volume_info *vol)
206 struct partinfo pinfo;
210 * Get basic information about the volume
212 if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
214 * Allow the formatting of regular files as HAMMER volumes
216 if (fstat(vol->fd, &st) < 0)
217 err(1, "Unable to stat %s", vol->name);
218 vol->size = st.st_size;
219 vol->type = "REGFILE";
222 * When formatting a block device as a HAMMER volume the
223 * sector size must be compatible. HAMMER uses 16384 byte
224 * filesystem buffers.
226 if (pinfo.reserved_blocks) {
227 errx(1, "HAMMER cannot be placed in a partition "
228 "which overlaps the disklabel or MBR");
230 if (pinfo.media_blksize > HAMMER_BUFSIZE ||
231 HAMMER_BUFSIZE % pinfo.media_blksize) {
232 errx(1, "A media sector size of %d is not supported",
233 pinfo.media_blksize);
236 vol->size = pinfo.media_size;
237 vol->device_offset = pinfo.media_offset;
238 vol->type = "DEVICE";
243 get_volume(int32_t vol_no)
245 struct volume_info *vol;
247 TAILQ_FOREACH(vol, &VolList, entry) {
248 if (vol->vol_no == vol_no)
252 errx(1, "get_volume: Volume %d does not exist!", vol_no);
255 /* not added to or removed from hammer cache */
260 get_root_volume(void)
262 return(get_volume(HAMMER_ROOT_VOLNO));
266 rel_volume(struct volume_info *volume)
270 /* not added to or removed from hammer cache */
271 --volume->cache.refs;
275 * Acquire the specified buffer. isnew is -1 only when called
276 * via get_buffer_readahead() to prevent another readahead.
279 get_buffer(hammer_off_t buf_offset, int isnew)
282 struct buffer_info *buf;
283 struct volume_info *volume;
284 hammer_off_t orig_offset = buf_offset;
290 zone = HAMMER_ZONE_DECODE(buf_offset);
291 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
292 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
294 if (buf_offset == HAMMER_OFF_BAD)
296 assert((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
298 vol_no = HAMMER_VOL_DECODE(buf_offset);
299 volume = get_volume(vol_no);
303 buf_offset &= ~HAMMER_BUFMASK64;
304 buf = find_buffer(volume, buf_offset);
307 buf = malloc(sizeof(*buf));
308 bzero(buf, sizeof(*buf));
310 fprintf(stderr, "get_buffer: %016llx %016llx at %p\n",
311 (long long)orig_offset, (long long)buf_offset,
314 buf->buf_offset = buf_offset;
315 buf->raw_offset = hammer_xlate_to_phys(volume->ondisk,
317 buf->volume = volume;
318 hi = buffer_hash(buf_offset);
319 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
320 ++volume->cache.refs;
321 buf->cache.u.buffer = buf;
322 hammer_cache_add(&buf->cache, ISBUFFER);
326 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
327 (long long)orig_offset, (long long)buf_offset,
330 hammer_cache_used(&buf->cache);
334 hammer_cache_flush();
335 if ((ondisk = buf->ondisk) == NULL) {
336 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
338 n = readhammerbuf(volume, ondisk, buf->raw_offset);
341 err(1, "get_buffer: %s:%016llx "
342 "Read failed at offset %016llx",
344 (long long)buf->buf_offset,
345 (long long)buf->raw_offset);
346 bzero(ondisk, HAMMER_BUFSIZE);
351 bzero(ondisk, HAMMER_BUFSIZE);
352 buf->cache.modified = 1;
355 get_buffer_readahead(buf);
360 get_buffer_readahead(struct buffer_info *base)
362 struct buffer_info *buf;
363 struct volume_info *vol;
364 hammer_off_t buf_offset;
366 int ri = UseReadBehind;
367 int re = UseReadAhead;
369 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
373 if (raw_offset >= vol->ondisk->vol_buf_end)
375 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
377 raw_offset += HAMMER_BUFSIZE;
380 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
381 raw_offset - vol->ondisk->vol_buf_beg);
382 buf = find_buffer(vol, buf_offset);
384 buf = get_buffer(buf_offset, -1);
388 raw_offset += HAMMER_BUFSIZE;
393 rel_buffer(struct buffer_info *buffer)
395 struct volume_info *volume;
400 assert(buffer->cache.refs > 0);
401 if (--buffer->cache.refs == 0) {
402 if (buffer->cache.delete) {
403 hi = buffer_hash(buffer->buf_offset);
404 volume = buffer->volume;
405 if (buffer->cache.modified)
406 flush_buffer(buffer);
407 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
408 hammer_cache_del(&buffer->cache);
409 free(buffer->ondisk);
417 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
418 * bufferp is freed if isnew or the offset is out of range of the cached data.
419 * If bufferp is freed a referenced buffer is loaded into it.
422 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
425 if (*bufferp != NULL) {
427 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
428 rel_buffer(*bufferp);
432 return(get_ondisk(buf_offset, bufferp, isnew));
436 * Retrieve a pointer to a B-Tree node given a zone offset. The underlying
437 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
440 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
442 if (*bufferp != NULL) {
443 rel_buffer(*bufferp);
446 return(get_ondisk(node_offset, bufferp, 0));
450 * Return a pointer to a buffer data given a buffer offset.
451 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
455 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew)
457 struct buffer_info *buffer;
460 if (buffer == NULL) {
461 buffer = *bufferp = get_buffer(buf_offset, isnew);
466 return((char *)buffer->ondisk +
467 ((int32_t)buf_offset & HAMMER_BUFMASK));
471 * Allocate HAMMER elements - btree nodes, meta data, data storage
474 alloc_btree_element(hammer_off_t *offp, struct buffer_info **data_bufferp)
476 hammer_node_ondisk_t node;
478 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
480 bzero(node, sizeof(*node));
485 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
486 struct buffer_info **data_bufferp)
490 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
492 bzero(data, data_len);
497 * The only data_len supported by HAMMER userspace for large data zone
498 * (zone 10) is HAMMER_BUFSIZE which is 16KB. >16KB data does not fit
499 * in a buffer allocated by get_buffer(). Also alloc_blockmap() does
500 * not consider >16KB buffer size.
503 alloc_data_element(hammer_off_t *offp, int32_t data_len,
504 struct buffer_info **data_bufferp)
512 zone = hammer_data_zone_index(data_len);
513 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
514 assert(zone == HAMMER_ZONE_LARGE_DATA_INDEX ||
515 zone == HAMMER_ZONE_SMALL_DATA_INDEX);
517 data = alloc_blockmap(zone, data_len, offp, data_bufferp);
518 bzero(data, data_len);
523 * Format a new blockmap. This is mostly a degenerate case because
524 * all allocations are now actually done from the freemap.
527 format_blockmap(struct volume_info *root_vol, int zone, hammer_off_t offset)
529 hammer_blockmap_t blockmap;
530 hammer_off_t zone_base;
532 /* Only root volume needs formatting */
533 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
535 assert(hammer_is_zone2_mapped_index(zone));
537 blockmap = &root_vol->ondisk->vol0_blockmap[zone];
538 zone_base = HAMMER_ZONE_ENCODE(zone, offset);
540 bzero(blockmap, sizeof(*blockmap));
541 blockmap->phys_offset = 0;
542 blockmap->first_offset = zone_base;
543 blockmap->next_offset = zone_base;
544 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
545 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
546 root_vol->cache.modified = 1;
550 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
551 * code will load each volume's freemap.
554 format_freemap(struct volume_info *root_vol)
556 struct buffer_info *buffer = NULL;
557 hammer_off_t layer1_offset;
558 hammer_blockmap_t blockmap;
559 struct hammer_blockmap_layer1 *layer1;
562 /* Only root volume needs formatting */
563 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
565 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
566 for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
567 isnew = ((i % HAMMER_BUFSIZE) == 0);
568 layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
569 bzero(layer1, sizeof(*layer1));
570 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
571 layer1->blocks_free = 0;
572 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
574 assert(i == HAMMER_BIGBLOCK_SIZE);
577 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
578 bzero(blockmap, sizeof(*blockmap));
579 blockmap->phys_offset = layer1_offset;
580 blockmap->first_offset = 0;
581 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
582 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
583 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
584 root_vol->cache.modified = 1;
588 * Load the volume's remaining free space into the freemap.
590 * Returns the number of big-blocks available.
593 initialize_freemap(struct volume_info *vol)
595 struct volume_info *root_vol;
596 struct buffer_info *buffer1 = NULL;
597 struct buffer_info *buffer2 = NULL;
598 struct hammer_blockmap_layer1 *layer1;
599 struct hammer_blockmap_layer2 *layer2;
600 hammer_off_t layer1_base;
601 hammer_off_t layer1_offset;
602 hammer_off_t layer2_offset;
603 hammer_off_t phys_offset;
604 hammer_off_t block_offset;
605 hammer_off_t aligned_vol_free_end;
606 hammer_blockmap_t freemap;
608 int64_t layer1_count = 0;
610 root_vol = get_root_volume();
611 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
612 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
614 printf("initialize freemap volume %d\n", vol->vol_no);
617 * Initialize the freemap. First preallocate the big-blocks required
618 * to implement layer2. This preallocation is a bootstrap allocation
619 * using blocks from the target volume.
621 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
622 layer1_base = freemap->phys_offset;
624 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
625 phys_offset < aligned_vol_free_end;
626 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
627 layer1_offset = layer1_base +
628 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
629 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
630 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
631 layer1->phys_offset = alloc_bigblock(vol,
632 HAMMER_ZONE_FREEMAP_INDEX);
633 layer1->blocks_free = 0;
634 buffer1->cache.modified = 1;
635 layer1->layer1_crc = crc32(layer1,
636 HAMMER_LAYER1_CRCSIZE);
641 * Now fill everything in.
643 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
644 phys_offset < aligned_vol_free_end;
645 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
647 layer1_offset = layer1_base +
648 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
649 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
650 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
652 for (block_offset = 0;
653 block_offset < HAMMER_BLOCKMAP_LAYER2;
654 block_offset += HAMMER_BIGBLOCK_SIZE) {
655 layer2_offset = layer1->phys_offset +
656 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
657 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
658 bzero(layer2, sizeof(*layer2));
660 if (phys_offset + block_offset < vol->vol_free_off) {
662 * Fixups XXX - big-blocks already allocated as part
663 * of the freemap bootstrap.
665 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
666 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
667 layer2->bytes_free = 0;
668 } else if (phys_offset + block_offset < vol->vol_free_end) {
670 layer2->append_off = 0;
671 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
675 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
676 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
677 layer2->bytes_free = 0;
679 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
680 buffer2->cache.modified = 1;
683 layer1->blocks_free += layer1_count;
684 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
685 buffer1->cache.modified = 1;
690 rel_volume(root_vol);
695 * Returns the number of big-blocks available for filesystem data and undos
696 * without formatting.
699 count_freemap(struct volume_info *vol)
701 hammer_off_t phys_offset;
702 hammer_off_t vol_free_off;
703 hammer_off_t aligned_vol_free_end;
706 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
707 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
708 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
710 if (vol->vol_no == HAMMER_ROOT_VOLNO)
711 vol_free_off += HAMMER_BIGBLOCK_SIZE;
713 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
714 phys_offset < aligned_vol_free_end;
715 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
716 vol_free_off += HAMMER_BIGBLOCK_SIZE;
719 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
720 phys_offset < aligned_vol_free_end;
721 phys_offset += HAMMER_BIGBLOCK_SIZE) {
722 if (phys_offset < vol_free_off) {
724 } else if (phys_offset < vol->vol_free_end) {
733 * Format the undomap for the root volume.
736 format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size)
738 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
739 hammer_off_t undo_limit;
740 hammer_blockmap_t blockmap;
741 struct hammer_volume_ondisk *ondisk;
742 struct buffer_info *buffer = NULL;
748 /* Only root volume needs formatting */
749 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
750 ondisk = root_vol->ondisk;
753 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
754 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
757 * The minimum UNDO fifo size is 500MB, or approximately 1% of
758 * the recommended 50G disk.
760 * Changing this minimum is rather dangerous as complex filesystem
761 * operations can cause the UNDO FIFO to fill up otherwise.
763 undo_limit = *undo_buffer_size;
764 if (undo_limit == 0) {
765 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
766 if (undo_limit < 500*1024*1024)
767 undo_limit = 500*1024*1024;
769 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
770 ~HAMMER_BIGBLOCK_MASK64;
771 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
772 undo_limit = HAMMER_BIGBLOCK_SIZE;
773 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
774 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
775 *undo_buffer_size = undo_limit;
777 blockmap = &ondisk->vol0_blockmap[undo_zone];
778 bzero(blockmap, sizeof(*blockmap));
779 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
780 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
781 blockmap->next_offset = blockmap->first_offset;
782 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
783 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
785 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
786 assert(limit_index <= HAMMER_UNDO_LAYER2);
788 for (n = 0; n < limit_index; ++n) {
789 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
790 HAMMER_ZONE_UNDO_INDEX);
792 while (n < HAMMER_UNDO_LAYER2) {
793 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
797 * Pre-initialize the UNDO blocks (HAMMER version 4+)
799 printf("initializing the undo map (%jd MB)\n",
800 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
803 scan = blockmap->first_offset;
806 while (scan < blockmap->alloc_offset) {
807 hammer_fifo_head_t head;
808 hammer_fifo_tail_t tail;
810 int bytes = HAMMER_UNDO_ALIGN;
812 isnew = ((scan & HAMMER_BUFMASK64) == 0);
813 head = get_buffer_data(scan, &buffer, isnew);
814 buffer->cache.modified = 1;
815 tail = (void *)((char *)head + bytes - sizeof(*tail));
818 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
819 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
820 head->hdr_size = bytes;
821 head->hdr_seq = seqno++;
823 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
824 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
825 tail->tail_size = bytes;
827 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
828 crc32(head + 1, bytes - sizeof(*head));
836 * Flush various tracking structures to disk
839 flush_all_volumes(void)
841 struct volume_info *vol;
843 TAILQ_FOREACH(vol, &VolList, entry)
848 flush_volume(struct volume_info *volume)
850 struct buffer_info *buffer;
853 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
854 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
855 flush_buffer(buffer);
857 if (writehammerbuf(volume, volume->ondisk, 0) == -1)
858 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
859 volume->cache.modified = 0;
863 flush_buffer(struct buffer_info *buffer)
865 struct volume_info *vol;
867 vol = buffer->volume;
868 if (writehammerbuf(vol, buffer->ondisk, buffer->raw_offset) == -1)
869 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
870 buffer->cache.modified = 0;
874 * Core I/O operations
877 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
881 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
882 if (n != HAMMER_BUFSIZE)
888 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
895 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
896 if (n != HAMMER_BUFSIZE)
901 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
904 value = HAMMER_BOOT_NOMBYTES;
905 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
907 if (value < HAMMER_BOOT_MINBYTES)
909 } else if (value < HAMMER_BOOT_MINBYTES) {
910 value = HAMMER_BOOT_MINBYTES;
916 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size)
919 value = HAMMER_MEM_NOMBYTES;
920 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
922 if (value < HAMMER_MEM_MINBYTES)
924 } else if (value < HAMMER_MEM_MINBYTES) {
925 value = HAMMER_MEM_MINBYTES;