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(volume_info_t volume
);
41 static void get_buffer_readahead(buffer_info_t base
);
42 static __inline
int readhammervol(volume_info_t volume
);
43 static __inline
int readhammerbuf(buffer_info_t buffer
);
44 static __inline
int writehammervol(volume_info_t volume
);
45 static __inline
int writehammerbuf(buffer_info_t buffer
);
49 int UseReadBehind
= -4;
52 uint32_t HammerVersion
= -1;
54 TAILQ_HEAD(volume_list
, volume_info
);
55 static struct volume_list VolList
= TAILQ_HEAD_INITIALIZER(VolList
);
56 static int valid_hammer_volumes
;
60 buffer_hash(hammer_off_t zone2_offset
)
64 hi
= (int)(zone2_offset
/ HAMMER_BUFSIZE
) & HAMMER_BUFLISTMASK
;
70 find_buffer(hammer_off_t zone2_offset
)
76 volume
= get_volume(HAMMER_VOL_DECODE(zone2_offset
));
79 hi
= buffer_hash(zone2_offset
);
80 TAILQ_FOREACH(buffer
, &volume
->buffer_lists
[hi
], entry
) {
81 if (buffer
->zone2_offset
== zone2_offset
)
89 __alloc_volume(const char *volname
, int oflags
)
94 volume
= calloc(1, sizeof(*volume
));
96 volume
->rdonly
= (oflags
== O_RDONLY
);
97 volume
->name
= strdup(volname
);
98 volume
->fd
= open(volume
->name
, oflags
);
100 err(1, "alloc_volume: Failed to open %s", volume
->name
);
103 check_volume(volume
);
105 volume
->ondisk
= calloc(1, HAMMER_BUFSIZE
);
107 for (i
= 0; i
< HAMMER_BUFLISTS
; ++i
)
108 TAILQ_INIT(&volume
->buffer_lists
[i
]);
115 __add_volume(const volume_info_t volume
)
118 struct stat st1
, st2
;
120 if (fstat(volume
->fd
, &st1
) != 0) {
121 errx(1, "add_volume: %s: Failed to stat", volume
->name
);
125 TAILQ_FOREACH(scan
, &VolList
, entry
) {
126 if (scan
->vol_no
== volume
->vol_no
) {
127 errx(1, "add_volume: %s: Duplicate volume number %d "
129 volume
->name
, volume
->vol_no
, scan
->name
);
132 if (fstat(scan
->fd
, &st2
) != 0) {
133 errx(1, "add_volume: %s: Failed to stat %s",
134 volume
->name
, scan
->name
);
137 if ((st1
.st_ino
== st2
.st_ino
) && (st1
.st_dev
== st2
.st_dev
)) {
138 errx(1, "add_volume: %s: Specified more than once",
144 TAILQ_INSERT_TAIL(&VolList
, volume
, entry
);
149 __verify_volume(const volume_info_t volume
)
151 hammer_volume_ondisk_t ondisk
= volume
->ondisk
;
153 if (ondisk
->vol_signature
!= HAMMER_FSBUF_VOLUME
) {
154 errx(1, "verify_volume: Invalid volume signature %016jx",
155 ondisk
->vol_signature
);
158 if (ondisk
->vol_rootvol
!= HAMMER_ROOT_VOLNO
) {
159 errx(1, "verify_volume: Invalid root volume# %d",
160 ondisk
->vol_rootvol
);
163 if (bcmp(&Hammer_FSType
, &ondisk
->vol_fstype
, sizeof(Hammer_FSType
))) {
164 errx(1, "verify_volume: %s: Header does not indicate "
165 "that this is a HAMMER volume", volume
->name
);
168 if (bcmp(&Hammer_FSId
, &ondisk
->vol_fsid
, sizeof(Hammer_FSId
))) {
169 errx(1, "verify_volume: %s: FSId does not match other volumes!",
173 if (ondisk
->vol_version
< HAMMER_VOL_VERSION_MIN
||
174 ondisk
->vol_version
>= HAMMER_VOL_VERSION_WIP
) {
175 errx(1, "verify_volume: %s: Invalid volume version %u",
176 volume
->name
, ondisk
->vol_version
);
182 * Initialize a volume structure and ondisk vol_no field.
185 init_volume(const char *filename
, int oflags
, int32_t vol_no
)
187 volume_info_t volume
;
189 volume
= __alloc_volume(filename
, oflags
);
190 volume
->vol_no
= volume
->ondisk
->vol_no
= vol_no
;
192 __add_volume(volume
);
198 * Initialize a volume structure and read ondisk volume header.
201 load_volume(const char *filename
, int oflags
, int verify_volume
)
203 volume_info_t volume
;
206 volume
= __alloc_volume(filename
, oflags
);
208 n
= readhammervol(volume
);
210 err(1, "load_volume: %s: Read failed at offset 0",
214 volume
->vol_no
= volume
->ondisk
->vol_no
;
215 if (volume
->vol_no
== HAMMER_ROOT_VOLNO
)
216 HammerVersion
= volume
->ondisk
->vol_version
;
218 if (valid_hammer_volumes
++ == 0)
219 Hammer_FSId
= volume
->ondisk
->vol_fsid
;
221 __verify_volume(volume
);
223 __add_volume(volume
);
229 * Check basic volume characteristics.
233 check_volume(volume_info_t volume
)
235 struct partinfo pinfo
;
239 * Allow the formatting of block devices or regular files
241 if (ioctl(volume
->fd
, DIOCGPART
, &pinfo
) < 0) {
242 if (fstat(volume
->fd
, &st
) < 0) {
243 err(1, "Unable to stat %s", volume
->name
);
246 if (S_ISREG(st
.st_mode
)) {
247 volume
->size
= st
.st_size
;
248 volume
->type
= "REGFILE";
250 errx(1, "Unsupported file type for %s", volume
->name
);
255 * When formatting a block device as a HAMMER volume the
256 * sector size must be compatible. HAMMER uses 16384 byte
257 * filesystem buffers.
259 if (pinfo
.reserved_blocks
) {
260 errx(1, "HAMMER cannot be placed in a partition "
261 "which overlaps the disklabel or MBR");
264 if (pinfo
.media_blksize
> HAMMER_BUFSIZE
||
265 HAMMER_BUFSIZE
% pinfo
.media_blksize
) {
266 errx(1, "A media sector size of %d is not supported",
267 pinfo
.media_blksize
);
271 volume
->size
= pinfo
.media_size
;
272 volume
->device_offset
= pinfo
.media_offset
;
273 volume
->type
= "DEVICE";
278 is_regfile(const volume_info_t volume
)
280 return(strcmp(volume
->type
, "REGFILE") ? 0 : 1);
284 assert_volume_offset(const volume_info_t volume
)
286 assert(hammer_is_zone_raw_buffer(volume
->vol_free_off
));
287 assert(hammer_is_zone_raw_buffer(volume
->vol_free_end
));
288 if (volume
->vol_free_off
>= volume
->vol_free_end
) {
289 errx(1, "Ran out of room, filesystem too small");
295 get_volume(int32_t vol_no
)
297 volume_info_t volume
;
299 TAILQ_FOREACH(volume
, &VolList
, entry
) {
300 if (volume
->vol_no
== vol_no
)
308 get_root_volume(void)
310 return(get_volume(HAMMER_ROOT_VOLNO
));
315 __blockmap_xlate_to_zone2(hammer_off_t buf_offset
)
317 hammer_off_t zone2_offset
;
320 if (hammer_is_zone_raw_buffer(buf_offset
))
321 zone2_offset
= buf_offset
;
323 zone2_offset
= blockmap_lookup(buf_offset
, &error
);
326 return(HAMMER_OFF_BAD
);
327 assert(hammer_is_zone_raw_buffer(zone2_offset
));
329 return(zone2_offset
);
334 __alloc_buffer(hammer_off_t zone2_offset
, int isnew
)
336 volume_info_t volume
;
337 buffer_info_t buffer
;
340 volume
= get_volume(HAMMER_VOL_DECODE(zone2_offset
));
341 assert(volume
!= NULL
);
343 buffer
= calloc(1, sizeof(*buffer
));
344 buffer
->zone2_offset
= zone2_offset
;
345 buffer
->raw_offset
= hammer_xlate_to_phys(volume
->ondisk
, zone2_offset
);
346 buffer
->volume
= volume
;
347 buffer
->ondisk
= calloc(1, HAMMER_BUFSIZE
);
350 if (readhammerbuf(buffer
) == -1) {
351 err(1, "Failed to read %s:%016jx at %016jx",
353 (intmax_t)buffer
->zone2_offset
,
354 (intmax_t)buffer
->raw_offset
);
359 hi
= buffer_hash(zone2_offset
);
360 TAILQ_INSERT_TAIL(&volume
->buffer_lists
[hi
], buffer
, entry
);
361 hammer_cache_add(&buffer
->cache
);
367 * Acquire the 16KB buffer for specified zone offset.
371 get_buffer(hammer_off_t buf_offset
, int isnew
)
373 buffer_info_t buffer
;
374 hammer_off_t zone2_offset
;
377 zone2_offset
= __blockmap_xlate_to_zone2(buf_offset
);
378 if (zone2_offset
== HAMMER_OFF_BAD
)
381 zone2_offset
&= ~HAMMER_BUFMASK64
;
382 buffer
= find_buffer(zone2_offset
);
384 if (buffer
== NULL
) {
385 buffer
= __alloc_buffer(zone2_offset
, isnew
);
389 hammer_cache_used(&buffer
->cache
);
391 assert(buffer
->ondisk
!= NULL
);
393 ++buffer
->cache
.refs
;
394 hammer_cache_flush();
397 assert(buffer
->cache
.modified
== 0);
398 bzero(buffer
->ondisk
, HAMMER_BUFSIZE
);
399 buffer
->cache
.modified
= 1;
402 get_buffer_readahead(buffer
);
408 get_buffer_readahead(const buffer_info_t base
)
410 buffer_info_t buffer
;
411 volume_info_t volume
;
412 hammer_off_t zone2_offset
;
414 int ri
= UseReadBehind
;
415 int re
= UseReadAhead
;
417 raw_offset
= base
->raw_offset
+ ri
* HAMMER_BUFSIZE
;
418 volume
= base
->volume
;
421 if (raw_offset
>= volume
->ondisk
->vol_buf_end
)
423 if (raw_offset
< volume
->ondisk
->vol_buf_beg
|| ri
== 0) {
425 raw_offset
+= HAMMER_BUFSIZE
;
428 zone2_offset
= HAMMER_ENCODE_RAW_BUFFER(volume
->vol_no
,
429 raw_offset
- volume
->ondisk
->vol_buf_beg
);
430 buffer
= find_buffer(zone2_offset
);
431 if (buffer
== NULL
) {
432 /* call with -1 to prevent another readahead */
433 buffer
= get_buffer(zone2_offset
, -1);
437 raw_offset
+= HAMMER_BUFSIZE
;
442 rel_buffer(buffer_info_t buffer
)
444 volume_info_t volume
;
449 assert(buffer
->cache
.refs
> 0);
450 if (--buffer
->cache
.refs
== 0) {
451 if (buffer
->cache
.delete) {
452 hi
= buffer_hash(buffer
->zone2_offset
);
453 volume
= buffer
->volume
;
454 if (buffer
->cache
.modified
)
455 flush_buffer(buffer
);
456 TAILQ_REMOVE(&volume
->buffer_lists
[hi
], buffer
, entry
);
457 hammer_cache_del(&buffer
->cache
);
458 free(buffer
->ondisk
);
465 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
466 * bufferp is freed if isnew or the offset is out of range of the cached data.
467 * If bufferp is freed a referenced buffer is loaded into it.
470 get_buffer_data(hammer_off_t buf_offset
, buffer_info_t
*bufferp
, int isnew
)
474 if (*bufferp
!= NULL
) {
475 /* XXX xor is always non zero for indirect zones */
476 xor = HAMMER_OFF_LONG_ENCODE(buf_offset
) ^
477 HAMMER_OFF_LONG_ENCODE((*bufferp
)->zone2_offset
);
478 if (isnew
> 0 || (xor & ~HAMMER_BUFMASK64
)) {
479 rel_buffer(*bufferp
);
484 if (*bufferp
== NULL
) {
485 *bufferp
= get_buffer(buf_offset
, isnew
);
486 if (*bufferp
== NULL
)
490 return(((char *)(*bufferp
)->ondisk
) +
491 ((int32_t)buf_offset
& HAMMER_BUFMASK
));
495 * Allocate HAMMER elements - B-Tree nodes
498 alloc_btree_node(hammer_off_t
*offp
, buffer_info_t
*data_bufferp
)
500 hammer_node_ondisk_t node
;
502 node
= alloc_blockmap(HAMMER_ZONE_BTREE_INDEX
, sizeof(*node
),
504 bzero(node
, sizeof(*node
));
509 * Allocate HAMMER elements - meta data (inode, direntry, PFS, etc)
512 alloc_meta_element(hammer_off_t
*offp
, int32_t data_len
,
513 buffer_info_t
*data_bufferp
)
517 data
= alloc_blockmap(HAMMER_ZONE_META_INDEX
, data_len
,
519 bzero(data
, data_len
);
524 * Format a new blockmap. This is mostly a degenerate case because
525 * all allocations are now actually done from the freemap.
528 format_blockmap(volume_info_t root_vol
, int zone
, hammer_off_t offset
)
530 hammer_blockmap_t blockmap
;
531 hammer_off_t zone_base
;
533 /* Only root volume needs formatting */
534 assert(root_vol
->vol_no
== HAMMER_ROOT_VOLNO
);
536 assert(hammer_is_index_record(zone
));
538 blockmap
= &root_vol
->ondisk
->vol0_blockmap
[zone
];
539 zone_base
= HAMMER_ZONE_ENCODE(zone
, offset
);
541 bzero(blockmap
, sizeof(*blockmap
));
542 blockmap
->phys_offset
= 0;
543 blockmap
->first_offset
= zone_base
;
544 blockmap
->next_offset
= zone_base
;
545 blockmap
->alloc_offset
= HAMMER_ENCODE(zone
, 255, -1);
546 hammer_crc_set_blockmap(HammerVersion
, blockmap
);
550 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
551 * code will load each volume's freemap.
554 format_freemap(volume_info_t root_vol
)
556 buffer_info_t buffer
= NULL
;
557 hammer_off_t layer1_offset
;
558 hammer_blockmap_t blockmap
;
559 hammer_blockmap_layer1_t layer1
;
562 /* Only root volume needs formatting */
563 assert(root_vol
->vol_no
== HAMMER_ROOT_VOLNO
);
565 layer1_offset
= bootstrap_bigblock(root_vol
);
566 for (i
= 0; i
< HAMMER_BIGBLOCK_SIZE
; i
+= sizeof(*layer1
)) {
567 isnew
= ((i
% HAMMER_BUFSIZE
) == 0);
568 layer1
= get_buffer_data(layer1_offset
+ i
, &buffer
, isnew
);
569 bzero(layer1
, sizeof(*layer1
));
570 layer1
->phys_offset
= HAMMER_BLOCKMAP_UNAVAIL
;
571 layer1
->blocks_free
= 0;
572 hammer_crc_set_layer1(HammerVersion
, layer1
);
574 assert(i
== HAMMER_BIGBLOCK_SIZE
);
577 blockmap
= &root_vol
->ondisk
->vol0_blockmap
[HAMMER_ZONE_FREEMAP_INDEX
];
578 bzero(blockmap
, sizeof(*blockmap
));
579 blockmap
->phys_offset
= layer1_offset
;
580 blockmap
->first_offset
= 0;
581 blockmap
->next_offset
= HAMMER_ENCODE_RAW_BUFFER(0, 0);
582 blockmap
->alloc_offset
= HAMMER_ENCODE_RAW_BUFFER(255, -1);
583 hammer_crc_set_blockmap(HammerVersion
, blockmap
);
587 * Load the volume's remaining free space into the freemap.
589 * Returns the number of big-blocks available.
592 initialize_freemap(volume_info_t volume
)
594 volume_info_t root_vol
;
595 buffer_info_t buffer1
= NULL
;
596 buffer_info_t buffer2
= NULL
;
597 hammer_blockmap_layer1_t layer1
;
598 hammer_blockmap_layer2_t layer2
;
599 hammer_off_t layer1_offset
;
600 hammer_off_t layer2_offset
;
601 hammer_off_t phys_offset
;
602 hammer_off_t block_offset
;
603 hammer_off_t aligned_vol_free_end
;
604 hammer_blockmap_t freemap
;
606 int64_t layer1_count
= 0;
608 root_vol
= get_root_volume();
610 assert_volume_offset(volume
);
611 aligned_vol_free_end
= HAMMER_BLOCKMAP_LAYER2_DOALIGN(volume
->vol_free_end
);
613 printf("initialize freemap volume %d\n", volume
->vol_no
);
616 * Initialize the freemap. First preallocate the big-blocks required
617 * to implement layer2. This preallocation is a bootstrap allocation
618 * using blocks from the target volume.
620 freemap
= &root_vol
->ondisk
->vol0_blockmap
[HAMMER_ZONE_FREEMAP_INDEX
];
622 for (phys_offset
= HAMMER_ENCODE_RAW_BUFFER(volume
->vol_no
, 0);
623 phys_offset
< aligned_vol_free_end
;
624 phys_offset
+= HAMMER_BLOCKMAP_LAYER2
) {
625 layer1_offset
= freemap
->phys_offset
+
626 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset
);
627 layer1
= get_buffer_data(layer1_offset
, &buffer1
, 0);
628 if (layer1
->phys_offset
== HAMMER_BLOCKMAP_UNAVAIL
) {
629 layer1
->phys_offset
= bootstrap_bigblock(volume
);
630 layer1
->blocks_free
= 0;
631 buffer1
->cache
.modified
= 1;
632 hammer_crc_set_layer1(HammerVersion
, layer1
);
637 * Now fill everything in.
639 for (phys_offset
= HAMMER_ENCODE_RAW_BUFFER(volume
->vol_no
, 0);
640 phys_offset
< aligned_vol_free_end
;
641 phys_offset
+= HAMMER_BLOCKMAP_LAYER2
) {
643 layer1_offset
= freemap
->phys_offset
+
644 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset
);
645 layer1
= get_buffer_data(layer1_offset
, &buffer1
, 0);
646 assert(layer1
->phys_offset
!= HAMMER_BLOCKMAP_UNAVAIL
);
648 for (block_offset
= 0;
649 block_offset
< HAMMER_BLOCKMAP_LAYER2
;
650 block_offset
+= HAMMER_BIGBLOCK_SIZE
) {
651 layer2_offset
= layer1
->phys_offset
+
652 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset
);
653 layer2
= get_buffer_data(layer2_offset
, &buffer2
, 0);
654 bzero(layer2
, sizeof(*layer2
));
656 if (phys_offset
+ block_offset
< volume
->vol_free_off
) {
658 * Big-blocks already allocated as part
659 * of the freemap bootstrap.
661 layer2
->zone
= HAMMER_ZONE_FREEMAP_INDEX
;
662 layer2
->append_off
= HAMMER_BIGBLOCK_SIZE
;
663 layer2
->bytes_free
= 0;
664 } else if (phys_offset
+ block_offset
< volume
->vol_free_end
) {
666 layer2
->append_off
= 0;
667 layer2
->bytes_free
= HAMMER_BIGBLOCK_SIZE
;
671 layer2
->zone
= HAMMER_ZONE_UNAVAIL_INDEX
;
672 layer2
->append_off
= HAMMER_BIGBLOCK_SIZE
;
673 layer2
->bytes_free
= 0;
675 hammer_crc_set_layer2(HammerVersion
, layer2
);
676 buffer2
->cache
.modified
= 1;
679 layer1
->blocks_free
+= layer1_count
;
680 hammer_crc_set_layer1(HammerVersion
, layer1
);
681 buffer1
->cache
.modified
= 1;
690 * Returns the number of big-blocks available for filesystem data and undos
691 * without formatting.
694 count_freemap(const volume_info_t volume
)
696 hammer_off_t phys_offset
;
697 hammer_off_t vol_free_off
;
698 hammer_off_t aligned_vol_free_end
;
701 vol_free_off
= HAMMER_ENCODE_RAW_BUFFER(volume
->vol_no
, 0);
703 assert_volume_offset(volume
);
704 aligned_vol_free_end
= HAMMER_BLOCKMAP_LAYER2_DOALIGN(volume
->vol_free_end
);
706 if (volume
->vol_no
== HAMMER_ROOT_VOLNO
)
707 vol_free_off
+= HAMMER_BIGBLOCK_SIZE
;
709 for (phys_offset
= HAMMER_ENCODE_RAW_BUFFER(volume
->vol_no
, 0);
710 phys_offset
< aligned_vol_free_end
;
711 phys_offset
+= HAMMER_BLOCKMAP_LAYER2
) {
712 vol_free_off
+= HAMMER_BIGBLOCK_SIZE
;
715 for (phys_offset
= HAMMER_ENCODE_RAW_BUFFER(volume
->vol_no
, 0);
716 phys_offset
< aligned_vol_free_end
;
717 phys_offset
+= HAMMER_BIGBLOCK_SIZE
) {
718 if (phys_offset
< vol_free_off
)
720 else if (phys_offset
< volume
->vol_free_end
)
728 * Format the undomap for the root volume.
731 format_undomap(volume_info_t root_vol
, int64_t *undo_buffer_size
)
733 hammer_off_t undo_limit
;
734 hammer_blockmap_t blockmap
;
735 hammer_volume_ondisk_t ondisk
;
736 buffer_info_t buffer
= NULL
;
742 /* Only root volume needs formatting */
743 assert(root_vol
->vol_no
== HAMMER_ROOT_VOLNO
);
744 ondisk
= root_vol
->ondisk
;
747 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
748 * up to HAMMER_MAX_UNDO_BIGBLOCKS big-blocks.
749 * Size to approximately 0.1% of the disk.
751 * The minimum UNDO fifo size is 512MB, or approximately 1% of
752 * the recommended 50G disk.
754 * Changing this minimum is rather dangerous as complex filesystem
755 * operations can cause the UNDO FIFO to fill up otherwise.
757 undo_limit
= *undo_buffer_size
;
758 if (undo_limit
== 0) {
759 undo_limit
= HAMMER_VOL_BUF_SIZE(ondisk
) / 1000;
760 if (undo_limit
< HAMMER_BIGBLOCK_SIZE
* HAMMER_MIN_UNDO_BIGBLOCKS
)
761 undo_limit
= HAMMER_BIGBLOCK_SIZE
* HAMMER_MIN_UNDO_BIGBLOCKS
;
763 undo_limit
= HAMMER_BIGBLOCK_DOALIGN(undo_limit
);
764 if (undo_limit
< HAMMER_BIGBLOCK_SIZE
)
765 undo_limit
= HAMMER_BIGBLOCK_SIZE
;
766 if (undo_limit
> HAMMER_BIGBLOCK_SIZE
* HAMMER_MAX_UNDO_BIGBLOCKS
)
767 undo_limit
= HAMMER_BIGBLOCK_SIZE
* HAMMER_MAX_UNDO_BIGBLOCKS
;
768 *undo_buffer_size
= undo_limit
;
770 blockmap
= &ondisk
->vol0_blockmap
[HAMMER_ZONE_UNDO_INDEX
];
771 bzero(blockmap
, sizeof(*blockmap
));
772 blockmap
->phys_offset
= HAMMER_BLOCKMAP_UNAVAIL
;
773 blockmap
->first_offset
= HAMMER_ENCODE_UNDO(0);
774 blockmap
->next_offset
= blockmap
->first_offset
;
775 blockmap
->alloc_offset
= HAMMER_ENCODE_UNDO(undo_limit
);
776 hammer_crc_set_blockmap(HammerVersion
, blockmap
);
778 limit_index
= undo_limit
/ HAMMER_BIGBLOCK_SIZE
;
779 assert(limit_index
<= HAMMER_MAX_UNDO_BIGBLOCKS
);
781 for (n
= 0; n
< limit_index
; ++n
)
782 ondisk
->vol0_undo_array
[n
] = alloc_undo_bigblock(root_vol
);
783 while (n
< HAMMER_MAX_UNDO_BIGBLOCKS
)
784 ondisk
->vol0_undo_array
[n
++] = HAMMER_BLOCKMAP_UNAVAIL
;
787 * Pre-initialize the UNDO blocks (HAMMER version 4+)
789 printf("initializing the undo map (%jd MB)\n",
790 (intmax_t)HAMMER_OFF_LONG_ENCODE(blockmap
->alloc_offset
) /
793 scan
= blockmap
->first_offset
;
796 while (scan
< blockmap
->alloc_offset
) {
797 hammer_fifo_head_t head
;
798 hammer_fifo_tail_t tail
;
799 int bytes
= HAMMER_UNDO_ALIGN
;
800 int isnew
= ((scan
& HAMMER_BUFMASK64
) == 0);
802 head
= get_buffer_data(scan
, &buffer
, isnew
);
803 buffer
->cache
.modified
= 1;
804 tail
= (void *)((char *)head
+ bytes
- sizeof(*tail
));
807 head
->hdr_signature
= HAMMER_HEAD_SIGNATURE
;
808 head
->hdr_type
= HAMMER_HEAD_TYPE_DUMMY
;
809 head
->hdr_size
= bytes
;
810 head
->hdr_seq
= seqno
++;
812 tail
->tail_signature
= HAMMER_TAIL_SIGNATURE
;
813 tail
->tail_type
= HAMMER_HEAD_TYPE_DUMMY
;
814 tail
->tail_size
= bytes
;
816 hammer_crc_set_fifo_head(HammerVersion
, head
, bytes
);
823 const char *zone_labels
[] = {
825 "raw_volume", /* 1 */
826 "raw_buffer", /* 2 */
834 "large_data", /* 10 */
835 "small_data", /* 11 */
843 print_blockmap(const volume_info_t volume
)
845 hammer_blockmap_t blockmap
;
846 hammer_volume_ondisk_t ondisk
;
851 ondisk
= volume
->ondisk
;
852 printf(INDENT
"vol_label\t%s\n", ondisk
->vol_label
);
853 printf(INDENT
"vol_count\t%d\n", ondisk
->vol_count
);
854 printf(INDENT
"vol_bot_beg\t%s\n", sizetostr(ondisk
->vol_bot_beg
));
855 printf(INDENT
"vol_mem_beg\t%s\n", sizetostr(ondisk
->vol_mem_beg
));
856 printf(INDENT
"vol_buf_beg\t%s\n", sizetostr(ondisk
->vol_buf_beg
));
857 printf(INDENT
"vol_buf_end\t%s\n", sizetostr(ondisk
->vol_buf_end
));
858 printf(INDENT
"vol0_next_tid\t%016jx\n",
859 (uintmax_t)ondisk
->vol0_next_tid
);
861 blockmap
= &ondisk
->vol0_blockmap
[HAMMER_ZONE_UNDO_INDEX
];
862 size
= HAMMER_OFF_LONG_ENCODE(blockmap
->alloc_offset
);
863 if (blockmap
->first_offset
<= blockmap
->next_offset
)
864 used
= blockmap
->next_offset
- blockmap
->first_offset
;
866 used
= blockmap
->alloc_offset
- blockmap
->first_offset
+
867 HAMMER_OFF_LONG_ENCODE(blockmap
->next_offset
);
868 printf(INDENT
"undo_size\t%s\n", sizetostr(size
));
869 printf(INDENT
"undo_used\t%s\n", sizetostr(used
));
871 printf(INDENT
"zone # "
872 "phys first next alloc\n");
873 for (i
= 0; i
< HAMMER_MAX_ZONES
; i
++) {
874 blockmap
= &ondisk
->vol0_blockmap
[i
];
875 printf(INDENT
"zone %-2d %-10s %016jx %016jx %016jx %016jx\n",
877 (uintmax_t)blockmap
->phys_offset
,
878 (uintmax_t)blockmap
->first_offset
,
879 (uintmax_t)blockmap
->next_offset
,
880 (uintmax_t)blockmap
->alloc_offset
);
885 * Flush various tracking structures to disk
888 flush_all_volumes(void)
890 volume_info_t volume
;
892 TAILQ_FOREACH(volume
, &VolList
, entry
)
893 flush_volume(volume
);
897 flush_volume(volume_info_t volume
)
899 buffer_info_t buffer
;
902 for (i
= 0; i
< HAMMER_BUFLISTS
; ++i
) {
903 TAILQ_FOREACH(buffer
, &volume
->buffer_lists
[i
], entry
)
904 flush_buffer(buffer
);
906 if (writehammervol(volume
) == -1) {
907 err(1, "Write volume %d (%s)", volume
->vol_no
, volume
->name
);
913 flush_buffer(buffer_info_t buffer
)
915 volume_info_t volume
;
917 volume
= buffer
->volume
;
918 if (writehammerbuf(buffer
) == -1) {
919 err(1, "Write volume %d (%s)", volume
->vol_no
, volume
->name
);
922 buffer
->cache
.modified
= 0;
926 * Core I/O operations
930 __read(volume_info_t volume
, void *data
, int64_t offset
, int size
)
934 n
= pread(volume
->fd
, data
, size
, offset
);
942 readhammervol(volume_info_t volume
)
944 return(__read(volume
, volume
->ondisk
, 0, HAMMER_BUFSIZE
));
949 readhammerbuf(buffer_info_t buffer
)
951 return(__read(buffer
->volume
, buffer
->ondisk
, buffer
->raw_offset
,
957 __write(volume_info_t volume
, const void *data
, int64_t offset
, int size
)
964 n
= pwrite(volume
->fd
, data
, size
, offset
);
972 writehammervol(volume_info_t volume
)
974 return(__write(volume
, volume
->ondisk
, 0, HAMMER_BUFSIZE
));
979 writehammerbuf(buffer_info_t buffer
)
981 return(__write(buffer
->volume
, buffer
->ondisk
, buffer
->raw_offset
,
986 init_boot_area_size(int64_t value
, off_t avg_vol_size
)
989 value
= HAMMER_BOOT_NOMBYTES
;
990 while (value
> avg_vol_size
/ HAMMER_MAX_VOLUMES
)
994 if (value
< HAMMER_BOOT_MINBYTES
)
995 value
= HAMMER_BOOT_MINBYTES
;
996 else if (value
> HAMMER_BOOT_MAXBYTES
)
997 value
= HAMMER_BOOT_MAXBYTES
;
1003 init_memory_log_size(int64_t value
, off_t avg_vol_size
)
1006 value
= HAMMER_MEM_NOMBYTES
;
1007 while (value
> avg_vol_size
/ HAMMER_MAX_VOLUMES
)
1011 if (value
< HAMMER_MEM_MINBYTES
)
1012 value
= HAMMER_MEM_MINBYTES
;
1013 else if (value
> HAMMER_MEM_MAXBYTES
)
1014 value
= HAMMER_MEM_MAXBYTES
;