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 void *get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
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;
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 (valid_hammer_volumes++ == 0) {
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)
251 errx(1, "get_volume: Volume %d does not exist!", vol_no);
253 /* not added to or removed from hammer cache */
258 get_root_volume(void)
260 return(get_volume(HAMMER_ROOT_VOLNO));
264 rel_volume(struct volume_info *volume __unused)
270 * Acquire the specified buffer. isnew is -1 only when called
271 * via get_buffer_readahead() to prevent another readahead.
274 get_buffer(hammer_off_t buf_offset, int isnew)
276 struct buffer_info *buf;
277 struct volume_info *volume;
278 hammer_off_t orig_offset = buf_offset;
284 zone = HAMMER_ZONE_DECODE(buf_offset);
285 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
286 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
288 if (buf_offset == HAMMER_OFF_BAD)
290 assert(hammer_is_zone_raw_buffer(buf_offset));
292 vol_no = HAMMER_VOL_DECODE(buf_offset);
293 volume = get_volume(vol_no);
295 buf_offset &= ~HAMMER_BUFMASK64;
296 buf = find_buffer(volume, buf_offset);
299 buf = malloc(sizeof(*buf));
300 bzero(buf, sizeof(*buf));
302 fprintf(stderr, "get_buffer: %016jx %016jx at %p\n",
303 (intmax_t)orig_offset, (intmax_t)buf_offset,
306 buf->buf_offset = buf_offset;
307 buf->raw_offset = hammer_xlate_to_phys(volume->ondisk,
309 buf->volume = volume;
310 buf->ondisk = malloc(HAMMER_BUFSIZE);
312 n = readhammerbuf(volume, buf->ondisk, buf->raw_offset);
315 err(1, "get_buffer: %s:%016jx "
316 "Read failed at offset %016jx",
318 (intmax_t)buf->buf_offset,
319 (intmax_t)buf->raw_offset);
320 bzero(buf->ondisk, HAMMER_BUFSIZE);
324 hi = buffer_hash(buf_offset);
325 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
326 buf->cache.buffer = buf;
327 hammer_cache_add(&buf->cache);
331 fprintf(stderr, "get_buffer: %016jx %016jx at %p *\n",
332 (intmax_t)orig_offset, (intmax_t)buf_offset,
335 assert(buf->ondisk != NULL);
336 hammer_cache_used(&buf->cache);
340 hammer_cache_flush();
343 assert(buf->cache.modified == 0);
344 bzero(buf->ondisk, HAMMER_BUFSIZE);
345 buf->cache.modified = 1;
348 get_buffer_readahead(buf);
353 get_buffer_readahead(struct buffer_info *base)
355 struct buffer_info *buf;
356 struct volume_info *vol;
357 hammer_off_t buf_offset;
359 int ri = UseReadBehind;
360 int re = UseReadAhead;
362 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
366 if (raw_offset >= vol->ondisk->vol_buf_end)
368 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
370 raw_offset += HAMMER_BUFSIZE;
373 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
374 raw_offset - vol->ondisk->vol_buf_beg);
375 buf = find_buffer(vol, buf_offset);
377 buf = get_buffer(buf_offset, -1);
381 raw_offset += HAMMER_BUFSIZE;
386 rel_buffer(struct buffer_info *buffer)
388 struct volume_info *volume;
393 assert(buffer->cache.refs > 0);
394 if (--buffer->cache.refs == 0) {
395 if (buffer->cache.delete) {
396 hi = buffer_hash(buffer->buf_offset);
397 volume = buffer->volume;
398 if (buffer->cache.modified)
399 flush_buffer(buffer);
400 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
401 hammer_cache_del(&buffer->cache);
402 free(buffer->ondisk);
410 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
411 * bufferp is freed if isnew or the offset is out of range of the cached data.
412 * If bufferp is freed a referenced buffer is loaded into it.
415 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
418 if (*bufferp != NULL) {
420 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
421 rel_buffer(*bufferp);
425 return(get_ondisk(buf_offset, bufferp, isnew));
429 * Retrieve a pointer to a B-Tree node given a zone offset. The underlying
430 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
433 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
435 if (*bufferp != NULL) {
436 rel_buffer(*bufferp);
439 return(get_ondisk(node_offset, bufferp, 0));
443 * Return a pointer to a buffer data given a buffer offset.
444 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
447 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew)
449 struct buffer_info *buffer;
452 if (buffer == NULL) {
453 buffer = *bufferp = get_buffer(buf_offset, isnew);
458 return((char *)buffer->ondisk +
459 ((int32_t)buf_offset & HAMMER_BUFMASK));
463 * Allocate HAMMER elements - B-Tree nodse
466 alloc_btree_element(hammer_off_t *offp, struct buffer_info **data_bufferp)
468 hammer_node_ondisk_t node;
470 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
472 bzero(node, sizeof(*node));
477 * Allocate HAMMER elements - meta data (inode, direntry, PFS, etc)
480 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
481 struct buffer_info **data_bufferp)
485 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
487 bzero(data, data_len);
492 * Allocate HAMMER elements - data storage
494 * The only data_len supported by HAMMER userspace for large data zone
495 * (zone 10) is HAMMER_BUFSIZE which is 16KB. >16KB data does not fit
496 * in a buffer allocated by get_buffer(). Also alloc_blockmap() does
497 * not consider >16KB buffer size.
500 alloc_data_element(hammer_off_t *offp, int32_t data_len,
501 struct buffer_info **data_bufferp)
509 zone = hammer_data_zone_index(data_len);
510 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
511 assert(zone == HAMMER_ZONE_LARGE_DATA_INDEX ||
512 zone == HAMMER_ZONE_SMALL_DATA_INDEX);
514 data = alloc_blockmap(zone, data_len, offp, data_bufferp);
515 bzero(data, data_len);
520 * Format a new blockmap. This is mostly a degenerate case because
521 * all allocations are now actually done from the freemap.
524 format_blockmap(struct volume_info *root_vol, int zone, hammer_off_t offset)
526 hammer_blockmap_t blockmap;
527 hammer_off_t zone_base;
529 /* Only root volume needs formatting */
530 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
532 assert(hammer_is_zone2_mapped_index(zone));
534 blockmap = &root_vol->ondisk->vol0_blockmap[zone];
535 zone_base = HAMMER_ZONE_ENCODE(zone, offset);
537 bzero(blockmap, sizeof(*blockmap));
538 blockmap->phys_offset = 0;
539 blockmap->first_offset = zone_base;
540 blockmap->next_offset = zone_base;
541 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
542 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
546 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
547 * code will load each volume's freemap.
550 format_freemap(struct volume_info *root_vol)
552 struct buffer_info *buffer = NULL;
553 hammer_off_t layer1_offset;
554 hammer_blockmap_t blockmap;
555 struct hammer_blockmap_layer1 *layer1;
558 /* Only root volume needs formatting */
559 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
561 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
562 for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
563 isnew = ((i % HAMMER_BUFSIZE) == 0);
564 layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
565 bzero(layer1, sizeof(*layer1));
566 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
567 layer1->blocks_free = 0;
568 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
570 assert(i == HAMMER_BIGBLOCK_SIZE);
573 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
574 bzero(blockmap, sizeof(*blockmap));
575 blockmap->phys_offset = layer1_offset;
576 blockmap->first_offset = 0;
577 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
578 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
579 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
583 * Load the volume's remaining free space into the freemap.
585 * Returns the number of big-blocks available.
588 initialize_freemap(struct volume_info *vol)
590 struct volume_info *root_vol;
591 struct buffer_info *buffer1 = NULL;
592 struct buffer_info *buffer2 = NULL;
593 struct hammer_blockmap_layer1 *layer1;
594 struct hammer_blockmap_layer2 *layer2;
595 hammer_off_t layer1_base;
596 hammer_off_t layer1_offset;
597 hammer_off_t layer2_offset;
598 hammer_off_t phys_offset;
599 hammer_off_t block_offset;
600 hammer_off_t aligned_vol_free_end;
601 hammer_blockmap_t freemap;
603 int64_t layer1_count = 0;
605 root_vol = get_root_volume();
606 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
607 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
609 printf("initialize freemap volume %d\n", vol->vol_no);
612 * Initialize the freemap. First preallocate the big-blocks required
613 * to implement layer2. This preallocation is a bootstrap allocation
614 * using blocks from the target volume.
616 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
617 layer1_base = freemap->phys_offset;
619 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
620 phys_offset < aligned_vol_free_end;
621 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
622 layer1_offset = layer1_base +
623 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
624 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
625 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
626 layer1->phys_offset = alloc_bigblock(vol,
627 HAMMER_ZONE_FREEMAP_INDEX);
628 layer1->blocks_free = 0;
629 buffer1->cache.modified = 1;
630 layer1->layer1_crc = crc32(layer1,
631 HAMMER_LAYER1_CRCSIZE);
636 * Now fill everything in.
638 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
639 phys_offset < aligned_vol_free_end;
640 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
642 layer1_offset = layer1_base +
643 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
644 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
645 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
647 for (block_offset = 0;
648 block_offset < HAMMER_BLOCKMAP_LAYER2;
649 block_offset += HAMMER_BIGBLOCK_SIZE) {
650 layer2_offset = layer1->phys_offset +
651 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
652 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
653 bzero(layer2, sizeof(*layer2));
655 if (phys_offset + block_offset < vol->vol_free_off) {
657 * Fixups XXX - big-blocks already allocated as part
658 * of the freemap bootstrap.
660 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
661 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
662 layer2->bytes_free = 0;
663 } else if (phys_offset + block_offset < vol->vol_free_end) {
665 layer2->append_off = 0;
666 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
670 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
671 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
672 layer2->bytes_free = 0;
674 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
675 buffer2->cache.modified = 1;
678 layer1->blocks_free += layer1_count;
679 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
680 buffer1->cache.modified = 1;
685 rel_volume(root_vol);
690 * Returns the number of big-blocks available for filesystem data and undos
691 * without formatting.
694 count_freemap(struct volume_info *vol)
696 hammer_off_t phys_offset;
697 hammer_off_t vol_free_off;
698 hammer_off_t aligned_vol_free_end;
701 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
702 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
703 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
705 if (vol->vol_no == HAMMER_ROOT_VOLNO)
706 vol_free_off += HAMMER_BIGBLOCK_SIZE;
708 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
709 phys_offset < aligned_vol_free_end;
710 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
711 vol_free_off += HAMMER_BIGBLOCK_SIZE;
714 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
715 phys_offset < aligned_vol_free_end;
716 phys_offset += HAMMER_BIGBLOCK_SIZE) {
717 if (phys_offset < vol_free_off) {
719 } else if (phys_offset < vol->vol_free_end) {
728 * Format the undomap for the root volume.
731 format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size)
733 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
734 hammer_off_t undo_limit;
735 hammer_blockmap_t blockmap;
736 struct hammer_volume_ondisk *ondisk;
737 struct buffer_info *buffer = NULL;
743 /* Only root volume needs formatting */
744 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
745 ondisk = root_vol->ondisk;
748 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
749 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
752 * The minimum UNDO fifo size is 500MB, or approximately 1% of
753 * the recommended 50G disk.
755 * Changing this minimum is rather dangerous as complex filesystem
756 * operations can cause the UNDO FIFO to fill up otherwise.
758 undo_limit = *undo_buffer_size;
759 if (undo_limit == 0) {
760 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
761 if (undo_limit < 500*1024*1024)
762 undo_limit = 500*1024*1024;
764 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
765 ~HAMMER_BIGBLOCK_MASK64;
766 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
767 undo_limit = HAMMER_BIGBLOCK_SIZE;
768 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
769 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
770 *undo_buffer_size = undo_limit;
772 blockmap = &ondisk->vol0_blockmap[undo_zone];
773 bzero(blockmap, sizeof(*blockmap));
774 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
775 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
776 blockmap->next_offset = blockmap->first_offset;
777 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
778 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
780 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
781 assert(limit_index <= HAMMER_UNDO_LAYER2);
783 for (n = 0; n < limit_index; ++n) {
784 ondisk->vol0_undo_array[n] = alloc_bigblock(root_vol,
785 HAMMER_ZONE_UNDO_INDEX);
787 while (n < HAMMER_UNDO_LAYER2) {
788 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
792 * Pre-initialize the UNDO blocks (HAMMER version 4+)
794 printf("initializing the undo map (%jd MB)\n",
795 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
798 scan = blockmap->first_offset;
801 while (scan < blockmap->alloc_offset) {
802 hammer_fifo_head_t head;
803 hammer_fifo_tail_t tail;
805 int bytes = HAMMER_UNDO_ALIGN;
807 isnew = ((scan & HAMMER_BUFMASK64) == 0);
808 head = get_buffer_data(scan, &buffer, isnew);
809 buffer->cache.modified = 1;
810 tail = (void *)((char *)head + bytes - sizeof(*tail));
813 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
814 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
815 head->hdr_size = bytes;
816 head->hdr_seq = seqno++;
818 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
819 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
820 tail->tail_size = bytes;
822 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
823 crc32(head + 1, bytes - sizeof(*head));
831 * Flush various tracking structures to disk
834 flush_all_volumes(void)
836 struct volume_info *vol;
838 TAILQ_FOREACH(vol, &VolList, entry)
843 flush_volume(struct volume_info *volume)
845 struct buffer_info *buffer;
848 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
849 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
850 flush_buffer(buffer);
852 if (writehammerbuf(volume, volume->ondisk, 0) == -1)
853 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
857 flush_buffer(struct buffer_info *buffer)
859 struct volume_info *vol;
861 vol = buffer->volume;
862 if (writehammerbuf(vol, buffer->ondisk, buffer->raw_offset) == -1)
863 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
864 buffer->cache.modified = 0;
868 * Core I/O operations
871 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
875 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
876 if (n != HAMMER_BUFSIZE)
882 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
889 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
890 if (n != HAMMER_BUFSIZE)
895 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
898 value = HAMMER_BOOT_NOMBYTES;
899 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
901 if (value < HAMMER_BOOT_MINBYTES)
903 } else if (value < HAMMER_BOOT_MINBYTES) {
904 value = HAMMER_BOOT_MINBYTES;
910 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size)
913 value = HAMMER_MEM_NOMBYTES;
914 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
916 if (value < HAMMER_MEM_MINBYTES)
918 } else if (value < HAMMER_MEM_MINBYTES) {
919 value = HAMMER_MEM_MINBYTES;