sbin/hammer: Add /* not reached */
[dragonfly.git] / sbin / hammer / ondisk.c
blob885143fdec8c63e995140f3c3c849ce176b32148
1 /*
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
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
35 #include <sys/diskslice.h>
36 #include <sys/diskmbr.h>
38 #include "hammer_util.h"
40 static void check_volume(struct volume_info *volume);
41 static void get_buffer_readahead(struct buffer_info *base);
42 static __inline int readhammervol(struct volume_info *volume);
43 static __inline int readhammerbuf(struct buffer_info *buffer);
44 static __inline int writehammervol(struct volume_info *volume);
45 static __inline int writehammerbuf(struct buffer_info *buffer);
47 uuid_t Hammer_FSType;
48 uuid_t Hammer_FSId;
49 int UseReadBehind = -4;
50 int UseReadAhead = 4;
51 int DebugOpt;
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;
58 static __inline
59 int
60 buffer_hash(hammer_off_t zone2_offset)
62 int hi;
64 hi = (int)(zone2_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
65 return(hi);
68 static struct buffer_info*
69 find_buffer(hammer_off_t zone2_offset)
71 struct volume_info *volume;
72 struct buffer_info *buffer;
73 int hi;
75 volume = get_volume(HAMMER_VOL_DECODE(zone2_offset));
76 assert(volume);
78 hi = buffer_hash(zone2_offset);
79 TAILQ_FOREACH(buffer, &volume->buffer_lists[hi], entry)
80 if (buffer->zone2_offset == zone2_offset)
81 return(buffer);
82 return(NULL);
85 static
86 struct volume_info *
87 __alloc_volume(const char *volname, int oflags)
89 struct volume_info *volume;
90 int i;
92 volume = calloc(1, sizeof(*volume));
93 volume->vol_no = -1;
94 volume->rdonly = (oflags == O_RDONLY);
95 volume->name = strdup(volname);
96 volume->fd = open(volume->name, oflags);
97 if (volume->fd < 0) {
98 err(1, "alloc_volume: Failed to open %s", volume->name);
99 /* not reached */
101 check_volume(volume);
103 volume->ondisk = calloc(1, HAMMER_BUFSIZE);
105 for (i = 0; i < HAMMER_BUFLISTS; ++i)
106 TAILQ_INIT(&volume->buffer_lists[i]);
108 return(volume);
111 static void
112 __add_volume(struct volume_info *volume)
114 struct volume_info *scan;
115 struct stat st1, st2;
117 if (fstat(volume->fd, &st1) != 0) {
118 errx(1, "add_volume: %s: Failed to stat", volume->name);
119 /* not reached */
122 TAILQ_FOREACH(scan, &VolList, entry) {
123 if (scan->vol_no == volume->vol_no) {
124 errx(1, "add_volume: %s: Duplicate volume number %d "
125 "against %s",
126 volume->name, volume->vol_no, scan->name);
127 /* not reached */
129 if (fstat(scan->fd, &st2) != 0) {
130 errx(1, "add_volume: %s: Failed to stat %s",
131 volume->name, scan->name);
132 /* not reached */
134 if ((st1.st_ino == st2.st_ino) && (st1.st_dev == st2.st_dev)) {
135 errx(1, "add_volume: %s: Specified more than once",
136 volume->name);
137 /* not reached */
141 TAILQ_INSERT_TAIL(&VolList, volume, entry);
144 static void
145 __verify_volume(struct volume_info *volume)
147 hammer_volume_ondisk_t ondisk = volume->ondisk;
149 if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) {
150 errx(1, "verify_volume: Invalid volume signature %016jx",
151 ondisk->vol_signature);
152 /* not reached */
154 if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) {
155 errx(1, "verify_volume: Invalid root volume# %d",
156 ondisk->vol_rootvol);
157 /* not reached */
159 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType))) {
160 errx(1, "verify_volume: %s: Header does not indicate "
161 "that this is a HAMMER volume", volume->name);
162 /* not reached */
164 if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId))) {
165 errx(1, "verify_volume: %s: FSId does not match other volumes!",
166 volume->name);
167 /* not reached */
172 * Initialize a volume structure and ondisk vol_no field.
174 struct volume_info *
175 init_volume(const char *filename, int oflags, int32_t vol_no)
177 struct volume_info *volume;
179 volume = __alloc_volume(filename, oflags);
180 volume->vol_no = volume->ondisk->vol_no = vol_no;
182 __add_volume(volume);
184 return(volume);
188 * Initialize a volume structure and read ondisk volume header.
190 struct volume_info*
191 load_volume(const char *filename, int oflags, int verify)
193 struct volume_info *volume;
194 int n;
196 volume = __alloc_volume(filename, oflags);
198 n = readhammervol(volume);
199 if (n == -1) {
200 err(1, "load_volume: %s: Read failed at offset 0",
201 volume->name);
202 /* not reached */
204 volume->vol_no = volume->ondisk->vol_no;
205 HammerVersion = volume->ondisk->vol_version;
207 if (valid_hammer_volumes++ == 0)
208 Hammer_FSId = volume->ondisk->vol_fsid;
209 if (verify)
210 __verify_volume(volume);
212 __add_volume(volume);
214 return(volume);
218 * Check basic volume characteristics.
220 static void
221 check_volume(struct volume_info *volume)
223 struct partinfo pinfo;
224 struct stat st;
227 * Allow the formatting of block devices or regular files
229 if (ioctl(volume->fd, DIOCGPART, &pinfo) < 0) {
230 if (fstat(volume->fd, &st) < 0) {
231 err(1, "Unable to stat %s", volume->name);
232 /* not reached */
234 if (S_ISREG(st.st_mode)) {
235 volume->size = st.st_size;
236 volume->type = "REGFILE";
237 } else {
238 errx(1, "Unsupported file type for %s", volume->name);
239 /* not reached */
241 } else {
243 * When formatting a block device as a HAMMER volume the
244 * sector size must be compatible. HAMMER uses 16384 byte
245 * filesystem buffers.
247 if (pinfo.reserved_blocks) {
248 errx(1, "HAMMER cannot be placed in a partition "
249 "which overlaps the disklabel or MBR");
250 /* not reached */
252 if (pinfo.media_blksize > HAMMER_BUFSIZE ||
253 HAMMER_BUFSIZE % pinfo.media_blksize) {
254 errx(1, "A media sector size of %d is not supported",
255 pinfo.media_blksize);
256 /* not reached */
259 volume->size = pinfo.media_size;
260 volume->device_offset = pinfo.media_offset;
261 volume->type = "DEVICE";
266 is_regfile(struct volume_info *volume)
268 return(strcmp(volume->type, "REGFILE") ? 0 : 1);
271 void
272 assert_volume_offset(struct volume_info *volume)
274 assert(hammer_is_zone_raw_buffer(volume->vol_free_off));
275 assert(hammer_is_zone_raw_buffer(volume->vol_free_end));
276 if (volume->vol_free_off >= volume->vol_free_end) {
277 errx(1, "Ran out of room, filesystem too small");
278 /* not reached */
282 struct volume_info *
283 get_volume(int32_t vol_no)
285 struct volume_info *volume;
287 TAILQ_FOREACH(volume, &VolList, entry) {
288 if (volume->vol_no == vol_no)
289 break;
292 return(volume);
295 struct volume_info *
296 get_root_volume(void)
298 return(get_volume(HAMMER_ROOT_VOLNO));
301 static hammer_off_t
302 __blockmap_xlate_to_zone2(hammer_off_t buf_offset)
304 hammer_off_t zone2_offset;
305 int error = 0;
307 if (hammer_is_zone_raw_buffer(buf_offset))
308 zone2_offset = buf_offset;
309 else
310 zone2_offset = blockmap_lookup(buf_offset, &error);
312 if (error)
313 return(HAMMER_OFF_BAD);
314 assert(hammer_is_zone_raw_buffer(zone2_offset));
316 return(zone2_offset);
319 static struct buffer_info *
320 __alloc_buffer(hammer_off_t zone2_offset, int isnew)
322 struct volume_info *volume;
323 struct buffer_info *buffer;
324 int hi;
326 volume = get_volume(HAMMER_VOL_DECODE(zone2_offset));
327 assert(volume != NULL);
329 buffer = calloc(1, sizeof(*buffer));
330 buffer->zone2_offset = zone2_offset;
331 buffer->raw_offset = hammer_xlate_to_phys(volume->ondisk, zone2_offset);
332 buffer->volume = volume;
333 buffer->ondisk = calloc(1, HAMMER_BUFSIZE);
335 if (isnew <= 0) {
336 if (readhammerbuf(buffer) == -1) {
337 err(1, "Failed to read %s:%016jx at %016jx",
338 volume->name,
339 (intmax_t)buffer->zone2_offset,
340 (intmax_t)buffer->raw_offset);
341 /* not reached */
345 hi = buffer_hash(zone2_offset);
346 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buffer, entry);
347 hammer_cache_add(&buffer->cache);
349 return(buffer);
353 * Acquire the 16KB buffer for specified zone offset.
355 static struct buffer_info *
356 get_buffer(hammer_off_t buf_offset, int isnew)
358 struct buffer_info *buffer;
359 hammer_off_t zone2_offset;
360 int dora = 0;
362 zone2_offset = __blockmap_xlate_to_zone2(buf_offset);
363 if (zone2_offset == HAMMER_OFF_BAD)
364 return(NULL);
366 zone2_offset &= ~HAMMER_BUFMASK64;
367 buffer = find_buffer(zone2_offset);
369 if (buffer == NULL) {
370 buffer = __alloc_buffer(zone2_offset, isnew);
371 dora = (isnew == 0);
372 } else {
373 assert(isnew != -1);
374 hammer_cache_used(&buffer->cache);
376 assert(buffer->ondisk != NULL);
378 ++buffer->cache.refs;
379 hammer_cache_flush();
381 if (isnew > 0) {
382 assert(buffer->cache.modified == 0);
383 bzero(buffer->ondisk, HAMMER_BUFSIZE);
384 buffer->cache.modified = 1;
386 if (dora)
387 get_buffer_readahead(buffer);
388 return(buffer);
391 static void
392 get_buffer_readahead(struct buffer_info *base)
394 struct buffer_info *buffer;
395 struct volume_info *volume;
396 hammer_off_t zone2_offset;
397 int64_t raw_offset;
398 int ri = UseReadBehind;
399 int re = UseReadAhead;
401 raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
402 volume = base->volume;
404 while (ri < re) {
405 if (raw_offset >= volume->ondisk->vol_buf_end)
406 break;
407 if (raw_offset < volume->ondisk->vol_buf_beg || ri == 0) {
408 ++ri;
409 raw_offset += HAMMER_BUFSIZE;
410 continue;
412 zone2_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no,
413 raw_offset - volume->ondisk->vol_buf_beg);
414 buffer = find_buffer(zone2_offset);
415 if (buffer == NULL) {
416 /* call with -1 to prevent another readahead */
417 buffer = get_buffer(zone2_offset, -1);
418 rel_buffer(buffer);
420 ++ri;
421 raw_offset += HAMMER_BUFSIZE;
425 void
426 rel_buffer(struct buffer_info *buffer)
428 struct volume_info *volume;
429 int hi;
431 if (buffer == NULL)
432 return;
433 assert(buffer->cache.refs > 0);
434 if (--buffer->cache.refs == 0) {
435 if (buffer->cache.delete) {
436 hi = buffer_hash(buffer->zone2_offset);
437 volume = buffer->volume;
438 if (buffer->cache.modified)
439 flush_buffer(buffer);
440 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
441 hammer_cache_del(&buffer->cache);
442 free(buffer->ondisk);
443 free(buffer);
449 * Retrieve a pointer to a buffer data given a buffer offset. The underlying
450 * bufferp is freed if isnew or the offset is out of range of the cached data.
451 * If bufferp is freed a referenced buffer is loaded into it.
453 void *
454 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
455 int isnew)
457 hammer_off_t xor;
459 if (*bufferp != NULL) {
460 /* XXX xor is always non zero for indirect zones */
461 xor = HAMMER_OFF_LONG_ENCODE(buf_offset) ^
462 HAMMER_OFF_LONG_ENCODE((*bufferp)->zone2_offset);
463 if (isnew > 0 || (xor & ~HAMMER_BUFMASK64)) {
464 rel_buffer(*bufferp);
465 *bufferp = NULL;
469 if (*bufferp == NULL) {
470 *bufferp = get_buffer(buf_offset, isnew);
471 if (*bufferp == NULL)
472 return(NULL);
475 return(((char *)(*bufferp)->ondisk) +
476 ((int32_t)buf_offset & HAMMER_BUFMASK));
480 * Allocate HAMMER elements - B-Tree nodes
482 hammer_node_ondisk_t
483 alloc_btree_node(hammer_off_t *offp, struct buffer_info **data_bufferp)
485 hammer_node_ondisk_t node;
487 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
488 offp, data_bufferp);
489 bzero(node, sizeof(*node));
490 return(node);
494 * Allocate HAMMER elements - meta data (inode, direntry, PFS, etc)
496 void *
497 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
498 struct buffer_info **data_bufferp)
500 void *data;
502 data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
503 offp, data_bufferp);
504 bzero(data, data_len);
505 return(data);
509 * Format a new blockmap. This is mostly a degenerate case because
510 * all allocations are now actually done from the freemap.
512 void
513 format_blockmap(struct volume_info *root_vol, int zone, hammer_off_t offset)
515 hammer_blockmap_t blockmap;
516 hammer_off_t zone_base;
518 /* Only root volume needs formatting */
519 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
521 assert(hammer_is_index_record(zone));
523 blockmap = &root_vol->ondisk->vol0_blockmap[zone];
524 zone_base = HAMMER_ZONE_ENCODE(zone, offset);
526 bzero(blockmap, sizeof(*blockmap));
527 blockmap->phys_offset = 0;
528 blockmap->first_offset = zone_base;
529 blockmap->next_offset = zone_base;
530 blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
531 hammer_crc_set_blockmap(HammerVersion, blockmap);
535 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
536 * code will load each volume's freemap.
538 void
539 format_freemap(struct volume_info *root_vol)
541 struct buffer_info *buffer = NULL;
542 hammer_off_t layer1_offset;
543 hammer_blockmap_t blockmap;
544 hammer_blockmap_layer1_t layer1;
545 int i, isnew;
547 /* Only root volume needs formatting */
548 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
550 layer1_offset = bootstrap_bigblock(root_vol);
551 for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
552 isnew = ((i % HAMMER_BUFSIZE) == 0);
553 layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
554 bzero(layer1, sizeof(*layer1));
555 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
556 layer1->blocks_free = 0;
557 hammer_crc_set_layer1(HammerVersion, layer1);
559 assert(i == HAMMER_BIGBLOCK_SIZE);
560 rel_buffer(buffer);
562 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
563 bzero(blockmap, sizeof(*blockmap));
564 blockmap->phys_offset = layer1_offset;
565 blockmap->first_offset = 0;
566 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
567 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
568 hammer_crc_set_blockmap(HammerVersion, blockmap);
572 * Load the volume's remaining free space into the freemap.
574 * Returns the number of big-blocks available.
576 int64_t
577 initialize_freemap(struct volume_info *volume)
579 struct volume_info *root_vol;
580 struct buffer_info *buffer1 = NULL;
581 struct buffer_info *buffer2 = NULL;
582 hammer_blockmap_layer1_t layer1;
583 hammer_blockmap_layer2_t layer2;
584 hammer_off_t layer1_offset;
585 hammer_off_t layer2_offset;
586 hammer_off_t phys_offset;
587 hammer_off_t block_offset;
588 hammer_off_t aligned_vol_free_end;
589 hammer_blockmap_t freemap;
590 int64_t count = 0;
591 int64_t layer1_count = 0;
593 root_vol = get_root_volume();
595 assert_volume_offset(volume);
596 aligned_vol_free_end = HAMMER_BLOCKMAP_LAYER2_DOALIGN(volume->vol_free_end);
598 printf("initialize freemap volume %d\n", volume->vol_no);
601 * Initialize the freemap. First preallocate the big-blocks required
602 * to implement layer2. This preallocation is a bootstrap allocation
603 * using blocks from the target volume.
605 freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
607 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
608 phys_offset < aligned_vol_free_end;
609 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
610 layer1_offset = freemap->phys_offset +
611 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
612 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
613 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
614 layer1->phys_offset = bootstrap_bigblock(volume);
615 layer1->blocks_free = 0;
616 buffer1->cache.modified = 1;
617 hammer_crc_set_layer1(HammerVersion, layer1);
622 * Now fill everything in.
624 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
625 phys_offset < aligned_vol_free_end;
626 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
627 layer1_count = 0;
628 layer1_offset = freemap->phys_offset +
629 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
630 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
631 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
633 for (block_offset = 0;
634 block_offset < HAMMER_BLOCKMAP_LAYER2;
635 block_offset += HAMMER_BIGBLOCK_SIZE) {
636 layer2_offset = layer1->phys_offset +
637 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
638 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
639 bzero(layer2, sizeof(*layer2));
641 if (phys_offset + block_offset < volume->vol_free_off) {
643 * Big-blocks already allocated as part
644 * of the freemap bootstrap.
646 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
647 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
648 layer2->bytes_free = 0;
649 } else if (phys_offset + block_offset < volume->vol_free_end) {
650 layer2->zone = 0;
651 layer2->append_off = 0;
652 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
653 ++count;
654 ++layer1_count;
655 } else {
656 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
657 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
658 layer2->bytes_free = 0;
660 hammer_crc_set_layer2(HammerVersion, layer2);
661 buffer2->cache.modified = 1;
664 layer1->blocks_free += layer1_count;
665 hammer_crc_set_layer1(HammerVersion, layer1);
666 buffer1->cache.modified = 1;
669 rel_buffer(buffer1);
670 rel_buffer(buffer2);
671 return(count);
675 * Returns the number of big-blocks available for filesystem data and undos
676 * without formatting.
678 int64_t
679 count_freemap(struct volume_info *volume)
681 hammer_off_t phys_offset;
682 hammer_off_t vol_free_off;
683 hammer_off_t aligned_vol_free_end;
684 int64_t count = 0;
686 vol_free_off = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
688 assert_volume_offset(volume);
689 aligned_vol_free_end = HAMMER_BLOCKMAP_LAYER2_DOALIGN(volume->vol_free_end);
691 if (volume->vol_no == HAMMER_ROOT_VOLNO)
692 vol_free_off += HAMMER_BIGBLOCK_SIZE;
694 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
695 phys_offset < aligned_vol_free_end;
696 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
697 vol_free_off += HAMMER_BIGBLOCK_SIZE;
700 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
701 phys_offset < aligned_vol_free_end;
702 phys_offset += HAMMER_BIGBLOCK_SIZE) {
703 if (phys_offset < vol_free_off)
705 else if (phys_offset < volume->vol_free_end)
706 ++count;
709 return(count);
713 * Format the undomap for the root volume.
715 void
716 format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size)
718 hammer_off_t undo_limit;
719 hammer_blockmap_t blockmap;
720 hammer_volume_ondisk_t ondisk;
721 struct buffer_info *buffer = NULL;
722 hammer_off_t scan;
723 int n;
724 int limit_index;
725 uint32_t seqno;
727 /* Only root volume needs formatting */
728 assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
729 ondisk = root_vol->ondisk;
732 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
733 * up to HAMMER_MAX_UNDO_BIGBLOCKS big-blocks.
734 * Size to approximately 0.1% of the disk.
736 * The minimum UNDO fifo size is 512MB, or approximately 1% of
737 * the recommended 50G disk.
739 * Changing this minimum is rather dangerous as complex filesystem
740 * operations can cause the UNDO FIFO to fill up otherwise.
742 undo_limit = *undo_buffer_size;
743 if (undo_limit == 0) {
744 undo_limit = HAMMER_VOL_BUF_SIZE(ondisk) / 1000;
745 if (undo_limit < HAMMER_BIGBLOCK_SIZE * HAMMER_MIN_UNDO_BIGBLOCKS)
746 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_MIN_UNDO_BIGBLOCKS;
748 undo_limit = HAMMER_BIGBLOCK_DOALIGN(undo_limit);
749 if (undo_limit < HAMMER_BIGBLOCK_SIZE)
750 undo_limit = HAMMER_BIGBLOCK_SIZE;
751 if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_MAX_UNDO_BIGBLOCKS)
752 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_MAX_UNDO_BIGBLOCKS;
753 *undo_buffer_size = undo_limit;
755 blockmap = &ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
756 bzero(blockmap, sizeof(*blockmap));
757 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
758 blockmap->first_offset = HAMMER_ENCODE_UNDO(0);
759 blockmap->next_offset = blockmap->first_offset;
760 blockmap->alloc_offset = HAMMER_ENCODE_UNDO(undo_limit);
761 hammer_crc_set_blockmap(HammerVersion, blockmap);
763 limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
764 assert(limit_index <= HAMMER_MAX_UNDO_BIGBLOCKS);
766 for (n = 0; n < limit_index; ++n)
767 ondisk->vol0_undo_array[n] = alloc_undo_bigblock(root_vol);
768 while (n < HAMMER_MAX_UNDO_BIGBLOCKS)
769 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
772 * Pre-initialize the UNDO blocks (HAMMER version 4+)
774 printf("initializing the undo map (%jd MB)\n",
775 (intmax_t)HAMMER_OFF_LONG_ENCODE(blockmap->alloc_offset) /
776 (1024 * 1024));
778 scan = blockmap->first_offset;
779 seqno = 0;
781 while (scan < blockmap->alloc_offset) {
782 hammer_fifo_head_t head;
783 hammer_fifo_tail_t tail;
784 int isnew;
785 int bytes = HAMMER_UNDO_ALIGN;
787 isnew = ((scan & HAMMER_BUFMASK64) == 0);
788 head = get_buffer_data(scan, &buffer, isnew);
789 buffer->cache.modified = 1;
790 tail = (void *)((char *)head + bytes - sizeof(*tail));
792 bzero(head, bytes);
793 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
794 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
795 head->hdr_size = bytes;
796 head->hdr_seq = seqno++;
798 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
799 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
800 tail->tail_size = bytes;
802 hammer_crc_set_fifo_head(HammerVersion, head, bytes);
804 scan += bytes;
806 rel_buffer(buffer);
809 const char *zone_labels[] = {
810 "", /* 0 */
811 "raw_volume", /* 1 */
812 "raw_buffer", /* 2 */
813 "undo", /* 3 */
814 "freemap", /* 4 */
815 "", /* 5 */
816 "", /* 6 */
817 "", /* 7 */
818 "btree", /* 8 */
819 "meta", /* 9 */
820 "large_data", /* 10 */
821 "small_data", /* 11 */
822 "", /* 12 */
823 "", /* 13 */
824 "", /* 14 */
825 "unavail", /* 15 */
828 void
829 print_blockmap(const struct volume_info *volume)
831 hammer_blockmap_t blockmap;
832 hammer_volume_ondisk_t ondisk;
833 int64_t size, used;
834 int i;
835 #define INDENT ""
837 ondisk = volume->ondisk;
838 printf(INDENT"vol_label\t%s\n", ondisk->vol_label);
839 printf(INDENT"vol_count\t%d\n", ondisk->vol_count);
840 printf(INDENT"vol_bot_beg\t%s\n", sizetostr(ondisk->vol_bot_beg));
841 printf(INDENT"vol_mem_beg\t%s\n", sizetostr(ondisk->vol_mem_beg));
842 printf(INDENT"vol_buf_beg\t%s\n", sizetostr(ondisk->vol_buf_beg));
843 printf(INDENT"vol_buf_end\t%s\n", sizetostr(ondisk->vol_buf_end));
844 printf(INDENT"vol0_next_tid\t%016jx\n",
845 (uintmax_t)ondisk->vol0_next_tid);
847 blockmap = &ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
848 size = HAMMER_OFF_LONG_ENCODE(blockmap->alloc_offset);
849 if (blockmap->first_offset <= blockmap->next_offset)
850 used = blockmap->next_offset - blockmap->first_offset;
851 else
852 used = blockmap->alloc_offset - blockmap->first_offset +
853 HAMMER_OFF_LONG_ENCODE(blockmap->next_offset);
854 printf(INDENT"undo_size\t%s\n", sizetostr(size));
855 printf(INDENT"undo_used\t%s\n", sizetostr(used));
857 printf(INDENT"zone # "
858 "phys first next alloc\n");
859 for (i = 0; i < HAMMER_MAX_ZONES; i++) {
860 blockmap = &ondisk->vol0_blockmap[i];
861 printf(INDENT"zone %-2d %-10s %016jx %016jx %016jx %016jx\n",
862 i, zone_labels[i],
863 (uintmax_t)blockmap->phys_offset,
864 (uintmax_t)blockmap->first_offset,
865 (uintmax_t)blockmap->next_offset,
866 (uintmax_t)blockmap->alloc_offset);
871 * Flush various tracking structures to disk
873 void
874 flush_all_volumes(void)
876 struct volume_info *volume;
878 TAILQ_FOREACH(volume, &VolList, entry)
879 flush_volume(volume);
882 void
883 flush_volume(struct volume_info *volume)
885 struct buffer_info *buffer;
886 int i;
888 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
889 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
890 flush_buffer(buffer);
892 if (writehammervol(volume) == -1) {
893 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
894 /* not reached */
898 void
899 flush_buffer(struct buffer_info *buffer)
901 struct volume_info *volume;
903 volume = buffer->volume;
904 if (writehammerbuf(buffer) == -1) {
905 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
906 /* not reached */
908 buffer->cache.modified = 0;
912 * Core I/O operations
914 static int
915 __read(struct volume_info *volume, void *data, int64_t offset, int size)
917 ssize_t n;
919 n = pread(volume->fd, data, size, offset);
920 if (n != size)
921 return(-1);
922 return(0);
925 static __inline int
926 readhammervol(struct volume_info *volume)
928 return(__read(volume, volume->ondisk, 0, HAMMER_BUFSIZE));
931 static __inline int
932 readhammerbuf(struct buffer_info *buffer)
934 return(__read(buffer->volume, buffer->ondisk, buffer->raw_offset,
935 HAMMER_BUFSIZE));
938 static int
939 __write(struct volume_info *volume, const void *data, int64_t offset, int size)
941 ssize_t n;
943 if (volume->rdonly)
944 return(0);
946 n = pwrite(volume->fd, data, size, offset);
947 if (n != size)
948 return(-1);
949 return(0);
952 static __inline int
953 writehammervol(struct volume_info *volume)
955 return(__write(volume, volume->ondisk, 0, HAMMER_BUFSIZE));
958 static __inline int
959 writehammerbuf(struct buffer_info *buffer)
961 return(__write(buffer->volume, buffer->ondisk, buffer->raw_offset,
962 HAMMER_BUFSIZE));
965 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
967 if (value == 0) {
968 value = HAMMER_BOOT_NOMBYTES;
969 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
970 value >>= 1;
973 if (value < HAMMER_BOOT_MINBYTES)
974 value = HAMMER_BOOT_MINBYTES;
975 else if (value > HAMMER_BOOT_MAXBYTES)
976 value = HAMMER_BOOT_MAXBYTES;
978 return(value);
981 int64_t init_memory_log_size(int64_t value, off_t avg_vol_size)
983 if (value == 0) {
984 value = HAMMER_MEM_NOMBYTES;
985 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
986 value >>= 1;
989 if (value < HAMMER_MEM_MINBYTES)
990 value = HAMMER_MEM_MINBYTES;
991 else if (value > HAMMER_MEM_MAXBYTES)
992 value = HAMMER_MEM_MAXBYTES;
994 return(value);