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 __inline
int readhammervol(struct volume_info
*vol
);
43 static __inline
int readhammerbuf(struct buffer_info
*buffer
);
44 static __inline
int writehammervol(struct volume_info
*vol
);
45 static __inline
int writehammerbuf(struct buffer_info
*buffer
);
49 int UseReadBehind
= -4;
53 TAILQ_HEAD(volume_list
, volume_info
);
54 static struct volume_list VolList
= TAILQ_HEAD_INITIALIZER(VolList
);
55 static int valid_hammer_volumes
;
59 buffer_hash(hammer_off_t zone2_offset
)
63 hi
= (int)(zone2_offset
/ HAMMER_BUFSIZE
) & HAMMER_BUFLISTMASK
;
67 static struct buffer_info
*
68 find_buffer(hammer_off_t zone2_offset
)
70 struct volume_info
*volume
;
71 struct buffer_info
*buffer
;
74 volume
= get_volume(HAMMER_VOL_DECODE(zone2_offset
));
77 hi
= buffer_hash(zone2_offset
);
78 TAILQ_FOREACH(buffer
, &volume
->buffer_lists
[hi
], entry
)
79 if (buffer
->zone2_offset
== zone2_offset
)
86 __alloc_volume(const char *volname
, int oflags
)
88 struct volume_info
*vol
;
91 vol
= calloc(1, sizeof(*vol
));
93 vol
->rdonly
= (oflags
== O_RDONLY
);
94 vol
->name
= strdup(volname
);
95 vol
->fd
= open(vol
->name
, oflags
);
97 err(1, "alloc_volume: Failed to open %s", vol
->name
);
100 vol
->ondisk
= calloc(1, HAMMER_BUFSIZE
);
102 for (i
= 0; i
< HAMMER_BUFLISTS
; ++i
)
103 TAILQ_INIT(&vol
->buffer_lists
[i
]);
109 __add_volume(struct volume_info
*vol
)
111 struct volume_info
*scan
;
112 struct stat st1
, st2
;
114 if (fstat(vol
->fd
, &st1
) != 0)
115 errx(1, "add_volume: %s: Failed to stat", vol
->name
);
117 TAILQ_FOREACH(scan
, &VolList
, entry
) {
118 if (scan
->vol_no
== vol
->vol_no
) {
119 errx(1, "add_volume: %s: Duplicate volume number %d "
121 vol
->name
, vol
->vol_no
, scan
->name
);
123 if (fstat(scan
->fd
, &st2
) != 0) {
124 errx(1, "add_volume: %s: Failed to stat %s",
125 vol
->name
, scan
->name
);
127 if ((st1
.st_ino
== st2
.st_ino
) && (st1
.st_dev
== st2
.st_dev
)) {
128 errx(1, "add_volume: %s: Specified more than once",
133 TAILQ_INSERT_TAIL(&VolList
, vol
, entry
);
137 __verify_volume(struct volume_info
*vol
)
139 hammer_volume_ondisk_t ondisk
= vol
->ondisk
;
141 if (ondisk
->vol_signature
!= HAMMER_FSBUF_VOLUME
) {
142 errx(1, "verify_volume: Invalid volume signature %016jx",
143 ondisk
->vol_signature
);
145 if (ondisk
->vol_rootvol
!= HAMMER_ROOT_VOLNO
) {
146 errx(1, "verify_volume: Invalid root volume# %d",
147 ondisk
->vol_rootvol
);
149 if (bcmp(&Hammer_FSType
, &ondisk
->vol_fstype
, sizeof(Hammer_FSType
))) {
150 errx(1, "verify_volume: %s: Header does not indicate "
151 "that this is a HAMMER volume", vol
->name
);
153 if (bcmp(&Hammer_FSId
, &ondisk
->vol_fsid
, sizeof(Hammer_FSId
))) {
154 errx(1, "verify_volume: %s: FSId does not match other volumes!",
160 * Initialize a volume structure and ondisk vol_no field.
163 init_volume(const char *filename
, int oflags
, int32_t vol_no
)
165 struct volume_info
*vol
;
167 vol
= __alloc_volume(filename
, oflags
);
168 vol
->vol_no
= vol
->ondisk
->vol_no
= vol_no
;
176 * Initialize a volume structure and read ondisk volume header.
179 load_volume(const char *filename
, int oflags
, int verify
)
181 struct volume_info
*vol
;
184 vol
= __alloc_volume(filename
, oflags
);
186 n
= readhammervol(vol
);
188 err(1, "load_volume: %s: Read failed at offset 0", vol
->name
);
190 vol
->vol_no
= vol
->ondisk
->vol_no
;
192 if (valid_hammer_volumes
++ == 0)
193 Hammer_FSId
= vol
->ondisk
->vol_fsid
;
195 __verify_volume(vol
);
203 * Check basic volume characteristics.
206 check_volume(struct volume_info
*vol
)
208 struct partinfo pinfo
;
212 * Get basic information about the volume
214 if (ioctl(vol
->fd
, DIOCGPART
, &pinfo
) < 0) {
216 * Allow the formatting of regular files as HAMMER volumes
218 if (fstat(vol
->fd
, &st
) < 0)
219 err(1, "Unable to stat %s", vol
->name
);
220 vol
->size
= st
.st_size
;
221 vol
->type
= "REGFILE";
224 * When formatting a block device as a HAMMER volume the
225 * sector size must be compatible. HAMMER uses 16384 byte
226 * filesystem buffers.
228 if (pinfo
.reserved_blocks
) {
229 errx(1, "HAMMER cannot be placed in a partition "
230 "which overlaps the disklabel or MBR");
232 if (pinfo
.media_blksize
> HAMMER_BUFSIZE
||
233 HAMMER_BUFSIZE
% pinfo
.media_blksize
) {
234 errx(1, "A media sector size of %d is not supported",
235 pinfo
.media_blksize
);
238 vol
->size
= pinfo
.media_size
;
239 vol
->device_offset
= pinfo
.media_offset
;
240 vol
->type
= "DEVICE";
245 assert_volume_offset(struct volume_info
*vol
)
247 assert(hammer_is_zone_raw_buffer(vol
->vol_free_off
));
248 assert(hammer_is_zone_raw_buffer(vol
->vol_free_end
));
249 if (vol
->vol_free_off
>= vol
->vol_free_end
)
250 errx(1, "Ran out of room, filesystem too small");
254 get_volume(int32_t vol_no
)
256 struct volume_info
*vol
;
258 TAILQ_FOREACH(vol
, &VolList
, entry
) {
259 if (vol
->vol_no
== vol_no
)
267 get_root_volume(void)
269 return(get_volume(HAMMER_ROOT_VOLNO
));
273 __blockmap_xlate_to_zone2(hammer_off_t buf_offset
)
275 hammer_off_t zone2_offset
;
278 if (hammer_is_zone_raw_buffer(buf_offset
))
279 zone2_offset
= buf_offset
;
281 zone2_offset
= blockmap_lookup(buf_offset
, &error
);
284 return(HAMMER_OFF_BAD
);
285 assert(hammer_is_zone_raw_buffer(zone2_offset
));
287 return(zone2_offset
);
290 static struct buffer_info
*
291 __alloc_buffer(hammer_off_t zone2_offset
, int isnew
)
293 struct volume_info
*volume
;
294 struct buffer_info
*buffer
;
297 volume
= get_volume(HAMMER_VOL_DECODE(zone2_offset
));
298 assert(volume
!= NULL
);
300 buffer
= calloc(1, sizeof(*buffer
));
301 buffer
->zone2_offset
= zone2_offset
;
302 buffer
->raw_offset
= hammer_xlate_to_phys(volume
->ondisk
, zone2_offset
);
303 buffer
->volume
= volume
;
304 buffer
->ondisk
= calloc(1, HAMMER_BUFSIZE
);
307 if (readhammerbuf(buffer
) == -1) {
308 err(1, "Failed to read %s:%016jx at %016jx",
310 (intmax_t)buffer
->zone2_offset
,
311 (intmax_t)buffer
->raw_offset
);
315 hi
= buffer_hash(zone2_offset
);
316 TAILQ_INSERT_TAIL(&volume
->buffer_lists
[hi
], buffer
, entry
);
317 hammer_cache_add(&buffer
->cache
);
323 * Acquire the 16KB buffer for specified zone offset.
325 static struct buffer_info
*
326 get_buffer(hammer_off_t buf_offset
, int isnew
)
328 struct buffer_info
*buffer
;
329 hammer_off_t zone2_offset
;
332 zone2_offset
= __blockmap_xlate_to_zone2(buf_offset
);
333 if (zone2_offset
== HAMMER_OFF_BAD
)
336 zone2_offset
&= ~HAMMER_BUFMASK64
;
337 buffer
= find_buffer(zone2_offset
);
339 if (buffer
== NULL
) {
340 buffer
= __alloc_buffer(zone2_offset
, isnew
);
344 hammer_cache_used(&buffer
->cache
);
346 assert(buffer
->ondisk
!= NULL
);
348 ++buffer
->cache
.refs
;
349 hammer_cache_flush();
352 assert(buffer
->cache
.modified
== 0);
353 bzero(buffer
->ondisk
, HAMMER_BUFSIZE
);
354 buffer
->cache
.modified
= 1;
357 get_buffer_readahead(buffer
);
362 get_buffer_readahead(struct buffer_info
*base
)
364 struct buffer_info
*buffer
;
365 struct volume_info
*vol
;
366 hammer_off_t zone2_offset
;
368 int ri
= UseReadBehind
;
369 int re
= UseReadAhead
;
371 raw_offset
= base
->raw_offset
+ ri
* HAMMER_BUFSIZE
;
375 if (raw_offset
>= vol
->ondisk
->vol_buf_end
)
377 if (raw_offset
< vol
->ondisk
->vol_buf_beg
|| ri
== 0) {
379 raw_offset
+= HAMMER_BUFSIZE
;
382 zone2_offset
= HAMMER_ENCODE_RAW_BUFFER(vol
->vol_no
,
383 raw_offset
- vol
->ondisk
->vol_buf_beg
);
384 buffer
= find_buffer(zone2_offset
);
385 if (buffer
== NULL
) {
386 /* call with -1 to prevent another readahead */
387 buffer
= get_buffer(zone2_offset
, -1);
391 raw_offset
+= HAMMER_BUFSIZE
;
396 rel_buffer(struct buffer_info
*buffer
)
398 struct volume_info
*volume
;
403 assert(buffer
->cache
.refs
> 0);
404 if (--buffer
->cache
.refs
== 0) {
405 if (buffer
->cache
.delete) {
406 hi
= buffer_hash(buffer
->zone2_offset
);
407 volume
= buffer
->volume
;
408 if (buffer
->cache
.modified
)
409 flush_buffer(buffer
);
410 TAILQ_REMOVE(&volume
->buffer_lists
[hi
], buffer
, entry
);
411 hammer_cache_del(&buffer
->cache
);
412 free(buffer
->ondisk
);
419 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
420 * bufferp is freed if isnew or the offset is out of range of the cached data.
421 * If bufferp is freed a referenced buffer is loaded into it.
424 get_buffer_data(hammer_off_t buf_offset
, struct buffer_info
**bufferp
,
429 if (*bufferp
!= NULL
) {
430 /* XXX xor is always non zero for indirect zones */
431 xor = HAMMER_OFF_LONG_ENCODE(buf_offset
) ^
432 HAMMER_OFF_LONG_ENCODE((*bufferp
)->zone2_offset
);
433 if (isnew
> 0 || (xor & ~HAMMER_BUFMASK64
)) {
434 rel_buffer(*bufferp
);
439 if (*bufferp
== NULL
) {
440 *bufferp
= get_buffer(buf_offset
, isnew
);
441 if (*bufferp
== NULL
)
445 return(((char *)(*bufferp
)->ondisk
) +
446 ((int32_t)buf_offset
& HAMMER_BUFMASK
));
450 * Allocate HAMMER elements - B-Tree nodes
453 alloc_btree_node(hammer_off_t
*offp
, struct buffer_info
**data_bufferp
)
455 hammer_node_ondisk_t node
;
457 node
= alloc_blockmap(HAMMER_ZONE_BTREE_INDEX
, sizeof(*node
),
459 bzero(node
, sizeof(*node
));
464 * Allocate HAMMER elements - meta data (inode, direntry, PFS, etc)
467 alloc_meta_element(hammer_off_t
*offp
, int32_t data_len
,
468 struct buffer_info
**data_bufferp
)
472 data
= alloc_blockmap(HAMMER_ZONE_META_INDEX
, data_len
,
474 bzero(data
, data_len
);
479 * Format a new blockmap. This is mostly a degenerate case because
480 * all allocations are now actually done from the freemap.
483 format_blockmap(struct volume_info
*root_vol
, int zone
, hammer_off_t offset
)
485 hammer_blockmap_t blockmap
;
486 hammer_off_t zone_base
;
488 /* Only root volume needs formatting */
489 assert(root_vol
->vol_no
== HAMMER_ROOT_VOLNO
);
491 assert(hammer_is_index_record(zone
));
493 blockmap
= &root_vol
->ondisk
->vol0_blockmap
[zone
];
494 zone_base
= HAMMER_ZONE_ENCODE(zone
, offset
);
496 bzero(blockmap
, sizeof(*blockmap
));
497 blockmap
->phys_offset
= 0;
498 blockmap
->first_offset
= zone_base
;
499 blockmap
->next_offset
= zone_base
;
500 blockmap
->alloc_offset
= HAMMER_ENCODE(zone
, 255, -1);
501 hammer_crc_set_blockmap(blockmap
);
505 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
506 * code will load each volume's freemap.
509 format_freemap(struct volume_info
*root_vol
)
511 struct buffer_info
*buffer
= NULL
;
512 hammer_off_t layer1_offset
;
513 hammer_blockmap_t blockmap
;
514 hammer_blockmap_layer1_t layer1
;
517 /* Only root volume needs formatting */
518 assert(root_vol
->vol_no
== HAMMER_ROOT_VOLNO
);
520 layer1_offset
= bootstrap_bigblock(root_vol
);
521 for (i
= 0; i
< HAMMER_BIGBLOCK_SIZE
; i
+= sizeof(*layer1
)) {
522 isnew
= ((i
% HAMMER_BUFSIZE
) == 0);
523 layer1
= get_buffer_data(layer1_offset
+ i
, &buffer
, isnew
);
524 bzero(layer1
, sizeof(*layer1
));
525 layer1
->phys_offset
= HAMMER_BLOCKMAP_UNAVAIL
;
526 layer1
->blocks_free
= 0;
527 hammer_crc_set_layer1(layer1
);
529 assert(i
== HAMMER_BIGBLOCK_SIZE
);
532 blockmap
= &root_vol
->ondisk
->vol0_blockmap
[HAMMER_ZONE_FREEMAP_INDEX
];
533 bzero(blockmap
, sizeof(*blockmap
));
534 blockmap
->phys_offset
= layer1_offset
;
535 blockmap
->first_offset
= 0;
536 blockmap
->next_offset
= HAMMER_ENCODE_RAW_BUFFER(0, 0);
537 blockmap
->alloc_offset
= HAMMER_ENCODE_RAW_BUFFER(255, -1);
538 hammer_crc_set_blockmap(blockmap
);
542 * Load the volume's remaining free space into the freemap.
544 * Returns the number of big-blocks available.
547 initialize_freemap(struct volume_info
*vol
)
549 struct volume_info
*root_vol
;
550 struct buffer_info
*buffer1
= NULL
;
551 struct buffer_info
*buffer2
= NULL
;
552 hammer_blockmap_layer1_t layer1
;
553 hammer_blockmap_layer2_t layer2
;
554 hammer_off_t layer1_offset
;
555 hammer_off_t layer2_offset
;
556 hammer_off_t phys_offset
;
557 hammer_off_t block_offset
;
558 hammer_off_t aligned_vol_free_end
;
559 hammer_blockmap_t freemap
;
561 int64_t layer1_count
= 0;
563 root_vol
= get_root_volume();
565 assert_volume_offset(vol
);
566 aligned_vol_free_end
= HAMMER_BLOCKMAP_LAYER2_DOALIGN(vol
->vol_free_end
);
568 printf("initialize freemap volume %d\n", vol
->vol_no
);
571 * Initialize the freemap. First preallocate the big-blocks required
572 * to implement layer2. This preallocation is a bootstrap allocation
573 * using blocks from the target volume.
575 freemap
= &root_vol
->ondisk
->vol0_blockmap
[HAMMER_ZONE_FREEMAP_INDEX
];
577 for (phys_offset
= HAMMER_ENCODE_RAW_BUFFER(vol
->vol_no
, 0);
578 phys_offset
< aligned_vol_free_end
;
579 phys_offset
+= HAMMER_BLOCKMAP_LAYER2
) {
580 layer1_offset
= freemap
->phys_offset
+
581 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset
);
582 layer1
= get_buffer_data(layer1_offset
, &buffer1
, 0);
583 if (layer1
->phys_offset
== HAMMER_BLOCKMAP_UNAVAIL
) {
584 layer1
->phys_offset
= bootstrap_bigblock(vol
);
585 layer1
->blocks_free
= 0;
586 buffer1
->cache
.modified
= 1;
587 hammer_crc_set_layer1(layer1
);
592 * Now fill everything in.
594 for (phys_offset
= HAMMER_ENCODE_RAW_BUFFER(vol
->vol_no
, 0);
595 phys_offset
< aligned_vol_free_end
;
596 phys_offset
+= HAMMER_BLOCKMAP_LAYER2
) {
598 layer1_offset
= freemap
->phys_offset
+
599 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset
);
600 layer1
= get_buffer_data(layer1_offset
, &buffer1
, 0);
601 assert(layer1
->phys_offset
!= HAMMER_BLOCKMAP_UNAVAIL
);
603 for (block_offset
= 0;
604 block_offset
< HAMMER_BLOCKMAP_LAYER2
;
605 block_offset
+= HAMMER_BIGBLOCK_SIZE
) {
606 layer2_offset
= layer1
->phys_offset
+
607 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset
);
608 layer2
= get_buffer_data(layer2_offset
, &buffer2
, 0);
609 bzero(layer2
, sizeof(*layer2
));
611 if (phys_offset
+ block_offset
< vol
->vol_free_off
) {
613 * Big-blocks already allocated as part
614 * of the freemap bootstrap.
616 layer2
->zone
= HAMMER_ZONE_FREEMAP_INDEX
;
617 layer2
->append_off
= HAMMER_BIGBLOCK_SIZE
;
618 layer2
->bytes_free
= 0;
619 } else if (phys_offset
+ block_offset
< vol
->vol_free_end
) {
621 layer2
->append_off
= 0;
622 layer2
->bytes_free
= HAMMER_BIGBLOCK_SIZE
;
626 layer2
->zone
= HAMMER_ZONE_UNAVAIL_INDEX
;
627 layer2
->append_off
= HAMMER_BIGBLOCK_SIZE
;
628 layer2
->bytes_free
= 0;
630 hammer_crc_set_layer2(layer2
);
631 buffer2
->cache
.modified
= 1;
634 layer1
->blocks_free
+= layer1_count
;
635 hammer_crc_set_layer1(layer1
);
636 buffer1
->cache
.modified
= 1;
645 * Returns the number of big-blocks available for filesystem data and undos
646 * without formatting.
649 count_freemap(struct volume_info
*vol
)
651 hammer_off_t phys_offset
;
652 hammer_off_t vol_free_off
;
653 hammer_off_t aligned_vol_free_end
;
656 vol_free_off
= HAMMER_ENCODE_RAW_BUFFER(vol
->vol_no
, 0);
658 assert_volume_offset(vol
);
659 aligned_vol_free_end
= HAMMER_BLOCKMAP_LAYER2_DOALIGN(vol
->vol_free_end
);
661 if (vol
->vol_no
== HAMMER_ROOT_VOLNO
)
662 vol_free_off
+= HAMMER_BIGBLOCK_SIZE
;
664 for (phys_offset
= HAMMER_ENCODE_RAW_BUFFER(vol
->vol_no
, 0);
665 phys_offset
< aligned_vol_free_end
;
666 phys_offset
+= HAMMER_BLOCKMAP_LAYER2
) {
667 vol_free_off
+= HAMMER_BIGBLOCK_SIZE
;
670 for (phys_offset
= HAMMER_ENCODE_RAW_BUFFER(vol
->vol_no
, 0);
671 phys_offset
< aligned_vol_free_end
;
672 phys_offset
+= HAMMER_BIGBLOCK_SIZE
) {
673 if (phys_offset
< vol_free_off
) {
675 } else if (phys_offset
< vol
->vol_free_end
) {
684 * Format the undomap for the root volume.
687 format_undomap(struct volume_info
*root_vol
, int64_t *undo_buffer_size
)
689 hammer_off_t undo_limit
;
690 hammer_blockmap_t blockmap
;
691 hammer_volume_ondisk_t ondisk
;
692 struct buffer_info
*buffer
= NULL
;
698 /* Only root volume needs formatting */
699 assert(root_vol
->vol_no
== HAMMER_ROOT_VOLNO
);
700 ondisk
= root_vol
->ondisk
;
703 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
704 * up to HAMMER_MAX_UNDO_BIGBLOCKS big-blocks.
705 * Size to approximately 0.1% of the disk.
707 * The minimum UNDO fifo size is 512MB, or approximately 1% of
708 * the recommended 50G disk.
710 * Changing this minimum is rather dangerous as complex filesystem
711 * operations can cause the UNDO FIFO to fill up otherwise.
713 undo_limit
= *undo_buffer_size
;
714 if (undo_limit
== 0) {
715 undo_limit
= HAMMER_VOL_BUF_SIZE(ondisk
) / 1000;
716 if (undo_limit
< HAMMER_BIGBLOCK_SIZE
* HAMMER_MIN_UNDO_BIGBLOCKS
)
717 undo_limit
= HAMMER_BIGBLOCK_SIZE
* HAMMER_MIN_UNDO_BIGBLOCKS
;
719 undo_limit
= HAMMER_BIGBLOCK_DOALIGN(undo_limit
);
720 if (undo_limit
< HAMMER_BIGBLOCK_SIZE
)
721 undo_limit
= HAMMER_BIGBLOCK_SIZE
;
722 if (undo_limit
> HAMMER_BIGBLOCK_SIZE
* HAMMER_MAX_UNDO_BIGBLOCKS
)
723 undo_limit
= HAMMER_BIGBLOCK_SIZE
* HAMMER_MAX_UNDO_BIGBLOCKS
;
724 *undo_buffer_size
= undo_limit
;
726 blockmap
= &ondisk
->vol0_blockmap
[HAMMER_ZONE_UNDO_INDEX
];
727 bzero(blockmap
, sizeof(*blockmap
));
728 blockmap
->phys_offset
= HAMMER_BLOCKMAP_UNAVAIL
;
729 blockmap
->first_offset
= HAMMER_ENCODE_UNDO(0);
730 blockmap
->next_offset
= blockmap
->first_offset
;
731 blockmap
->alloc_offset
= HAMMER_ENCODE_UNDO(undo_limit
);
732 hammer_crc_set_blockmap(blockmap
);
734 limit_index
= undo_limit
/ HAMMER_BIGBLOCK_SIZE
;
735 assert(limit_index
<= HAMMER_MAX_UNDO_BIGBLOCKS
);
737 for (n
= 0; n
< limit_index
; ++n
) {
738 ondisk
->vol0_undo_array
[n
] = alloc_undo_bigblock(root_vol
);
740 while (n
< HAMMER_MAX_UNDO_BIGBLOCKS
) {
741 ondisk
->vol0_undo_array
[n
++] = HAMMER_BLOCKMAP_UNAVAIL
;
745 * Pre-initialize the UNDO blocks (HAMMER version 4+)
747 printf("initializing the undo map (%jd MB)\n",
748 (intmax_t)HAMMER_OFF_LONG_ENCODE(blockmap
->alloc_offset
) /
751 scan
= blockmap
->first_offset
;
754 while (scan
< blockmap
->alloc_offset
) {
755 hammer_fifo_head_t head
;
756 hammer_fifo_tail_t tail
;
758 int bytes
= HAMMER_UNDO_ALIGN
;
760 isnew
= ((scan
& HAMMER_BUFMASK64
) == 0);
761 head
= get_buffer_data(scan
, &buffer
, isnew
);
762 buffer
->cache
.modified
= 1;
763 tail
= (void *)((char *)head
+ bytes
- sizeof(*tail
));
766 head
->hdr_signature
= HAMMER_HEAD_SIGNATURE
;
767 head
->hdr_type
= HAMMER_HEAD_TYPE_DUMMY
;
768 head
->hdr_size
= bytes
;
769 head
->hdr_seq
= seqno
++;
771 tail
->tail_signature
= HAMMER_TAIL_SIGNATURE
;
772 tail
->tail_type
= HAMMER_HEAD_TYPE_DUMMY
;
773 tail
->tail_size
= bytes
;
775 hammer_crc_set_fifo_head(head
, bytes
);
782 const char *zone_labels
[] = {
784 "raw_volume", /* 1 */
785 "raw_buffer", /* 2 */
793 "large_data", /* 10 */
794 "small_data", /* 11 */
802 print_blockmap(const struct volume_info
*vol
)
804 hammer_blockmap_t blockmap
;
805 hammer_volume_ondisk_t ondisk
;
810 ondisk
= vol
->ondisk
;
811 printf(INDENT
"vol_label\t%s\n", ondisk
->vol_label
);
812 printf(INDENT
"vol_count\t%d\n", ondisk
->vol_count
);
813 printf(INDENT
"vol_bot_beg\t%s\n", sizetostr(ondisk
->vol_bot_beg
));
814 printf(INDENT
"vol_mem_beg\t%s\n", sizetostr(ondisk
->vol_mem_beg
));
815 printf(INDENT
"vol_buf_beg\t%s\n", sizetostr(ondisk
->vol_buf_beg
));
816 printf(INDENT
"vol_buf_end\t%s\n", sizetostr(ondisk
->vol_buf_end
));
817 printf(INDENT
"vol0_next_tid\t%016jx\n",
818 (uintmax_t)ondisk
->vol0_next_tid
);
820 blockmap
= &ondisk
->vol0_blockmap
[HAMMER_ZONE_UNDO_INDEX
];
821 size
= HAMMER_OFF_LONG_ENCODE(blockmap
->alloc_offset
);
822 if (blockmap
->first_offset
<= blockmap
->next_offset
)
823 used
= blockmap
->next_offset
- blockmap
->first_offset
;
825 used
= blockmap
->alloc_offset
- blockmap
->first_offset
+
826 HAMMER_OFF_LONG_ENCODE(blockmap
->next_offset
);
827 printf(INDENT
"undo_size\t%s\n", sizetostr(size
));
828 printf(INDENT
"undo_used\t%s\n", sizetostr(used
));
830 printf(INDENT
"zone # "
831 "phys first next alloc\n");
832 for (i
= 0; i
< HAMMER_MAX_ZONES
; i
++) {
833 blockmap
= &ondisk
->vol0_blockmap
[i
];
834 printf(INDENT
"zone %-2d %-10s %016jx %016jx %016jx %016jx\n",
836 (uintmax_t)blockmap
->phys_offset
,
837 (uintmax_t)blockmap
->first_offset
,
838 (uintmax_t)blockmap
->next_offset
,
839 (uintmax_t)blockmap
->alloc_offset
);
844 * Flush various tracking structures to disk
847 flush_all_volumes(void)
849 struct volume_info
*vol
;
851 TAILQ_FOREACH(vol
, &VolList
, entry
)
856 flush_volume(struct volume_info
*volume
)
858 struct buffer_info
*buffer
;
861 for (i
= 0; i
< HAMMER_BUFLISTS
; ++i
) {
862 TAILQ_FOREACH(buffer
, &volume
->buffer_lists
[i
], entry
)
863 flush_buffer(buffer
);
865 if (writehammervol(volume
) == -1)
866 err(1, "Write volume %d (%s)", volume
->vol_no
, volume
->name
);
870 flush_buffer(struct buffer_info
*buffer
)
872 struct volume_info
*vol
;
874 vol
= buffer
->volume
;
875 if (writehammerbuf(buffer
) == -1)
876 err(1, "Write volume %d (%s)", vol
->vol_no
, vol
->name
);
877 buffer
->cache
.modified
= 0;
881 * Core I/O operations
884 __read(struct volume_info
*vol
, void *data
, int64_t offset
, int size
)
888 n
= pread(vol
->fd
, data
, size
, offset
);
895 readhammervol(struct volume_info
*vol
)
897 return(__read(vol
, vol
->ondisk
, 0, HAMMER_BUFSIZE
));
901 readhammerbuf(struct buffer_info
*buffer
)
903 return(__read(buffer
->volume
, buffer
->ondisk
, buffer
->raw_offset
,
908 __write(struct volume_info
*vol
, const void *data
, int64_t offset
, int size
)
915 n
= pwrite(vol
->fd
, data
, size
, offset
);
922 writehammervol(struct volume_info
*vol
)
924 return(__write(vol
, vol
->ondisk
, 0, HAMMER_BUFSIZE
));
928 writehammerbuf(struct buffer_info
*buffer
)
930 return(__write(buffer
->volume
, buffer
->ondisk
, buffer
->raw_offset
,
934 int64_t init_boot_area_size(int64_t value
, off_t avg_vol_size
)
937 value
= HAMMER_BOOT_NOMBYTES
;
938 while (value
> avg_vol_size
/ HAMMER_MAX_VOLUMES
)
942 if (value
< HAMMER_BOOT_MINBYTES
) {
943 value
= HAMMER_BOOT_MINBYTES
;
944 } else if (value
> HAMMER_BOOT_MAXBYTES
) {
945 value
= HAMMER_BOOT_MAXBYTES
;
951 int64_t init_memory_log_size(int64_t value
, off_t avg_vol_size
)
954 value
= HAMMER_MEM_NOMBYTES
;
955 while (value
> avg_vol_size
/ HAMMER_MAX_VOLUMES
)
959 if (value
< HAMMER_MEM_MINBYTES
) {
960 value
= HAMMER_MEM_MINBYTES
;
961 } else if (value
> HAMMER_MEM_MAXBYTES
) {
962 value
= HAMMER_MEM_MAXBYTES
;