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/diskslice.h>
36 #include <sys/diskmbr.h>
38 #include "hammer_util.h"
40 static void check_volume(struct volume_info *vol);
41 static void get_buffer_readahead(struct buffer_info *base);
42 static void *get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
44 static __inline int readhammervol(struct volume_info *vol);
45 static __inline int readhammerbuf(struct buffer_info *buf);
46 static __inline int writehammervol(struct volume_info *vol);
47 static __inline int writehammerbuf(struct buffer_info *buf);
51 int UseReadBehind = -4;
55 TAILQ_HEAD(volume_list, volume_info);
56 static struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
57 static int valid_hammer_volumes;
61 buffer_hash(hammer_off_t buf_offset)
65 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
69 static struct buffer_info*
70 find_buffer(struct volume_info *volume, hammer_off_t buf_offset)
73 struct buffer_info *buf;
75 hi = buffer_hash(buf_offset);
76 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
77 if (buf->buf_offset == buf_offset)
84 __alloc_volume(const char *volname, int oflags)
86 struct volume_info *vol;
89 vol = malloc(sizeof(*vol));
91 err(1, "alloc_volume");
92 bzero(vol, sizeof(*vol));
95 vol->rdonly = (oflags == O_RDONLY);
96 vol->name = strdup(volname);
97 vol->fd = open(vol->name, oflags);
99 err(1, "alloc_volume: Failed to open %s", vol->name);
102 vol->ondisk = malloc(HAMMER_BUFSIZE);
103 if (vol->ondisk == NULL)
104 err(1, "alloc_volume");
105 bzero(vol->ondisk, HAMMER_BUFSIZE);
107 for (i = 0; i < HAMMER_BUFLISTS; ++i)
108 TAILQ_INIT(&vol->buffer_lists[i]);
114 __add_volume(struct volume_info *vol)
116 struct volume_info *scan;
117 struct stat st1, st2;
119 if (fstat(vol->fd, &st1) != 0)
120 errx(1, "add_volume: %s: Failed to stat", vol->name);
122 TAILQ_FOREACH(scan, &VolList, entry) {
123 if (scan->vol_no == vol->vol_no) {
124 errx(1, "add_volume: %s: Duplicate volume number %d "
126 vol->name, vol->vol_no, scan->name);
128 if (fstat(scan->fd, &st2) != 0) {
129 errx(1, "add_volume: %s: Failed to stat %s",
130 vol->name, scan->name);
132 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
133 errx(1, "add_volume: %s: Specified more than once",
138 TAILQ_INSERT_TAIL(&VolList, vol, entry);
142 * Initialize a volume structure and ondisk vol_no field.
145 init_volume(int32_t vol_no, const char *filename, int oflags)
147 struct volume_info *vol;
149 vol = __alloc_volume(filename, oflags);
150 vol->vol_no = vol->ondisk->vol_no = vol_no;
158 * Initialize a volume structure and read ondisk volume header.
161 load_volume(const char *filename, int oflags)
163 struct volume_info *vol;
164 hammer_volume_ondisk_t ondisk;
167 vol = __alloc_volume(filename, oflags);
169 n = readhammervol(vol);
171 err(1, "load_volume: %s: Read failed at offset 0", vol->name);
173 ondisk = vol->ondisk;
174 vol->vol_no = ondisk->vol_no;
176 if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) {
177 errx(1, "load_volume: Invalid root volume# %d",
178 ondisk->vol_rootvol);
181 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType))) {
182 errx(1, "load_volume: %s: Header does not indicate "
183 "that this is a hammer volume", vol->name);
186 if (valid_hammer_volumes++ == 0) {
187 Hammer_FSId = ondisk->vol_fsid;
188 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId))) {
189 errx(1, "load_volume: %s: FSId does match other volumes!",
199 * Check basic volume characteristics.
202 check_volume(struct volume_info *vol)
204 struct partinfo pinfo;
208 * Get basic information about the volume
210 if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
212 * Allow the formatting of regular files as HAMMER volumes
214 if (fstat(vol->fd, &st) < 0)
215 err(1, "Unable to stat %s", vol->name);
216 vol->size = st.st_size;
217 vol->type = "REGFILE";
220 * When formatting a block device as a HAMMER volume the
221 * sector size must be compatible. HAMMER uses 16384 byte
222 * filesystem buffers.
224 if (pinfo.reserved_blocks) {
225 errx(1, "HAMMER cannot be placed in a partition "
226 "which overlaps the disklabel or MBR");
228 if (pinfo.media_blksize > HAMMER_BUFSIZE ||
229 HAMMER_BUFSIZE % pinfo.media_blksize) {
230 errx(1, "A media sector size of %d is not supported",
231 pinfo.media_blksize);
234 vol->size = pinfo.media_size;
235 vol->device_offset = pinfo.media_offset;
236 vol->type = "DEVICE";
241 assert_volume_offset(struct volume_info *vol)
243 assert(hammer_is_zone_raw_buffer(vol->vol_free_off));
244 assert(hammer_is_zone_raw_buffer(vol->vol_free_end));
248 get_volume(int32_t vol_no)
250 struct volume_info *vol;
252 TAILQ_FOREACH(vol, &VolList, entry) {
253 if (vol->vol_no == vol_no)
261 get_root_volume(void)
263 struct volume_info *root_vol;
265 root_vol = get_volume(HAMMER_ROOT_VOLNO);
266 assert(root_vol != NULL);
272 * Acquire the specified buffer. isnew is -1 only when called
273 * via get_buffer_readahead() to prevent another readahead.
275 static struct buffer_info *
276 get_buffer(hammer_off_t buf_offset, int isnew)
278 struct buffer_info *buf;
279 struct volume_info *volume;
286 zone = HAMMER_ZONE_DECODE(buf_offset);
287 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX)
288 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, &error);
289 if (error || buf_offset == HAMMER_OFF_BAD)
291 assert(hammer_is_zone_raw_buffer(buf_offset));
293 vol_no = HAMMER_VOL_DECODE(buf_offset);
294 volume = get_volume(vol_no);
295 assert(volume != NULL);
297 buf_offset &= ~HAMMER_BUFMASK64;
298 buf = find_buffer(volume, buf_offset);
301 buf = malloc(sizeof(*buf));
302 bzero(buf, sizeof(*buf));
303 buf->buf_offset = buf_offset;
304 buf->raw_offset = hammer_xlate_to_phys(volume->ondisk,
306 buf->volume = volume;
307 buf->ondisk = malloc(HAMMER_BUFSIZE);
309 if (readhammerbuf(buf) == -1) {
310 err(1, "get_buffer: %s:%016jx "
311 "Read failed at offset %016jx",
313 (intmax_t)buf->buf_offset,
314 (intmax_t)buf->raw_offset);
318 hi = buffer_hash(buf_offset);
319 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
320 hammer_cache_add(&buf->cache);
323 assert(buf->ondisk != NULL);
325 hammer_cache_used(&buf->cache);
329 hammer_cache_flush();
332 assert(buf->cache.modified == 0);
333 bzero(buf->ondisk, HAMMER_BUFSIZE);
334 buf->cache.modified = 1;
337 get_buffer_readahead(buf);
342 get_buffer_readahead(struct buffer_info *base)
344 struct buffer_info *buf;
345 struct volume_info *vol;
346 hammer_off_t buf_offset;
348 int ri = UseReadBehind;
349 int re = UseReadAhead;
351 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
355 if (raw_offset >= vol->ondisk->vol_buf_end)
357 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
359 raw_offset += HAMMER_BUFSIZE;
362 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
363 raw_offset - vol->ondisk->vol_buf_beg);
364 buf = find_buffer(vol, buf_offset);
366 buf = get_buffer(buf_offset, -1);
370 raw_offset += HAMMER_BUFSIZE;
375 rel_buffer(struct buffer_info *buffer)
377 struct volume_info *volume;
382 assert(buffer->cache.refs > 0);
383 if (--buffer->cache.refs == 0) {
384 if (buffer->cache.delete) {
385 hi = buffer_hash(buffer->buf_offset);
386 volume = buffer->volume;
387 if (buffer->cache.modified)
388 flush_buffer(buffer);
389 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
390 hammer_cache_del(&buffer->cache);
391 free(buffer->ondisk);
398 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
399 * bufferp is freed if isnew or the offset is out of range of the cached data.
400 * If bufferp is freed a referenced buffer is loaded into it.
403 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
406 if (*bufferp != NULL) {
408 (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
409 rel_buffer(*bufferp);
413 return(get_ondisk(buf_offset, bufferp, isnew));
417 * Return a pointer to a buffer data given a buffer offset.
418 * If *bufferp is NULL acquire the buffer otherwise use that buffer.
421 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew)
423 if (*bufferp == NULL) {
424 *bufferp = get_buffer(buf_offset, isnew);
425 if (*bufferp == NULL)
429 return(((char *)(*bufferp)->ondisk) +
430 ((int32_t)buf_offset & HAMMER_BUFMASK));
434 * Allocate HAMMER elements - B-Tree nodes
437 alloc_btree_element(hammer_off_t *offp, struct buffer_info **data_bufferp)
439 hammer_node_ondisk_t node;
441 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
443 bzero(node, sizeof(*node));
448 * Allocate HAMMER elements - meta data (inode, direntry, PFS, etc)
451 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
452 struct buffer_info **data_bufferp)
456 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
458 bzero(data, data_len);
463 * Allocate HAMMER elements - data storage
465 * The only data_len supported by HAMMER userspace for large data zone
466 * (zone 10) is HAMMER_BUFSIZE which is 16KB. >16KB data does not fit
467 * in a buffer allocated by get_buffer(). Also alloc_blockmap() does
468 * not consider >16KB buffer size.
471 alloc_data_element(hammer_off_t *offp, int32_t data_len,
472 struct buffer_info **data_bufferp)
480 zone = hammer_data_zone_index(data_len);
481 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
482 assert(zone == HAMMER_ZONE_LARGE_DATA_INDEX ||
483 zone == HAMMER_ZONE_SMALL_DATA_INDEX);
485 data = alloc_blockmap(zone, data_len, offp, data_bufferp);
486 bzero(data, data_len);
491 * Format a new blockmap. This is mostly a degenerate case because
492 * all allocations are now actually done from the freemap.
495 format_blockmap(struct volume_info *root_vol, int zone, hammer_off_t offset)
497 hammer_blockmap_t blockmap;
498 hammer_off_t zone_base;
500 /* Only root volume needs formatting */
501 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
503 assert(hammer_is_zone2_mapped_index(zone));
505 blockmap = &root_vol->ondisk->vol0_blockmap[zone];
506 zone_base = HAMMER_ZONE_ENCODE(zone, offset);
508 bzero(blockmap, sizeof(*blockmap));
509 blockmap->phys_offset = 0;
510 blockmap->first_offset = zone_base;
511 blockmap->next_offset = zone_base;
512 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
513 hammer_crc_set_blockmap(blockmap);
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 hammer_blockmap_layer1_t layer1;
529 /* Only root volume needs formatting */
530 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
532 layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
533 for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
534 isnew = ((i % HAMMER_BUFSIZE) == 0);
535 layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
536 bzero(layer1, sizeof(*layer1));
537 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
538 layer1->blocks_free = 0;
539 hammer_crc_set_layer1(layer1);
541 assert(i == HAMMER_BIGBLOCK_SIZE);
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 hammer_crc_set_blockmap(blockmap);
554 * Load the volume's remaining free space into the freemap.
556 * Returns the number of big-blocks available.
559 initialize_freemap(struct volume_info *vol)
561 struct volume_info *root_vol;
562 struct buffer_info *buffer1 = NULL;
563 struct buffer_info *buffer2 = NULL;
564 hammer_blockmap_layer1_t layer1;
565 hammer_blockmap_layer2_t layer2;
566 hammer_off_t layer1_offset;
567 hammer_off_t layer2_offset;
568 hammer_off_t phys_offset;
569 hammer_off_t block_offset;
570 hammer_off_t aligned_vol_free_end;
571 hammer_blockmap_t freemap;
573 int64_t layer1_count = 0;
575 root_vol = get_root_volume();
577 assert_volume_offset(vol);
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];
590 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
591 phys_offset < aligned_vol_free_end;
592 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
593 layer1_offset = freemap->phys_offset +
594 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
595 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
596 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
597 layer1->phys_offset = alloc_bigblock(vol,
598 HAMMER_ZONE_FREEMAP_INDEX);
599 layer1->blocks_free = 0;
600 buffer1->cache.modified = 1;
601 hammer_crc_set_layer1(layer1);
606 * Now fill everything in.
608 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
609 phys_offset < aligned_vol_free_end;
610 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
612 layer1_offset = freemap->phys_offset +
613 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
614 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
615 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
617 for (block_offset = 0;
618 block_offset < HAMMER_BLOCKMAP_LAYER2;
619 block_offset += HAMMER_BIGBLOCK_SIZE) {
620 layer2_offset = layer1->phys_offset +
621 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
622 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
623 bzero(layer2, sizeof(*layer2));
625 if (phys_offset + block_offset < vol->vol_free_off) {
627 * Fixups XXX - big-blocks already allocated as part
628 * of the freemap bootstrap.
630 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
631 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
632 layer2->bytes_free = 0;
633 } else if (phys_offset + block_offset < vol->vol_free_end) {
635 layer2->append_off = 0;
636 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
640 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
641 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
642 layer2->bytes_free = 0;
644 hammer_crc_set_layer2(layer2);
645 buffer2->cache.modified = 1;
648 layer1->blocks_free += layer1_count;
649 hammer_crc_set_layer1(layer1);
650 buffer1->cache.modified = 1;
659 * Returns the number of big-blocks available for filesystem data and undos
660 * without formatting.
663 count_freemap(struct volume_info *vol)
665 hammer_off_t phys_offset;
666 hammer_off_t vol_free_off;
667 hammer_off_t aligned_vol_free_end;
670 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
672 assert_volume_offset(vol);
673 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
674 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
676 if (vol->vol_no == HAMMER_ROOT_VOLNO)
677 vol_free_off += HAMMER_BIGBLOCK_SIZE;
679 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
680 phys_offset < aligned_vol_free_end;
681 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
682 vol_free_off += HAMMER_BIGBLOCK_SIZE;
685 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
686 phys_offset < aligned_vol_free_end;
687 phys_offset += HAMMER_BIGBLOCK_SIZE) {
688 if (phys_offset < vol_free_off) {
690 } else if (phys_offset < vol->vol_free_end) {
699 * Format the undomap for the root volume.
702 format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size)
704 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
705 hammer_off_t undo_limit;
706 hammer_blockmap_t blockmap;
707 hammer_volume_ondisk_t ondisk;
708 struct buffer_info *buffer = NULL;
714 /* Only root volume needs formatting */
715 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
716 ondisk = root_vol->ondisk;
719 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
720 * up to HAMMER_UNDO_LAYER2 big-blocks. Size to approximately
723 * The minimum UNDO fifo size is 500MB, or approximately 1% of
724 * the recommended 50G disk.
726 * Changing this minimum is rather dangerous as complex filesystem
727 * operations can cause the UNDO FIFO to fill up otherwise.
729 undo_limit = *undo_buffer_size;
730 if (undo_limit == 0) {
731 undo_limit = HAMMER_VOL_BUF_SIZE(ondisk) / 1000;
732 if (undo_limit < 500*1024*1024)
733 undo_limit = 500*1024*1024;
735 undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
736 ~HAMMER_BIGBLOCK_MASK64;
737 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
738 undo_limit = HAMMER_BIGBLOCK_SIZE;
739 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
740 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
741 *undo_buffer_size = undo_limit;
743 blockmap = &ondisk->vol0_blockmap[undo_zone];
744 bzero(blockmap, sizeof(*blockmap));
745 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
746 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
747 blockmap->next_offset = blockmap->first_offset;
748 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
749 hammer_crc_set_blockmap(blockmap);
751 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
752 assert(limit_index <= HAMMER_UNDO_LAYER2);
754 for (n = 0; n < limit_index; ++n) {
755 ondisk->vol0_undo_array[n] = alloc_bigblock(root_vol,
756 HAMMER_ZONE_UNDO_INDEX);
758 while (n < HAMMER_UNDO_LAYER2) {
759 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
763 * Pre-initialize the UNDO blocks (HAMMER version 4+)
765 printf("initializing the undo map (%jd MB)\n",
766 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
769 scan = blockmap->first_offset;
772 while (scan < blockmap->alloc_offset) {
773 hammer_fifo_head_t head;
774 hammer_fifo_tail_t tail;
776 int bytes = HAMMER_UNDO_ALIGN;
778 isnew = ((scan & HAMMER_BUFMASK64) == 0);
779 head = get_buffer_data(scan, &buffer, isnew);
780 buffer->cache.modified = 1;
781 tail = (void *)((char *)head + bytes - sizeof(*tail));
784 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
785 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
786 head->hdr_size = bytes;
787 head->hdr_seq = seqno++;
789 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
790 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
791 tail->tail_size = bytes;
793 hammer_crc_set_fifo_head(head, bytes);
800 const char *zone_labels[] = {
802 "raw_volume", /* 1 */
803 "raw_buffer", /* 2 */
811 "large_data", /* 10 */
812 "small_data", /* 11 */
820 print_blockmap(const struct volume_info *root_vol)
822 hammer_blockmap_t blockmap;
823 hammer_volume_ondisk_t ondisk;
828 ondisk = root_vol->ondisk;
829 printf(INDENT"vol_label\t%s\n", ondisk->vol_label);
830 printf(INDENT"vol_count\t%d\n", ondisk->vol_count);
831 printf(INDENT"vol_bot_beg\t%s\n", sizetostr(ondisk->vol_bot_beg));
832 printf(INDENT"vol_mem_beg\t%s\n", sizetostr(ondisk->vol_mem_beg));
833 printf(INDENT"vol_buf_beg\t%s\n", sizetostr(ondisk->vol_buf_beg));
834 printf(INDENT"vol_buf_end\t%s\n", sizetostr(ondisk->vol_buf_end));
835 printf(INDENT"vol0_next_tid\t%016jx\n",
836 (uintmax_t)ondisk->vol0_next_tid);
838 blockmap = &ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
839 size = blockmap->alloc_offset & HAMMER_OFF_LONG_MASK;
840 if (blockmap->first_offset <= blockmap->next_offset)
841 used = blockmap->next_offset - blockmap->first_offset;
843 used = blockmap->alloc_offset - blockmap->first_offset +
844 (blockmap->next_offset & HAMMER_OFF_LONG_MASK);
845 printf(INDENT"undo_size\t%s\n", sizetostr(size));
846 printf(INDENT"undo_used\t%s\n", sizetostr(used));
848 printf(INDENT"zone # "
849 "phys first next alloc\n");
850 for (i = 0; i < HAMMER_MAX_ZONES; i++) {
851 blockmap = &ondisk->vol0_blockmap[i];
852 printf(INDENT"zone %-2d %-10s %016jx %016jx %016jx %016jx\n",
854 (uintmax_t)blockmap->phys_offset,
855 (uintmax_t)blockmap->first_offset,
856 (uintmax_t)blockmap->next_offset,
857 (uintmax_t)blockmap->alloc_offset);
862 * Flush various tracking structures to disk
865 flush_all_volumes(void)
867 struct volume_info *vol;
869 TAILQ_FOREACH(vol, &VolList, entry)
874 flush_volume(struct volume_info *volume)
876 struct buffer_info *buffer;
879 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
880 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
881 flush_buffer(buffer);
883 if (writehammervol(volume) == -1)
884 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
888 flush_buffer(struct buffer_info *buffer)
890 struct volume_info *vol;
892 vol = buffer->volume;
893 if (writehammerbuf(buffer) == -1)
894 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
895 buffer->cache.modified = 0;
899 * Core I/O operations
902 __read(struct volume_info *vol, void *data, int64_t offset, int size)
906 n = pread(vol->fd, data, size, offset);
913 readhammervol(struct volume_info *vol)
915 return(__read(vol, vol->ondisk, 0, HAMMER_BUFSIZE));
919 readhammerbuf(struct buffer_info *buf)
921 return(__read(buf->volume, buf->ondisk, buf->raw_offset, HAMMER_BUFSIZE));
925 __write(struct volume_info *vol, const void *data, int64_t offset, int size)
932 n = pwrite(vol->fd, data, size, offset);
939 writehammervol(struct volume_info *vol)
941 return(__write(vol, vol->ondisk, 0, HAMMER_BUFSIZE));
945 writehammerbuf(struct buffer_info *buf)
947 return(__write(buf->volume, buf->ondisk, buf->raw_offset, HAMMER_BUFSIZE));
950 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
953 value = HAMMER_BOOT_NOMBYTES;
954 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
956 if (value < HAMMER_BOOT_MINBYTES)
958 } else if (value < HAMMER_BOOT_MINBYTES) {
959 value = HAMMER_BOOT_MINBYTES;
965 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size)
968 value = HAMMER_MEM_NOMBYTES;
969 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
971 if (value < HAMMER_MEM_MINBYTES)
973 } else if (value < HAMMER_MEM_MINBYTES) {
974 value = HAMMER_MEM_MINBYTES;