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 hammer_off_t hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
56 struct buffer_info **bufp, u_int16_t hdr_type);
57 static void readhammerbuf(struct volume_info *vol, void *data,
60 static void writehammerbuf(struct volume_info *vol, const void *data,
69 int64_t UndoBufferSize;
70 int UsingSuperClusters;
73 int UseReadBehind = -4;
75 int AssertOnFailure = 1;
76 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
80 buffer_hash(hammer_off_t buf_offset)
84 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
89 * Lookup the requested information structure and related on-disk buffer.
90 * Missing structures are created.
93 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
95 struct volume_info *vol;
96 struct volume_info *scan;
97 struct hammer_volume_ondisk *ondisk;
101 * Allocate the volume structure
103 vol = malloc(sizeof(*vol));
104 bzero(vol, sizeof(*vol));
105 for (i = 0; i < HAMMER_BUFLISTS; ++i)
106 TAILQ_INIT(&vol->buffer_lists[i]);
107 vol->name = strdup(filename);
108 vol->fd = open(filename, oflags);
112 err(1, "setup_volume: %s: Open failed", filename);
116 * Read or initialize the volume header
118 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
120 bzero(ondisk, HAMMER_BUFSIZE);
122 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
123 if (n != HAMMER_BUFSIZE) {
124 err(1, "setup_volume: %s: Read failed at offset 0",
127 vol_no = ondisk->vol_no;
129 RootVolNo = ondisk->vol_rootvol;
130 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
131 errx(1, "setup_volume: %s: root volume disagreement: "
133 vol->name, RootVolNo, ondisk->vol_rootvol);
136 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
137 errx(1, "setup_volume: %s: Header does not indicate "
138 "that this is a hammer volume", vol->name);
140 if (TAILQ_EMPTY(&VolList)) {
141 Hammer_FSId = vol->ondisk->vol_fsid;
142 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
143 errx(1, "setup_volume: %s: FSId does match other "
144 "volumes!", vol->name);
147 vol->vol_no = vol_no;
150 /*init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);*/
151 vol->cache.modified = 1;
155 * Link the volume structure in
157 TAILQ_FOREACH(scan, &VolList, entry) {
158 if (scan->vol_no == vol_no) {
159 errx(1, "setup_volume %s: Duplicate volume number %d "
160 "against %s", filename, vol_no, scan->name);
163 TAILQ_INSERT_TAIL(&VolList, vol, entry);
168 test_volume(int32_t vol_no)
170 struct volume_info *vol;
172 TAILQ_FOREACH(vol, &VolList, entry) {
173 if (vol->vol_no == vol_no)
179 /* not added to or removed from hammer cache */
184 get_volume(int32_t vol_no)
186 struct volume_info *vol;
188 TAILQ_FOREACH(vol, &VolList, entry) {
189 if (vol->vol_no == vol_no)
193 errx(1, "get_volume: Volume %d does not exist!", vol_no);
195 /* not added to or removed from hammer cache */
200 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\n",
253 (long long)orig_offset, (long long)buf_offset);
255 buf->buf_offset = buf_offset;
256 buf->raw_offset = volume->ondisk->vol_buf_beg +
257 (buf_offset & HAMMER_OFF_SHORT_MASK);
258 buf->volume = volume;
259 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
260 ++volume->cache.refs;
261 buf->cache.u.buffer = buf;
262 hammer_cache_add(&buf->cache, ISBUFFER);
265 buf->flags |= HAMMER_BUFINFO_READAHEAD;
268 buf->flags &= ~HAMMER_BUFINFO_READAHEAD;
269 hammer_cache_used(&buf->cache);
274 hammer_cache_flush();
275 if ((ondisk = buf->ondisk) == NULL) {
276 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
278 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
280 if (n != HAMMER_BUFSIZE) {
282 err(1, "get_buffer: %s:%016llx "
283 "Read failed at offset %016llx",
285 (long long)buf->buf_offset,
286 (long long)buf->raw_offset);
287 bzero(ondisk, HAMMER_BUFSIZE);
292 bzero(ondisk, HAMMER_BUFSIZE);
293 buf->cache.modified = 1;
296 get_buffer_readahead(buf);
301 get_buffer_readahead(struct buffer_info *base)
303 struct buffer_info *buf;
304 struct volume_info *vol;
305 hammer_off_t buf_offset;
307 int ri = UseReadBehind;
308 int re = UseReadAhead;
311 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
315 if (raw_offset >= vol->ondisk->vol_buf_end)
317 if (raw_offset < vol->ondisk->vol_buf_beg) {
319 raw_offset += HAMMER_BUFSIZE;
322 buf_offset = HAMMER_VOL_ENCODE(vol->vol_no) |
323 HAMMER_ZONE_RAW_BUFFER |
324 (raw_offset - vol->ondisk->vol_buf_beg);
325 hi = buffer_hash(raw_offset);
326 TAILQ_FOREACH(buf, &vol->buffer_lists[hi], entry) {
327 if (buf->raw_offset == raw_offset)
331 buf = get_buffer(buf_offset, -1);
335 raw_offset += HAMMER_BUFSIZE;
340 rel_buffer(struct buffer_info *buffer)
342 struct volume_info *volume;
345 assert(buffer->cache.refs > 0);
346 if (--buffer->cache.refs == 0) {
347 if (buffer->cache.delete) {
348 hi = buffer_hash(buffer->buf_offset);
349 volume = buffer->volume;
350 if (buffer->cache.modified)
351 flush_buffer(buffer);
352 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
353 hammer_cache_del(&buffer->cache);
354 free(buffer->ondisk);
362 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
363 * bufferp is freed if isnew or the offset is out of range of the cached data.
364 * If bufferp is freed a referenced buffer is loaded into it.
367 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
370 struct buffer_info *buffer;
372 if ((buffer = *bufferp) != NULL) {
374 ((buffer->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
376 buffer = *bufferp = NULL;
379 return get_ondisk(buf_offset, bufferp, isnew);
383 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
384 * bufp is freed if non-NULL and a referenced buffer is loaded into it.
387 get_node(hammer_off_t node_offset, struct buffer_info **bufp)
393 return get_ondisk(node_offset, bufp, 0);
397 * Return a pointer to a buffer data given a buffer offset.
398 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
402 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
405 struct buffer_info *buffer;
408 if (buffer == NULL) {
409 buffer = *bufferp = get_buffer(buf_offset, isnew);
414 return (char *)buffer->ondisk +
415 ((int32_t)buf_offset & HAMMER_BUFMASK);
419 * Allocate HAMMER elements - btree nodes, data storage, and record elements
421 * NOTE: hammer_alloc_fifo() initializes the fifo header for the returned
422 * item and zero's out the remainder, so don't bzero() it.
425 alloc_btree_element(hammer_off_t *offp)
427 struct buffer_info *buffer = NULL;
428 hammer_node_ondisk_t node;
430 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
432 bzero(node, sizeof(*node));
433 /* XXX buffer not released, pointer remains valid */
438 alloc_data_element(hammer_off_t *offp, int32_t data_len,
439 struct buffer_info **data_bufferp)
443 if (data_len >= HAMMER_BUFSIZE) {
444 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
445 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
447 bzero(data, data_len);
448 } else if (data_len) {
449 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
451 bzero(data, data_len);
459 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
460 * code will load each volume's freemap.
463 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
465 struct buffer_info *buffer = NULL;
466 hammer_off_t layer1_offset;
467 struct hammer_blockmap_layer1 *layer1;
470 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
471 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
472 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
473 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
475 bzero(layer1, sizeof(*layer1));
476 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
477 layer1->blocks_free = 0;
478 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
482 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
483 blockmap->phys_offset = layer1_offset;
484 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
485 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
486 blockmap->reserved01 = 0;
487 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
488 root_vol->cache.modified = 1;
492 * Load the volume's remaining free space into the freemap.
494 * Returns the number of bigblocks available.
497 initialize_freemap(struct volume_info *vol)
499 struct volume_info *root_vol;
500 struct buffer_info *buffer1 = NULL;
501 struct buffer_info *buffer2 = NULL;
502 struct hammer_blockmap_layer1 *layer1;
503 struct hammer_blockmap_layer2 *layer2;
504 hammer_off_t layer1_base;
505 hammer_off_t layer1_offset;
506 hammer_off_t layer2_offset;
507 hammer_off_t phys_offset;
508 hammer_off_t aligned_vol_free_end;
512 root_vol = get_volume(RootVolNo);
513 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
514 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
516 printf("initialize freemap volume %d\n", vol->vol_no);
519 * Initialize the freemap. First preallocate the bigblocks required
520 * to implement layer2. This preallocation is a bootstrap allocation
521 * using blocks from the target volume.
523 layer1_base = root_vol->ondisk->vol0_blockmap[
524 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
525 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
526 phys_offset < aligned_vol_free_end;
527 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
528 layer1_offset = layer1_base +
529 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
530 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
531 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
532 layer1->phys_offset = alloc_bigblock(vol,
533 HAMMER_ZONE_FREEMAP_INDEX);
534 layer1->blocks_free = 0;
535 buffer1->cache.modified = 1;
536 layer1->layer1_crc = crc32(layer1,
537 HAMMER_LAYER1_CRCSIZE);
542 * Now fill everything in.
544 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
545 phys_offset < aligned_vol_free_end;
546 phys_offset += HAMMER_BIGBLOCK_SIZE) {
548 layer1_offset = layer1_base +
549 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
550 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
552 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
553 layer2_offset = layer1->phys_offset +
554 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
556 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
557 bzero(layer2, sizeof(*layer2));
558 if (phys_offset < vol->vol_free_off) {
560 * Fixups XXX - bigblocks already allocated as part
561 * of the freemap bootstrap.
563 if (layer2->zone == 0) {
564 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
565 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
566 layer2->bytes_free = 0;
568 } else if (phys_offset < vol->vol_free_end) {
569 ++layer1->blocks_free;
570 buffer1->cache.modified = 1;
572 layer2->append_off = 0;
573 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
577 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
578 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
579 layer2->bytes_free = 0;
581 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
582 buffer2->cache.modified = 1;
588 layer1->layer1_crc = crc32(layer1,
589 HAMMER_LAYER1_CRCSIZE);
590 buffer1->cache.modified = 1;
595 rel_volume(root_vol);
600 * Allocate big-blocks using our poor-man's volume->vol_free_off.
602 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
603 * itself and cannot update it yet.
606 alloc_bigblock(struct volume_info *volume, int zone)
608 struct buffer_info *buffer = NULL;
609 struct volume_info *root_vol;
610 hammer_off_t result_offset;
611 hammer_off_t layer_offset;
612 struct hammer_blockmap_layer1 *layer1;
613 struct hammer_blockmap_layer2 *layer2;
616 if (volume == NULL) {
617 volume = get_volume(RootVolNo);
622 result_offset = volume->vol_free_off;
623 if (result_offset >= volume->vol_free_end)
624 panic("alloc_bigblock: Ran out of room, filesystem too small");
625 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
628 * Update the freemap.
630 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
631 root_vol = get_volume(RootVolNo);
632 layer_offset = root_vol->ondisk->vol0_blockmap[
633 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
634 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
635 layer1 = get_buffer_data(layer_offset, &buffer, 0);
636 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
637 --layer1->blocks_free;
638 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
639 buffer->cache.modified = 1;
640 layer_offset = layer1->phys_offset +
641 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
642 layer2 = get_buffer_data(layer_offset, &buffer, 0);
643 assert(layer2->zone == 0);
645 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
646 layer2->bytes_free = 0;
647 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
648 buffer->cache.modified = 1;
650 --root_vol->ondisk->vol0_stat_freebigblocks;
651 root_vol->cache.modified = 1;
654 rel_volume(root_vol);
659 return(result_offset);
663 * Format the undo-map for the root volume.
666 format_undomap(hammer_volume_ondisk_t ondisk)
668 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
669 hammer_off_t undo_limit;
670 hammer_blockmap_t blockmap;
671 struct buffer_info *buffer = NULL;
678 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
679 * up to HAMMER_UNDO_LAYER2 big blocks. Size to approximately
682 * The minimum UNDO fifo size is 500MB, or approximately 1% of
683 * the recommended 50G disk.
685 * Changing this minimum is rather dangerous as complex filesystem
686 * operations can cause the UNDO FIFO to fill up otherwise.
688 undo_limit = UndoBufferSize;
689 if (undo_limit == 0) {
690 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
691 if (undo_limit < 500*1024*1024)
692 undo_limit = 500*1024*1024;
694 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
695 ~HAMMER_BIGBLOCK_MASK64;
696 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
697 undo_limit = HAMMER_BIGBLOCK_SIZE;
698 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
699 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
700 UndoBufferSize = undo_limit;
702 blockmap = &ondisk->vol0_blockmap[undo_zone];
703 bzero(blockmap, sizeof(*blockmap));
704 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
705 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
706 blockmap->next_offset = blockmap->first_offset;
707 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
708 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
711 scan = blockmap->next_offset;
712 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
714 assert(limit_index <= HAMMER_UNDO_LAYER2);
716 for (n = 0; n < limit_index; ++n) {
717 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
718 HAMMER_ZONE_UNDO_INDEX);
719 scan += HAMMER_BIGBLOCK_SIZE;
721 while (n < HAMMER_UNDO_LAYER2) {
722 ondisk->vol0_undo_array[n] = HAMMER_BLOCKMAP_UNAVAIL;
727 * Pre-initialize the UNDO blocks (HAMMER version 4+)
729 printf("initializing the undo map (%jd MB)\n",
730 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
733 scan = blockmap->first_offset;
736 while (scan < blockmap->alloc_offset) {
737 hammer_fifo_head_t head;
738 hammer_fifo_tail_t tail;
740 int bytes = HAMMER_UNDO_ALIGN;
742 isnew = ((scan & HAMMER_BUFMASK64) == 0);
743 head = get_buffer_data(scan, &buffer, isnew);
744 buffer->cache.modified = 1;
745 tail = (void *)((char *)head + bytes - sizeof(*tail));
748 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
749 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
750 head->hdr_size = bytes;
751 head->hdr_seq = seqno++;
753 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
754 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
755 tail->tail_size = bytes;
757 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
758 crc32(head + 1, bytes - sizeof(*head));
767 * Format a new blockmap. This is mostly a degenerate case because
768 * all allocations are now actually done from the freemap.
771 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base)
773 blockmap->phys_offset = 0;
774 blockmap->alloc_offset = zone_base | HAMMER_VOL_ENCODE(255) |
775 HAMMER_SHORT_OFF_ENCODE(-1);
776 blockmap->first_offset = zone_base;
777 blockmap->next_offset = zone_base;
778 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
782 * Allocate a chunk of data out of a blockmap. This is a simplified
783 * version which uses next_offset as a simple allocation iterator.
787 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
788 struct buffer_info **bufferp)
790 struct buffer_info *buffer1 = NULL;
791 struct buffer_info *buffer2 = NULL;
792 struct volume_info *volume;
793 hammer_blockmap_t blockmap;
794 hammer_blockmap_t freemap;
795 struct hammer_blockmap_layer1 *layer1;
796 struct hammer_blockmap_layer2 *layer2;
797 hammer_off_t layer1_offset;
798 hammer_off_t layer2_offset;
799 hammer_off_t zone2_offset;
802 volume = get_volume(RootVolNo);
804 blockmap = &volume->ondisk->vol0_blockmap[zone];
805 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
808 * Alignment and buffer-boundary issues. If the allocation would
809 * cross a buffer boundary we have to skip to the next buffer.
811 bytes = (bytes + 15) & ~15;
814 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
816 volume->cache.modified = 1;
817 blockmap->next_offset = (blockmap->next_offset + bytes) &
822 * Dive layer 1. For now we can't allocate data outside of volume 0.
824 layer1_offset = freemap->phys_offset +
825 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
827 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
829 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
830 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
837 layer2_offset = layer1->phys_offset +
838 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
840 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
842 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
843 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
848 * If we are entering a new bigblock assign ownership to our
849 * zone. If the bigblock is owned by another zone skip it.
851 if (layer2->zone == 0) {
852 --layer1->blocks_free;
854 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
855 assert(layer2->append_off == 0);
857 if (layer2->zone != zone) {
858 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
859 ~HAMMER_BIGBLOCK_MASK64;
863 buffer1->cache.modified = 1;
864 buffer2->cache.modified = 1;
865 volume->cache.modified = 1;
866 assert(layer2->append_off ==
867 (blockmap->next_offset & HAMMER_BIGBLOCK_MASK));
868 layer2->bytes_free -= bytes;
869 *result_offp = blockmap->next_offset;
870 blockmap->next_offset += bytes;
871 layer2->append_off = (int)blockmap->next_offset &
872 HAMMER_BIGBLOCK_MASK;
874 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
875 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
877 zone2_offset = (*result_offp & ~HAMMER_OFF_ZONE_MASK) |
878 HAMMER_ZONE_ENCODE(zone, 0);
880 ptr = get_buffer_data(zone2_offset, bufferp, 0);
881 (*bufferp)->cache.modified = 1;
893 * Flush various tracking structures to disk
897 * Flush various tracking structures to disk
900 flush_all_volumes(void)
902 struct volume_info *vol;
904 TAILQ_FOREACH(vol, &VolList, entry)
909 flush_volume(struct volume_info *volume)
911 struct buffer_info *buffer;
914 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
915 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
916 flush_buffer(buffer);
918 writehammerbuf(volume, volume->ondisk, 0);
919 volume->cache.modified = 0;
923 flush_buffer(struct buffer_info *buffer)
925 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
926 buffer->cache.modified = 0;
931 * Generic buffer initialization
934 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
936 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
937 head->hdr_type = hdr_type;
947 * Core I/O operations
950 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
954 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
955 if (n != HAMMER_BUFSIZE)
956 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
962 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
966 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
967 if (n != HAMMER_BUFSIZE)
968 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
972 panic(const char *ctl, ...)
977 vfprintf(stderr, ctl, va);
979 fprintf(stderr, "\n");