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/types.h>
45 #include "hammer_util.h"
47 static void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
48 struct buffer_info **bufferp);
49 static hammer_off_t alloc_bigblock(struct volume_info *volume, int zone);
50 static void get_buffer_readahead(struct buffer_info *base);
51 static __inline void *get_ondisk(hammer_off_t buf_offset,
52 struct buffer_info **bufferp, int isnew);
54 static void init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type);
55 static void readhammerbuf(struct volume_info *vol, void *data,
58 static void writehammerbuf(struct volume_info *vol, const void *data,
67 int64_t UndoBufferSize;
68 int UsingSuperClusters;
71 int UseReadBehind = -4;
73 int AssertOnFailure = 1;
74 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
78 buffer_hash(hammer_off_t buf_offset)
82 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
87 * Lookup the requested information structure and related on-disk buffer.
88 * Missing structures are created.
91 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
93 struct volume_info *vol;
94 struct volume_info *scan;
95 struct hammer_volume_ondisk *ondisk;
99 * Allocate the volume structure
101 vol = malloc(sizeof(*vol));
102 bzero(vol, sizeof(*vol));
103 for (i = 0; i < HAMMER_BUFLISTS; ++i)
104 TAILQ_INIT(&vol->buffer_lists[i]);
105 vol->name = strdup(filename);
106 vol->fd = open(filename, oflags);
110 err(1, "setup_volume: %s: Open failed", filename);
114 * Read or initialize the volume header
116 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
118 bzero(ondisk, HAMMER_BUFSIZE);
120 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
121 if (n != HAMMER_BUFSIZE) {
122 err(1, "setup_volume: %s: Read failed at offset 0",
125 vol_no = ondisk->vol_no;
127 RootVolNo = ondisk->vol_rootvol;
128 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
129 errx(1, "setup_volume: %s: root volume disagreement: "
131 vol->name, RootVolNo, ondisk->vol_rootvol);
134 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
135 errx(1, "setup_volume: %s: Header does not indicate "
136 "that this is a hammer volume", vol->name);
138 if (TAILQ_EMPTY(&VolList)) {
139 Hammer_FSId = vol->ondisk->vol_fsid;
140 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
141 errx(1, "setup_volume: %s: FSId does match other "
142 "volumes!", vol->name);
145 vol->vol_no = vol_no;
148 /*init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);*/
149 vol->cache.modified = 1;
153 * Link the volume structure in
155 TAILQ_FOREACH(scan, &VolList, entry) {
156 if (scan->vol_no == vol_no) {
157 errx(1, "setup_volume %s: Duplicate volume number %d "
158 "against %s", filename, vol_no, scan->name);
161 TAILQ_INSERT_TAIL(&VolList, vol, entry);
166 test_volume(int32_t vol_no)
168 struct volume_info *vol;
170 TAILQ_FOREACH(vol, &VolList, entry) {
171 if (vol->vol_no == vol_no)
177 /* not added to or removed from hammer cache */
182 get_volume(int32_t vol_no)
184 struct volume_info *vol;
186 TAILQ_FOREACH(vol, &VolList, entry) {
187 if (vol->vol_no == vol_no)
191 errx(1, "get_volume: Volume %d does not exist!", vol_no);
193 /* not added to or removed from hammer cache */
198 rel_volume(struct volume_info *volume)
202 /* not added to or removed from hammer cache */
203 --volume->cache.refs;
207 * Acquire the specified buffer.
210 get_buffer(hammer_off_t buf_offset, int isnew)
213 struct buffer_info *buf;
214 struct volume_info *volume;
215 hammer_off_t orig_offset = buf_offset;
221 zone = HAMMER_ZONE_DECODE(buf_offset);
222 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
223 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
225 if (buf_offset == HAMMER_OFF_BAD)
228 if (AssertOnFailure) {
229 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
230 HAMMER_ZONE_RAW_BUFFER);
232 vol_no = HAMMER_VOL_DECODE(buf_offset);
233 volume = test_volume(vol_no);
234 if (volume == NULL) {
236 errx(1, "get_buffer: Volume %d not found!", vol_no);
240 buf_offset &= ~HAMMER_BUFMASK64;
242 hi = buffer_hash(buf_offset);
244 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry) {
245 if (buf->buf_offset == buf_offset)
249 buf = malloc(sizeof(*buf));
250 bzero(buf, sizeof(*buf));
252 fprintf(stderr, "get_buffer: %016llx %016llx at %p\n",
253 (long long)orig_offset, (long long)buf_offset,
256 buf->buf_offset = buf_offset;
257 buf->raw_offset = volume->ondisk->vol_buf_beg +
258 (buf_offset & HAMMER_OFF_SHORT_MASK);
259 buf->volume = volume;
260 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
261 ++volume->cache.refs;
262 buf->cache.u.buffer = buf;
263 hammer_cache_add(&buf->cache, ISBUFFER);
266 buf->flags |= HAMMER_BUFINFO_READAHEAD;
269 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
270 (long long)orig_offset, (long long)buf_offset,
274 buf->flags &= ~HAMMER_BUFINFO_READAHEAD;
275 hammer_cache_used(&buf->cache);
280 hammer_cache_flush();
281 if ((ondisk = buf->ondisk) == NULL) {
282 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
284 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
286 if (n != HAMMER_BUFSIZE) {
288 err(1, "get_buffer: %s:%016llx "
289 "Read failed at offset %016llx",
291 (long long)buf->buf_offset,
292 (long long)buf->raw_offset);
293 bzero(ondisk, HAMMER_BUFSIZE);
298 bzero(ondisk, HAMMER_BUFSIZE);
299 buf->cache.modified = 1;
302 get_buffer_readahead(buf);
307 get_buffer_readahead(struct buffer_info *base)
309 struct buffer_info *buf;
310 struct volume_info *vol;
311 hammer_off_t buf_offset;
313 int ri = UseReadBehind;
314 int re = UseReadAhead;
317 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
321 if (raw_offset >= vol->ondisk->vol_buf_end)
323 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
325 raw_offset += HAMMER_BUFSIZE;
328 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
329 raw_offset - vol->ondisk->vol_buf_beg);
330 hi = buffer_hash(buf_offset);
331 TAILQ_FOREACH(buf, &vol->buffer_lists[hi], entry) {
332 if (buf->buf_offset == buf_offset)
336 buf = get_buffer(buf_offset, -1);
340 raw_offset += HAMMER_BUFSIZE;
345 rel_buffer(struct buffer_info *buffer)
347 struct volume_info *volume;
352 assert(buffer->cache.refs > 0);
353 if (--buffer->cache.refs == 0) {
354 if (buffer->cache.delete) {
355 hi = buffer_hash(buffer->buf_offset);
356 volume = buffer->volume;
357 if (buffer->cache.modified)
358 flush_buffer(buffer);
359 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
360 hammer_cache_del(&buffer->cache);
361 free(buffer->ondisk);
369 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
370 * bufferp is freed if isnew or the offset is out of range of the cached data.
371 * If bufferp is freed a referenced buffer is loaded into it.
374 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
377 if (*bufferp != NULL) {
379 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
380 rel_buffer(*bufferp);
384 return(get_ondisk(buf_offset, bufferp, isnew));
388 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
389 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
392 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
394 if (*bufferp != NULL) {
395 rel_buffer(*bufferp);
398 return(get_ondisk(node_offset, bufferp, 0));
402 * Return a pointer to a buffer data given a buffer offset.
403 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
407 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
410 struct buffer_info *buffer;
413 if (buffer == NULL) {
414 buffer = *bufferp = get_buffer(buf_offset, isnew);
419 return((char *)buffer->ondisk +
420 ((int32_t)buf_offset & HAMMER_BUFMASK));
424 * Allocate HAMMER elements - btree nodes, meta data, data storage
427 alloc_btree_element(hammer_off_t *offp,
428 struct buffer_info **data_bufferp)
430 hammer_node_ondisk_t node;
432 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
434 bzero(node, sizeof(*node));
439 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
440 struct buffer_info **data_bufferp)
444 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
446 bzero(data, data_len);
451 alloc_data_element(hammer_off_t *offp, int32_t data_len,
452 struct buffer_info **data_bufferp)
456 if (data_len >= HAMMER_BUFSIZE) {
457 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
458 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
460 bzero(data, data_len);
461 } else if (data_len) {
462 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
464 bzero(data, data_len);
472 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
473 * code will load each volume's freemap.
476 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
478 struct buffer_info *buffer = NULL;
479 hammer_off_t layer1_offset;
480 struct hammer_blockmap_layer1 *layer1;
483 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
484 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
485 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
486 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
488 bzero(layer1, sizeof(*layer1));
489 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
490 layer1->blocks_free = 0;
491 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
495 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
496 blockmap->phys_offset = layer1_offset;
497 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
498 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
499 blockmap->reserved01 = 0;
500 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
501 root_vol->cache.modified = 1;
505 * Load the volume's remaining free space into the freemap.
507 * Returns the number of bigblocks available.
510 initialize_freemap(struct volume_info *vol)
512 struct volume_info *root_vol;
513 struct buffer_info *buffer1 = NULL;
514 struct buffer_info *buffer2 = NULL;
515 struct hammer_blockmap_layer1 *layer1;
516 struct hammer_blockmap_layer2 *layer2;
517 hammer_off_t layer1_base;
518 hammer_off_t layer1_offset;
519 hammer_off_t layer2_offset;
520 hammer_off_t phys_offset;
521 hammer_off_t aligned_vol_free_end;
525 root_vol = get_volume(RootVolNo);
526 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
527 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
529 printf("initialize freemap volume %d\n", vol->vol_no);
532 * Initialize the freemap. First preallocate the bigblocks required
533 * to implement layer2. This preallocation is a bootstrap allocation
534 * using blocks from the target volume.
536 layer1_base = root_vol->ondisk->vol0_blockmap[
537 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
538 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
539 phys_offset < aligned_vol_free_end;
540 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
541 layer1_offset = layer1_base +
542 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
543 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
544 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
545 layer1->phys_offset = alloc_bigblock(vol,
546 HAMMER_ZONE_FREEMAP_INDEX);
547 layer1->blocks_free = 0;
548 buffer1->cache.modified = 1;
549 layer1->layer1_crc = crc32(layer1,
550 HAMMER_LAYER1_CRCSIZE);
555 * Now fill everything in.
557 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
558 phys_offset < aligned_vol_free_end;
559 phys_offset += HAMMER_BIGBLOCK_SIZE) {
561 layer1_offset = layer1_base +
562 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
563 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
565 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
566 layer2_offset = layer1->phys_offset +
567 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
569 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
570 bzero(layer2, sizeof(*layer2));
571 if (phys_offset < vol->vol_free_off) {
573 * Fixups XXX - bigblocks already allocated as part
574 * of the freemap bootstrap.
576 if (layer2->zone == 0) {
577 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
578 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
579 layer2->bytes_free = 0;
581 } else if (phys_offset < vol->vol_free_end) {
582 ++layer1->blocks_free;
583 buffer1->cache.modified = 1;
585 layer2->append_off = 0;
586 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
590 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
591 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
592 layer2->bytes_free = 0;
594 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
595 buffer2->cache.modified = 1;
601 layer1->layer1_crc = crc32(layer1,
602 HAMMER_LAYER1_CRCSIZE);
603 buffer1->cache.modified = 1;
608 rel_volume(root_vol);
613 * Allocate big-blocks using our poor-man's volume->vol_free_off.
615 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
616 * itself and cannot update it yet.
619 alloc_bigblock(struct volume_info *volume, int zone)
621 struct buffer_info *buffer1 = NULL;
622 struct buffer_info *buffer2 = NULL;
623 struct volume_info *root_vol;
624 hammer_off_t result_offset;
625 hammer_off_t layer_offset;
626 struct hammer_blockmap_layer1 *layer1;
627 struct hammer_blockmap_layer2 *layer2;
630 volume = get_volume(RootVolNo);
632 result_offset = volume->vol_free_off;
633 if (result_offset >= volume->vol_free_end)
634 panic("alloc_bigblock: Ran out of room, filesystem too small");
635 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
638 * Update the freemap.
640 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
641 root_vol = get_volume(RootVolNo);
642 layer_offset = root_vol->ondisk->vol0_blockmap[
643 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
644 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
645 layer1 = get_buffer_data(layer_offset, &buffer1, 0);
646 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
647 --layer1->blocks_free;
648 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
649 buffer1->cache.modified = 1;
650 layer_offset = layer1->phys_offset +
651 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
652 layer2 = get_buffer_data(layer_offset, &buffer2, 0);
653 assert(layer2->zone == 0);
655 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
656 layer2->bytes_free = 0;
657 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
658 buffer2->cache.modified = 1;
660 --root_vol->ondisk->vol0_stat_freebigblocks;
661 root_vol->cache.modified = 1;
665 rel_volume(root_vol);
669 return(result_offset);
673 * Format the undo-map for the root volume.
676 format_undomap(hammer_volume_ondisk_t ondisk)
678 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
679 hammer_off_t undo_limit;
680 hammer_blockmap_t blockmap;
681 struct buffer_info *buffer = NULL;
688 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
689 * up to HAMMER_UNDO_LAYER2 big blocks. Size to approximately
692 * The minimum UNDO fifo size is 500MB, or approximately 1% of
693 * the recommended 50G disk.
695 * Changing this minimum is rather dangerous as complex filesystem
696 * operations can cause the UNDO FIFO to fill up otherwise.
698 undo_limit = UndoBufferSize;
699 if (undo_limit == 0) {
700 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
701 if (undo_limit < 500*1024*1024)
702 undo_limit = 500*1024*1024;
704 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
705 ~HAMMER_BIGBLOCK_MASK64;
706 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
707 undo_limit = HAMMER_BIGBLOCK_SIZE;
708 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
709 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
710 UndoBufferSize = undo_limit;
712 blockmap = &ondisk->vol0_blockmap[undo_zone];
713 bzero(blockmap, sizeof(*blockmap));
714 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
715 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
716 blockmap->next_offset = blockmap->first_offset;
717 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
718 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
721 scan = blockmap->next_offset;
722 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
724 assert(limit_index <= HAMMER_UNDO_LAYER2);
726 for (n = 0; n < limit_index; ++n) {
727 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
728 HAMMER_ZONE_UNDO_INDEX);
729 scan += HAMMER_BIGBLOCK_SIZE;
731 while (n < HAMMER_UNDO_LAYER2) {
732 ondisk->vol0_undo_array[n] = HAMMER_BLOCKMAP_UNAVAIL;
737 * Pre-initialize the UNDO blocks (HAMMER version 4+)
739 printf("initializing the undo map (%jd MB)\n",
740 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
743 scan = blockmap->first_offset;
746 while (scan < blockmap->alloc_offset) {
747 hammer_fifo_head_t head;
748 hammer_fifo_tail_t tail;
750 int bytes = HAMMER_UNDO_ALIGN;
752 isnew = ((scan & HAMMER_BUFMASK64) == 0);
753 head = get_buffer_data(scan, &buffer, isnew);
754 buffer->cache.modified = 1;
755 tail = (void *)((char *)head + bytes - sizeof(*tail));
758 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
759 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
760 head->hdr_size = bytes;
761 head->hdr_seq = seqno++;
763 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
764 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
765 tail->tail_size = bytes;
767 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
768 crc32(head + 1, bytes - sizeof(*head));
776 * Format a new blockmap. This is mostly a degenerate case because
777 * all allocations are now actually done from the freemap.
780 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base)
782 blockmap->phys_offset = 0;
783 blockmap->alloc_offset = zone_base | HAMMER_VOL_ENCODE(255) |
784 HAMMER_SHORT_OFF_ENCODE(-1);
785 blockmap->first_offset = zone_base;
786 blockmap->next_offset = zone_base;
787 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
791 * Allocate a chunk of data out of a blockmap. This is a simplified
792 * version which uses next_offset as a simple allocation iterator.
796 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
797 struct buffer_info **bufferp)
799 struct buffer_info *buffer1 = NULL;
800 struct buffer_info *buffer2 = NULL;
801 struct volume_info *volume;
802 hammer_blockmap_t blockmap;
803 hammer_blockmap_t freemap;
804 struct hammer_blockmap_layer1 *layer1;
805 struct hammer_blockmap_layer2 *layer2;
806 hammer_off_t layer1_offset;
807 hammer_off_t layer2_offset;
808 hammer_off_t zone2_offset;
811 volume = get_volume(RootVolNo);
813 blockmap = &volume->ondisk->vol0_blockmap[zone];
814 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
817 * Alignment and buffer-boundary issues. If the allocation would
818 * cross a buffer boundary we have to skip to the next buffer.
820 bytes = (bytes + 15) & ~15;
823 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
825 volume->cache.modified = 1;
826 blockmap->next_offset = (blockmap->next_offset + bytes) &
831 * Dive layer 1. For now we can't allocate data outside of volume 0.
833 layer1_offset = freemap->phys_offset +
834 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
836 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
838 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
839 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
846 layer2_offset = layer1->phys_offset +
847 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
849 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
851 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
852 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
857 * If we are entering a new bigblock assign ownership to our
858 * zone. If the bigblock is owned by another zone skip it.
860 if (layer2->zone == 0) {
861 --layer1->blocks_free;
863 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
864 assert(layer2->append_off == 0);
866 if (layer2->zone != zone) {
867 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
868 ~HAMMER_BIGBLOCK_MASK64;
872 buffer1->cache.modified = 1;
873 buffer2->cache.modified = 1;
874 volume->cache.modified = 1;
875 assert(layer2->append_off ==
876 (blockmap->next_offset & HAMMER_BIGBLOCK_MASK));
877 layer2->bytes_free -= bytes;
878 *result_offp = blockmap->next_offset;
879 blockmap->next_offset += bytes;
880 layer2->append_off = (int)blockmap->next_offset &
881 HAMMER_BIGBLOCK_MASK;
883 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
884 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
886 zone2_offset = HAMMER_ZONE_ENCODE(zone,
887 *result_offp & ~HAMMER_OFF_ZONE_MASK);
889 ptr = get_buffer_data(zone2_offset, bufferp, 0);
890 (*bufferp)->cache.modified = 1;
899 * Flush various tracking structures to disk
902 flush_all_volumes(void)
904 struct volume_info *vol;
906 TAILQ_FOREACH(vol, &VolList, entry)
911 flush_volume(struct volume_info *volume)
913 struct buffer_info *buffer;
916 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
917 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
918 flush_buffer(buffer);
920 writehammerbuf(volume, volume->ondisk, 0);
921 volume->cache.modified = 0;
925 flush_buffer(struct buffer_info *buffer)
927 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
928 buffer->cache.modified = 0;
933 * Generic buffer initialization
936 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
938 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
939 head->hdr_type = hdr_type;
949 * Core I/O operations
952 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
956 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
957 if (n != HAMMER_BUFSIZE)
958 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
964 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
968 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
969 if (n != HAMMER_BUFSIZE)
970 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
974 panic(const char *ctl, ...)
979 vfprintf(stderr, ctl, va);
981 fprintf(stderr, "\n");