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
36 #include <sys/diskslice.h>
37 #include <sys/diskmbr.h>
48 #include "hammer_util.h"
50 static void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
51 struct buffer_info **bufferp);
52 static hammer_off_t alloc_bigblock(struct volume_info *volume, int zone);
53 static void get_buffer_readahead(struct buffer_info *base);
54 static __inline void *get_ondisk(hammer_off_t buf_offset,
55 struct buffer_info **bufferp, int isnew);
56 static int readhammerbuf(struct volume_info *vol, void *data, int64_t offset);
57 static void writehammerbuf(struct volume_info *vol, const void *data,
66 int64_t UndoBufferSize;
67 int UsingSuperClusters;
70 int UseReadBehind = -4;
72 int AssertOnFailure = 1;
73 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
77 buffer_hash(hammer_off_t buf_offset)
81 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
85 static struct buffer_info*
86 find_buffer(struct volume_info *volume, hammer_off_t buf_offset)
89 struct buffer_info *buf;
91 hi = buffer_hash(buf_offset);
92 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
93 if (buf->buf_offset == buf_offset)
99 * Lookup the requested information structure and related on-disk buffer.
100 * Missing structures are created.
103 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
105 struct volume_info *vol;
106 struct volume_info *scan;
107 struct hammer_volume_ondisk *ondisk;
109 struct stat st1, st2;
112 * Allocate the volume structure
114 vol = malloc(sizeof(*vol));
115 bzero(vol, sizeof(*vol));
116 for (i = 0; i < HAMMER_BUFLISTS; ++i)
117 TAILQ_INIT(&vol->buffer_lists[i]);
118 vol->name = strdup(filename);
119 vol->fd = open(vol->name, oflags);
121 err(1, "setup_volume: %s: Open failed", vol->name);
125 * Read or initialize the volume header
127 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
129 bzero(ondisk, HAMMER_BUFSIZE);
131 n = readhammerbuf(vol, ondisk, 0);
133 err(1, "setup_volume: %s: Read failed at offset 0",
136 vol_no = ondisk->vol_no;
138 RootVolNo = ondisk->vol_rootvol;
139 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
140 errx(1, "setup_volume: %s: root volume disagreement: "
142 vol->name, RootVolNo, ondisk->vol_rootvol);
145 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
146 errx(1, "setup_volume: %s: Header does not indicate "
147 "that this is a hammer volume", vol->name);
149 if (TAILQ_EMPTY(&VolList)) {
150 Hammer_FSId = vol->ondisk->vol_fsid;
151 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
152 errx(1, "setup_volume: %s: FSId does match other "
153 "volumes!", vol->name);
156 vol->vol_no = vol_no;
159 vol->cache.modified = 1;
162 if (fstat(vol->fd, &st1) != 0){
163 errx(1, "setup_volume: %s: Failed to stat", vol->name);
167 * Link the volume structure in
169 TAILQ_FOREACH(scan, &VolList, entry) {
170 if (scan->vol_no == vol_no) {
171 errx(1, "setup_volume: %s: Duplicate volume number %d "
172 "against %s", vol->name, vol_no, scan->name);
174 if (fstat(scan->fd, &st2) != 0){
175 errx(1, "setup_volume: %s: Failed to stat %s",
176 vol->name, scan->name);
178 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
179 errx(1, "setup_volume: %s: Specified more than once",
183 TAILQ_INSERT_TAIL(&VolList, vol, entry);
188 * Check basic volume characteristics.
191 check_volume(struct volume_info *vol)
193 struct partinfo pinfo;
197 * Get basic information about the volume
199 if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
201 * Allow the formatting of regular files as HAMMER volumes
203 if (fstat(vol->fd, &st) < 0)
204 err(1, "Unable to stat %s", vol->name);
205 vol->size = st.st_size;
206 vol->type = "REGFILE";
209 * When formatting a block device as a HAMMER volume the
210 * sector size must be compatible. HAMMER uses 16384 byte
211 * filesystem buffers.
213 if (pinfo.reserved_blocks) {
214 errx(1, "HAMMER cannot be placed in a partition "
215 "which overlaps the disklabel or MBR");
217 if (pinfo.media_blksize > HAMMER_BUFSIZE ||
218 HAMMER_BUFSIZE % pinfo.media_blksize) {
219 errx(1, "A media sector size of %d is not supported",
220 pinfo.media_blksize);
223 vol->size = pinfo.media_size;
224 vol->device_offset = pinfo.media_offset;
225 vol->type = "DEVICE";
229 * Reserve space for (future) header junk, setup our poor-man's
230 * big-block allocator.
232 vol->vol_alloc = HAMMER_BUFSIZE * 16;
236 get_volume(int32_t vol_no)
238 struct volume_info *vol;
240 TAILQ_FOREACH(vol, &VolList, entry) {
241 if (vol->vol_no == vol_no)
246 errx(1, "get_volume: Volume %d does not exist!",
251 /* not added to or removed from hammer cache */
256 rel_volume(struct volume_info *volume)
260 /* not added to or removed from hammer cache */
261 --volume->cache.refs;
265 * Acquire the specified buffer. isnew is -1 only when called
266 * via get_buffer_readahead() to prevent another readahead.
269 get_buffer(hammer_off_t buf_offset, int isnew)
272 struct buffer_info *buf;
273 struct volume_info *volume;
274 hammer_off_t orig_offset = buf_offset;
280 zone = HAMMER_ZONE_DECODE(buf_offset);
281 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
282 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
284 if (buf_offset == HAMMER_OFF_BAD)
287 if (AssertOnFailure) {
288 assert((buf_offset & HAMMER_OFF_ZONE_MASK) ==
289 HAMMER_ZONE_RAW_BUFFER);
291 vol_no = HAMMER_VOL_DECODE(buf_offset);
292 volume = get_volume(vol_no);
296 buf_offset &= ~HAMMER_BUFMASK64;
297 buf = find_buffer(volume, buf_offset);
300 buf = malloc(sizeof(*buf));
301 bzero(buf, sizeof(*buf));
303 fprintf(stderr, "get_buffer: %016llx %016llx at %p\n",
304 (long long)orig_offset, (long long)buf_offset,
307 buf->buf_offset = buf_offset;
308 buf->raw_offset = volume->ondisk->vol_buf_beg +
309 (buf_offset & HAMMER_OFF_SHORT_MASK);
310 buf->volume = volume;
311 hi = buffer_hash(buf_offset);
312 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
313 ++volume->cache.refs;
314 buf->cache.u.buffer = buf;
315 hammer_cache_add(&buf->cache, ISBUFFER);
319 fprintf(stderr, "get_buffer: %016llx %016llx at %p *\n",
320 (long long)orig_offset, (long long)buf_offset,
323 hammer_cache_used(&buf->cache);
327 hammer_cache_flush();
328 if ((ondisk = buf->ondisk) == NULL) {
329 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
331 n = readhammerbuf(volume, ondisk, buf->raw_offset);
334 err(1, "get_buffer: %s:%016llx "
335 "Read failed at offset %016llx",
337 (long long)buf->buf_offset,
338 (long long)buf->raw_offset);
339 bzero(ondisk, HAMMER_BUFSIZE);
344 bzero(ondisk, HAMMER_BUFSIZE);
345 buf->cache.modified = 1;
348 get_buffer_readahead(buf);
353 get_buffer_readahead(struct buffer_info *base)
355 struct buffer_info *buf;
356 struct volume_info *vol;
357 hammer_off_t buf_offset;
359 int ri = UseReadBehind;
360 int re = UseReadAhead;
362 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
366 if (raw_offset >= vol->ondisk->vol_buf_end)
368 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
370 raw_offset += HAMMER_BUFSIZE;
373 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
374 raw_offset - vol->ondisk->vol_buf_beg);
375 buf = find_buffer(vol, buf_offset);
377 buf = get_buffer(buf_offset, -1);
381 raw_offset += HAMMER_BUFSIZE;
386 rel_buffer(struct buffer_info *buffer)
388 struct volume_info *volume;
393 assert(buffer->cache.refs > 0);
394 if (--buffer->cache.refs == 0) {
395 if (buffer->cache.delete) {
396 hi = buffer_hash(buffer->buf_offset);
397 volume = buffer->volume;
398 if (buffer->cache.modified)
399 flush_buffer(buffer);
400 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
401 hammer_cache_del(&buffer->cache);
402 free(buffer->ondisk);
410 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
411 * bufferp is freed if isnew or the offset is out of range of the cached data.
412 * If bufferp is freed a referenced buffer is loaded into it.
415 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
418 if (*bufferp != NULL) {
420 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
421 rel_buffer(*bufferp);
425 return(get_ondisk(buf_offset, bufferp, isnew));
429 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
430 * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
433 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
435 if (*bufferp != NULL) {
436 rel_buffer(*bufferp);
439 return(get_ondisk(node_offset, bufferp, 0));
443 * Return a pointer to a buffer data given a buffer offset.
444 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
448 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew)
450 struct buffer_info *buffer;
453 if (buffer == NULL) {
454 buffer = *bufferp = get_buffer(buf_offset, isnew);
459 return((char *)buffer->ondisk +
460 ((int32_t)buf_offset & HAMMER_BUFMASK));
464 * Allocate HAMMER elements - btree nodes, meta data, data storage
467 alloc_btree_element(hammer_off_t *offp, struct buffer_info **data_bufferp)
469 hammer_node_ondisk_t node;
471 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
473 bzero(node, sizeof(*node));
478 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
479 struct buffer_info **data_bufferp)
483 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
485 bzero(data, data_len);
490 * The only data_len supported by HAMMER userspace for large data zone
491 * (zone 10) is HAMMER_BUFSIZE which is 16KB. >16KB data does not fit
492 * in a buffer allocated by get_buffer(). Also alloc_blockmap() does
493 * not consider >16KB buffer size.
496 alloc_data_element(hammer_off_t *offp, int32_t data_len,
497 struct buffer_info **data_bufferp)
501 if (data_len >= HAMMER_BUFSIZE) {
502 assert(data_len == HAMMER_BUFSIZE); /* just one buffer */
503 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
505 bzero(data, data_len);
506 } else if (data_len) {
507 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
509 bzero(data, data_len);
517 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
518 * code will load each volume's freemap.
521 format_freemap(struct volume_info *root_vol)
523 struct buffer_info *buffer = NULL;
524 hammer_off_t layer1_offset;
525 hammer_blockmap_t blockmap;
526 struct hammer_blockmap_layer1 *layer1;
529 /* Only root volume needs formatting */
530 assert(root_vol->vol_no == RootVolNo);
532 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
533 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
534 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
535 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
537 bzero(layer1, sizeof(*layer1));
538 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
539 layer1->blocks_free = 0;
540 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
544 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
545 bzero(blockmap, sizeof(*blockmap));
546 blockmap->phys_offset = layer1_offset;
547 blockmap->first_offset = 0;
548 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
549 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
550 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
551 root_vol->cache.modified = 1;
555 * Load the volume's remaining free space into the freemap.
557 * Returns the number of big-blocks available.
560 initialize_freemap(struct volume_info *vol)
562 struct volume_info *root_vol;
563 struct buffer_info *buffer1 = NULL;
564 struct buffer_info *buffer2 = NULL;
565 struct hammer_blockmap_layer1 *layer1;
566 struct hammer_blockmap_layer2 *layer2;
567 hammer_off_t layer1_base;
568 hammer_off_t layer1_offset;
569 hammer_off_t layer2_offset;
570 hammer_off_t phys_offset;
571 hammer_off_t block_offset;
572 hammer_off_t aligned_vol_free_end;
573 hammer_blockmap_t freemap;
575 int64_t layer1_count = 0;
577 root_vol = get_volume(RootVolNo);
578 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
579 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
581 printf("initialize freemap volume %d\n", vol->vol_no);
584 * Initialize the freemap. First preallocate the big-blocks required
585 * to implement layer2. This preallocation is a bootstrap allocation
586 * using blocks from the target volume.
588 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
589 layer1_base = freemap->phys_offset;
591 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
592 phys_offset < aligned_vol_free_end;
593 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
594 layer1_offset = layer1_base +
595 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
596 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
597 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
598 layer1->phys_offset = alloc_bigblock(vol,
599 HAMMER_ZONE_FREEMAP_INDEX);
600 layer1->blocks_free = 0;
601 buffer1->cache.modified = 1;
602 layer1->layer1_crc = crc32(layer1,
603 HAMMER_LAYER1_CRCSIZE);
608 * Now fill everything in.
610 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
611 phys_offset < aligned_vol_free_end;
612 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
614 layer1_offset = layer1_base +
615 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
616 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
617 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
619 for (block_offset = 0;
620 block_offset < HAMMER_BLOCKMAP_LAYER2;
621 block_offset += HAMMER_BIGBLOCK_SIZE) {
622 layer2_offset = layer1->phys_offset +
623 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
624 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
625 bzero(layer2, sizeof(*layer2));
627 if (phys_offset + block_offset < vol->vol_free_off) {
629 * Fixups XXX - big-blocks already allocated as part
630 * of the freemap bootstrap.
632 if (layer2->zone == 0) {
633 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
634 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
635 layer2->bytes_free = 0;
637 } else if (phys_offset + block_offset < vol->vol_free_end) {
639 layer2->append_off = 0;
640 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
644 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
645 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
646 layer2->bytes_free = 0;
648 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
649 buffer2->cache.modified = 1;
652 layer1->blocks_free += layer1_count;
653 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
654 buffer1->cache.modified = 1;
659 rel_volume(root_vol);
664 * Returns the number of big-blocks available for filesystem data and undos
665 * without formatting.
668 count_freemap(struct volume_info *vol)
670 hammer_off_t phys_offset;
671 hammer_off_t vol_free_off;
672 hammer_off_t aligned_vol_free_end;
675 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
676 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
677 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
679 if (vol->vol_no == RootVolNo)
680 vol_free_off += HAMMER_BIGBLOCK_SIZE;
682 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
683 phys_offset < aligned_vol_free_end;
684 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
685 vol_free_off += HAMMER_BIGBLOCK_SIZE;
688 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
689 phys_offset < aligned_vol_free_end;
690 phys_offset += HAMMER_BIGBLOCK_SIZE) {
691 if (phys_offset < vol_free_off) {
693 } else if (phys_offset < vol->vol_free_end) {
702 * Allocate big-blocks using our poor-man's volume->vol_free_off.
704 * If the zone is HAMMER_ZONE_FREEMAP_INDEX we are bootstrapping the freemap
705 * itself and cannot update it yet.
708 alloc_bigblock(struct volume_info *volume, int zone)
710 struct buffer_info *buffer1 = NULL;
711 struct buffer_info *buffer2 = NULL;
712 struct volume_info *root_vol;
713 hammer_off_t result_offset;
714 hammer_off_t layer_offset;
715 hammer_blockmap_t freemap;
716 struct hammer_blockmap_layer1 *layer1;
717 struct hammer_blockmap_layer2 *layer2;
720 volume = get_volume(RootVolNo);
722 result_offset = volume->vol_free_off;
723 if (result_offset >= volume->vol_free_end)
724 errx(1, "alloc_bigblock: Ran out of room, filesystem too small");
725 volume->vol_free_off += HAMMER_BIGBLOCK_SIZE;
728 * Update the freemap.
730 if (zone != HAMMER_ZONE_FREEMAP_INDEX) {
731 root_vol = get_volume(RootVolNo);
732 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
733 layer_offset = freemap->phys_offset +
734 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
735 layer1 = get_buffer_data(layer_offset, &buffer1, 0);
736 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
737 --layer1->blocks_free;
738 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
739 buffer1->cache.modified = 1;
740 layer_offset = layer1->phys_offset +
741 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
742 layer2 = get_buffer_data(layer_offset, &buffer2, 0);
743 assert(layer2->zone == 0);
745 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
746 layer2->bytes_free = 0;
747 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
748 buffer2->cache.modified = 1;
750 --root_vol->ondisk->vol0_stat_freebigblocks;
751 root_vol->cache.modified = 1;
755 rel_volume(root_vol);
759 return(result_offset);
763 * Format the undomap for the root volume.
766 format_undomap(struct volume_info *root_vol)
768 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
769 hammer_off_t undo_limit;
770 hammer_blockmap_t blockmap;
771 struct hammer_volume_ondisk *ondisk;
772 struct buffer_info *buffer = NULL;
778 /* Only root volume needs formatting */
779 assert(root_vol->vol_no == RootVolNo);
780 ondisk = root_vol->ondisk;
783 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
784 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
787 * The minimum UNDO fifo size is 500MB, or approximately 1% of
788 * the recommended 50G disk.
790 * Changing this minimum is rather dangerous as complex filesystem
791 * operations can cause the UNDO FIFO to fill up otherwise.
793 undo_limit = UndoBufferSize;
794 if (undo_limit == 0) {
795 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
796 if (undo_limit < 500*1024*1024)
797 undo_limit = 500*1024*1024;
799 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
800 ~HAMMER_BIGBLOCK_MASK64;
801 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
802 undo_limit = HAMMER_BIGBLOCK_SIZE;
803 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
804 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
805 UndoBufferSize = undo_limit;
807 blockmap = &ondisk->vol0_blockmap[undo_zone];
808 bzero(blockmap, sizeof(*blockmap));
809 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
810 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
811 blockmap->next_offset = blockmap->first_offset;
812 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
813 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
815 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
816 assert(limit_index <= HAMMER_UNDO_LAYER2);
818 for (n = 0; n < limit_index; ++n) {
819 ondisk->vol0_undo_array[n] = alloc_bigblock(NULL,
820 HAMMER_ZONE_UNDO_INDEX);
822 while (n < HAMMER_UNDO_LAYER2) {
823 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
827 * Pre-initialize the UNDO blocks (HAMMER version 4+)
829 printf("initializing the undo map (%jd MB)\n",
830 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
833 scan = blockmap->first_offset;
836 while (scan < blockmap->alloc_offset) {
837 hammer_fifo_head_t head;
838 hammer_fifo_tail_t tail;
840 int bytes = HAMMER_UNDO_ALIGN;
842 isnew = ((scan & HAMMER_BUFMASK64) == 0);
843 head = get_buffer_data(scan, &buffer, isnew);
844 buffer->cache.modified = 1;
845 tail = (void *)((char *)head + bytes - sizeof(*tail));
848 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
849 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
850 head->hdr_size = bytes;
851 head->hdr_seq = seqno++;
853 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
854 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
855 tail->tail_size = bytes;
857 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
858 crc32(head + 1, bytes - sizeof(*head));
866 * Format a new blockmap. This is mostly a degenerate case because
867 * all allocations are now actually done from the freemap.
870 format_blockmap(hammer_blockmap_t blockmap, int zone, hammer_off_t offset)
872 hammer_off_t zone_base = HAMMER_ZONE_ENCODE(zone, offset);
874 bzero(blockmap, sizeof(*blockmap));
875 blockmap->phys_offset = 0;
876 blockmap->first_offset = zone_base;
877 blockmap->next_offset = zone_base;
878 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
879 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
883 * Allocate a chunk of data out of a blockmap. This is a simplified
884 * version which uses next_offset as a simple allocation iterator.
888 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
889 struct buffer_info **bufferp)
891 struct buffer_info *buffer1 = NULL;
892 struct buffer_info *buffer2 = NULL;
893 struct volume_info *volume;
894 hammer_blockmap_t blockmap;
895 hammer_blockmap_t freemap;
896 struct hammer_blockmap_layer1 *layer1;
897 struct hammer_blockmap_layer2 *layer2;
898 hammer_off_t layer1_offset;
899 hammer_off_t layer2_offset;
900 hammer_off_t chunk_offset;
903 volume = get_volume(RootVolNo);
905 blockmap = &volume->ondisk->vol0_blockmap[zone];
906 freemap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
907 assert(HAMMER_ZONE_DECODE(blockmap->next_offset) == zone);
910 * Alignment and buffer-boundary issues. If the allocation would
911 * cross a buffer boundary we have to skip to the next buffer.
913 bytes = (bytes + 15) & ~15;
914 assert(bytes > 0 && bytes <= HAMMER_BUFSIZE); /* not HAMMER_XBUFSIZE */
915 assert(zone >= HAMMER_ZONE2_MAPPED_INDEX && zone < HAMMER_MAX_ZONES);
918 assert(blockmap->next_offset != HAMMER_ZONE_ENCODE(zone + 1, 0));
920 if ((blockmap->next_offset ^ (blockmap->next_offset + bytes - 1)) &
922 volume->cache.modified = 1;
923 blockmap->next_offset = (blockmap->next_offset + bytes - 1) &
926 chunk_offset = blockmap->next_offset & HAMMER_BIGBLOCK_MASK;
931 layer1_offset = freemap->phys_offset +
932 HAMMER_BLOCKMAP_LAYER1_OFFSET(blockmap->next_offset);
934 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
935 assert(!(chunk_offset == 0 && layer1->blocks_free == 0));
937 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
938 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
943 * Dive layer 2, each entry represents a big-block.
945 layer2_offset = layer1->phys_offset +
946 HAMMER_BLOCKMAP_LAYER2_OFFSET(blockmap->next_offset);
948 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
950 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
951 fprintf(stderr, "alloc_blockmap: ran out of space!\n");
956 * If we are entering a new big-block assign ownership to our
957 * zone. If the big-block is owned by another zone skip it.
959 if (layer2->zone == 0) {
960 --layer1->blocks_free;
961 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
963 --volume->ondisk->vol0_stat_freebigblocks;
964 assert(layer2->bytes_free == HAMMER_BIGBLOCK_SIZE);
965 assert(layer2->append_off == 0);
967 if (layer2->zone != zone) {
968 volume->cache.modified = 1;
969 blockmap->next_offset = (blockmap->next_offset + HAMMER_BIGBLOCK_SIZE) &
970 ~HAMMER_BIGBLOCK_MASK64;
974 buffer1->cache.modified = 1;
975 buffer2->cache.modified = 1;
976 volume->cache.modified = 1;
977 assert(layer2->append_off == chunk_offset);
978 layer2->bytes_free -= bytes;
979 *result_offp = blockmap->next_offset;
980 blockmap->next_offset += bytes;
981 layer2->append_off = (int)blockmap->next_offset &
982 HAMMER_BIGBLOCK_MASK;
984 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
986 ptr = get_buffer_data(*result_offp, bufferp, 0);
987 (*bufferp)->cache.modified = 1;
996 * Flush various tracking structures to disk
999 flush_all_volumes(void)
1001 struct volume_info *vol;
1003 TAILQ_FOREACH(vol, &VolList, entry)
1008 flush_volume(struct volume_info *volume)
1010 struct buffer_info *buffer;
1013 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
1014 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
1015 flush_buffer(buffer);
1017 writehammerbuf(volume, volume->ondisk, 0);
1018 volume->cache.modified = 0;
1022 flush_buffer(struct buffer_info *buffer)
1024 writehammerbuf(buffer->volume, buffer->ondisk, buffer->raw_offset);
1025 buffer->cache.modified = 0;
1029 * Core I/O operations
1032 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
1036 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
1037 if (n != HAMMER_BUFSIZE)
1043 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
1047 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
1048 if (n != HAMMER_BUFSIZE)
1049 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
1052 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
1055 value = HAMMER_BOOT_NOMBYTES;
1056 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
1058 if (value < HAMMER_BOOT_MINBYTES)
1060 } else if (value < HAMMER_BOOT_MINBYTES) {
1061 value = HAMMER_BOOT_MINBYTES;
1067 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size)
1070 value = HAMMER_MEM_NOMBYTES;
1071 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
1073 if (value < HAMMER_MEM_MINBYTES)
1075 } else if (value < HAMMER_MEM_MINBYTES) {
1076 value = HAMMER_MEM_MINBYTES;