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
34 * $DragonFly: src/sbin/hammer/ondisk.c,v 1.25 2008/08/21 23:28:43 thomas Exp $
37 #include <sys/types.h>
47 #include "hammer_util.h"
49 static void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
50 struct buffer_info **bufferp);
51 static hammer_off_t alloc_bigblock(struct volume_info *volume, int zone);
52 static void get_buffer_readahead(struct buffer_info *base);
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 get_volume(int32_t vol_no)
170 struct volume_info *vol;
172 TAILQ_FOREACH(vol, &VolList, entry) {
173 if (vol->vol_no == vol_no)
177 errx(1, "get_volume: Volume %d does not exist!", vol_no);
179 /* not added to or removed from hammer cache */
184 rel_volume(struct volume_info *volume)
186 /* not added to or removed from hammer cache */
187 --volume->cache.refs;
191 * Acquire the specified buffer.
194 get_buffer(hammer_off_t buf_offset, int isnew)
197 struct buffer_info *buf;
198 struct volume_info *volume;
199 hammer_off_t orig_offset = buf_offset;
205 zone = HAMMER_ZONE_DECODE(buf_offset);
206 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
207 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
209 if (buf_offset == HAMMER_OFF_BAD)
212 if (AssertOnFailure) {
213 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
214 HAMMER_ZONE_RAW_BUFFER);
216 vol_no = HAMMER_VOL_DECODE(buf_offset);
217 volume = get_volume(vol_no);
218 buf_offset &= ~HAMMER_BUFMASK64;
220 hi = buffer_hash(buf_offset);
222 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry) {
223 if (buf->buf_offset == buf_offset)
227 buf = malloc(sizeof(*buf));
228 bzero(buf, sizeof(*buf));
230 fprintf(stderr, "get_buffer %016llx %016llx\n",
231 (long long)orig_offset, (long long)buf_offset);
233 buf->buf_offset = buf_offset;
234 buf->raw_offset = volume->ondisk->vol_buf_beg +
235 (buf_offset & HAMMER_OFF_SHORT_MASK);
236 buf->volume = volume;
237 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
238 ++volume->cache.refs;
239 buf->cache.u.buffer = buf;
240 hammer_cache_add(&buf->cache, ISBUFFER);
243 buf->flags |= HAMMER_BUFINFO_READAHEAD;
246 buf->flags &= ~HAMMER_BUFINFO_READAHEAD;
247 hammer_cache_used(&buf->cache);
252 hammer_cache_flush();
253 if ((ondisk = buf->ondisk) == NULL) {
254 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
256 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
258 if (n != HAMMER_BUFSIZE) {
260 err(1, "get_buffer: %s:%016llx Read failed at "
263 (long long)buf->buf_offset,
264 (long long)buf->raw_offset);
265 bzero(ondisk, HAMMER_BUFSIZE);
270 bzero(ondisk, HAMMER_BUFSIZE);
271 buf->cache.modified = 1;
274 get_buffer_readahead(buf);
279 get_buffer_readahead(struct buffer_info *base)
281 struct buffer_info *buf;
282 struct volume_info *vol;
283 hammer_off_t buf_offset;
285 int ri = UseReadBehind;
286 int re = UseReadAhead;
289 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
293 if (raw_offset >= vol->ondisk->vol_buf_end)
295 if (raw_offset < vol->ondisk->vol_buf_beg) {
297 raw_offset += HAMMER_BUFSIZE;
300 buf_offset = HAMMER_VOL_ENCODE(vol->vol_no) |
301 HAMMER_ZONE_RAW_BUFFER |
302 (raw_offset - vol->ondisk->vol_buf_beg);
303 hi = buffer_hash(raw_offset);
304 TAILQ_FOREACH(buf, &vol->buffer_lists[hi], entry) {
305 if (buf->raw_offset == raw_offset)
309 buf = get_buffer(buf_offset, -1);
313 raw_offset += HAMMER_BUFSIZE;
318 rel_buffer(struct buffer_info *buffer)
320 struct volume_info *volume;
323 assert(buffer->cache.refs > 0);
324 if (--buffer->cache.refs == 0) {
325 if (buffer->cache.delete) {
326 hi = buffer_hash(buffer->buf_offset);
327 volume = buffer->volume;
328 if (buffer->cache.modified)
329 flush_buffer(buffer);
330 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
331 hammer_cache_del(&buffer->cache);
332 free(buffer->ondisk);
340 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
343 struct buffer_info *buffer;
345 if ((buffer = *bufferp) != NULL) {
347 ((buffer->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
349 buffer = *bufferp = NULL;
353 buffer = *bufferp = get_buffer(buf_offset, isnew);
354 return((char *)buffer->ondisk + ((int32_t)buf_offset & HAMMER_BUFMASK));
358 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
359 * bufp is freed if non-NULL and a referenced buffer is loaded into it.
362 get_node(hammer_off_t node_offset, struct buffer_info **bufp)
364 struct buffer_info *buf;
368 *bufp = buf = get_buffer(node_offset, 0);
370 return((void *)((char *)buf->ondisk +
371 (int32_t)(node_offset & HAMMER_BUFMASK)));
378 * Allocate HAMMER elements - btree nodes, data storage, and record elements
380 * NOTE: hammer_alloc_fifo() initializes the fifo header for the returned
381 * item and zero's out the remainder, so don't bzero() it.
384 alloc_btree_element(hammer_off_t *offp)
386 struct buffer_info *buffer = NULL;
387 hammer_node_ondisk_t node;
389 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
391 bzero(node, sizeof(*node));
392 /* XXX buffer not released, pointer remains valid */
397 alloc_data_element(hammer_off_t *offp, int32_t data_len,
398 struct buffer_info **data_bufferp)
402 if (data_len >= HAMMER_BUFSIZE) {
403 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
404 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
406 bzero(data, data_len);
407 } else if (data_len) {
408 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
410 bzero(data, data_len);
418 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
419 * code will load each volume's freemap.
422 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
424 struct buffer_info *buffer = NULL;
425 hammer_off_t layer1_offset;
426 struct hammer_blockmap_layer1 *layer1;
429 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
430 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
431 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
432 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
434 bzero(layer1, sizeof(*layer1));
435 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
436 layer1->blocks_free = 0;
437 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
441 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
442 blockmap->phys_offset = layer1_offset;
443 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
444 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
445 blockmap->reserved01 = 0;
446 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
447 root_vol->cache.modified = 1;
451 * Load the volume's remaining free space into the freemap.
453 * Returns the number of bigblocks available.
456 initialize_freemap(struct volume_info *vol)
458 struct volume_info *root_vol;
459 struct buffer_info *buffer1 = NULL;
460 struct buffer_info *buffer2 = NULL;
461 struct hammer_blockmap_layer1 *layer1;
462 struct hammer_blockmap_layer2 *layer2;
463 hammer_off_t layer1_base;
464 hammer_off_t layer1_offset;
465 hammer_off_t layer2_offset;
466 hammer_off_t phys_offset;
467 hammer_off_t aligned_vol_free_end;
471 root_vol = get_volume(RootVolNo);
472 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
473 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
475 printf("initialize freemap volume %d\n", vol->vol_no);
478 * Initialize the freemap. First preallocate the bigblocks required
479 * to implement layer2. This preallocation is a bootstrap allocation
480 * using blocks from the target volume.
482 layer1_base = root_vol->ondisk->vol0_blockmap[
483 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
484 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
485 phys_offset < aligned_vol_free_end;
486 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
487 layer1_offset = layer1_base +
488 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
489 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
490 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
491 layer1->phys_offset = alloc_bigblock(vol,
492 HAMMER_ZONE_FREEMAP_INDEX);
493 layer1->blocks_free = 0;
494 buffer1->cache.modified = 1;
495 layer1->layer1_crc = crc32(layer1,
496 HAMMER_LAYER1_CRCSIZE);
501 * Now fill everything in.
503 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
504 phys_offset < aligned_vol_free_end;
505 phys_offset += HAMMER_LARGEBLOCK_SIZE) {
507 layer1_offset = layer1_base +
508 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
509 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
511 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
512 layer2_offset = layer1->phys_offset +
513 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
515 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
516 bzero(layer2, sizeof(*layer2));
517 if (phys_offset < vol->vol_free_off) {
519 * Fixups XXX - bigblocks already allocated as part
520 * of the freemap bootstrap.
522 if (layer2->zone == 0) {
523 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
524 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
525 layer2->bytes_free = 0;
527 } else if (phys_offset < vol->vol_free_end) {
528 ++layer1->blocks_free;
529 buffer1->cache.modified = 1;
531 layer2->append_off = 0;
532 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
536 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
537 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
538 layer2->bytes_free = 0;
540 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
541 buffer2->cache.modified = 1;
547 layer1->layer1_crc = crc32(layer1,
548 HAMMER_LAYER1_CRCSIZE);
549 buffer1->cache.modified = 1;
554 rel_volume(root_vol);
559 * Allocate big-blocks using our poor-man's volume->vol_free_off.
561 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
562 * itself and cannot update it yet.
565 alloc_bigblock(struct volume_info *volume, int zone)
567 struct buffer_info *buffer = NULL;
568 struct volume_info *root_vol;
569 hammer_off_t result_offset;
570 hammer_off_t layer_offset;
571 struct hammer_blockmap_layer1 *layer1;
572 struct hammer_blockmap_layer2 *layer2;
575 if (volume == NULL) {
576 volume = get_volume(RootVolNo);
581 result_offset = volume->vol_free_off;
582 if (result_offset >= volume->vol_free_end)
583 panic("alloc_bigblock: Ran out of room, filesystem too small");
584 volume->vol_free_off += HAMMER_LARGEBLOCK_SIZE;
587 * Update the freemap.
589 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
590 root_vol = get_volume(RootVolNo);
591 layer_offset = root_vol->ondisk->vol0_blockmap[
592 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
593 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
594 layer1 = get_buffer_data(layer_offset, &buffer, 0);
595 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
596 --layer1->blocks_free;
597 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
598 buffer->cache.modified = 1;
599 layer_offset = layer1->phys_offset +
600 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
601 layer2 = get_buffer_data(layer_offset, &buffer, 0);
602 assert(layer2->zone == 0);
604 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
605 layer2->bytes_free = 0;
606 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
607 buffer->cache.modified = 1;
609 --root_vol->ondisk->vol0_stat_freebigblocks;
610 root_vol->cache.modified = 1;
613 rel_volume(root_vol);
618 return(result_offset);
622 * Format the undo-map for the root volume.
625 format_undomap(hammer_volume_ondisk_t ondisk)
627 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
628 hammer_off_t undo_limit;
629 hammer_blockmap_t blockmap;
630 struct buffer_info *buffer = NULL;
637 * Size the undo buffer in multiples of HAMMER_LARGEBLOCK_SIZE,
638 * up to HAMMER_UNDO_LAYER2 large blocks. Size to approximately
641 * The minimum UNDO fifo size is 100MB.
643 undo_limit = UndoBufferSize;
644 if (undo_limit == 0) {
645 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
646 if (undo_limit < 100*1024*1024)
647 undo_limit = 100*1024*1024;
649 undo_limit = (undo_limit + HAMMER_LARGEBLOCK_MASK64) &
650 ~HAMMER_LARGEBLOCK_MASK64;
651 if (undo_limit < HAMMER_LARGEBLOCK_SIZE)
652 undo_limit = HAMMER_LARGEBLOCK_SIZE;
653 if (undo_limit > HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2)
654 undo_limit = HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2;
655 UndoBufferSize = undo_limit;
657 blockmap = &ondisk->vol0_blockmap[undo_zone];
658 bzero(blockmap, sizeof(*blockmap));
659 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
660 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
661 blockmap->next_offset = blockmap->first_offset;
662 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
663 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
666 scan = blockmap->next_offset;
667 limit_index = undo_limit / HAMMER_LARGEBLOCK_SIZE;
669 assert(limit_index <= HAMMER_UNDO_LAYER2);
671 for (n = 0; n < limit_index; ++n) {
672 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
673 HAMMER_ZONE_UNDO_INDEX);
674 scan += HAMMER_LARGEBLOCK_SIZE;
676 while (n < HAMMER_UNDO_LAYER2) {
677 ondisk->vol0_undo_array[n] = HAMMER_BLOCKMAP_UNAVAIL;
682 * Pre-initialize the UNDO blocks (HAMMER version 4+)
684 printf("initializing the undo map (%jd MB)\n",
685 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
688 scan = blockmap->first_offset;
691 while (scan < blockmap->alloc_offset) {
692 hammer_fifo_head_t head;
693 hammer_fifo_tail_t tail;
695 int bytes = HAMMER_UNDO_ALIGN;
697 isnew = ((scan & HAMMER_BUFMASK64) == 0);
698 head = get_buffer_data(scan, &buffer, isnew);
699 buffer->cache.modified = 1;
700 tail = (void *)((char *)head + bytes - sizeof(*tail));
703 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
704 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
705 head->hdr_size = bytes;
706 head->hdr_seq = seqno++;
708 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
709 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
710 tail->tail_size = bytes;
712 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
713 crc32(head + 1, bytes - sizeof(*head));
722 * Format a new blockmap. This is mostly a degenerate case because
723 * all allocations are now actually done from the freemap.
726 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base)
728 blockmap->phys_offset = 0;
729 blockmap->alloc_offset = zone_base | HAMMER_VOL_ENCODE(255) |
730 HAMMER_SHORT_OFF_ENCODE(-1);
731 blockmap->first_offset = zone_base;
732 blockmap->next_offset = zone_base;
733 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
737 * Allocate a chunk of data out of a blockmap. This is a simplified
738 * version which uses next_offset as a simple allocation iterator.
742 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
743 struct buffer_info **bufferp)
745 struct buffer_info *buffer1 = NULL;
746 struct buffer_info *buffer2 = NULL;
747 struct volume_info *volume;
748 hammer_blockmap_t blockmap;
749 hammer_blockmap_t freemap;
750 struct hammer_blockmap_layer1 *layer1;
751 struct hammer_blockmap_layer2 *layer2;
752 hammer_off_t layer1_offset;
753 hammer_off_t layer2_offset;
754 hammer_off_t zone2_offset;
757 volume = get_volume(RootVolNo);
759 blockmap = &volume->ondisk->vol0_blockmap[zone];
760 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
763 * Alignment and buffer-boundary issues. If the allocation would
764 * cross a buffer boundary we have to skip to the next buffer.
766 bytes = (bytes + 15) & ~15;
769 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
771 volume->cache.modified = 1;
772 blockmap->next_offset = (blockmap->next_offset + bytes) &
777 * Dive layer 1. For now we can't allocate data outside of volume 0.
779 layer1_offset = freemap->phys_offset +
780 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
782 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
784 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
785 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
792 layer2_offset = layer1->phys_offset +
793 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
795 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
797 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
798 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
803 * If we are entering a new bigblock assign ownership to our
804 * zone. If the bigblock is owned by another zone skip it.
806 if (layer2->zone == 0) {
807 --layer1->blocks_free;
809 assert(layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE);
810 assert(layer2->append_off == 0);
812 if (layer2->zone != zone) {
813 blockmap->next_offset = (blockmap->next_offset + HAMMER_LARGEBLOCK_SIZE) &
814 ~HAMMER_LARGEBLOCK_MASK64;
818 buffer1->cache.modified = 1;
819 buffer2->cache.modified = 1;
820 volume->cache.modified = 1;
821 assert(layer2->append_off ==
822 (blockmap->next_offset & HAMMER_LARGEBLOCK_MASK));
823 layer2->bytes_free -= bytes;
824 *result_offp = blockmap->next_offset;
825 blockmap->next_offset += bytes;
826 layer2->append_off = (int)blockmap->next_offset &
827 HAMMER_LARGEBLOCK_MASK;
829 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
830 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
832 zone2_offset = (*result_offp & ~HAMMER_OFF_ZONE_MASK) |
833 HAMMER_ZONE_ENCODE(zone, 0);
835 ptr = get_buffer_data(zone2_offset, bufferp, 0);
836 (*bufferp)->cache.modified = 1;
848 * Flush various tracking structures to disk
852 * Flush various tracking structures to disk
855 flush_all_volumes(void)
857 struct volume_info *vol;
859 TAILQ_FOREACH(vol, &VolList, entry)
864 flush_volume(struct volume_info *volume)
866 struct buffer_info *buffer;
869 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
870 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
871 flush_buffer(buffer);
873 writehammerbuf(volume, volume->ondisk, 0);
874 volume->cache.modified = 0;
878 flush_buffer(struct buffer_info *buffer)
880 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
881 buffer->cache.modified = 0;
886 * Generic buffer initialization
889 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
891 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
892 head->hdr_type = hdr_type;
902 * Core I/O operations
905 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
909 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
910 if (n != HAMMER_BUFSIZE)
911 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
917 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
921 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
922 if (n != HAMMER_BUFSIZE)
923 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
927 panic(const char *ctl, ...)
932 vfprintf(stderr, ctl, va);
934 fprintf(stderr, "\n");