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);
55 static void init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type);
56 static void readhammerbuf(struct volume_info *vol, void *data,
59 static void writehammerbuf(struct volume_info *vol, const void *data,
68 int64_t UndoBufferSize;
69 int UsingSuperClusters;
72 int UseReadBehind = -4;
74 int AssertOnFailure = 1;
75 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
79 buffer_hash(hammer_off_t buf_offset)
83 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
87 static struct buffer_info*
88 find_buffer(struct volume_info *volume, hammer_off_t buf_offset)
91 struct buffer_info *buf;
93 hi = buffer_hash(buf_offset);
94 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
95 if (buf->buf_offset == buf_offset)
101 * Lookup the requested information structure and related on-disk buffer.
102 * Missing structures are created.
105 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
107 struct volume_info *vol;
108 struct volume_info *scan;
109 struct hammer_volume_ondisk *ondisk;
111 struct stat st1, st2;
114 * Allocate the volume structure
116 vol = malloc(sizeof(*vol));
117 bzero(vol, sizeof(*vol));
118 for (i = 0; i < HAMMER_BUFLISTS; ++i)
119 TAILQ_INIT(&vol->buffer_lists[i]);
120 vol->name = strdup(filename);
121 vol->fd = open(vol->name, oflags);
123 err(1, "setup_volume: %s: Open failed", vol->name);
129 * Read or initialize the volume header
131 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
133 bzero(ondisk, HAMMER_BUFSIZE);
135 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
136 if (n != HAMMER_BUFSIZE) {
137 err(1, "setup_volume: %s: Read failed at offset 0",
140 vol_no = ondisk->vol_no;
142 RootVolNo = ondisk->vol_rootvol;
143 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
144 errx(1, "setup_volume: %s: root volume disagreement: "
146 vol->name, RootVolNo, ondisk->vol_rootvol);
149 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
150 errx(1, "setup_volume: %s: Header does not indicate "
151 "that this is a hammer volume", vol->name);
153 if (TAILQ_EMPTY(&VolList)) {
154 Hammer_FSId = vol->ondisk->vol_fsid;
155 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
156 errx(1, "setup_volume: %s: FSId does match other "
157 "volumes!", vol->name);
160 vol->vol_no = vol_no;
163 /*init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);*/
164 vol->cache.modified = 1;
167 if (fstat(vol->fd, &st1) != 0){
168 errx(1, "setup_volume: %s: Failed to stat", vol->name);
172 * Link the volume structure in
174 TAILQ_FOREACH(scan, &VolList, entry) {
175 if (scan->vol_no == vol_no) {
176 errx(1, "setup_volume: %s: Duplicate volume number %d "
177 "against %s", vol->name, vol_no, scan->name);
179 if (fstat(scan->fd, &st2) != 0){
180 errx(1, "setup_volume: %s: Failed to stat %s",
181 vol->name, scan->name);
183 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
184 errx(1, "setup_volume: %s: Specified more than once",
188 TAILQ_INSERT_TAIL(&VolList, vol, entry);
193 get_volume(int32_t vol_no)
195 struct volume_info *vol;
197 TAILQ_FOREACH(vol, &VolList, entry) {
198 if (vol->vol_no == vol_no)
203 errx(1, "get_volume: Volume %d does not exist!",
208 /* not added to or removed from hammer cache */
213 rel_volume(struct volume_info *volume)
217 /* not added to or removed from hammer cache */
218 --volume->cache.refs;
222 * Acquire the specified buffer. isnew is -1 only when called
223 * via get_buffer_readahead() to prevent another readahead.
226 get_buffer(hammer_off_t buf_offset, int isnew)
229 struct buffer_info *buf;
230 struct volume_info *volume;
231 hammer_off_t orig_offset = buf_offset;
237 zone = HAMMER_ZONE_DECODE(buf_offset);
238 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
239 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
241 if (buf_offset == HAMMER_OFF_BAD)
244 if (AssertOnFailure) {
245 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
246 HAMMER_ZONE_RAW_BUFFER);
248 vol_no = HAMMER_VOL_DECODE(buf_offset);
249 volume = get_volume(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);
276 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
277 (long long)orig_offset, (long long)buf_offset,
280 hammer_cache_used(&buf->cache);
284 hammer_cache_flush();
285 if ((ondisk = buf->ondisk) == NULL) {
286 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
288 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
290 if (n != HAMMER_BUFSIZE) {
292 err(1, "get_buffer: %s:%016llx "
293 "Read failed at offset %016llx",
295 (long long)buf->buf_offset,
296 (long long)buf->raw_offset);
297 bzero(ondisk, HAMMER_BUFSIZE);
302 bzero(ondisk, HAMMER_BUFSIZE);
303 buf->cache.modified = 1;
306 get_buffer_readahead(buf);
311 get_buffer_readahead(struct buffer_info *base)
313 struct buffer_info *buf;
314 struct volume_info *vol;
315 hammer_off_t buf_offset;
317 int ri = UseReadBehind;
318 int re = UseReadAhead;
320 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
324 if (raw_offset >= vol->ondisk->vol_buf_end)
326 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
328 raw_offset += HAMMER_BUFSIZE;
331 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
332 raw_offset - vol->ondisk->vol_buf_beg);
333 buf = find_buffer(vol, buf_offset);
335 buf = get_buffer(buf_offset, -1);
339 raw_offset += HAMMER_BUFSIZE;
344 rel_buffer(struct buffer_info *buffer)
346 struct volume_info *volume;
351 assert(buffer->cache.refs > 0);
352 if (--buffer->cache.refs == 0) {
353 if (buffer->cache.delete) {
354 hi = buffer_hash(buffer->buf_offset);
355 volume = buffer->volume;
356 if (buffer->cache.modified)
357 flush_buffer(buffer);
358 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
359 hammer_cache_del(&buffer->cache);
360 free(buffer->ondisk);
368 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
369 * bufferp is freed if isnew or the offset is out of range of the cached data.
370 * If bufferp is freed a referenced buffer is loaded into it.
373 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
376 if (*bufferp != NULL) {
378 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
379 rel_buffer(*bufferp);
383 return(get_ondisk(buf_offset, bufferp, isnew));
387 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
388 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
391 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
393 if (*bufferp != NULL) {
394 rel_buffer(*bufferp);
397 return(get_ondisk(node_offset, bufferp, 0));
401 * Return a pointer to a buffer data given a buffer offset.
402 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
406 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
409 struct buffer_info *buffer;
412 if (buffer == NULL) {
413 buffer = *bufferp = get_buffer(buf_offset, isnew);
418 return((char *)buffer->ondisk +
419 ((int32_t)buf_offset & HAMMER_BUFMASK));
423 * Allocate HAMMER elements - btree nodes, meta data, data storage
426 alloc_btree_element(hammer_off_t *offp,
427 struct buffer_info **data_bufferp)
429 hammer_node_ondisk_t node;
431 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
433 bzero(node, sizeof(*node));
438 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
439 struct buffer_info **data_bufferp)
443 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
445 bzero(data, data_len);
450 alloc_data_element(hammer_off_t *offp, int32_t data_len,
451 struct buffer_info **data_bufferp)
455 if (data_len >= HAMMER_BUFSIZE) {
456 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
457 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
459 bzero(data, data_len);
460 } else if (data_len) {
461 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
463 bzero(data, data_len);
471 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
472 * code will load each volume's freemap.
475 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
477 struct buffer_info *buffer = NULL;
478 hammer_off_t layer1_offset;
479 struct hammer_blockmap_layer1 *layer1;
482 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
483 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
484 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
485 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
487 bzero(layer1, sizeof(*layer1));
488 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
489 layer1->blocks_free = 0;
490 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
494 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
495 blockmap->phys_offset = layer1_offset;
496 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
497 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
498 blockmap->reserved01 = 0;
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;
524 root_vol = get_volume(RootVolNo);
525 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
526 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
528 printf("initialize freemap volume %d\n", vol->vol_no);
531 * Initialize the freemap. First preallocate the big-blocks required
532 * to implement layer2. This preallocation is a bootstrap allocation
533 * using blocks from the target volume.
535 layer1_base = root_vol->ondisk->vol0_blockmap[
536 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
537 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
538 phys_offset < aligned_vol_free_end;
539 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
540 layer1_offset = layer1_base +
541 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
542 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
543 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
544 layer1->phys_offset = alloc_bigblock(vol,
545 HAMMER_ZONE_FREEMAP_INDEX);
546 layer1->blocks_free = 0;
547 buffer1->cache.modified = 1;
548 layer1->layer1_crc = crc32(layer1,
549 HAMMER_LAYER1_CRCSIZE);
554 * Now fill everything in.
556 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
557 phys_offset < aligned_vol_free_end;
558 phys_offset += HAMMER_BIGBLOCK_SIZE) {
560 layer1_offset = layer1_base +
561 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
562 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
564 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
565 layer2_offset = layer1->phys_offset +
566 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
568 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
569 bzero(layer2, sizeof(*layer2));
570 if (phys_offset < vol->vol_free_off) {
572 * Fixups XXX - big-blocks already allocated as part
573 * of the freemap bootstrap.
575 if (layer2->zone == 0) {
576 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
577 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
578 layer2->bytes_free = 0;
580 } else if (phys_offset < vol->vol_free_end) {
581 ++layer1->blocks_free;
582 buffer1->cache.modified = 1;
584 layer2->append_off = 0;
585 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
589 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
590 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
591 layer2->bytes_free = 0;
593 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
594 buffer2->cache.modified = 1;
600 layer1->layer1_crc = crc32(layer1,
601 HAMMER_LAYER1_CRCSIZE);
602 buffer1->cache.modified = 1;
607 rel_volume(root_vol);
612 * Returns the number of big-blocks available for filesystem data and undos
613 * without formatting.
616 count_freemap(struct volume_info *vol)
618 hammer_off_t phys_offset;
619 hammer_off_t vol_free_off;
620 hammer_off_t aligned_vol_free_end;
623 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
624 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
625 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
627 if (vol->vol_no == RootVolNo)
628 vol_free_off += HAMMER_BIGBLOCK_SIZE;
630 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
631 phys_offset < aligned_vol_free_end;
632 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
633 vol_free_off += HAMMER_BIGBLOCK_SIZE;
636 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
637 phys_offset < aligned_vol_free_end;
638 phys_offset += HAMMER_BIGBLOCK_SIZE) {
639 if (phys_offset < vol_free_off) {
641 } else if (phys_offset < vol->vol_free_end) {
650 * Allocate big-blocks using our poor-man's volume->vol_free_off.
652 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
653 * itself and cannot update it yet.
656 alloc_bigblock(struct volume_info *volume, int zone)
658 struct buffer_info *buffer1 = NULL;
659 struct buffer_info *buffer2 = NULL;
660 struct volume_info *root_vol;
661 hammer_off_t result_offset;
662 hammer_off_t layer_offset;
663 struct hammer_blockmap_layer1 *layer1;
664 struct hammer_blockmap_layer2 *layer2;
667 volume = get_volume(RootVolNo);
669 result_offset = volume->vol_free_off;
670 if (result_offset >= volume->vol_free_end)
671 errx(1, "alloc_bigblock: Ran out of room, filesystem too small");
672 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
675 * Update the freemap.
677 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
678 root_vol = get_volume(RootVolNo);
679 layer_offset = root_vol->ondisk->vol0_blockmap[
680 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
681 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
682 layer1 = get_buffer_data(layer_offset, &buffer1, 0);
683 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
684 --layer1->blocks_free;
685 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
686 buffer1->cache.modified = 1;
687 layer_offset = layer1->phys_offset +
688 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
689 layer2 = get_buffer_data(layer_offset, &buffer2, 0);
690 assert(layer2->zone == 0);
692 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
693 layer2->bytes_free = 0;
694 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
695 buffer2->cache.modified = 1;
697 --root_vol->ondisk->vol0_stat_freebigblocks;
698 root_vol->cache.modified = 1;
702 rel_volume(root_vol);
706 return(result_offset);
710 * Format the undomap for the root volume.
713 format_undomap(hammer_volume_ondisk_t ondisk)
715 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
716 hammer_off_t undo_limit;
717 hammer_blockmap_t blockmap;
718 struct buffer_info *buffer = NULL;
725 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
726 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
729 * The minimum UNDO fifo size is 500MB, or approximately 1% of
730 * the recommended 50G disk.
732 * Changing this minimum is rather dangerous as complex filesystem
733 * operations can cause the UNDO FIFO to fill up otherwise.
735 undo_limit = UndoBufferSize;
736 if (undo_limit == 0) {
737 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
738 if (undo_limit < 500*1024*1024)
739 undo_limit = 500*1024*1024;
741 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
742 ~HAMMER_BIGBLOCK_MASK64;
743 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
744 undo_limit = HAMMER_BIGBLOCK_SIZE;
745 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
746 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
747 UndoBufferSize = undo_limit;
749 blockmap = &ondisk->vol0_blockmap[undo_zone];
750 bzero(blockmap, sizeof(*blockmap));
751 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
752 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
753 blockmap->next_offset = blockmap->first_offset;
754 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
755 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
758 scan = blockmap->next_offset;
759 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
761 assert(limit_index <= HAMMER_UNDO_LAYER2);
763 for (n = 0; n < limit_index; ++n) {
764 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
765 HAMMER_ZONE_UNDO_INDEX);
766 scan += HAMMER_BIGBLOCK_SIZE;
768 while (n < HAMMER_UNDO_LAYER2) {
769 ondisk->vol0_undo_array[n] = HAMMER_BLOCKMAP_UNAVAIL;
774 * Pre-initialize the UNDO blocks (HAMMER version 4+)
776 printf("initializing the undo map (%jd MB)\n",
777 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
780 scan = blockmap->first_offset;
783 while (scan < blockmap->alloc_offset) {
784 hammer_fifo_head_t head;
785 hammer_fifo_tail_t tail;
787 int bytes = HAMMER_UNDO_ALIGN;
789 isnew = ((scan & HAMMER_BUFMASK64) == 0);
790 head = get_buffer_data(scan, &buffer, isnew);
791 buffer->cache.modified = 1;
792 tail = (void *)((char *)head + bytes - sizeof(*tail));
795 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
796 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
797 head->hdr_size = bytes;
798 head->hdr_seq = seqno++;
800 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
801 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
802 tail->tail_size = bytes;
804 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
805 crc32(head + 1, bytes - sizeof(*head));
813 * Format a new blockmap. This is mostly a degenerate case because
814 * all allocations are now actually done from the freemap.
817 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base)
819 blockmap->phys_offset = 0;
820 blockmap->alloc_offset = HAMMER_ENCODE(zone_base, 255, -1);
821 blockmap->first_offset = zone_base;
822 blockmap->next_offset = zone_base;
823 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
827 * Allocate a chunk of data out of a blockmap. This is a simplified
828 * version which uses next_offset as a simple allocation iterator.
832 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
833 struct buffer_info **bufferp)
835 struct buffer_info *buffer1 = NULL;
836 struct buffer_info *buffer2 = NULL;
837 struct volume_info *volume;
838 hammer_blockmap_t blockmap;
839 hammer_blockmap_t freemap;
840 struct hammer_blockmap_layer1 *layer1;
841 struct hammer_blockmap_layer2 *layer2;
842 hammer_off_t layer1_offset;
843 hammer_off_t layer2_offset;
844 hammer_off_t zone2_offset;
847 volume = get_volume(RootVolNo);
849 blockmap = &volume->ondisk->vol0_blockmap[zone];
850 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
853 * Alignment and buffer-boundary issues. If the allocation would
854 * cross a buffer boundary we have to skip to the next buffer.
856 bytes = (bytes + 15) & ~15;
859 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
861 volume->cache.modified = 1;
862 blockmap->next_offset = (blockmap->next_offset + bytes) &
867 * Dive layer 1. For now we can't allocate data outside of volume 0.
869 layer1_offset = freemap->phys_offset +
870 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
872 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
874 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
875 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
882 layer2_offset = layer1->phys_offset +
883 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
885 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
887 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
888 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
893 * If we are entering a new big-block assign ownership to our
894 * zone. If the big-block is owned by another zone skip it.
896 if (layer2->zone == 0) {
897 --layer1->blocks_free;
899 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
900 assert(layer2->append_off == 0);
902 if (layer2->zone != zone) {
903 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
904 ~HAMMER_BIGBLOCK_MASK64;
908 buffer1->cache.modified = 1;
909 buffer2->cache.modified = 1;
910 volume->cache.modified = 1;
911 assert(layer2->append_off ==
912 (blockmap->next_offset & HAMMER_BIGBLOCK_MASK));
913 layer2->bytes_free -= bytes;
914 *result_offp = blockmap->next_offset;
915 blockmap->next_offset += bytes;
916 layer2->append_off = (int)blockmap->next_offset &
917 HAMMER_BIGBLOCK_MASK;
919 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
920 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
922 zone2_offset = HAMMER_ZONE_ENCODE(zone,
923 *result_offp & ~HAMMER_OFF_ZONE_MASK);
925 ptr = get_buffer_data(zone2_offset, bufferp, 0);
926 (*bufferp)->cache.modified = 1;
935 * Flush various tracking structures to disk
938 flush_all_volumes(void)
940 struct volume_info *vol;
942 TAILQ_FOREACH(vol, &VolList, entry)
947 flush_volume(struct volume_info *volume)
949 struct buffer_info *buffer;
952 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
953 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
954 flush_buffer(buffer);
956 writehammerbuf(volume, volume->ondisk, 0);
957 volume->cache.modified = 0;
961 flush_buffer(struct buffer_info *buffer)
963 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
964 buffer->cache.modified = 0;
969 * Generic buffer initialization
972 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
974 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
975 head->hdr_type = hdr_type;
985 * Core I/O operations
988 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
992 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
993 if (n != HAMMER_BUFSIZE)
994 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
1000 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
1004 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
1005 if (n != HAMMER_BUFSIZE)
1006 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);