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) {
325 raw_offset += HAMMER_BUFSIZE;
328 buf_offset = HAMMER_VOL_ENCODE(vol->vol_no) |
329 HAMMER_ZONE_RAW_BUFFER |
330 (raw_offset - vol->ondisk->vol_buf_beg);
331 hi = buffer_hash(raw_offset);
332 TAILQ_FOREACH(buf, &vol->buffer_lists[hi], entry) {
333 if (buf->raw_offset == raw_offset)
337 buf = get_buffer(buf_offset, -1);
341 raw_offset += HAMMER_BUFSIZE;
346 rel_buffer(struct buffer_info *buffer)
348 struct volume_info *volume;
353 assert(buffer->cache.refs > 0);
354 if (--buffer->cache.refs == 0) {
355 if (buffer->cache.delete) {
356 hi = buffer_hash(buffer->buf_offset);
357 volume = buffer->volume;
358 if (buffer->cache.modified)
359 flush_buffer(buffer);
360 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
361 hammer_cache_del(&buffer->cache);
362 free(buffer->ondisk);
370 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
371 * bufferp is freed if isnew or the offset is out of range of the cached data.
372 * If bufferp is freed a referenced buffer is loaded into it.
375 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
378 if (*bufferp != NULL) {
380 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
381 rel_buffer(*bufferp);
385 return(get_ondisk(buf_offset, bufferp, isnew));
389 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
390 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
393 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
395 if (*bufferp != NULL) {
396 rel_buffer(*bufferp);
399 return(get_ondisk(node_offset, bufferp, 0));
403 * Return a pointer to a buffer data given a buffer offset.
404 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
408 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
411 struct buffer_info *buffer;
414 if (buffer == NULL) {
415 buffer = *bufferp = get_buffer(buf_offset, isnew);
420 return((char *)buffer->ondisk +
421 ((int32_t)buf_offset & HAMMER_BUFMASK));
425 * Allocate HAMMER elements - btree nodes, meta data, data storage
428 alloc_btree_element(hammer_off_t *offp)
430 struct buffer_info *buffer = NULL;
431 hammer_node_ondisk_t node;
433 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
435 bzero(node, sizeof(*node));
436 /* XXX buffer not released, pointer remains valid */
441 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
442 struct buffer_info **data_bufferp)
446 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
448 bzero(data, data_len);
453 alloc_data_element(hammer_off_t *offp, int32_t data_len,
454 struct buffer_info **data_bufferp)
458 if (data_len >= HAMMER_BUFSIZE) {
459 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
460 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
462 bzero(data, data_len);
463 } else if (data_len) {
464 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
466 bzero(data, data_len);
474 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
475 * code will load each volume's freemap.
478 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
480 struct buffer_info *buffer = NULL;
481 hammer_off_t layer1_offset;
482 struct hammer_blockmap_layer1 *layer1;
485 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
486 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
487 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
488 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
490 bzero(layer1, sizeof(*layer1));
491 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
492 layer1->blocks_free = 0;
493 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
497 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
498 blockmap->phys_offset = layer1_offset;
499 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
500 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
501 blockmap->reserved01 = 0;
502 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
503 root_vol->cache.modified = 1;
507 * Load the volume's remaining free space into the freemap.
509 * Returns the number of bigblocks available.
512 initialize_freemap(struct volume_info *vol)
514 struct volume_info *root_vol;
515 struct buffer_info *buffer1 = NULL;
516 struct buffer_info *buffer2 = NULL;
517 struct hammer_blockmap_layer1 *layer1;
518 struct hammer_blockmap_layer2 *layer2;
519 hammer_off_t layer1_base;
520 hammer_off_t layer1_offset;
521 hammer_off_t layer2_offset;
522 hammer_off_t phys_offset;
523 hammer_off_t aligned_vol_free_end;
527 root_vol = get_volume(RootVolNo);
528 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
529 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
531 printf("initialize freemap volume %d\n", vol->vol_no);
534 * Initialize the freemap. First preallocate the bigblocks required
535 * to implement layer2. This preallocation is a bootstrap allocation
536 * using blocks from the target volume.
538 layer1_base = root_vol->ondisk->vol0_blockmap[
539 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
540 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
541 phys_offset < aligned_vol_free_end;
542 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
543 layer1_offset = layer1_base +
544 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
545 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
546 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
547 layer1->phys_offset = alloc_bigblock(vol,
548 HAMMER_ZONE_FREEMAP_INDEX);
549 layer1->blocks_free = 0;
550 buffer1->cache.modified = 1;
551 layer1->layer1_crc = crc32(layer1,
552 HAMMER_LAYER1_CRCSIZE);
557 * Now fill everything in.
559 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
560 phys_offset < aligned_vol_free_end;
561 phys_offset += HAMMER_BIGBLOCK_SIZE) {
563 layer1_offset = layer1_base +
564 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
565 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
567 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
568 layer2_offset = layer1->phys_offset +
569 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
571 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
572 bzero(layer2, sizeof(*layer2));
573 if (phys_offset < vol->vol_free_off) {
575 * Fixups XXX - bigblocks already allocated as part
576 * of the freemap bootstrap.
578 if (layer2->zone == 0) {
579 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
580 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
581 layer2->bytes_free = 0;
583 } else if (phys_offset < vol->vol_free_end) {
584 ++layer1->blocks_free;
585 buffer1->cache.modified = 1;
587 layer2->append_off = 0;
588 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
592 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
593 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
594 layer2->bytes_free = 0;
596 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
597 buffer2->cache.modified = 1;
603 layer1->layer1_crc = crc32(layer1,
604 HAMMER_LAYER1_CRCSIZE);
605 buffer1->cache.modified = 1;
610 rel_volume(root_vol);
615 * Allocate big-blocks using our poor-man's volume->vol_free_off.
617 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
618 * itself and cannot update it yet.
621 alloc_bigblock(struct volume_info *volume, int zone)
623 struct buffer_info *buffer1 = NULL;
624 struct buffer_info *buffer2 = NULL;
625 struct volume_info *root_vol;
626 hammer_off_t result_offset;
627 hammer_off_t layer_offset;
628 struct hammer_blockmap_layer1 *layer1;
629 struct hammer_blockmap_layer2 *layer2;
632 volume = get_volume(RootVolNo);
634 result_offset = volume->vol_free_off;
635 if (result_offset >= volume->vol_free_end)
636 panic("alloc_bigblock: Ran out of room, filesystem too small");
637 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
640 * Update the freemap.
642 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
643 root_vol = get_volume(RootVolNo);
644 layer_offset = root_vol->ondisk->vol0_blockmap[
645 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
646 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
647 layer1 = get_buffer_data(layer_offset, &buffer1, 0);
648 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
649 --layer1->blocks_free;
650 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
651 buffer1->cache.modified = 1;
652 layer_offset = layer1->phys_offset +
653 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
654 layer2 = get_buffer_data(layer_offset, &buffer2, 0);
655 assert(layer2->zone == 0);
657 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
658 layer2->bytes_free = 0;
659 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
660 buffer2->cache.modified = 1;
662 --root_vol->ondisk->vol0_stat_freebigblocks;
663 root_vol->cache.modified = 1;
667 rel_volume(root_vol);
671 return(result_offset);
675 * Format the undo-map for the root volume.
678 format_undomap(hammer_volume_ondisk_t ondisk)
680 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
681 hammer_off_t undo_limit;
682 hammer_blockmap_t blockmap;
683 struct buffer_info *buffer = NULL;
690 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
691 * up to HAMMER_UNDO_LAYER2 big blocks. Size to approximately
694 * The minimum UNDO fifo size is 500MB, or approximately 1% of
695 * the recommended 50G disk.
697 * Changing this minimum is rather dangerous as complex filesystem
698 * operations can cause the UNDO FIFO to fill up otherwise.
700 undo_limit = UndoBufferSize;
701 if (undo_limit == 0) {
702 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
703 if (undo_limit < 500*1024*1024)
704 undo_limit = 500*1024*1024;
706 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
707 ~HAMMER_BIGBLOCK_MASK64;
708 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
709 undo_limit = HAMMER_BIGBLOCK_SIZE;
710 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
711 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
712 UndoBufferSize = undo_limit;
714 blockmap = &ondisk->vol0_blockmap[undo_zone];
715 bzero(blockmap, sizeof(*blockmap));
716 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
717 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
718 blockmap->next_offset = blockmap->first_offset;
719 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
720 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
723 scan = blockmap->next_offset;
724 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
726 assert(limit_index <= HAMMER_UNDO_LAYER2);
728 for (n = 0; n < limit_index; ++n) {
729 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
730 HAMMER_ZONE_UNDO_INDEX);
731 scan += HAMMER_BIGBLOCK_SIZE;
733 while (n < HAMMER_UNDO_LAYER2) {
734 ondisk->vol0_undo_array[n] = HAMMER_BLOCKMAP_UNAVAIL;
739 * Pre-initialize the UNDO blocks (HAMMER version 4+)
741 printf("initializing the undo map (%jd MB)\n",
742 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
745 scan = blockmap->first_offset;
748 while (scan < blockmap->alloc_offset) {
749 hammer_fifo_head_t head;
750 hammer_fifo_tail_t tail;
752 int bytes = HAMMER_UNDO_ALIGN;
754 isnew = ((scan & HAMMER_BUFMASK64) == 0);
755 head = get_buffer_data(scan, &buffer, isnew);
756 buffer->cache.modified = 1;
757 tail = (void *)((char *)head + bytes - sizeof(*tail));
760 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
761 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
762 head->hdr_size = bytes;
763 head->hdr_seq = seqno++;
765 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
766 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
767 tail->tail_size = bytes;
769 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
770 crc32(head + 1, bytes - sizeof(*head));
778 * Format a new blockmap. This is mostly a degenerate case because
779 * all allocations are now actually done from the freemap.
782 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base)
784 blockmap->phys_offset = 0;
785 blockmap->alloc_offset = zone_base | HAMMER_VOL_ENCODE(255) |
786 HAMMER_SHORT_OFF_ENCODE(-1);
787 blockmap->first_offset = zone_base;
788 blockmap->next_offset = zone_base;
789 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
793 * Allocate a chunk of data out of a blockmap. This is a simplified
794 * version which uses next_offset as a simple allocation iterator.
798 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
799 struct buffer_info **bufferp)
801 struct buffer_info *buffer1 = NULL;
802 struct buffer_info *buffer2 = NULL;
803 struct volume_info *volume;
804 hammer_blockmap_t blockmap;
805 hammer_blockmap_t freemap;
806 struct hammer_blockmap_layer1 *layer1;
807 struct hammer_blockmap_layer2 *layer2;
808 hammer_off_t layer1_offset;
809 hammer_off_t layer2_offset;
810 hammer_off_t zone2_offset;
813 volume = get_volume(RootVolNo);
815 blockmap = &volume->ondisk->vol0_blockmap[zone];
816 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
819 * Alignment and buffer-boundary issues. If the allocation would
820 * cross a buffer boundary we have to skip to the next buffer.
822 bytes = (bytes + 15) & ~15;
825 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
827 volume->cache.modified = 1;
828 blockmap->next_offset = (blockmap->next_offset + bytes) &
833 * Dive layer 1. For now we can't allocate data outside of volume 0.
835 layer1_offset = freemap->phys_offset +
836 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
838 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
840 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
841 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
848 layer2_offset = layer1->phys_offset +
849 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
851 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
853 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
854 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
859 * If we are entering a new bigblock assign ownership to our
860 * zone. If the bigblock is owned by another zone skip it.
862 if (layer2->zone == 0) {
863 --layer1->blocks_free;
865 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
866 assert(layer2->append_off == 0);
868 if (layer2->zone != zone) {
869 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
870 ~HAMMER_BIGBLOCK_MASK64;
874 buffer1->cache.modified = 1;
875 buffer2->cache.modified = 1;
876 volume->cache.modified = 1;
877 assert(layer2->append_off ==
878 (blockmap->next_offset & HAMMER_BIGBLOCK_MASK));
879 layer2->bytes_free -= bytes;
880 *result_offp = blockmap->next_offset;
881 blockmap->next_offset += bytes;
882 layer2->append_off = (int)blockmap->next_offset &
883 HAMMER_BIGBLOCK_MASK;
885 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
886 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
888 zone2_offset = (*result_offp & ~HAMMER_OFF_ZONE_MASK) |
889 HAMMER_ZONE_ENCODE(zone, 0);
891 ptr = get_buffer_data(zone2_offset, bufferp, 0);
892 (*bufferp)->cache.modified = 1;
901 * Flush various tracking structures to disk
905 * Flush various tracking structures to disk
908 flush_all_volumes(void)
910 struct volume_info *vol;
912 TAILQ_FOREACH(vol, &VolList, entry)
917 flush_volume(struct volume_info *volume)
919 struct buffer_info *buffer;
922 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
923 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
924 flush_buffer(buffer);
926 writehammerbuf(volume, volume->ondisk, 0);
927 volume->cache.modified = 0;
931 flush_buffer(struct buffer_info *buffer)
933 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
934 buffer->cache.modified = 0;
939 * Generic buffer initialization
942 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
944 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
945 head->hdr_type = hdr_type;
955 * Core I/O operations
958 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
962 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
963 if (n != HAMMER_BUFSIZE)
964 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
970 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
974 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
975 if (n != HAMMER_BUFSIZE)
976 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
980 panic(const char *ctl, ...)
985 vfprintf(stderr, ctl, va);
987 fprintf(stderr, "\n");