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
34 * $DragonFly: src/sbin/hammer/ondisk.c,v 1.21 2008/06/02 16:57:53 dillon Exp $
37 #include <sys/types.h>
47 #include "hammer_util.h"
49 static void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
50 struct buffer_info **bufferp);
51 static hammer_off_t alloc_bigblock(struct volume_info *volume,
54 static void init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type);
55 static hammer_off_t hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
56 struct buffer_info **bufp, u_int16_t hdr_type);
57 static void readhammerbuf(struct volume_info *vol, void *data,
60 static void writehammerbuf(struct volume_info *vol, const void *data,
69 int64_t UndoBufferSize;
70 int UsingSuperClusters;
73 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
77 buffer_hash(hammer_off_t buf_offset)
81 hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
86 * Lookup the requested information structure and related on-disk buffer.
87 * Missing structures are created.
90 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
92 struct volume_info *vol;
93 struct volume_info *scan;
94 struct hammer_volume_ondisk *ondisk;
98 * Allocate the volume structure
100 vol = malloc(sizeof(*vol));
101 bzero(vol, sizeof(*vol));
102 for (i = 0; i < HAMMER_BUFLISTS; ++i)
103 TAILQ_INIT(&vol->buffer_lists[i]);
104 vol->name = strdup(filename);
105 vol->fd = open(filename, oflags);
109 err(1, "setup_volume: %s: Open failed", filename);
113 * Read or initialize the volume header
115 vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
117 bzero(ondisk, HAMMER_BUFSIZE);
119 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
120 if (n != HAMMER_BUFSIZE) {
121 err(1, "setup_volume: %s: Read failed at offset 0",
124 vol_no = ondisk->vol_no;
126 RootVolNo = ondisk->vol_rootvol;
127 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
128 errx(1, "setup_volume: %s: root volume disagreement: "
130 vol->name, RootVolNo, ondisk->vol_rootvol);
133 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
134 errx(1, "setup_volume: %s: Header does not indicate "
135 "that this is a hammer volume", vol->name);
137 if (TAILQ_EMPTY(&VolList)) {
138 Hammer_FSId = vol->ondisk->vol_fsid;
139 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
140 errx(1, "setup_volume: %s: FSId does match other "
141 "volumes!", vol->name);
144 vol->vol_no = vol_no;
147 /*init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);*/
148 vol->cache.modified = 1;
152 * Link the volume structure in
154 TAILQ_FOREACH(scan, &VolList, entry) {
155 if (scan->vol_no == vol_no) {
156 errx(1, "setup_volume %s: Duplicate volume number %d "
157 "against %s", filename, vol_no, scan->name);
160 TAILQ_INSERT_TAIL(&VolList, vol, entry);
165 get_volume(int32_t vol_no)
167 struct volume_info *vol;
169 TAILQ_FOREACH(vol, &VolList, entry) {
170 if (vol->vol_no == vol_no)
174 errx(1, "get_volume: Volume %d does not exist!", vol_no);
176 /* not added to or removed from hammer cache */
181 rel_volume(struct volume_info *volume)
183 /* not added to or removed from hammer cache */
184 --volume->cache.refs;
188 * Acquire the specified buffer.
191 get_buffer(hammer_off_t buf_offset, int isnew)
194 struct buffer_info *buf;
195 struct volume_info *volume;
196 hammer_off_t orig_offset = buf_offset;
201 zone = HAMMER_ZONE_DECODE(buf_offset);
202 if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
203 buf_offset = blockmap_lookup(buf_offset, NULL, NULL);
205 assert((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
206 vol_no = HAMMER_VOL_DECODE(buf_offset);
207 volume = get_volume(vol_no);
208 buf_offset &= ~HAMMER_BUFMASK64;
210 hi = buffer_hash(buf_offset);
212 TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry) {
213 if (buf->buf_offset == buf_offset)
217 buf = malloc(sizeof(*buf));
218 bzero(buf, sizeof(*buf));
220 fprintf(stderr, "get_buffer %016llx %016llx\n",
221 orig_offset, buf_offset);
223 buf->buf_offset = buf_offset;
224 buf->buf_disk_offset = volume->ondisk->vol_buf_beg +
225 (buf_offset & HAMMER_OFF_SHORT_MASK);
226 buf->volume = volume;
227 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
228 ++volume->cache.refs;
229 buf->cache.u.buffer = buf;
230 hammer_cache_add(&buf->cache, ISBUFFER);
233 hammer_cache_flush();
234 if ((ondisk = buf->ondisk) == NULL) {
235 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
237 n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
238 buf->buf_disk_offset);
239 if (n != HAMMER_BUFSIZE) {
240 err(1, "get_buffer: %s:%016llx Read failed at "
242 volume->name, buf->buf_offset,
243 buf->buf_disk_offset);
248 bzero(ondisk, HAMMER_BUFSIZE);
249 buf->cache.modified = 1;
255 rel_buffer(struct buffer_info *buffer)
257 struct volume_info *volume;
260 assert(buffer->cache.refs > 0);
261 if (--buffer->cache.refs == 0) {
262 if (buffer->cache.delete) {
263 hi = buffer_hash(buffer->buf_offset);
264 volume = buffer->volume;
265 if (buffer->cache.modified)
266 flush_buffer(buffer);
267 TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
268 hammer_cache_del(&buffer->cache);
269 free(buffer->ondisk);
277 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
280 struct buffer_info *buffer;
282 if ((buffer = *bufferp) != NULL) {
284 ((buffer->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
286 buffer = *bufferp = NULL;
290 buffer = *bufferp = get_buffer(buf_offset, isnew);
291 return((char *)buffer->ondisk + ((int32_t)buf_offset & HAMMER_BUFMASK));
295 * Retrieve a pointer to a B-Tree node given a cluster offset. The underlying
296 * bufp is freed if non-NULL and a referenced buffer is loaded into it.
299 get_node(hammer_off_t node_offset, struct buffer_info **bufp)
301 struct buffer_info *buf;
305 *bufp = buf = get_buffer(node_offset, 0);
306 return((void *)((char *)buf->ondisk +
307 (int32_t)(node_offset & HAMMER_BUFMASK)));
311 * Allocate HAMMER elements - btree nodes, data storage, and record elements
313 * NOTE: hammer_alloc_fifo() initializes the fifo header for the returned
314 * item and zero's out the remainder, so don't bzero() it.
317 alloc_btree_element(hammer_off_t *offp)
319 struct buffer_info *buffer = NULL;
320 hammer_node_ondisk_t node;
322 node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
324 bzero(node, sizeof(*node));
325 /* XXX buffer not released, pointer remains valid */
330 alloc_data_element(hammer_off_t *offp, int32_t data_len,
331 struct buffer_info **data_bufferp)
335 if (data_len >= HAMMER_BUFSIZE) {
336 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
337 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
339 bzero(data, data_len);
340 } else if (data_len) {
341 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
343 bzero(data, data_len);
351 * Format a new freemap. Set all layer1 entries to UNAVAIL. The initialize
352 * code will load each volume's freemap.
355 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
357 struct buffer_info *buffer = NULL;
358 hammer_off_t layer1_offset;
359 struct hammer_blockmap_layer1 *layer1;
362 layer1_offset = alloc_bigblock(root_vol, 0);
363 for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
364 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
365 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
367 bzero(layer1, sizeof(*layer1));
368 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
369 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
373 blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
374 blockmap->phys_offset = layer1_offset;
375 blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
376 blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
377 blockmap->reserved01 = 0;
378 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
379 root_vol->cache.modified = 1;
383 * Load the volume's remaining free space into the freemap. If this is
384 * the root volume, initialize the freemap owner for the layer1 bigblock.
386 * Returns the number of bigblocks available.
389 initialize_freemap(struct volume_info *vol)
391 struct volume_info *root_vol;
392 struct buffer_info *buffer1 = NULL;
393 struct buffer_info *buffer2 = NULL;
394 struct hammer_blockmap_layer1 *layer1;
395 struct hammer_blockmap_layer2 *layer2;
396 hammer_off_t layer1_base;
397 hammer_off_t layer1_offset;
398 hammer_off_t layer2_offset;
399 hammer_off_t phys_offset;
400 hammer_off_t aligned_vol_free_end;
404 root_vol = get_volume(RootVolNo);
405 aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
406 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
408 printf("initialize freemap volume %d\n", vol->vol_no);
411 * Initialize the freemap. First preallocate the bigblocks required
412 * to implement layer2. This preallocation is a bootstrap allocation
413 * using blocks from the target volume.
415 layer1_base = root_vol->ondisk->vol0_blockmap[
416 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
417 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
418 phys_offset < aligned_vol_free_end;
419 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
420 layer1_offset = layer1_base +
421 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
422 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
423 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
424 layer1->phys_offset = alloc_bigblock(vol, 0);
425 layer1->blocks_free = 0;
426 buffer1->cache.modified = 1;
427 layer1->layer1_crc = crc32(layer1,
428 HAMMER_LAYER1_CRCSIZE);
433 * Now fill everything in.
435 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
436 phys_offset < aligned_vol_free_end;
437 phys_offset += HAMMER_LARGEBLOCK_SIZE) {
439 layer1_offset = layer1_base +
440 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
441 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
443 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
444 layer2_offset = layer1->phys_offset +
445 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
447 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
448 if (phys_offset < vol->vol_free_off) {
450 * Fixups XXX - bigblocks already allocated as part
451 * of the freemap bootstrap.
453 layer2->u.owner = HAMMER_ENCODE_FREEMAP(0, 0); /* XXX */
454 } else if (phys_offset < vol->vol_free_end) {
455 ++layer1->blocks_free;
456 buffer1->cache.modified = 1;
457 layer2->u.owner = HAMMER_BLOCKMAP_FREE;
461 layer2->u.owner = HAMMER_BLOCKMAP_UNAVAIL;
463 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
464 buffer2->cache.modified = 1;
470 layer1->layer1_crc = crc32(layer1,
471 HAMMER_LAYER1_CRCSIZE);
472 buffer1->cache.modified = 1;
477 rel_volume(root_vol);
482 * Allocate big-blocks using our poor-man's volume->vol_free_off and
483 * update the freemap if owner != 0.
485 * An owner of 0 is used for bootstrapping the freemap.
488 alloc_bigblock(struct volume_info *volume, hammer_off_t owner)
490 struct buffer_info *buffer = NULL;
491 struct volume_info *root_vol;
492 hammer_off_t result_offset;
493 hammer_off_t layer_offset;
494 struct hammer_blockmap_layer1 *layer1;
495 struct hammer_blockmap_layer2 *layer2;
498 if (volume == NULL) {
499 volume = get_volume(RootVolNo);
504 result_offset = volume->vol_free_off;
505 if (result_offset >= volume->vol_free_end)
506 panic("alloc_bigblock: Ran out of room, filesystem too small");
507 volume->vol_free_off += HAMMER_LARGEBLOCK_SIZE;
513 root_vol = get_volume(RootVolNo);
514 layer_offset = root_vol->ondisk->vol0_blockmap[
515 HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
516 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
517 layer1 = get_buffer_data(layer_offset, &buffer, 0);
518 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
519 --layer1->blocks_free;
520 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
521 buffer->cache.modified = 1;
522 layer_offset = layer1->phys_offset +
523 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
524 layer2 = get_buffer_data(layer_offset, &buffer, 0);
525 assert(layer2->u.owner == HAMMER_BLOCKMAP_FREE);
526 layer2->u.owner = owner;
527 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
528 buffer->cache.modified = 1;
530 --root_vol->ondisk->vol0_stat_freebigblocks;
531 root_vol->cache.modified = 1;
534 rel_volume(root_vol);
539 return(result_offset);
543 * Format the undo-map for the root volume.
546 format_undomap(hammer_volume_ondisk_t ondisk)
548 const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
549 hammer_off_t undo_limit;
550 hammer_blockmap_t blockmap;
552 struct hammer_blockmap_layer2 *layer2;
557 * Size the undo buffer in multiples of HAMMER_LARGEBLOCK_SIZE,
558 * up to HAMMER_UNDO_LAYER2 large blocks. Size to approximately
561 undo_limit = UndoBufferSize;
563 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
564 undo_limit = (undo_limit + HAMMER_LARGEBLOCK_MASK64) &
565 ~HAMMER_LARGEBLOCK_MASK64;
566 if (undo_limit < HAMMER_LARGEBLOCK_SIZE)
567 undo_limit = HAMMER_LARGEBLOCK_SIZE;
568 if (undo_limit > HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2)
569 undo_limit = HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2;
570 UndoBufferSize = undo_limit;
572 blockmap = &ondisk->vol0_blockmap[undo_zone];
573 bzero(blockmap, sizeof(*blockmap));
574 blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
575 blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
576 blockmap->next_offset = blockmap->first_offset;
577 blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
578 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
580 layer2 = &ondisk->vol0_undo_array[0];
582 scan = blockmap->next_offset;
583 limit_index = undo_limit / HAMMER_LARGEBLOCK_SIZE;
585 assert(limit_index <= HAMMER_UNDO_LAYER2);
587 for (n = 0; n < limit_index; ++n) {
588 layer2->u.phys_offset = alloc_bigblock(NULL, scan);
589 layer2->bytes_free = -1; /* not used */
590 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
592 scan += HAMMER_LARGEBLOCK_SIZE;
595 while (n < HAMMER_UNDO_LAYER2) {
596 layer2->u.phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
597 layer2->bytes_free = -1;
598 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
605 * Format a new blockmap. Set the owner to the base of the blockmap
606 * (meaning either the blockmap layer1 bigblock, layer2 bigblock, or
610 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base)
612 blockmap->phys_offset = alloc_bigblock(NULL, zone_base);
613 blockmap->alloc_offset = zone_base;
614 blockmap->first_offset = zone_base;
615 blockmap->next_offset = zone_base;
616 blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
620 * Allocate a chunk of data out of a blockmap. This is a simplified
621 * version which uses next_offset as a simple allocation iterator.
625 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
626 struct buffer_info **bufferp)
628 struct buffer_info *buffer1 = NULL;
629 struct buffer_info *buffer2 = NULL;
630 struct volume_info *volume;
631 hammer_blockmap_t rootmap;
632 struct hammer_blockmap_layer1 *layer1;
633 struct hammer_blockmap_layer2 *layer2;
634 hammer_off_t layer1_offset;
635 hammer_off_t layer2_offset;
636 hammer_off_t bigblock_offset;
639 volume = get_volume(RootVolNo);
641 rootmap = &volume->ondisk->vol0_blockmap[zone];
644 * Alignment and buffer-boundary issues. If the allocation would
645 * cross a buffer boundary we have to skip to the next buffer.
647 bytes = (bytes + 7) & ~7;
648 if ((rootmap->next_offset ^ (rootmap->next_offset + bytes - 1)) &
650 volume->cache.modified = 1;
651 rootmap->next_offset = (rootmap->next_offset + bytes) &
658 layer1_offset = rootmap->phys_offset +
659 HAMMER_BLOCKMAP_LAYER1_OFFSET(rootmap->next_offset);
661 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
662 if ((rootmap->next_offset >= rootmap->alloc_offset &&
663 (rootmap->next_offset & HAMMER_BLOCKMAP_LAYER2_MASK) == 0) ||
664 layer1->phys_offset == HAMMER_BLOCKMAP_FREE
666 assert(rootmap->next_offset >= rootmap->alloc_offset);
667 buffer1->cache.modified = 1;
668 bzero(layer1, sizeof(*layer1));
669 layer1->blocks_free = HAMMER_BLOCKMAP_RADIX2;
670 layer1->phys_offset = alloc_bigblock(NULL,
671 rootmap->next_offset);
677 layer2_offset = layer1->phys_offset +
678 HAMMER_BLOCKMAP_LAYER2_OFFSET(rootmap->next_offset);
680 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
682 if ((rootmap->next_offset & HAMMER_LARGEBLOCK_MASK64) == 0 &&
683 (rootmap->next_offset >= rootmap->alloc_offset ||
684 layer2->u.phys_offset == HAMMER_BLOCKMAP_FREE)
686 assert(rootmap->next_offset >= rootmap->alloc_offset);
687 buffer2->cache.modified = 1;
688 bzero(layer2, sizeof(*layer2));
689 layer2->u.phys_offset = alloc_bigblock(NULL,
690 rootmap->next_offset);
691 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
692 --layer1->blocks_free;
695 buffer1->cache.modified = 1;
696 buffer2->cache.modified = 1;
697 volume->cache.modified = 1;
698 layer2->bytes_free -= bytes;
699 *result_offp = rootmap->next_offset;
700 rootmap->next_offset += bytes;
701 rootmap->alloc_offset = rootmap->next_offset;
703 bigblock_offset = layer2->u.phys_offset +
704 (*result_offp & HAMMER_LARGEBLOCK_MASK);
706 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
707 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
709 ptr = get_buffer_data(bigblock_offset, bufferp, 0);
710 (*bufferp)->cache.modified = 1;
722 * Presize a blockmap. Allocate all layer2 bigblocks required to map the
723 * blockmap through the specified zone limit.
725 * Note: This code is typically called later, after some data may have
726 * already been allocated, but can be called or re-called at any time.
728 * Note: vol0_zone_limit is not zone-encoded.
731 presize_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_base,
732 hammer_off_t vol0_zone_limit)
734 struct buffer_info *buffer1 = NULL;
735 struct buffer_info *buffer2 = NULL;
736 struct hammer_blockmap_layer1 *layer1;
737 struct hammer_blockmap_layer2 *layer2;
738 hammer_off_t zone_limit;
739 hammer_off_t layer1_offset;
740 hammer_off_t layer2_offset;
742 zone_limit = zone_base + vol0_zone_limit;
744 while (zone_base < zone_limit) {
745 layer1_offset = blockmap->phys_offset +
746 HAMMER_BLOCKMAP_LAYER1_OFFSET(zone_base);
747 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
749 if ((zone_base >= blockmap->alloc_offset &&
750 (zone_base & HAMMER_BLOCKMAP_LAYER2_MASK) == 0) ||
751 layer1->phys_offset == HAMMER_BLOCKMAP_FREE
753 bzero(layer1, sizeof(*layer1));
754 layer1->blocks_free = HAMMER_BLOCKMAP_RADIX2;
755 layer1->phys_offset = alloc_bigblock(NULL, zone_base);
756 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
757 buffer1->cache.modified = 1;
759 layer2_offset = layer1->phys_offset +
760 HAMMER_BLOCKMAP_LAYER2_OFFSET(zone_base);
761 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
762 if (zone_base >= blockmap->alloc_offset ||
763 layer2->u.phys_offset == HAMMER_BLOCKMAP_FREE) {
764 bzero(layer2, sizeof(*layer2));
765 layer2->u.phys_offset = HAMMER_BLOCKMAP_FREE;
766 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
767 layer2->entry_crc = crc32(layer2,
768 HAMMER_LAYER2_CRCSIZE);
769 buffer2->cache.modified = 1;
771 zone_base += HAMMER_LARGEBLOCK_SIZE64;
773 if (blockmap->alloc_offset < zone_limit)
774 blockmap->alloc_offset = zone_limit;
784 * Reserve space from the FIFO. Make sure that bytes does not cross a
787 * Zero out base_bytes and initialize the fifo head and tail. The
788 * data area is not zerod.
792 hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
793 struct buffer_info **bufp, u_int16_t hdr_type)
795 struct buffer_info *buf;
796 struct volume_info *volume;
797 hammer_fifo_head_t head;
798 hammer_fifo_tail_t tail;
800 int32_t aligned_bytes;
802 aligned_bytes = (base_bytes + ext_bytes + HAMMER_TAIL_ONDISK_SIZE +
803 HAMMER_HEAD_ALIGN_MASK) & ~HAMMER_HEAD_ALIGN_MASK;
805 volume = get_volume(RootVolNo);
806 off = volume->ondisk->vol0_fifo_end;
809 * For now don't deal with transitions across buffer boundaries,
810 * only newfs_hammer uses this function.
812 assert((off & ~HAMMER_BUFMASK64) ==
813 ((off + aligned_bytes) & ~HAMMER_BUFMASK));
815 *bufp = buf = get_buffer(off, 0);
817 buf->cache.modified = 1;
818 volume->cache.modified = 1;
820 head = (void *)((char *)buf->ondisk + ((int32_t)off & HAMMER_BUFMASK));
821 bzero(head, base_bytes);
823 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
824 head->hdr_type = hdr_type;
825 head->hdr_size = aligned_bytes;
826 head->hdr_seq = volume->ondisk->vol0_next_seq++;
828 tail = (void*)((char *)head + aligned_bytes - HAMMER_TAIL_ONDISK_SIZE);
829 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
830 tail->tail_type = hdr_type;
831 tail->tail_size = aligned_bytes;
833 volume->ondisk->vol0_fifo_end += aligned_bytes;
834 volume->cache.modified = 1;
844 * Flush various tracking structures to disk
848 * Flush various tracking structures to disk
851 flush_all_volumes(void)
853 struct volume_info *vol;
855 TAILQ_FOREACH(vol, &VolList, entry)
860 flush_volume(struct volume_info *volume)
862 struct buffer_info *buffer;
865 for (i = 0; i < HAMMER_BUFLISTS; ++i) {
866 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
867 flush_buffer(buffer);
869 writehammerbuf(volume, volume->ondisk, 0);
870 volume->cache.modified = 0;
874 flush_buffer(struct buffer_info *buffer)
876 writehammerbuf(buffer->volume, buffer->ondisk, buffer->buf_disk_offset);
877 buffer->cache.modified = 0;
882 * Generic buffer initialization
885 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
887 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
888 head->hdr_type = hdr_type;
898 * Core I/O operations
901 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
905 n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
906 if (n != HAMMER_BUFSIZE)
907 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
913 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
917 n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
918 if (n != HAMMER_BUFSIZE)
919 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
923 panic(const char *ctl, ...)
928 vfprintf(stderr, ctl, va);
930 fprintf(stderr, "\n");