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;
86 static struct buffer_info*
87 find_buffer(struct volume_info *volume, hammer_off_t buf_offset)
90 struct buffer_info *buf;
92 hi = buffer_hash(buf_offset);
93 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
94 if (buf->buf_offset == buf_offset)
100 * Lookup the requested information structure and related on-disk buffer.
101 * Missing structures are created.
104 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
106 struct volume_info *vol;
107 struct volume_info *scan;
108 struct hammer_volume_ondisk *ondisk;
112 * Allocate the volume structure
114 vol = malloc(sizeof(*vol));
115 bzero(vol, sizeof(*vol));
116 for (i = 0; i < HAMMER_BUFLISTS; ++i)
117 TAILQ_INIT(&vol->buffer_lists[i]);
118 vol->name = strdup(filename);
119 vol->fd = open(filename, oflags);
123 err(1, "setup_volume: %s: Open failed", filename);
127 * Read or initialize the volume header
129 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
131 bzero(ondisk, HAMMER_BUFSIZE);
133 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
134 if (n != HAMMER_BUFSIZE) {
135 err(1, "setup_volume: %s: Read failed at offset 0",
138 vol_no = ondisk->vol_no;
140 RootVolNo = ondisk->vol_rootvol;
141 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
142 errx(1, "setup_volume: %s: root volume disagreement: "
144 vol->name, RootVolNo, ondisk->vol_rootvol);
147 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
148 errx(1, "setup_volume: %s: Header does not indicate "
149 "that this is a hammer volume", vol->name);
151 if (TAILQ_EMPTY(&VolList)) {
152 Hammer_FSId = vol->ondisk->vol_fsid;
153 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
154 errx(1, "setup_volume: %s: FSId does match other "
155 "volumes!", vol->name);
158 vol->vol_no = vol_no;
161 /*init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);*/
162 vol->cache.modified = 1;
166 * Link the volume structure in
168 TAILQ_FOREACH(scan, &VolList, entry) {
169 if (scan->vol_no == vol_no) {
170 errx(1, "setup_volume %s: Duplicate volume number %d "
171 "against %s", filename, vol_no, scan->name);
174 TAILQ_INSERT_TAIL(&VolList, vol, entry);
179 test_volume(int32_t vol_no)
181 struct volume_info *vol;
183 TAILQ_FOREACH(vol, &VolList, entry) {
184 if (vol->vol_no == vol_no)
190 /* not added to or removed from hammer cache */
195 get_volume(int32_t vol_no)
197 struct volume_info *vol;
199 TAILQ_FOREACH(vol, &VolList, entry) {
200 if (vol->vol_no == vol_no)
204 errx(1, "get_volume: Volume %d does not exist!", vol_no);
206 /* not added to or removed from hammer cache */
211 rel_volume(struct volume_info *volume)
215 /* not added to or removed from hammer cache */
216 --volume->cache.refs;
220 * Acquire the specified buffer.
223 get_buffer(hammer_off_t buf_offset, int isnew)
226 struct buffer_info *buf;
227 struct volume_info *volume;
228 hammer_off_t orig_offset = buf_offset;
234 zone = HAMMER_ZONE_DECODE(buf_offset);
235 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
236 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
238 if (buf_offset == HAMMER_OFF_BAD)
241 if (AssertOnFailure) {
242 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
243 HAMMER_ZONE_RAW_BUFFER);
245 vol_no = HAMMER_VOL_DECODE(buf_offset);
246 volume = test_volume(vol_no);
247 if (volume == NULL) {
249 errx(1, "get_buffer: Volume %d not found!", vol_no);
253 buf_offset &= ~HAMMER_BUFMASK64;
254 buf = find_buffer(volume, buf_offset);
257 buf = malloc(sizeof(*buf));
258 bzero(buf, sizeof(*buf));
260 fprintf(stderr, "get_buffer: %016llx %016llx at %p\n",
261 (long long)orig_offset, (long long)buf_offset,
264 buf->buf_offset = buf_offset;
265 buf->raw_offset = volume->ondisk->vol_buf_beg +
266 (buf_offset & HAMMER_OFF_SHORT_MASK);
267 buf->volume = volume;
268 hi = buffer_hash(buf_offset);
269 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
270 ++volume->cache.refs;
271 buf->cache.u.buffer = buf;
272 hammer_cache_add(&buf->cache, ISBUFFER);
275 buf->flags |= HAMMER_BUFINFO_READAHEAD;
278 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
279 (long long)orig_offset, (long long)buf_offset,
283 buf->flags &= ~HAMMER_BUFINFO_READAHEAD;
284 hammer_cache_used(&buf->cache);
289 hammer_cache_flush();
290 if ((ondisk = buf->ondisk) == NULL) {
291 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
293 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
295 if (n != HAMMER_BUFSIZE) {
297 err(1, "get_buffer: %s:%016llx "
298 "Read failed at offset %016llx",
300 (long long)buf->buf_offset,
301 (long long)buf->raw_offset);
302 bzero(ondisk, HAMMER_BUFSIZE);
307 bzero(ondisk, HAMMER_BUFSIZE);
308 buf->cache.modified = 1;
311 get_buffer_readahead(buf);
316 get_buffer_readahead(struct buffer_info *base)
318 struct buffer_info *buf;
319 struct volume_info *vol;
320 hammer_off_t buf_offset;
322 int ri = UseReadBehind;
323 int re = UseReadAhead;
325 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
329 if (raw_offset >= vol->ondisk->vol_buf_end)
331 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
333 raw_offset += HAMMER_BUFSIZE;
336 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
337 raw_offset - vol->ondisk->vol_buf_beg);
338 buf = find_buffer(vol, buf_offset);
340 buf = get_buffer(buf_offset, -1);
344 raw_offset += HAMMER_BUFSIZE;
349 rel_buffer(struct buffer_info *buffer)
351 struct volume_info *volume;
356 assert(buffer->cache.refs > 0);
357 if (--buffer->cache.refs == 0) {
358 if (buffer->cache.delete) {
359 hi = buffer_hash(buffer->buf_offset);
360 volume = buffer->volume;
361 if (buffer->cache.modified)
362 flush_buffer(buffer);
363 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
364 hammer_cache_del(&buffer->cache);
365 free(buffer->ondisk);
373 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
374 * bufferp is freed if isnew or the offset is out of range of the cached data.
375 * If bufferp is freed a referenced buffer is loaded into it.
378 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
381 if (*bufferp != NULL) {
383 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
384 rel_buffer(*bufferp);
388 return(get_ondisk(buf_offset, bufferp, isnew));
392 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
393 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
396 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
398 if (*bufferp != NULL) {
399 rel_buffer(*bufferp);
402 return(get_ondisk(node_offset, bufferp, 0));
406 * Return a pointer to a buffer data given a buffer offset.
407 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
411 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
414 struct buffer_info *buffer;
417 if (buffer == NULL) {
418 buffer = *bufferp = get_buffer(buf_offset, isnew);
423 return((char *)buffer->ondisk +
424 ((int32_t)buf_offset & HAMMER_BUFMASK));
428 * Allocate HAMMER elements - btree nodes, meta data, data storage
431 alloc_btree_element(hammer_off_t *offp,
432 struct buffer_info **data_bufferp)
434 hammer_node_ondisk_t node;
436 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
438 bzero(node, sizeof(*node));
443 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
444 struct buffer_info **data_bufferp)
448 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
450 bzero(data, data_len);
455 alloc_data_element(hammer_off_t *offp, int32_t data_len,
456 struct buffer_info **data_bufferp)
460 if (data_len >= HAMMER_BUFSIZE) {
461 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
462 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
464 bzero(data, data_len);
465 } else if (data_len) {
466 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
468 bzero(data, data_len);
476 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
477 * code will load each volume's freemap.
480 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
482 struct buffer_info *buffer = NULL;
483 hammer_off_t layer1_offset;
484 struct hammer_blockmap_layer1 *layer1;
487 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
488 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
489 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
490 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
492 bzero(layer1, sizeof(*layer1));
493 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
494 layer1->blocks_free = 0;
495 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
499 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
500 blockmap->phys_offset = layer1_offset;
501 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
502 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
503 blockmap->reserved01 = 0;
504 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
505 root_vol->cache.modified = 1;
509 * Load the volume's remaining free space into the freemap.
511 * Returns the number of bigblocks available.
514 initialize_freemap(struct volume_info *vol)
516 struct volume_info *root_vol;
517 struct buffer_info *buffer1 = NULL;
518 struct buffer_info *buffer2 = NULL;
519 struct hammer_blockmap_layer1 *layer1;
520 struct hammer_blockmap_layer2 *layer2;
521 hammer_off_t layer1_base;
522 hammer_off_t layer1_offset;
523 hammer_off_t layer2_offset;
524 hammer_off_t phys_offset;
525 hammer_off_t aligned_vol_free_end;
529 root_vol = get_volume(RootVolNo);
530 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
531 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
533 printf("initialize freemap volume %d\n", vol->vol_no);
536 * Initialize the freemap. First preallocate the bigblocks required
537 * to implement layer2. This preallocation is a bootstrap allocation
538 * using blocks from the target volume.
540 layer1_base = root_vol->ondisk->vol0_blockmap[
541 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
542 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
543 phys_offset < aligned_vol_free_end;
544 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
545 layer1_offset = layer1_base +
546 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
547 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
548 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
549 layer1->phys_offset = alloc_bigblock(vol,
550 HAMMER_ZONE_FREEMAP_INDEX);
551 layer1->blocks_free = 0;
552 buffer1->cache.modified = 1;
553 layer1->layer1_crc = crc32(layer1,
554 HAMMER_LAYER1_CRCSIZE);
559 * Now fill everything in.
561 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
562 phys_offset < aligned_vol_free_end;
563 phys_offset += HAMMER_BIGBLOCK_SIZE) {
565 layer1_offset = layer1_base +
566 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
567 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
569 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
570 layer2_offset = layer1->phys_offset +
571 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
573 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
574 bzero(layer2, sizeof(*layer2));
575 if (phys_offset < vol->vol_free_off) {
577 * Fixups XXX - bigblocks already allocated as part
578 * of the freemap bootstrap.
580 if (layer2->zone == 0) {
581 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
582 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
583 layer2->bytes_free = 0;
585 } else if (phys_offset < vol->vol_free_end) {
586 ++layer1->blocks_free;
587 buffer1->cache.modified = 1;
589 layer2->append_off = 0;
590 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
594 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
595 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
596 layer2->bytes_free = 0;
598 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
599 buffer2->cache.modified = 1;
605 layer1->layer1_crc = crc32(layer1,
606 HAMMER_LAYER1_CRCSIZE);
607 buffer1->cache.modified = 1;
612 rel_volume(root_vol);
617 * Allocate big-blocks using our poor-man's volume->vol_free_off.
619 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
620 * itself and cannot update it yet.
623 alloc_bigblock(struct volume_info *volume, int zone)
625 struct buffer_info *buffer1 = NULL;
626 struct buffer_info *buffer2 = NULL;
627 struct volume_info *root_vol;
628 hammer_off_t result_offset;
629 hammer_off_t layer_offset;
630 struct hammer_blockmap_layer1 *layer1;
631 struct hammer_blockmap_layer2 *layer2;
634 volume = get_volume(RootVolNo);
636 result_offset = volume->vol_free_off;
637 if (result_offset >= volume->vol_free_end)
638 panic("alloc_bigblock: Ran out of room, filesystem too small");
639 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
642 * Update the freemap.
644 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
645 root_vol = get_volume(RootVolNo);
646 layer_offset = root_vol->ondisk->vol0_blockmap[
647 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
648 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
649 layer1 = get_buffer_data(layer_offset, &buffer1, 0);
650 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
651 --layer1->blocks_free;
652 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
653 buffer1->cache.modified = 1;
654 layer_offset = layer1->phys_offset +
655 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
656 layer2 = get_buffer_data(layer_offset, &buffer2, 0);
657 assert(layer2->zone == 0);
659 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
660 layer2->bytes_free = 0;
661 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
662 buffer2->cache.modified = 1;
664 --root_vol->ondisk->vol0_stat_freebigblocks;
665 root_vol->cache.modified = 1;
669 rel_volume(root_vol);
673 return(result_offset);
677 * Format the undo-map for the root volume.
680 format_undomap(hammer_volume_ondisk_t ondisk)
682 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
683 hammer_off_t undo_limit;
684 hammer_blockmap_t blockmap;
685 struct buffer_info *buffer = NULL;
692 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
693 * up to HAMMER_UNDO_LAYER2 big blocks. Size to approximately
696 * The minimum UNDO fifo size is 500MB, 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 = UndoBufferSize;
703 if (undo_limit == 0) {
704 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
705 if (undo_limit < 500*1024*1024)
706 undo_limit = 500*1024*1024;
708 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
709 ~HAMMER_BIGBLOCK_MASK64;
710 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
711 undo_limit = HAMMER_BIGBLOCK_SIZE;
712 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
713 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
714 UndoBufferSize = undo_limit;
716 blockmap = &ondisk->vol0_blockmap[undo_zone];
717 bzero(blockmap, sizeof(*blockmap));
718 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
719 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
720 blockmap->next_offset = blockmap->first_offset;
721 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
722 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
725 scan = blockmap->next_offset;
726 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
728 assert(limit_index <= HAMMER_UNDO_LAYER2);
730 for (n = 0; n < limit_index; ++n) {
731 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
732 HAMMER_ZONE_UNDO_INDEX);
733 scan += HAMMER_BIGBLOCK_SIZE;
735 while (n < HAMMER_UNDO_LAYER2) {
736 ondisk->vol0_undo_array[n] = HAMMER_BLOCKMAP_UNAVAIL;
741 * Pre-initialize the UNDO blocks (HAMMER version 4+)
743 printf("initializing the undo map (%jd MB)\n",
744 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
747 scan = blockmap->first_offset;
750 while (scan < blockmap->alloc_offset) {
751 hammer_fifo_head_t head;
752 hammer_fifo_tail_t tail;
754 int bytes = HAMMER_UNDO_ALIGN;
756 isnew = ((scan & HAMMER_BUFMASK64) == 0);
757 head = get_buffer_data(scan, &buffer, isnew);
758 buffer->cache.modified = 1;
759 tail = (void *)((char *)head + bytes - sizeof(*tail));
762 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
763 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
764 head->hdr_size = bytes;
765 head->hdr_seq = seqno++;
767 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
768 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
769 tail->tail_size = bytes;
771 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
772 crc32(head + 1, bytes - sizeof(*head));
780 * Format a new blockmap. This is mostly a degenerate case because
781 * all allocations are now actually done from the freemap.
784 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base)
786 blockmap->phys_offset = 0;
787 blockmap->alloc_offset = zone_base | HAMMER_VOL_ENCODE(255) |
788 HAMMER_SHORT_OFF_ENCODE(-1);
789 blockmap->first_offset = zone_base;
790 blockmap->next_offset = zone_base;
791 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
795 * Allocate a chunk of data out of a blockmap. This is a simplified
796 * version which uses next_offset as a simple allocation iterator.
800 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
801 struct buffer_info **bufferp)
803 struct buffer_info *buffer1 = NULL;
804 struct buffer_info *buffer2 = NULL;
805 struct volume_info *volume;
806 hammer_blockmap_t blockmap;
807 hammer_blockmap_t freemap;
808 struct hammer_blockmap_layer1 *layer1;
809 struct hammer_blockmap_layer2 *layer2;
810 hammer_off_t layer1_offset;
811 hammer_off_t layer2_offset;
812 hammer_off_t zone2_offset;
815 volume = get_volume(RootVolNo);
817 blockmap = &volume->ondisk->vol0_blockmap[zone];
818 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
821 * Alignment and buffer-boundary issues. If the allocation would
822 * cross a buffer boundary we have to skip to the next buffer.
824 bytes = (bytes + 15) & ~15;
827 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
829 volume->cache.modified = 1;
830 blockmap->next_offset = (blockmap->next_offset + bytes) &
835 * Dive layer 1. For now we can't allocate data outside of volume 0.
837 layer1_offset = freemap->phys_offset +
838 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
840 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
842 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
843 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
850 layer2_offset = layer1->phys_offset +
851 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
853 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
855 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
856 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
861 * If we are entering a new bigblock assign ownership to our
862 * zone. If the bigblock is owned by another zone skip it.
864 if (layer2->zone == 0) {
865 --layer1->blocks_free;
867 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
868 assert(layer2->append_off == 0);
870 if (layer2->zone != zone) {
871 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
872 ~HAMMER_BIGBLOCK_MASK64;
876 buffer1->cache.modified = 1;
877 buffer2->cache.modified = 1;
878 volume->cache.modified = 1;
879 assert(layer2->append_off ==
880 (blockmap->next_offset & HAMMER_BIGBLOCK_MASK));
881 layer2->bytes_free -= bytes;
882 *result_offp = blockmap->next_offset;
883 blockmap->next_offset += bytes;
884 layer2->append_off = (int)blockmap->next_offset &
885 HAMMER_BIGBLOCK_MASK;
887 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
888 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
890 zone2_offset = HAMMER_ZONE_ENCODE(zone,
891 *result_offp & ~HAMMER_OFF_ZONE_MASK);
893 ptr = get_buffer_data(zone2_offset, bufferp, 0);
894 (*bufferp)->cache.modified = 1;
903 * Flush various tracking structures to disk
906 flush_all_volumes(void)
908 struct volume_info *vol;
910 TAILQ_FOREACH(vol, &VolList, entry)
915 flush_volume(struct volume_info *volume)
917 struct buffer_info *buffer;
920 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
921 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
922 flush_buffer(buffer);
924 writehammerbuf(volume, volume->ondisk, 0);
925 volume->cache.modified = 0;
929 flush_buffer(struct buffer_info *buffer)
931 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
932 buffer->cache.modified = 0;
937 * Generic buffer initialization
940 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
942 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
943 head->hdr_type = hdr_type;
953 * Core I/O operations
956 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
960 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
961 if (n != HAMMER_BUFSIZE)
962 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
968 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
972 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
973 if (n != HAMMER_BUFSIZE)
974 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
978 panic(const char *ctl, ...)
983 vfprintf(stderr, ctl, va);
985 fprintf(stderr, "\n");