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);
124 * Read or initialize the volume header
126 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
128 bzero(ondisk, HAMMER_BUFSIZE);
130 n = readhammerbuf(vol, ondisk, 0);
132 err(1, "setup_volume: %s: Read failed at offset 0",
135 vol_no = ondisk->vol_no;
137 RootVolNo = ondisk->vol_rootvol;
138 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
139 errx(1, "setup_volume: %s: root volume disagreement: "
141 vol->name, RootVolNo, ondisk->vol_rootvol);
144 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
145 errx(1, "setup_volume: %s: Header does not indicate "
146 "that this is a hammer volume", vol->name);
148 if (TAILQ_EMPTY(&VolList)) {
149 Hammer_FSId = vol->ondisk->vol_fsid;
150 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
151 errx(1, "setup_volume: %s: FSId does match other "
152 "volumes!", vol->name);
155 vol->vol_no = vol_no;
158 vol->cache.modified = 1;
161 if (fstat(vol->fd, &st1) != 0){
162 errx(1, "setup_volume: %s: Failed to stat", vol->name);
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", vol->name, vol_no, scan->name);
173 if (fstat(scan->fd, &st2) != 0){
174 errx(1, "setup_volume: %s: Failed to stat %s",
175 vol->name, scan->name);
177 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
178 errx(1, "setup_volume: %s: Specified more than once",
182 TAILQ_INSERT_TAIL(&VolList, vol, entry);
187 get_volume(int32_t vol_no)
189 struct volume_info *vol;
191 TAILQ_FOREACH(vol, &VolList, entry) {
192 if (vol->vol_no == vol_no)
197 errx(1, "get_volume: Volume %d does not exist!",
202 /* not added to or removed from hammer cache */
207 rel_volume(struct volume_info *volume)
211 /* not added to or removed from hammer cache */
212 --volume->cache.refs;
216 * Acquire the specified buffer. isnew is -1 only when called
217 * via get_buffer_readahead() to prevent another readahead.
220 get_buffer(hammer_off_t buf_offset, int isnew)
223 struct buffer_info *buf;
224 struct volume_info *volume;
225 hammer_off_t orig_offset = buf_offset;
231 zone = HAMMER_ZONE_DECODE(buf_offset);
232 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
233 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
235 if (buf_offset == HAMMER_OFF_BAD)
238 if (AssertOnFailure) {
239 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
240 HAMMER_ZONE_RAW_BUFFER);
242 vol_no = HAMMER_VOL_DECODE(buf_offset);
243 volume = get_volume(vol_no);
247 buf_offset &= ~HAMMER_BUFMASK64;
248 buf = find_buffer(volume, buf_offset);
251 buf = malloc(sizeof(*buf));
252 bzero(buf, sizeof(*buf));
254 fprintf(stderr, "get_buffer: %016llx %016llx at %p\n",
255 (long long)orig_offset, (long long)buf_offset,
258 buf->buf_offset = buf_offset;
259 buf->raw_offset = volume->ondisk->vol_buf_beg +
260 (buf_offset & HAMMER_OFF_SHORT_MASK);
261 buf->volume = volume;
262 hi = buffer_hash(buf_offset);
263 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
264 ++volume->cache.refs;
265 buf->cache.u.buffer = buf;
266 hammer_cache_add(&buf->cache, ISBUFFER);
270 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
271 (long long)orig_offset, (long long)buf_offset,
274 hammer_cache_used(&buf->cache);
278 hammer_cache_flush();
279 if ((ondisk = buf->ondisk) == NULL) {
280 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
282 n = readhammerbuf(volume, ondisk, buf->raw_offset);
285 err(1, "get_buffer: %s:%016llx "
286 "Read failed at offset %016llx",
288 (long long)buf->buf_offset,
289 (long long)buf->raw_offset);
290 bzero(ondisk, HAMMER_BUFSIZE);
295 bzero(ondisk, HAMMER_BUFSIZE);
296 buf->cache.modified = 1;
299 get_buffer_readahead(buf);
304 get_buffer_readahead(struct buffer_info *base)
306 struct buffer_info *buf;
307 struct volume_info *vol;
308 hammer_off_t buf_offset;
310 int ri = UseReadBehind;
311 int re = UseReadAhead;
313 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
317 if (raw_offset >= vol->ondisk->vol_buf_end)
319 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
321 raw_offset += HAMMER_BUFSIZE;
324 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
325 raw_offset - vol->ondisk->vol_buf_beg);
326 buf = find_buffer(vol, buf_offset);
328 buf = get_buffer(buf_offset, -1);
332 raw_offset += HAMMER_BUFSIZE;
337 rel_buffer(struct buffer_info *buffer)
339 struct volume_info *volume;
344 assert(buffer->cache.refs > 0);
345 if (--buffer->cache.refs == 0) {
346 if (buffer->cache.delete) {
347 hi = buffer_hash(buffer->buf_offset);
348 volume = buffer->volume;
349 if (buffer->cache.modified)
350 flush_buffer(buffer);
351 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
352 hammer_cache_del(&buffer->cache);
353 free(buffer->ondisk);
361 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
362 * bufferp is freed if isnew or the offset is out of range of the cached data.
363 * If bufferp is freed a referenced buffer is loaded into it.
366 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
369 if (*bufferp != NULL) {
371 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
372 rel_buffer(*bufferp);
376 return(get_ondisk(buf_offset, bufferp, isnew));
380 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
381 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
384 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
386 if (*bufferp != NULL) {
387 rel_buffer(*bufferp);
390 return(get_ondisk(node_offset, bufferp, 0));
394 * Return a pointer to a buffer data given a buffer offset.
395 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
399 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
402 struct buffer_info *buffer;
405 if (buffer == NULL) {
406 buffer = *bufferp = get_buffer(buf_offset, isnew);
411 return((char *)buffer->ondisk +
412 ((int32_t)buf_offset & HAMMER_BUFMASK));
416 * Allocate HAMMER elements - btree nodes, meta data, data storage
419 alloc_btree_element(hammer_off_t *offp,
420 struct buffer_info **data_bufferp)
422 hammer_node_ondisk_t node;
424 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
426 bzero(node, sizeof(*node));
431 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
432 struct buffer_info **data_bufferp)
436 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
438 bzero(data, data_len);
443 * The only data_len supported by HAMMER userspace for large data zone
444 * (zone 10) is HAMMER_BUFSIZE which is 16KB. >16KB data does not fit
445 * in a buffer allocated by get_buffer(). Also alloc_blockmap() does
446 * not consider >16KB buffer size.
449 alloc_data_element(hammer_off_t *offp, int32_t data_len,
450 struct buffer_info **data_bufferp)
454 if (data_len >= HAMMER_BUFSIZE) {
455 assert(data_len == HAMMER_BUFSIZE); /* just one buffer */
456 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
458 bzero(data, data_len);
459 } else if (data_len) {
460 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
462 bzero(data, data_len);
470 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
471 * code will load each volume's freemap.
474 format_freemap(struct volume_info *root_vol)
476 struct buffer_info *buffer = NULL;
477 hammer_off_t layer1_offset;
478 hammer_blockmap_t blockmap;
479 struct hammer_blockmap_layer1 *layer1;
482 /* Only root volume needs formatting */
483 assert(root_vol->vol_no == RootVolNo);
485 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
486 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
487 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
488 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
490 bzero(layer1, sizeof(*layer1));
491 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
492 layer1->blocks_free = 0;
493 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
497 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
498 bzero(blockmap, sizeof(*blockmap));
499 blockmap->phys_offset = layer1_offset;
500 blockmap->first_offset = 0;
501 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
502 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
503 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
504 root_vol->cache.modified = 1;
508 * Load the volume's remaining free space into the freemap.
510 * Returns the number of big-blocks available.
513 initialize_freemap(struct volume_info *vol)
515 struct volume_info *root_vol;
516 struct buffer_info *buffer1 = NULL;
517 struct buffer_info *buffer2 = NULL;
518 struct hammer_blockmap_layer1 *layer1;
519 struct hammer_blockmap_layer2 *layer2;
520 hammer_off_t layer1_base;
521 hammer_off_t layer1_offset;
522 hammer_off_t layer2_offset;
523 hammer_off_t phys_offset;
524 hammer_off_t aligned_vol_free_end;
525 hammer_blockmap_t freemap;
528 root_vol = get_volume(RootVolNo);
529 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
530 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
532 printf("initialize freemap volume %d\n", vol->vol_no);
535 * Initialize the freemap. First preallocate the big-blocks required
536 * to implement layer2. This preallocation is a bootstrap allocation
537 * using blocks from the target volume.
539 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
540 layer1_base = freemap->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) {
564 layer1_offset = layer1_base +
565 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
566 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
568 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
569 layer2_offset = layer1->phys_offset +
570 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
572 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
573 bzero(layer2, sizeof(*layer2));
574 if (phys_offset < vol->vol_free_off) {
576 * Fixups XXX - big-blocks already allocated as part
577 * of the freemap bootstrap.
579 if (layer2->zone == 0) {
580 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
581 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
582 layer2->bytes_free = 0;
584 } else if (phys_offset < vol->vol_free_end) {
585 ++layer1->blocks_free;
586 layer1->layer1_crc = crc32(layer1,
587 HAMMER_LAYER1_CRCSIZE);
588 buffer1->cache.modified = 1;
590 layer2->append_off = 0;
591 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;
603 rel_volume(root_vol);
608 * Returns the number of big-blocks available for filesystem data and undos
609 * without formatting.
612 count_freemap(struct volume_info *vol)
614 hammer_off_t phys_offset;
615 hammer_off_t vol_free_off;
616 hammer_off_t aligned_vol_free_end;
619 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
620 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
621 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
623 if (vol->vol_no == RootVolNo)
624 vol_free_off += HAMMER_BIGBLOCK_SIZE;
626 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
627 phys_offset < aligned_vol_free_end;
628 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
629 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_BIGBLOCK_SIZE) {
635 if (phys_offset < vol_free_off) {
637 } else if (phys_offset < vol->vol_free_end) {
646 * Allocate big-blocks using our poor-man's volume->vol_free_off.
648 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
649 * itself and cannot update it yet.
652 alloc_bigblock(struct volume_info *volume, int zone)
654 struct buffer_info *buffer1 = NULL;
655 struct buffer_info *buffer2 = NULL;
656 struct volume_info *root_vol;
657 hammer_off_t result_offset;
658 hammer_off_t layer_offset;
659 hammer_blockmap_t freemap;
660 struct hammer_blockmap_layer1 *layer1;
661 struct hammer_blockmap_layer2 *layer2;
664 volume = get_volume(RootVolNo);
666 result_offset = volume->vol_free_off;
667 if (result_offset >= volume->vol_free_end)
668 errx(1, "alloc_bigblock: Ran out of room, filesystem too small");
669 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
672 * Update the freemap.
674 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
675 root_vol = get_volume(RootVolNo);
676 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
677 layer_offset = freemap->phys_offset +
678 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
679 layer1 = get_buffer_data(layer_offset, &buffer1, 0);
680 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
681 --layer1->blocks_free;
682 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
683 buffer1->cache.modified = 1;
684 layer_offset = layer1->phys_offset +
685 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
686 layer2 = get_buffer_data(layer_offset, &buffer2, 0);
687 assert(layer2->zone == 0);
689 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
690 layer2->bytes_free = 0;
691 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
692 buffer2->cache.modified = 1;
694 --root_vol->ondisk->vol0_stat_freebigblocks;
695 root_vol->cache.modified = 1;
699 rel_volume(root_vol);
703 return(result_offset);
707 * Format the undomap for the root volume.
710 format_undomap(struct volume_info *root_vol)
712 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
713 hammer_off_t undo_limit;
714 hammer_blockmap_t blockmap;
715 struct hammer_volume_ondisk *ondisk;
716 struct buffer_info *buffer = NULL;
722 /* Only root volume needs formatting */
723 assert(root_vol->vol_no == RootVolNo);
724 ondisk = root_vol->ondisk;
727 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
728 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
731 * The minimum UNDO fifo size is 500MB, or approximately 1% of
732 * the recommended 50G disk.
734 * Changing this minimum is rather dangerous as complex filesystem
735 * operations can cause the UNDO FIFO to fill up otherwise.
737 undo_limit = UndoBufferSize;
738 if (undo_limit == 0) {
739 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
740 if (undo_limit < 500*1024*1024)
741 undo_limit = 500*1024*1024;
743 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
744 ~HAMMER_BIGBLOCK_MASK64;
745 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
746 undo_limit = HAMMER_BIGBLOCK_SIZE;
747 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
748 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
749 UndoBufferSize = undo_limit;
751 blockmap = &ondisk->vol0_blockmap[undo_zone];
752 bzero(blockmap, sizeof(*blockmap));
753 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
754 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
755 blockmap->next_offset = blockmap->first_offset;
756 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
757 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
759 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
760 assert(limit_index <= HAMMER_UNDO_LAYER2);
762 for (n = 0; n < limit_index; ++n) {
763 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
764 HAMMER_ZONE_UNDO_INDEX);
766 while (n < HAMMER_UNDO_LAYER2) {
767 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
771 * Pre-initialize the UNDO blocks (HAMMER version 4+)
773 printf("initializing the undo map (%jd MB)\n",
774 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
777 scan = blockmap->first_offset;
780 while (scan < blockmap->alloc_offset) {
781 hammer_fifo_head_t head;
782 hammer_fifo_tail_t tail;
784 int bytes = HAMMER_UNDO_ALIGN;
786 isnew = ((scan & HAMMER_BUFMASK64) == 0);
787 head = get_buffer_data(scan, &buffer, isnew);
788 buffer->cache.modified = 1;
789 tail = (void *)((char *)head + bytes - sizeof(*tail));
792 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
793 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
794 head->hdr_size = bytes;
795 head->hdr_seq = seqno++;
797 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
798 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
799 tail->tail_size = bytes;
801 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
802 crc32(head + 1, bytes - sizeof(*head));
810 * Format a new blockmap. This is mostly a degenerate case because
811 * all allocations are now actually done from the freemap.
814 format_blockmap(hammer_blockmap_t blockmap, int zone, hammer_off_t offset)
816 hammer_off_t zone_base = HAMMER_ZONE_ENCODE(zone, offset);
818 bzero(blockmap, sizeof(*blockmap));
819 blockmap->phys_offset = 0;
820 blockmap->first_offset = zone_base;
821 blockmap->next_offset = zone_base;
822 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
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 chunk_offset;
847 volume = get_volume(RootVolNo);
849 blockmap = &volume->ondisk->vol0_blockmap[zone];
850 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
851 assert(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
854 * Alignment and buffer-boundary issues. If the allocation would
855 * cross a buffer boundary we have to skip to the next buffer.
857 bytes = (bytes + 15) & ~15;
858 assert(bytes > 0 && bytes <= HAMMER_BUFSIZE); /* not HAMMER_XBUFSIZE */
859 assert(zone >= HAMMER_ZONE2_MAPPED_INDEX && zone < HAMMER_MAX_ZONES);
862 assert(blockmap->next_offset != HAMMER_ZONE_ENCODE(zone + 1, 0));
864 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
866 volume->cache.modified = 1;
867 blockmap->next_offset = (blockmap->next_offset + bytes - 1) &
870 chunk_offset = blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
875 layer1_offset = freemap->phys_offset +
876 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
878 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
879 assert(!(chunk_offset == 0 && layer1->blocks_free == 0));
881 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
882 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
887 * Dive layer 2, each entry represents a big-block.
889 layer2_offset = layer1->phys_offset +
890 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
892 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
894 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
895 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
900 * If we are entering a new big-block assign ownership to our
901 * zone. If the big-block is owned by another zone skip it.
903 if (layer2->zone == 0) {
904 --layer1->blocks_free;
905 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
907 --volume->ondisk->vol0_stat_freebigblocks;
908 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
909 assert(layer2->append_off == 0);
911 if (layer2->zone != zone) {
912 volume->cache.modified = 1;
913 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
914 ~HAMMER_BIGBLOCK_MASK64;
918 buffer1->cache.modified = 1;
919 buffer2->cache.modified = 1;
920 volume->cache.modified = 1;
921 assert(layer2->append_off == chunk_offset);
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 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
930 ptr = get_buffer_data(*result_offp, bufferp, 0);
931 (*bufferp)->cache.modified = 1;
940 * Flush various tracking structures to disk
943 flush_all_volumes(void)
945 struct volume_info *vol;
947 TAILQ_FOREACH(vol, &VolList, entry)
952 flush_volume(struct volume_info *volume)
954 struct buffer_info *buffer;
957 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
958 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
959 flush_buffer(buffer);
961 writehammerbuf(volume, volume->ondisk, 0);
962 volume->cache.modified = 0;
966 flush_buffer(struct buffer_info *buffer)
968 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
969 buffer->cache.modified = 0;
973 * Core I/O operations
976 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
980 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
981 if (n != HAMMER_BUFSIZE)
987 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
991 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
992 if (n != HAMMER_BUFSIZE)
993 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);