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