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>
46 #include "hammer_util.h"
48 static void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
49 struct buffer_info **bufferp);
50 static hammer_off_t alloc_bigblock(struct volume_info *volume, int zone);
51 static void get_buffer_readahead(struct buffer_info *base);
52 static __inline void *get_ondisk(hammer_off_t buf_offset,
53 struct buffer_info **bufferp, int isnew);
54 static int readhammerbuf(struct volume_info *vol, void *data,
56 static void writehammerbuf(struct volume_info *vol, const void *data,
65 int64_t UndoBufferSize;
66 int UsingSuperClusters;
69 int UseReadBehind = -4;
71 int AssertOnFailure = 1;
72 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
76 buffer_hash(hammer_off_t buf_offset)
80 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
84 static struct buffer_info*
85 find_buffer(struct volume_info *volume, hammer_off_t buf_offset)
88 struct buffer_info *buf;
90 hi = buffer_hash(buf_offset);
91 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
92 if (buf->buf_offset == buf_offset)
98 * Lookup the requested information structure and related on-disk buffer.
99 * Missing structures are created.
102 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
104 struct volume_info *vol;
105 struct volume_info *scan;
106 struct hammer_volume_ondisk *ondisk;
108 struct stat st1, st2;
111 * Allocate the volume structure
113 vol = malloc(sizeof(*vol));
114 bzero(vol, sizeof(*vol));
115 for (i = 0; i < HAMMER_BUFLISTS; ++i)
116 TAILQ_INIT(&vol->buffer_lists[i]);
117 vol->name = strdup(filename);
118 vol->fd = open(vol->name, oflags);
120 err(1, "setup_volume: %s: Open failed", vol->name);
126 * Read or initialize the volume header
128 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
130 bzero(ondisk, HAMMER_BUFSIZE);
132 n = readhammerbuf(vol, ondisk, 0);
134 err(1, "setup_volume: %s: Read failed at offset 0",
137 vol_no = ondisk->vol_no;
139 RootVolNo = ondisk->vol_rootvol;
140 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
141 errx(1, "setup_volume: %s: root volume disagreement: "
143 vol->name, RootVolNo, ondisk->vol_rootvol);
146 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
147 errx(1, "setup_volume: %s: Header does not indicate "
148 "that this is a hammer volume", vol->name);
150 if (TAILQ_EMPTY(&VolList)) {
151 Hammer_FSId = vol->ondisk->vol_fsid;
152 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
153 errx(1, "setup_volume: %s: FSId does match other "
154 "volumes!", vol->name);
157 vol->vol_no = vol_no;
160 vol->cache.modified = 1;
163 if (fstat(vol->fd, &st1) != 0){
164 errx(1, "setup_volume: %s: Failed to stat", vol->name);
168 * Link the volume structure in
170 TAILQ_FOREACH(scan, &VolList, entry) {
171 if (scan->vol_no == vol_no) {
172 errx(1, "setup_volume: %s: Duplicate volume number %d "
173 "against %s", vol->name, vol_no, scan->name);
175 if (fstat(scan->fd, &st2) != 0){
176 errx(1, "setup_volume: %s: Failed to stat %s",
177 vol->name, scan->name);
179 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
180 errx(1, "setup_volume: %s: Specified more than once",
184 TAILQ_INSERT_TAIL(&VolList, vol, entry);
189 get_volume(int32_t vol_no)
191 struct volume_info *vol;
193 TAILQ_FOREACH(vol, &VolList, entry) {
194 if (vol->vol_no == vol_no)
199 errx(1, "get_volume: Volume %d does not exist!",
204 /* not added to or removed from hammer cache */
209 rel_volume(struct volume_info *volume)
213 /* not added to or removed from hammer cache */
214 --volume->cache.refs;
218 * Acquire the specified buffer. isnew is -1 only when called
219 * via get_buffer_readahead() to prevent another readahead.
222 get_buffer(hammer_off_t buf_offset, int isnew)
225 struct buffer_info *buf;
226 struct volume_info *volume;
227 hammer_off_t orig_offset = buf_offset;
233 zone = HAMMER_ZONE_DECODE(buf_offset);
234 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
235 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
237 if (buf_offset == HAMMER_OFF_BAD)
240 if (AssertOnFailure) {
241 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
242 HAMMER_ZONE_RAW_BUFFER);
244 vol_no = HAMMER_VOL_DECODE(buf_offset);
245 volume = get_volume(vol_no);
249 buf_offset &= ~HAMMER_BUFMASK64;
250 buf = find_buffer(volume, buf_offset);
253 buf = malloc(sizeof(*buf));
254 bzero(buf, sizeof(*buf));
256 fprintf(stderr, "get_buffer: %016llx %016llx at %p\n",
257 (long long)orig_offset, (long long)buf_offset,
260 buf->buf_offset = buf_offset;
261 buf->raw_offset = volume->ondisk->vol_buf_beg +
262 (buf_offset & HAMMER_OFF_SHORT_MASK);
263 buf->volume = volume;
264 hi = buffer_hash(buf_offset);
265 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
266 ++volume->cache.refs;
267 buf->cache.u.buffer = buf;
268 hammer_cache_add(&buf->cache, ISBUFFER);
272 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
273 (long long)orig_offset, (long long)buf_offset,
276 hammer_cache_used(&buf->cache);
280 hammer_cache_flush();
281 if ((ondisk = buf->ondisk) == NULL) {
282 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
284 n = readhammerbuf(volume, ondisk, buf->raw_offset);
287 err(1, "get_buffer: %s:%016llx "
288 "Read failed at offset %016llx",
290 (long long)buf->buf_offset,
291 (long long)buf->raw_offset);
292 bzero(ondisk, HAMMER_BUFSIZE);
297 bzero(ondisk, HAMMER_BUFSIZE);
298 buf->cache.modified = 1;
301 get_buffer_readahead(buf);
306 get_buffer_readahead(struct buffer_info *base)
308 struct buffer_info *buf;
309 struct volume_info *vol;
310 hammer_off_t buf_offset;
312 int ri = UseReadBehind;
313 int re = UseReadAhead;
315 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
319 if (raw_offset >= vol->ondisk->vol_buf_end)
321 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
323 raw_offset += HAMMER_BUFSIZE;
326 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
327 raw_offset - vol->ondisk->vol_buf_beg);
328 buf = find_buffer(vol, buf_offset);
330 buf = get_buffer(buf_offset, -1);
334 raw_offset += HAMMER_BUFSIZE;
339 rel_buffer(struct buffer_info *buffer)
341 struct volume_info *volume;
346 assert(buffer->cache.refs > 0);
347 if (--buffer->cache.refs == 0) {
348 if (buffer->cache.delete) {
349 hi = buffer_hash(buffer->buf_offset);
350 volume = buffer->volume;
351 if (buffer->cache.modified)
352 flush_buffer(buffer);
353 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
354 hammer_cache_del(&buffer->cache);
355 free(buffer->ondisk);
363 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
364 * bufferp is freed if isnew or the offset is out of range of the cached data.
365 * If bufferp is freed a referenced buffer is loaded into it.
368 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
371 if (*bufferp != NULL) {
373 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
374 rel_buffer(*bufferp);
378 return(get_ondisk(buf_offset, bufferp, isnew));
382 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
383 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
386 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
388 if (*bufferp != NULL) {
389 rel_buffer(*bufferp);
392 return(get_ondisk(node_offset, bufferp, 0));
396 * Return a pointer to a buffer data given a buffer offset.
397 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
401 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
404 struct buffer_info *buffer;
407 if (buffer == NULL) {
408 buffer = *bufferp = get_buffer(buf_offset, isnew);
413 return((char *)buffer->ondisk +
414 ((int32_t)buf_offset & HAMMER_BUFMASK));
418 * Allocate HAMMER elements - btree nodes, meta data, data storage
421 alloc_btree_element(hammer_off_t *offp,
422 struct buffer_info **data_bufferp)
424 hammer_node_ondisk_t node;
426 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
428 bzero(node, sizeof(*node));
433 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
434 struct buffer_info **data_bufferp)
438 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
440 bzero(data, data_len);
445 alloc_data_element(hammer_off_t *offp, int32_t data_len,
446 struct buffer_info **data_bufferp)
450 if (data_len >= HAMMER_BUFSIZE) {
451 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
452 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
454 bzero(data, data_len);
455 } else if (data_len) {
456 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
458 bzero(data, data_len);
466 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
467 * code will load each volume's freemap.
470 format_freemap(struct volume_info *root_vol)
472 struct buffer_info *buffer = NULL;
473 hammer_off_t layer1_offset;
474 hammer_blockmap_t blockmap;
475 struct hammer_blockmap_layer1 *layer1;
478 /* Only root volume needs formatting */
479 assert(root_vol->vol_no == RootVolNo);
481 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
482 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
483 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
484 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
486 bzero(layer1, sizeof(*layer1));
487 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
488 layer1->blocks_free = 0;
489 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
493 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
494 bzero(blockmap, sizeof(*blockmap));
495 blockmap->phys_offset = layer1_offset;
496 blockmap->first_offset = 0;
497 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
498 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
499 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
500 root_vol->cache.modified = 1;
504 * Load the volume's remaining free space into the freemap.
506 * Returns the number of big-blocks available.
509 initialize_freemap(struct volume_info *vol)
511 struct volume_info *root_vol;
512 struct buffer_info *buffer1 = NULL;
513 struct buffer_info *buffer2 = NULL;
514 struct hammer_blockmap_layer1 *layer1;
515 struct hammer_blockmap_layer2 *layer2;
516 hammer_off_t layer1_base;
517 hammer_off_t layer1_offset;
518 hammer_off_t layer2_offset;
519 hammer_off_t phys_offset;
520 hammer_off_t aligned_vol_free_end;
521 hammer_blockmap_t freemap;
525 root_vol = get_volume(RootVolNo);
526 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
527 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
529 printf("initialize freemap volume %d\n", vol->vol_no);
532 * Initialize the freemap. First preallocate the big-blocks required
533 * to implement layer2. This preallocation is a bootstrap allocation
534 * using blocks from the target volume.
536 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
537 layer1_base = freemap->phys_offset;
539 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
540 phys_offset < aligned_vol_free_end;
541 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
542 layer1_offset = layer1_base +
543 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
544 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
545 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
546 layer1->phys_offset = alloc_bigblock(vol,
547 HAMMER_ZONE_FREEMAP_INDEX);
548 layer1->blocks_free = 0;
549 buffer1->cache.modified = 1;
550 layer1->layer1_crc = crc32(layer1,
551 HAMMER_LAYER1_CRCSIZE);
556 * Now fill everything in.
558 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
559 phys_offset < aligned_vol_free_end;
560 phys_offset += HAMMER_BIGBLOCK_SIZE) {
562 layer1_offset = layer1_base +
563 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
564 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
566 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
567 layer2_offset = layer1->phys_offset +
568 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
570 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
571 bzero(layer2, sizeof(*layer2));
572 if (phys_offset < vol->vol_free_off) {
574 * Fixups XXX - big-blocks already allocated as part
575 * of the freemap bootstrap.
577 if (layer2->zone == 0) {
578 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
579 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
580 layer2->bytes_free = 0;
582 } else if (phys_offset < vol->vol_free_end) {
583 ++layer1->blocks_free;
584 buffer1->cache.modified = 1;
586 layer2->append_off = 0;
587 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
591 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
592 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
593 layer2->bytes_free = 0;
595 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
596 buffer2->cache.modified = 1;
602 layer1->layer1_crc = crc32(layer1,
603 HAMMER_LAYER1_CRCSIZE);
604 buffer1->cache.modified = 1;
609 rel_volume(root_vol);
614 * Returns the number of big-blocks available for filesystem data and undos
615 * without formatting.
618 count_freemap(struct volume_info *vol)
620 hammer_off_t phys_offset;
621 hammer_off_t vol_free_off;
622 hammer_off_t aligned_vol_free_end;
625 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
626 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
627 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
629 if (vol->vol_no == RootVolNo)
630 vol_free_off += HAMMER_BIGBLOCK_SIZE;
632 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
633 phys_offset < aligned_vol_free_end;
634 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
635 vol_free_off += HAMMER_BIGBLOCK_SIZE;
638 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
639 phys_offset < aligned_vol_free_end;
640 phys_offset += HAMMER_BIGBLOCK_SIZE) {
641 if (phys_offset < vol_free_off) {
643 } else if (phys_offset < vol->vol_free_end) {
652 * Allocate big-blocks using our poor-man's volume->vol_free_off.
654 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
655 * itself and cannot update it yet.
658 alloc_bigblock(struct volume_info *volume, int zone)
660 struct buffer_info *buffer1 = NULL;
661 struct buffer_info *buffer2 = NULL;
662 struct volume_info *root_vol;
663 hammer_off_t result_offset;
664 hammer_off_t layer_offset;
665 hammer_blockmap_t freemap;
666 struct hammer_blockmap_layer1 *layer1;
667 struct hammer_blockmap_layer2 *layer2;
670 volume = get_volume(RootVolNo);
672 result_offset = volume->vol_free_off;
673 if (result_offset >= volume->vol_free_end)
674 errx(1, "alloc_bigblock: Ran out of room, filesystem too small");
675 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
678 * Update the freemap.
680 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
681 root_vol = get_volume(RootVolNo);
682 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
683 layer_offset = freemap->phys_offset +
684 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
685 layer1 = get_buffer_data(layer_offset, &buffer1, 0);
686 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
687 --layer1->blocks_free;
688 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
689 buffer1->cache.modified = 1;
690 layer_offset = layer1->phys_offset +
691 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
692 layer2 = get_buffer_data(layer_offset, &buffer2, 0);
693 assert(layer2->zone == 0);
695 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
696 layer2->bytes_free = 0;
697 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
698 buffer2->cache.modified = 1;
700 --root_vol->ondisk->vol0_stat_freebigblocks;
701 root_vol->cache.modified = 1;
705 rel_volume(root_vol);
709 return(result_offset);
713 * Format the undomap for the root volume.
716 format_undomap(struct volume_info *root_vol)
718 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
719 hammer_off_t undo_limit;
720 hammer_blockmap_t blockmap;
721 struct hammer_volume_ondisk *ondisk;
722 struct buffer_info *buffer = NULL;
728 /* Only root volume needs formatting */
729 assert(root_vol->vol_no == RootVolNo);
730 ondisk = root_vol->ondisk;
733 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
734 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
737 * The minimum UNDO fifo size is 500MB, or approximately 1% of
738 * the recommended 50G disk.
740 * Changing this minimum is rather dangerous as complex filesystem
741 * operations can cause the UNDO FIFO to fill up otherwise.
743 undo_limit = UndoBufferSize;
744 if (undo_limit == 0) {
745 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
746 if (undo_limit < 500*1024*1024)
747 undo_limit = 500*1024*1024;
749 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
750 ~HAMMER_BIGBLOCK_MASK64;
751 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
752 undo_limit = HAMMER_BIGBLOCK_SIZE;
753 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
754 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
755 UndoBufferSize = undo_limit;
757 blockmap = &ondisk->vol0_blockmap[undo_zone];
758 bzero(blockmap, sizeof(*blockmap));
759 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
760 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
761 blockmap->next_offset = blockmap->first_offset;
762 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
763 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
766 scan = blockmap->next_offset;
767 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
769 assert(limit_index <= HAMMER_UNDO_LAYER2);
771 for (n = 0; n < limit_index; ++n) {
772 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
773 HAMMER_ZONE_UNDO_INDEX);
774 scan += HAMMER_BIGBLOCK_SIZE;
776 while (n < HAMMER_UNDO_LAYER2) {
777 ondisk->vol0_undo_array[n] = HAMMER_BLOCKMAP_UNAVAIL;
782 * Pre-initialize the UNDO blocks (HAMMER version 4+)
784 printf("initializing the undo map (%jd MB)\n",
785 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
788 scan = blockmap->first_offset;
791 while (scan < blockmap->alloc_offset) {
792 hammer_fifo_head_t head;
793 hammer_fifo_tail_t tail;
795 int bytes = HAMMER_UNDO_ALIGN;
797 isnew = ((scan & HAMMER_BUFMASK64) == 0);
798 head = get_buffer_data(scan, &buffer, isnew);
799 buffer->cache.modified = 1;
800 tail = (void *)((char *)head + bytes - sizeof(*tail));
803 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
804 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
805 head->hdr_size = bytes;
806 head->hdr_seq = seqno++;
808 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
809 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
810 tail->tail_size = bytes;
812 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
813 crc32(head + 1, bytes - sizeof(*head));
821 * Format a new blockmap. This is mostly a degenerate case because
822 * all allocations are now actually done from the freemap.
825 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base)
827 bzero(blockmap, sizeof(*blockmap));
828 blockmap->phys_offset = 0;
829 blockmap->first_offset = zone_base;
830 blockmap->next_offset = zone_base;
831 blockmap->alloc_offset = HAMMER_ENCODE(zone_base, 255, -1);
832 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
836 * Allocate a chunk of data out of a blockmap. This is a simplified
837 * version which uses next_offset as a simple allocation iterator.
841 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
842 struct buffer_info **bufferp)
844 struct buffer_info *buffer1 = NULL;
845 struct buffer_info *buffer2 = NULL;
846 struct volume_info *volume;
847 hammer_blockmap_t blockmap;
848 hammer_blockmap_t freemap;
849 struct hammer_blockmap_layer1 *layer1;
850 struct hammer_blockmap_layer2 *layer2;
851 hammer_off_t layer1_offset;
852 hammer_off_t layer2_offset;
853 hammer_off_t zone2_offset;
856 volume = get_volume(RootVolNo);
858 blockmap = &volume->ondisk->vol0_blockmap[zone];
859 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
862 * Alignment and buffer-boundary issues. If the allocation would
863 * cross a buffer boundary we have to skip to the next buffer.
865 bytes = (bytes + 15) & ~15;
868 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
870 volume->cache.modified = 1;
871 blockmap->next_offset = (blockmap->next_offset + bytes) &
878 layer1_offset = freemap->phys_offset +
879 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
881 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
883 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
884 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
889 * Dive layer 2, each entry represents a big-block.
891 layer2_offset = layer1->phys_offset +
892 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
894 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
896 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
897 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
902 * If we are entering a new big-block assign ownership to our
903 * zone. If the big-block is owned by another zone skip it.
905 if (layer2->zone == 0) {
906 --layer1->blocks_free;
908 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
909 assert(layer2->append_off == 0);
911 if (layer2->zone != zone) {
912 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
913 ~HAMMER_BIGBLOCK_MASK64;
917 buffer1->cache.modified = 1;
918 buffer2->cache.modified = 1;
919 volume->cache.modified = 1;
920 assert(layer2->append_off ==
921 (blockmap->next_offset & HAMMER_BIGBLOCK_MASK));
922 layer2->bytes_free -= bytes;
923 *result_offp = blockmap->next_offset;
924 blockmap->next_offset += bytes;
925 layer2->append_off = (int)blockmap->next_offset &
926 HAMMER_BIGBLOCK_MASK;
928 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
929 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
931 zone2_offset = HAMMER_ZONE_ENCODE(zone,
932 *result_offp & ~HAMMER_OFF_ZONE_MASK);
934 ptr = get_buffer_data(zone2_offset, bufferp, 0);
935 (*bufferp)->cache.modified = 1;
944 * Flush various tracking structures to disk
947 flush_all_volumes(void)
949 struct volume_info *vol;
951 TAILQ_FOREACH(vol, &VolList, entry)
956 flush_volume(struct volume_info *volume)
958 struct buffer_info *buffer;
961 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
962 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
963 flush_buffer(buffer);
965 writehammerbuf(volume, volume->ondisk, 0);
966 volume->cache.modified = 0;
970 flush_buffer(struct buffer_info *buffer)
972 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
973 buffer->cache.modified = 0;
977 * Core I/O operations
980 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
984 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
985 if (n != HAMMER_BUFSIZE)
991 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
995 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
996 if (n != HAMMER_BUFSIZE)
997 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);