xref: /dragonfly/sbin/hammer/ondisk.c (revision 277350a0)
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  */
34 
35 #include <sys/diskslice.h>
36 #include <sys/diskmbr.h>
37 
38 #include "hammer_util.h"
39 
40 static void get_buffer_readahead(struct buffer_info *base);
41 static void *get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp,
42 			int isnew);
43 static __inline int readhammervol(struct volume_info *vol);
44 static __inline int readhammerbuf(struct buffer_info *buf);
45 static __inline int writehammervol(struct volume_info *vol);
46 static __inline int writehammerbuf(struct buffer_info *buf);
47 
48 int DebugOpt;
49 
50 uuid_t Hammer_FSType;
51 uuid_t Hammer_FSId;
52 int	UseReadBehind = -4;
53 int	UseReadAhead = 4;
54 int	AssertOnFailure = 1;
55 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
56 static int valid_hammer_volumes;
57 
58 static __inline
59 int
60 buffer_hash(hammer_off_t buf_offset)
61 {
62 	int hi;
63 
64 	hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
65 	return(hi);
66 }
67 
68 static struct buffer_info*
69 find_buffer(struct volume_info *volume, hammer_off_t buf_offset)
70 {
71 	int hi;
72 	struct buffer_info *buf;
73 
74 	hi = buffer_hash(buf_offset);
75 	TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry)
76 		if (buf->buf_offset == buf_offset)
77 			return(buf);
78 	return(NULL);
79 }
80 
81 static
82 struct volume_info *
83 __alloc_volume(const char *volname, int oflags)
84 {
85 	struct volume_info *vol;
86 	int i;
87 
88 	vol = malloc(sizeof(*vol));
89 	if (vol == NULL)
90 		err(1, "alloc_volume");
91 	bzero(vol, sizeof(*vol));
92 
93 	vol->vol_no = -1;
94 	vol->rdonly = (oflags == O_RDONLY);
95 	vol->name = strdup(volname);
96 	vol->fd = open(vol->name, oflags);
97 	if (vol->fd < 0)
98 		err(1, "alloc_volume: Failed to open %s", vol->name);
99 
100 	vol->size = 0;
101 	vol->device_offset = 0;
102 	vol->type = NULL;
103 
104 	vol->ondisk = malloc(HAMMER_BUFSIZE);
105 	if (vol->ondisk == NULL)
106 		err(1, "alloc_volume");
107 	bzero(vol->ondisk, HAMMER_BUFSIZE);
108 
109 	for (i = 0; i < HAMMER_BUFLISTS; ++i)
110 		TAILQ_INIT(&vol->buffer_lists[i]);
111 
112 	return(vol);
113 }
114 
115 static void
116 __add_volume(struct volume_info *vol)
117 {
118 	struct volume_info *scan;
119 	struct stat st1, st2;
120 
121 	if (fstat(vol->fd, &st1) != 0)
122 		errx(1, "add_volume: %s: Failed to stat", vol->name);
123 
124 	TAILQ_FOREACH(scan, &VolList, entry) {
125 		if (scan->vol_no == vol->vol_no) {
126 			errx(1, "add_volume: %s: Duplicate volume number %d "
127 				"against %s",
128 				vol->name, vol->vol_no, scan->name);
129 		}
130 		if (fstat(scan->fd, &st2) != 0) {
131 			errx(1, "add_volume: %s: Failed to stat %s",
132 				vol->name, scan->name);
133 		}
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 				vol->name);
137 		}
138 	}
139 
140 	TAILQ_INSERT_TAIL(&VolList, vol, entry);
141 }
142 
143 /*
144  * Initialize a volume structure and ondisk vol_no field.
145  */
146 struct volume_info *
147 init_volume(int32_t vol_no, const char *filename, int oflags)
148 {
149 	struct volume_info *vol;
150 
151 	vol = __alloc_volume(filename, oflags);
152 	vol->vol_no = vol->ondisk->vol_no = vol_no;
153 
154 	__add_volume(vol);
155 
156 	return(vol);
157 }
158 
159 /*
160  * Initialize a volume structure and read ondisk volume header.
161  */
162 struct volume_info*
163 load_volume(const char *filename, int oflags)
164 {
165 	struct volume_info *vol;
166 	struct hammer_volume_ondisk *ondisk;
167 	int n;
168 
169 	vol = __alloc_volume(filename, oflags);
170 
171 	n = readhammervol(vol);
172 	if (n == -1) {
173 		err(1, "load_volume: %s: Read failed at offset 0", vol->name);
174 	}
175 	ondisk = vol->ondisk;
176 	vol->vol_no = ondisk->vol_no;
177 
178 	if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) {
179 		errx(1, "load_volume: Invalid root volume# %d",
180 			ondisk->vol_rootvol);
181 	}
182 
183 	if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType))) {
184 		errx(1, "load_volume: %s: Header does not indicate "
185 			"that this is a hammer volume", vol->name);
186 	}
187 
188 	if (valid_hammer_volumes++ == 0) {
189 		Hammer_FSId = ondisk->vol_fsid;
190 	} else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId))) {
191 		errx(1, "load_volume: %s: FSId does match other volumes!",
192 			vol->name);
193 	}
194 
195 	__add_volume(vol);
196 
197 	return(vol);
198 }
199 
200 /*
201  * Check basic volume characteristics.
202  */
203 void
204 check_volume(struct volume_info *vol)
205 {
206 	struct partinfo pinfo;
207 	struct stat st;
208 
209 	/*
210 	 * Get basic information about the volume
211 	 */
212 	if (ioctl(vol->fd, DIOCGPART, &pinfo) < 0) {
213 		/*
214 		 * Allow the formatting of regular files as HAMMER volumes
215 		 */
216 		if (fstat(vol->fd, &st) < 0)
217 			err(1, "Unable to stat %s", vol->name);
218 		vol->size = st.st_size;
219 		vol->type = "REGFILE";
220 	} else {
221 		/*
222 		 * When formatting a block device as a HAMMER volume the
223 		 * sector size must be compatible.  HAMMER uses 16384 byte
224 		 * filesystem buffers.
225 		 */
226 		if (pinfo.reserved_blocks) {
227 			errx(1, "HAMMER cannot be placed in a partition "
228 				"which overlaps the disklabel or MBR");
229 		}
230 		if (pinfo.media_blksize > HAMMER_BUFSIZE ||
231 		    HAMMER_BUFSIZE % pinfo.media_blksize) {
232 			errx(1, "A media sector size of %d is not supported",
233 			     pinfo.media_blksize);
234 		}
235 
236 		vol->size = pinfo.media_size;
237 		vol->device_offset = pinfo.media_offset;
238 		vol->type = "DEVICE";
239 	}
240 }
241 
242 struct volume_info *
243 get_volume(int32_t vol_no)
244 {
245 	struct volume_info *vol;
246 
247 	TAILQ_FOREACH(vol, &VolList, entry) {
248 		if (vol->vol_no == vol_no)
249 			break;
250 	}
251 	if (vol == NULL)
252 		errx(1, "get_volume: Volume %d does not exist!", vol_no);
253 
254 	/* not added to or removed from hammer cache */
255 	return(vol);
256 }
257 
258 struct volume_info *
259 get_root_volume(void)
260 {
261 	return(get_volume(HAMMER_ROOT_VOLNO));
262 }
263 
264 void
265 rel_volume(struct volume_info *volume __unused)
266 {
267 	/* nothing to do */
268 }
269 
270 /*
271  * Acquire the specified buffer.  isnew is -1 only when called
272  * via get_buffer_readahead() to prevent another readahead.
273  */
274 struct buffer_info *
275 get_buffer(hammer_off_t buf_offset, int isnew)
276 {
277 	struct buffer_info *buf;
278 	struct volume_info *volume;
279 	int vol_no;
280 	int zone;
281 	int hi;
282 	int dora = 0;
283 
284 	zone = HAMMER_ZONE_DECODE(buf_offset);
285 	if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
286 		buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
287 	}
288 	if (buf_offset == HAMMER_OFF_BAD)
289 		return(NULL);
290 	assert(hammer_is_zone_raw_buffer(buf_offset));
291 
292 	vol_no = HAMMER_VOL_DECODE(buf_offset);
293 	volume = get_volume(vol_no);
294 
295 	buf_offset &= ~HAMMER_BUFMASK64;
296 	buf = find_buffer(volume, buf_offset);
297 
298 	if (buf == NULL) {
299 		buf = malloc(sizeof(*buf));
300 		bzero(buf, sizeof(*buf));
301 		buf->buf_offset = buf_offset;
302 		buf->raw_offset = hammer_xlate_to_phys(volume->ondisk,
303 							buf_offset);
304 		buf->volume = volume;
305 		buf->ondisk = malloc(HAMMER_BUFSIZE);
306 		if (isnew <= 0) {
307 			if (readhammerbuf(buf) == -1) {
308 				err(1, "get_buffer: %s:%016jx "
309 				    "Read failed at offset %016jx",
310 				    volume->name,
311 				    (intmax_t)buf->buf_offset,
312 				    (intmax_t)buf->raw_offset);
313 			}
314 		}
315 
316 		hi = buffer_hash(buf_offset);
317 		TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
318 		buf->cache.buffer = buf;
319 		hammer_cache_add(&buf->cache);
320 		dora = (isnew == 0);
321 	} else {
322 		assert(buf->ondisk != NULL);
323 		assert(isnew != -1);
324 		hammer_cache_used(&buf->cache);
325 	}
326 
327 	++buf->cache.refs;
328 	hammer_cache_flush();
329 
330 	if (isnew > 0) {
331 		assert(buf->cache.modified == 0);
332 		bzero(buf->ondisk, HAMMER_BUFSIZE);
333 		buf->cache.modified = 1;
334 	}
335 	if (dora)
336 		get_buffer_readahead(buf);
337 	return(buf);
338 }
339 
340 static void
341 get_buffer_readahead(struct buffer_info *base)
342 {
343 	struct buffer_info *buf;
344 	struct volume_info *vol;
345 	hammer_off_t buf_offset;
346 	int64_t raw_offset;
347 	int ri = UseReadBehind;
348 	int re = UseReadAhead;
349 
350 	raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
351 	vol = base->volume;
352 
353 	while (ri < re) {
354 		if (raw_offset >= vol->ondisk->vol_buf_end)
355 			break;
356 		if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
357 			++ri;
358 			raw_offset += HAMMER_BUFSIZE;
359 			continue;
360 		}
361 		buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
362 			raw_offset - vol->ondisk->vol_buf_beg);
363 		buf = find_buffer(vol, buf_offset);
364 		if (buf == NULL) {
365 			buf = get_buffer(buf_offset, -1);
366 			rel_buffer(buf);
367 		}
368 		++ri;
369 		raw_offset += HAMMER_BUFSIZE;
370 	}
371 }
372 
373 void
374 rel_buffer(struct buffer_info *buffer)
375 {
376 	struct volume_info *volume;
377 	int hi;
378 
379 	if (buffer == NULL)
380 		return;
381 	assert(buffer->cache.refs > 0);
382 	if (--buffer->cache.refs == 0) {
383 		if (buffer->cache.delete) {
384 			hi = buffer_hash(buffer->buf_offset);
385 			volume = buffer->volume;
386 			if (buffer->cache.modified)
387 				flush_buffer(buffer);
388 			TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
389 			hammer_cache_del(&buffer->cache);
390 			free(buffer->ondisk);
391 			free(buffer);
392 			rel_volume(volume);
393 		}
394 	}
395 }
396 
397 /*
398  * Retrieve a pointer to a buffer data given a buffer offset.  The underlying
399  * bufferp is freed if isnew or the offset is out of range of the cached data.
400  * If bufferp is freed a referenced buffer is loaded into it.
401  */
402 void *
403 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
404 		int isnew)
405 {
406 	if (*bufferp != NULL) {
407 		if (isnew > 0 ||
408 		    (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
409 			rel_buffer(*bufferp);
410 			*bufferp = NULL;
411 		}
412 	}
413 	return(get_ondisk(buf_offset, bufferp, isnew));
414 }
415 
416 /*
417  * Retrieve a pointer to a B-Tree node given a zone offset.  The underlying
418  * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
419  */
420 hammer_node_ondisk_t
421 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
422 {
423 	if (*bufferp != NULL) {
424 		rel_buffer(*bufferp);
425 		*bufferp = NULL;
426 	}
427 	return(get_ondisk(node_offset, bufferp, 0));
428 }
429 
430 /*
431  * Return a pointer to a buffer data given a buffer offset.
432  * If *bufferp is NULL acquire the buffer otherwise use that buffer.
433  */
434 static void *
435 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew)
436 {
437 	struct buffer_info *buffer;
438 
439 	buffer = *bufferp;
440 	if (buffer == NULL) {
441 		buffer = *bufferp = get_buffer(buf_offset, isnew);
442 		if (buffer == NULL)
443 			return(NULL);
444 	}
445 
446 	return((char *)buffer->ondisk +
447 		((int32_t)buf_offset & HAMMER_BUFMASK));
448 }
449 
450 /*
451  * Allocate HAMMER elements - B-Tree nodes
452  */
453 void *
454 alloc_btree_element(hammer_off_t *offp, struct buffer_info **data_bufferp)
455 {
456 	hammer_node_ondisk_t node;
457 
458 	node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
459 			      offp, data_bufferp);
460 	bzero(node, sizeof(*node));
461 	return (node);
462 }
463 
464 /*
465  * Allocate HAMMER elements - meta data (inode, direntry, PFS, etc)
466  */
467 void *
468 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
469 		   struct buffer_info **data_bufferp)
470 {
471 	void *data;
472 
473 	data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
474 			      offp, data_bufferp);
475 	bzero(data, data_len);
476 	return (data);
477 }
478 
479 /*
480  * Allocate HAMMER elements - data storage
481  *
482  * The only data_len supported by HAMMER userspace for large data zone
483  * (zone 10) is HAMMER_BUFSIZE which is 16KB.  >16KB data does not fit
484  * in a buffer allocated by get_buffer().  Also alloc_blockmap() does
485  * not consider >16KB buffer size.
486  */
487 void *
488 alloc_data_element(hammer_off_t *offp, int32_t data_len,
489 		   struct buffer_info **data_bufferp)
490 {
491 	void *data;
492 	int zone;
493 
494 	if (data_len == 0)
495 		return(NULL);
496 
497 	zone = hammer_data_zone_index(data_len);
498 	assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
499 	assert(zone == HAMMER_ZONE_LARGE_DATA_INDEX ||
500 	       zone == HAMMER_ZONE_SMALL_DATA_INDEX);
501 
502 	data = alloc_blockmap(zone, data_len, offp, data_bufferp);
503 	bzero(data, data_len);
504 	return(data);
505 }
506 
507 /*
508  * Format a new blockmap.  This is mostly a degenerate case because
509  * all allocations are now actually done from the freemap.
510  */
511 void
512 format_blockmap(struct volume_info *root_vol, int zone, hammer_off_t offset)
513 {
514 	hammer_blockmap_t blockmap;
515 	hammer_off_t zone_base;
516 
517 	/* Only root volume needs formatting */
518 	assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
519 
520 	assert(hammer_is_zone2_mapped_index(zone));
521 
522 	blockmap = &root_vol->ondisk->vol0_blockmap[zone];
523 	zone_base = HAMMER_ZONE_ENCODE(zone, offset);
524 
525 	bzero(blockmap, sizeof(*blockmap));
526 	blockmap->phys_offset = 0;
527 	blockmap->first_offset = zone_base;
528 	blockmap->next_offset = zone_base;
529 	blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
530 	blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
531 }
532 
533 /*
534  * Format a new freemap.  Set all layer1 entries to UNAVAIL.  The initialize
535  * code will load each volume's freemap.
536  */
537 void
538 format_freemap(struct volume_info *root_vol)
539 {
540 	struct buffer_info *buffer = NULL;
541 	hammer_off_t layer1_offset;
542 	hammer_blockmap_t blockmap;
543 	struct hammer_blockmap_layer1 *layer1;
544 	int i, isnew;
545 
546 	/* Only root volume needs formatting */
547 	assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
548 
549 	layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
550 	for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
551 		isnew = ((i % HAMMER_BUFSIZE) == 0);
552 		layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
553 		bzero(layer1, sizeof(*layer1));
554 		layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
555 		layer1->blocks_free = 0;
556 		layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
557 	}
558 	assert(i == HAMMER_BIGBLOCK_SIZE);
559 	rel_buffer(buffer);
560 
561 	blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
562 	bzero(blockmap, sizeof(*blockmap));
563 	blockmap->phys_offset = layer1_offset;
564 	blockmap->first_offset = 0;
565 	blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
566 	blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
567 	blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
568 }
569 
570 /*
571  * Load the volume's remaining free space into the freemap.
572  *
573  * Returns the number of big-blocks available.
574  */
575 int64_t
576 initialize_freemap(struct volume_info *vol)
577 {
578 	struct volume_info *root_vol;
579 	struct buffer_info *buffer1 = NULL;
580 	struct buffer_info *buffer2 = NULL;
581 	struct hammer_blockmap_layer1 *layer1;
582 	struct hammer_blockmap_layer2 *layer2;
583 	hammer_off_t layer1_offset;
584 	hammer_off_t layer2_offset;
585 	hammer_off_t phys_offset;
586 	hammer_off_t block_offset;
587 	hammer_off_t aligned_vol_free_end;
588 	hammer_blockmap_t freemap;
589 	int64_t count = 0;
590 	int64_t layer1_count = 0;
591 
592 	root_vol = get_root_volume();
593 	aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
594 				& ~HAMMER_BLOCKMAP_LAYER2_MASK;
595 
596 	printf("initialize freemap volume %d\n", vol->vol_no);
597 
598 	/*
599 	 * Initialize the freemap.  First preallocate the big-blocks required
600 	 * to implement layer2.   This preallocation is a bootstrap allocation
601 	 * using blocks from the target volume.
602 	 */
603 	freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
604 
605 	for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
606 	     phys_offset < aligned_vol_free_end;
607 	     phys_offset += HAMMER_BLOCKMAP_LAYER2) {
608 		layer1_offset = freemap->phys_offset +
609 				HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
610 		layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
611 		if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
612 			layer1->phys_offset = alloc_bigblock(vol,
613 						HAMMER_ZONE_FREEMAP_INDEX);
614 			layer1->blocks_free = 0;
615 			buffer1->cache.modified = 1;
616 			layer1->layer1_crc = crc32(layer1,
617 						   HAMMER_LAYER1_CRCSIZE);
618 		}
619 	}
620 
621 	/*
622 	 * Now fill everything in.
623 	 */
624 	for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->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);
632 
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));
640 
641 			if (phys_offset + block_offset < vol->vol_free_off) {
642 				/*
643 				 * Fixups XXX - big-blocks already allocated as part
644 				 * of the freemap bootstrap.
645 				 */
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 < vol->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;
659 			}
660 			layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
661 			buffer2->cache.modified = 1;
662 		}
663 
664 		layer1->blocks_free += layer1_count;
665 		layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
666 		buffer1->cache.modified = 1;
667 	}
668 
669 	rel_buffer(buffer1);
670 	rel_buffer(buffer2);
671 	rel_volume(root_vol);
672 	return(count);
673 }
674 
675 /*
676  * Returns the number of big-blocks available for filesystem data and undos
677  * without formatting.
678  */
679 int64_t
680 count_freemap(struct volume_info *vol)
681 {
682 	hammer_off_t phys_offset;
683 	hammer_off_t vol_free_off;
684 	hammer_off_t aligned_vol_free_end;
685 	int64_t count = 0;
686 
687 	vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
688 	aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
689 				& ~HAMMER_BLOCKMAP_LAYER2_MASK;
690 
691 	if (vol->vol_no == HAMMER_ROOT_VOLNO)
692 		vol_free_off += HAMMER_BIGBLOCK_SIZE;
693 
694 	for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
695 	     phys_offset < aligned_vol_free_end;
696 	     phys_offset += HAMMER_BLOCKMAP_LAYER2) {
697 		vol_free_off += HAMMER_BIGBLOCK_SIZE;
698 	}
699 
700 	for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
701 	     phys_offset < aligned_vol_free_end;
702 	     phys_offset += HAMMER_BIGBLOCK_SIZE) {
703 		if (phys_offset < vol_free_off) {
704 			;
705 		} else if (phys_offset < vol->vol_free_end) {
706 			++count;
707 		}
708 	}
709 
710 	return(count);
711 }
712 
713 /*
714  * Format the undomap for the root volume.
715  */
716 void
717 format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size)
718 {
719 	const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
720 	hammer_off_t undo_limit;
721 	hammer_blockmap_t blockmap;
722 	struct hammer_volume_ondisk *ondisk;
723 	struct buffer_info *buffer = NULL;
724 	hammer_off_t scan;
725 	int n;
726 	int limit_index;
727 	uint32_t seqno;
728 
729 	/* Only root volume needs formatting */
730 	assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
731 	ondisk = root_vol->ondisk;
732 
733 	/*
734 	 * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
735 	 * up to HAMMER_UNDO_LAYER2 big-blocks.  Size to approximately
736 	 * 0.1% of the disk.
737 	 *
738 	 * The minimum UNDO fifo size is 500MB, or approximately 1% of
739 	 * the recommended 50G disk.
740 	 *
741 	 * Changing this minimum is rather dangerous as complex filesystem
742 	 * operations can cause the UNDO FIFO to fill up otherwise.
743 	 */
744 	undo_limit = *undo_buffer_size;
745 	if (undo_limit == 0) {
746 		undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
747 		if (undo_limit < 500*1024*1024)
748 			undo_limit = 500*1024*1024;
749 	}
750 	undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
751 		     ~HAMMER_BIGBLOCK_MASK64;
752 	if (undo_limit < HAMMER_BIGBLOCK_SIZE)
753 		undo_limit = HAMMER_BIGBLOCK_SIZE;
754 	if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
755 		undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
756 	*undo_buffer_size = undo_limit;
757 
758 	blockmap = &ondisk->vol0_blockmap[undo_zone];
759 	bzero(blockmap, sizeof(*blockmap));
760 	blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
761 	blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
762 	blockmap->next_offset = blockmap->first_offset;
763 	blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
764 	blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
765 
766 	limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
767 	assert(limit_index <= HAMMER_UNDO_LAYER2);
768 
769 	for (n = 0; n < limit_index; ++n) {
770 		ondisk->vol0_undo_array[n] = alloc_bigblock(root_vol,
771 							HAMMER_ZONE_UNDO_INDEX);
772 	}
773 	while (n < HAMMER_UNDO_LAYER2) {
774 		ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
775 	}
776 
777 	/*
778 	 * Pre-initialize the UNDO blocks (HAMMER version 4+)
779 	 */
780 	printf("initializing the undo map (%jd MB)\n",
781 		(intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
782 		(1024 * 1024));
783 
784 	scan = blockmap->first_offset;
785 	seqno = 0;
786 
787 	while (scan < blockmap->alloc_offset) {
788 		hammer_fifo_head_t head;
789 		hammer_fifo_tail_t tail;
790 		int isnew;
791 		int bytes = HAMMER_UNDO_ALIGN;
792 
793 		isnew = ((scan & HAMMER_BUFMASK64) == 0);
794 		head = get_buffer_data(scan, &buffer, isnew);
795 		buffer->cache.modified = 1;
796 		tail = (void *)((char *)head + bytes - sizeof(*tail));
797 
798 		bzero(head, bytes);
799 		head->hdr_signature = HAMMER_HEAD_SIGNATURE;
800 		head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
801 		head->hdr_size = bytes;
802 		head->hdr_seq = seqno++;
803 
804 		tail->tail_signature = HAMMER_TAIL_SIGNATURE;
805 		tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
806 		tail->tail_size = bytes;
807 
808 		head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
809 				crc32(head + 1, bytes - sizeof(*head));
810 
811 		scan += bytes;
812 	}
813 	rel_buffer(buffer);
814 }
815 
816 /*
817  * Flush various tracking structures to disk
818  */
819 void
820 flush_all_volumes(void)
821 {
822 	struct volume_info *vol;
823 
824 	TAILQ_FOREACH(vol, &VolList, entry)
825 		flush_volume(vol);
826 }
827 
828 void
829 flush_volume(struct volume_info *volume)
830 {
831 	struct buffer_info *buffer;
832 	int i;
833 
834 	for (i = 0; i < HAMMER_BUFLISTS; ++i) {
835 		TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
836 			flush_buffer(buffer);
837 	}
838 	if (writehammervol(volume) == -1)
839 		err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
840 }
841 
842 void
843 flush_buffer(struct buffer_info *buffer)
844 {
845 	struct volume_info *vol;
846 
847 	vol = buffer->volume;
848 	if (writehammerbuf(buffer) == -1)
849 		err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
850 	buffer->cache.modified = 0;
851 }
852 
853 /*
854  * Core I/O operations
855  */
856 static int
857 __read(struct volume_info *vol, void *data, int64_t offset, int size)
858 {
859 	ssize_t n;
860 
861 	n = pread(vol->fd, data, size, offset);
862 	if (n != size)
863 		return(-1);
864 	return(0);
865 }
866 
867 static __inline int
868 readhammervol(struct volume_info *vol)
869 {
870 	return(__read(vol, vol->ondisk, 0, HAMMER_BUFSIZE));
871 }
872 
873 static __inline int
874 readhammerbuf(struct buffer_info *buf)
875 {
876 	return(__read(buf->volume, buf->ondisk, buf->raw_offset, HAMMER_BUFSIZE));
877 }
878 
879 static int
880 __write(struct volume_info *vol, const void *data, int64_t offset, int size)
881 {
882 	ssize_t n;
883 
884 	if (vol->rdonly)
885 		return(0);
886 
887 	n = pwrite(vol->fd, data, size, offset);
888 	if (n != size)
889 		return(-1);
890 	return(0);
891 }
892 
893 static __inline int
894 writehammervol(struct volume_info *vol)
895 {
896 	return(__write(vol, vol->ondisk, 0, HAMMER_BUFSIZE));
897 }
898 
899 static __inline int
900 writehammerbuf(struct buffer_info *buf)
901 {
902 	return(__write(buf->volume, buf->ondisk, buf->raw_offset, HAMMER_BUFSIZE));
903 }
904 
905 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
906 {
907 	if (value == 0) {
908 		value = HAMMER_BOOT_NOMBYTES;
909 		while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
910 			value >>= 1;
911 		if (value < HAMMER_BOOT_MINBYTES)
912 			value = 0;
913 	} else if (value < HAMMER_BOOT_MINBYTES) {
914 		value = HAMMER_BOOT_MINBYTES;
915 	}
916 
917 	return(value);
918 }
919 
920 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size)
921 {
922 	if (value == 0) {
923 		value = HAMMER_MEM_NOMBYTES;
924 		while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
925 			value >>= 1;
926 		if (value < HAMMER_MEM_MINBYTES)
927 			value = 0;
928 	} else if (value < HAMMER_MEM_MINBYTES) {
929 		value = HAMMER_MEM_MINBYTES;
930 	}
931 
932 	return(value);
933 }
934