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 check_volume(struct volume_info *vol);
41 static void get_buffer_readahead(struct buffer_info *base);
42 static __inline int readhammervol(struct volume_info *vol);
43 static __inline int readhammerbuf(struct buffer_info *buf);
44 static __inline int writehammervol(struct volume_info *vol);
45 static __inline int writehammerbuf(struct buffer_info *buf);
49 int UseReadBehind = -4;
53 TAILQ_HEAD(volume_list, volume_info);
54 static 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(hammer_off_t buf_offset)
70 struct volume_info *volume;
71 struct buffer_info *buf;
74 volume = get_volume(HAMMER_VOL_DECODE(buf_offset));
77 hi = buffer_hash(buf_offset);
78 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
79 if (buf->buf_offset == buf_offset)
86 __alloc_volume(const char *volname, int oflags)
88 struct volume_info *vol;
91 vol = malloc(sizeof(*vol));
93 err(1, "alloc_volume");
94 bzero(vol, sizeof(*vol));
97 vol->rdonly = (oflags == O_RDONLY);
98 vol->name = strdup(volname);
99 vol->fd = open(vol->name, oflags);
101 err(1, "alloc_volume: Failed to open %s", vol->name);
104 vol->ondisk = malloc(HAMMER_BUFSIZE);
105 if (vol->ondisk == NULL)
106 err(1, "alloc_volume");
107 bzero(vol->ondisk, HAMMER_BUFSIZE);
109 for (i = 0; i < HAMMER_BUFLISTS; ++i)
110 TAILQ_INIT(&vol->buffer_lists[i]);
116 __add_volume(struct volume_info *vol)
118 struct volume_info *scan;
119 struct stat st1, st2;
121 if (fstat(vol->fd, &st1) != 0)
122 errx(1, "add_volume: %s: Failed to stat", vol->name);
124 TAILQ_FOREACH(scan, &VolList, entry) {
125 if (scan->vol_no == vol->vol_no) {
126 errx(1, "add_volume: %s: Duplicate volume number %d "
128 vol->name, vol->vol_no, scan->name);
130 if (fstat(scan->fd, &st2) != 0) {
131 errx(1, "add_volume: %s: Failed to stat %s",
132 vol->name, scan->name);
134 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
135 errx(1, "add_volume: %s: Specified more than once",
140 TAILQ_INSERT_TAIL(&VolList, vol, entry);
144 __verify_volume(struct volume_info *vol)
146 hammer_volume_ondisk_t ondisk = vol->ondisk;
148 if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) {
149 errx(1, "verify_volume: Invalid volume signature %016jx",
150 ondisk->vol_signature);
152 if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) {
153 errx(1, "verify_volume: Invalid root volume# %d",
154 ondisk->vol_rootvol);
156 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType))) {
157 errx(1, "verify_volume: %s: Header does not indicate "
158 "that this is a HAMMER volume", vol->name);
160 if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId))) {
161 errx(1, "verify_volume: %s: FSId does not match other volumes!",
167 * Initialize a volume structure and ondisk vol_no field.
170 init_volume(const char *filename, int oflags, int32_t vol_no)
172 struct volume_info *vol;
174 vol = __alloc_volume(filename, oflags);
175 vol->vol_no = vol->ondisk->vol_no = vol_no;
183 * Initialize a volume structure and read ondisk volume header.
186 load_volume(const char *filename, int oflags, int verify)
188 struct volume_info *vol;
191 vol = __alloc_volume(filename, oflags);
193 n = readhammervol(vol);
195 err(1, "load_volume: %s: Read failed at offset 0", vol->name);
197 vol->vol_no = vol->ondisk->vol_no;
199 if (valid_hammer_volumes++ == 0)
200 Hammer_FSId = vol->ondisk->vol_fsid;
202 __verify_volume(vol);
210 * Check basic volume characteristics.
213 check_volume(struct volume_info *vol)
215 struct partinfo pinfo;
219 * Get basic information about the volume
221 if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
223 * Allow the formatting of regular files as HAMMER volumes
225 if (fstat(vol->fd, &st) < 0)
226 err(1, "Unable to stat %s", vol->name);
227 vol->size = st.st_size;
228 vol->type = "REGFILE";
231 * When formatting a block device as a HAMMER volume the
232 * sector size must be compatible. HAMMER uses 16384 byte
233 * filesystem buffers.
235 if (pinfo.reserved_blocks) {
236 errx(1, "HAMMER cannot be placed in a partition "
237 "which overlaps the disklabel or MBR");
239 if (pinfo.media_blksize > HAMMER_BUFSIZE ||
240 HAMMER_BUFSIZE % pinfo.media_blksize) {
241 errx(1, "A media sector size of %d is not supported",
242 pinfo.media_blksize);
245 vol->size = pinfo.media_size;
246 vol->device_offset = pinfo.media_offset;
247 vol->type = "DEVICE";
252 assert_volume_offset(struct volume_info *vol)
254 assert(hammer_is_zone_raw_buffer(vol->vol_free_off));
255 assert(hammer_is_zone_raw_buffer(vol->vol_free_end));
256 if (vol->vol_free_off >= vol->vol_free_end)
257 errx(1, "Ran out of room, filesystem too small");
261 get_volume(int32_t vol_no)
263 struct volume_info *vol;
265 TAILQ_FOREACH(vol, &VolList, entry) {
266 if (vol->vol_no == vol_no)
274 get_root_volume(void)
276 return(get_volume(HAMMER_ROOT_VOLNO));
279 static struct buffer_info *
280 __alloc_buffer(hammer_off_t buf_offset, int isnew)
282 struct volume_info *volume;
283 struct buffer_info *buf;
286 volume = get_volume(HAMMER_VOL_DECODE(buf_offset));
287 assert(volume != NULL);
289 buf = calloc(sizeof(*buf), 1);
290 buf->buf_offset = buf_offset;
291 buf->raw_offset = hammer_xlate_to_phys(volume->ondisk, buf_offset);
292 buf->volume = volume;
293 buf->ondisk = calloc(HAMMER_BUFSIZE, 1);
296 if (readhammerbuf(buf) == -1) {
297 err(1, "Failed to read %s:%016jx at %016jx",
299 (intmax_t)buf->buf_offset,
300 (intmax_t)buf->raw_offset);
304 hi = buffer_hash(buf_offset);
305 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
306 hammer_cache_add(&buf->cache);
312 * Acquire the specified buffer. isnew is -1 only when called
313 * via get_buffer_readahead() to prevent another readahead.
315 static struct buffer_info *
316 get_buffer(hammer_off_t buf_offset, int isnew)
318 struct buffer_info *buf;
323 zone = HAMMER_ZONE_DECODE(buf_offset);
324 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX)
325 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, &error);
326 if (error || buf_offset == HAMMER_OFF_BAD)
328 assert(hammer_is_zone_raw_buffer(buf_offset));
330 buf_offset &= ~HAMMER_BUFMASK64;
331 buf = find_buffer(buf_offset);
334 buf = __alloc_buffer(buf_offset, isnew);
338 hammer_cache_used(&buf->cache);
340 assert(buf->ondisk != NULL);
343 hammer_cache_flush();
346 assert(buf->cache.modified == 0);
347 bzero(buf->ondisk, HAMMER_BUFSIZE);
348 buf->cache.modified = 1;
351 get_buffer_readahead(buf);
356 get_buffer_readahead(struct buffer_info *base)
358 struct buffer_info *buf;
359 struct volume_info *vol;
360 hammer_off_t buf_offset;
362 int ri = UseReadBehind;
363 int re = UseReadAhead;
365 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
369 if (raw_offset >= vol->ondisk->vol_buf_end)
371 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
373 raw_offset += HAMMER_BUFSIZE;
376 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
377 raw_offset - vol->ondisk->vol_buf_beg);
378 buf = find_buffer(buf_offset);
380 buf = get_buffer(buf_offset, -1);
384 raw_offset += HAMMER_BUFSIZE;
389 rel_buffer(struct buffer_info *buffer)
391 struct volume_info *volume;
396 assert(buffer->cache.refs > 0);
397 if (--buffer->cache.refs == 0) {
398 if (buffer->cache.delete) {
399 hi = buffer_hash(buffer->buf_offset);
400 volume = buffer->volume;
401 if (buffer->cache.modified)
402 flush_buffer(buffer);
403 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
404 hammer_cache_del(&buffer->cache);
405 free(buffer->ondisk);
412 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
413 * bufferp is freed if isnew or the offset is out of range of the cached data.
414 * If bufferp is freed a referenced buffer is loaded into it.
417 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
420 if (*bufferp != NULL) {
422 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
423 rel_buffer(*bufferp);
428 if (*bufferp == NULL) {
429 *bufferp = get_buffer(buf_offset, isnew);
430 if (*bufferp == NULL)
434 return(((char *)(*bufferp)->ondisk) +
435 ((int32_t)buf_offset & HAMMER_BUFMASK));
439 * Allocate HAMMER elements - B-Tree nodes
442 alloc_btree_node(hammer_off_t *offp, struct buffer_info **data_bufferp)
444 hammer_node_ondisk_t node;
446 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
448 bzero(node, sizeof(*node));
453 * Allocate HAMMER elements - meta data (inode, direntry, PFS, etc)
456 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
457 struct buffer_info **data_bufferp)
461 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
463 bzero(data, data_len);
468 * Format a new blockmap. This is mostly a degenerate case because
469 * all allocations are now actually done from the freemap.
472 format_blockmap(struct volume_info *root_vol, int zone, hammer_off_t offset)
474 hammer_blockmap_t blockmap;
475 hammer_off_t zone_base;
477 /* Only root volume needs formatting */
478 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
480 assert(hammer_is_zone2_mapped_index(zone));
482 blockmap = &root_vol->ondisk->vol0_blockmap[zone];
483 zone_base = HAMMER_ZONE_ENCODE(zone, offset);
485 bzero(blockmap, sizeof(*blockmap));
486 blockmap->phys_offset = 0;
487 blockmap->first_offset = zone_base;
488 blockmap->next_offset = zone_base;
489 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
490 hammer_crc_set_blockmap(blockmap);
494 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
495 * code will load each volume's freemap.
498 format_freemap(struct volume_info *root_vol)
500 struct buffer_info *buffer = NULL;
501 hammer_off_t layer1_offset;
502 hammer_blockmap_t blockmap;
503 hammer_blockmap_layer1_t layer1;
506 /* Only root volume needs formatting */
507 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
509 layer1_offset = bootstrap_bigblock(root_vol);
510 for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
511 isnew = ((i % HAMMER_BUFSIZE) == 0);
512 layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
513 bzero(layer1, sizeof(*layer1));
514 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
515 layer1->blocks_free = 0;
516 hammer_crc_set_layer1(layer1);
518 assert(i == HAMMER_BIGBLOCK_SIZE);
521 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
522 bzero(blockmap, sizeof(*blockmap));
523 blockmap->phys_offset = layer1_offset;
524 blockmap->first_offset = 0;
525 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
526 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
527 hammer_crc_set_blockmap(blockmap);
531 * Load the volume's remaining free space into the freemap.
533 * Returns the number of big-blocks available.
536 initialize_freemap(struct volume_info *vol)
538 struct volume_info *root_vol;
539 struct buffer_info *buffer1 = NULL;
540 struct buffer_info *buffer2 = NULL;
541 hammer_blockmap_layer1_t layer1;
542 hammer_blockmap_layer2_t layer2;
543 hammer_off_t layer1_offset;
544 hammer_off_t layer2_offset;
545 hammer_off_t phys_offset;
546 hammer_off_t block_offset;
547 hammer_off_t aligned_vol_free_end;
548 hammer_blockmap_t freemap;
550 int64_t layer1_count = 0;
552 root_vol = get_root_volume();
554 assert_volume_offset(vol);
555 aligned_vol_free_end = HAMMER_BLOCKMAP_LAYER2_DOALIGN(vol->vol_free_end);
557 printf("initialize freemap volume %d\n", vol->vol_no);
560 * Initialize the freemap. First preallocate the big-blocks required
561 * to implement layer2. This preallocation is a bootstrap allocation
562 * using blocks from the target volume.
564 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
566 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
567 phys_offset < aligned_vol_free_end;
568 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
569 layer1_offset = freemap->phys_offset +
570 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
571 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
572 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
573 layer1->phys_offset = bootstrap_bigblock(vol);
574 layer1->blocks_free = 0;
575 buffer1->cache.modified = 1;
576 hammer_crc_set_layer1(layer1);
581 * Now fill everything in.
583 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
584 phys_offset < aligned_vol_free_end;
585 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
587 layer1_offset = freemap->phys_offset +
588 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
589 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
590 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
592 for (block_offset = 0;
593 block_offset < HAMMER_BLOCKMAP_LAYER2;
594 block_offset += HAMMER_BIGBLOCK_SIZE) {
595 layer2_offset = layer1->phys_offset +
596 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
597 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
598 bzero(layer2, sizeof(*layer2));
600 if (phys_offset + block_offset < vol->vol_free_off) {
602 * Big-blocks already allocated as part
603 * of the freemap bootstrap.
605 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
606 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
607 layer2->bytes_free = 0;
608 } else if (phys_offset + block_offset < vol->vol_free_end) {
610 layer2->append_off = 0;
611 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
615 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
616 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
617 layer2->bytes_free = 0;
619 hammer_crc_set_layer2(layer2);
620 buffer2->cache.modified = 1;
623 layer1->blocks_free += layer1_count;
624 hammer_crc_set_layer1(layer1);
625 buffer1->cache.modified = 1;
634 * Returns the number of big-blocks available for filesystem data and undos
635 * without formatting.
638 count_freemap(struct volume_info *vol)
640 hammer_off_t phys_offset;
641 hammer_off_t vol_free_off;
642 hammer_off_t aligned_vol_free_end;
645 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
647 assert_volume_offset(vol);
648 aligned_vol_free_end = HAMMER_BLOCKMAP_LAYER2_DOALIGN(vol->vol_free_end);
650 if (vol->vol_no == HAMMER_ROOT_VOLNO)
651 vol_free_off += HAMMER_BIGBLOCK_SIZE;
653 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
654 phys_offset < aligned_vol_free_end;
655 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
656 vol_free_off += HAMMER_BIGBLOCK_SIZE;
659 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
660 phys_offset < aligned_vol_free_end;
661 phys_offset += HAMMER_BIGBLOCK_SIZE) {
662 if (phys_offset < vol_free_off) {
664 } else if (phys_offset < vol->vol_free_end) {
673 * Format the undomap for the root volume.
676 format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size)
678 hammer_off_t undo_limit;
679 hammer_blockmap_t blockmap;
680 hammer_volume_ondisk_t ondisk;
681 struct buffer_info *buffer = NULL;
687 /* Only root volume needs formatting */
688 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
689 ondisk = root_vol->ondisk;
692 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
693 * up to HAMMER_MAX_UNDO_BIGBLOCKS big-blocks.
694 * Size to approximately 0.1% of the disk.
696 * The minimum UNDO fifo size is 512MB, or approximately 1% of
697 * the recommended 50G disk.
699 * Changing this minimum is rather dangerous as complex filesystem
700 * operations can cause the UNDO FIFO to fill up otherwise.
702 undo_limit = *undo_buffer_size;
703 if (undo_limit == 0) {
704 undo_limit = HAMMER_VOL_BUF_SIZE(ondisk) / 1000;
705 if (undo_limit < HAMMER_BIGBLOCK_SIZE * HAMMER_MIN_UNDO_BIGBLOCKS)
706 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_MIN_UNDO_BIGBLOCKS;
708 undo_limit = HAMMER_BIGBLOCK_DOALIGN(undo_limit);
709 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
710 undo_limit = HAMMER_BIGBLOCK_SIZE;
711 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_MAX_UNDO_BIGBLOCKS)
712 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_MAX_UNDO_BIGBLOCKS;
713 *undo_buffer_size = undo_limit;
715 blockmap = &ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
716 bzero(blockmap, sizeof(*blockmap));
717 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
718 blockmap->first_offset = HAMMER_ENCODE_UNDO(0);
719 blockmap->next_offset = blockmap->first_offset;
720 blockmap->alloc_offset = HAMMER_ENCODE_UNDO(undo_limit);
721 hammer_crc_set_blockmap(blockmap);
723 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
724 assert(limit_index <= HAMMER_MAX_UNDO_BIGBLOCKS);
726 for (n = 0; n < limit_index; ++n) {
727 ondisk->vol0_undo_array[n] = alloc_undo_bigblock(root_vol);
729 while (n < HAMMER_MAX_UNDO_BIGBLOCKS) {
730 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
734 * Pre-initialize the UNDO blocks (HAMMER version 4+)
736 printf("initializing the undo map (%jd MB)\n",
737 (intmax_t)HAMMER_OFF_LONG_ENCODE(blockmap->alloc_offset) /
740 scan = blockmap->first_offset;
743 while (scan < blockmap->alloc_offset) {
744 hammer_fifo_head_t head;
745 hammer_fifo_tail_t tail;
747 int bytes = HAMMER_UNDO_ALIGN;
749 isnew = ((scan & HAMMER_BUFMASK64) == 0);
750 head = get_buffer_data(scan, &buffer, isnew);
751 buffer->cache.modified = 1;
752 tail = (void *)((char *)head + bytes - sizeof(*tail));
755 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
756 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
757 head->hdr_size = bytes;
758 head->hdr_seq = seqno++;
760 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
761 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
762 tail->tail_size = bytes;
764 hammer_crc_set_fifo_head(head, bytes);
771 const char *zone_labels[] = {
773 "raw_volume", /* 1 */
774 "raw_buffer", /* 2 */
782 "large_data", /* 10 */
783 "small_data", /* 11 */
791 print_blockmap(const struct volume_info *vol)
793 hammer_blockmap_t blockmap;
794 hammer_volume_ondisk_t ondisk;
799 ondisk = vol->ondisk;
800 printf(INDENT"vol_label\t%s\n", ondisk->vol_label);
801 printf(INDENT"vol_count\t%d\n", ondisk->vol_count);
802 printf(INDENT"vol_bot_beg\t%s\n", sizetostr(ondisk->vol_bot_beg));
803 printf(INDENT"vol_mem_beg\t%s\n", sizetostr(ondisk->vol_mem_beg));
804 printf(INDENT"vol_buf_beg\t%s\n", sizetostr(ondisk->vol_buf_beg));
805 printf(INDENT"vol_buf_end\t%s\n", sizetostr(ondisk->vol_buf_end));
806 printf(INDENT"vol0_next_tid\t%016jx\n",
807 (uintmax_t)ondisk->vol0_next_tid);
809 blockmap = &ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
810 size = HAMMER_OFF_LONG_ENCODE(blockmap->alloc_offset);
811 if (blockmap->first_offset <= blockmap->next_offset)
812 used = blockmap->next_offset - blockmap->first_offset;
814 used = blockmap->alloc_offset - blockmap->first_offset +
815 HAMMER_OFF_LONG_ENCODE(blockmap->next_offset);
816 printf(INDENT"undo_size\t%s\n", sizetostr(size));
817 printf(INDENT"undo_used\t%s\n", sizetostr(used));
819 printf(INDENT"zone # "
820 "phys first next alloc\n");
821 for (i = 0; i < HAMMER_MAX_ZONES; i++) {
822 blockmap = &ondisk->vol0_blockmap[i];
823 printf(INDENT"zone %-2d %-10s %016jx %016jx %016jx %016jx\n",
825 (uintmax_t)blockmap->phys_offset,
826 (uintmax_t)blockmap->first_offset,
827 (uintmax_t)blockmap->next_offset,
828 (uintmax_t)blockmap->alloc_offset);
833 * Flush various tracking structures to disk
836 flush_all_volumes(void)
838 struct volume_info *vol;
840 TAILQ_FOREACH(vol, &VolList, entry)
845 flush_volume(struct volume_info *volume)
847 struct buffer_info *buffer;
850 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
851 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
852 flush_buffer(buffer);
854 if (writehammervol(volume) == -1)
855 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
859 flush_buffer(struct buffer_info *buffer)
861 struct volume_info *vol;
863 vol = buffer->volume;
864 if (writehammerbuf(buffer) == -1)
865 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
866 buffer->cache.modified = 0;
870 * Core I/O operations
873 __read(struct volume_info *vol, void *data, int64_t offset, int size)
877 n = pread(vol->fd, data, size, offset);
884 readhammervol(struct volume_info *vol)
886 return(__read(vol, vol->ondisk, 0, HAMMER_BUFSIZE));
890 readhammerbuf(struct buffer_info *buf)
892 return(__read(buf->volume, buf->ondisk, buf->raw_offset, HAMMER_BUFSIZE));
896 __write(struct volume_info *vol, const void *data, int64_t offset, int size)
903 n = pwrite(vol->fd, data, size, offset);
910 writehammervol(struct volume_info *vol)
912 return(__write(vol, vol->ondisk, 0, HAMMER_BUFSIZE));
916 writehammerbuf(struct buffer_info *buf)
918 return(__write(buf->volume, buf->ondisk, buf->raw_offset, HAMMER_BUFSIZE));
921 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
924 value = HAMMER_BOOT_NOMBYTES;
925 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
929 if (value < HAMMER_BOOT_MINBYTES) {
930 value = HAMMER_BOOT_MINBYTES;
931 } else if (value > HAMMER_BOOT_MAXBYTES) {
932 value = HAMMER_BOOT_MAXBYTES;
938 int64_t init_memory_log_size(int64_t value, off_t avg_vol_size)
941 value = HAMMER_MEM_NOMBYTES;
942 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
946 if (value < HAMMER_MEM_MINBYTES) {
947 value = HAMMER_MEM_MINBYTES;
948 } else if (value > HAMMER_MEM_MAXBYTES) {
949 value = HAMMER_MEM_MAXBYTES;