sys/vfs/hammer: Add HAMMER_VOL_BUF_SIZE()
[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 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 /*
265  * Acquire the specified buffer.  isnew is -1 only when called
266  * via get_buffer_readahead() to prevent another readahead.
267  */
268 struct buffer_info *
269 get_buffer(hammer_off_t buf_offset, int isnew)
270 {
271         struct buffer_info *buf;
272         struct volume_info *volume;
273         int vol_no;
274         int zone;
275         int hi;
276         int dora = 0;
277
278         zone = HAMMER_ZONE_DECODE(buf_offset);
279         if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
280                 buf_offset = blockmap_lookup(buf_offset, NULL, NULL, NULL);
281         }
282         if (buf_offset == HAMMER_OFF_BAD)
283                 return(NULL);
284         assert(hammer_is_zone_raw_buffer(buf_offset));
285
286         vol_no = HAMMER_VOL_DECODE(buf_offset);
287         volume = get_volume(vol_no);
288
289         buf_offset &= ~HAMMER_BUFMASK64;
290         buf = find_buffer(volume, buf_offset);
291
292         if (buf == NULL) {
293                 buf = malloc(sizeof(*buf));
294                 bzero(buf, sizeof(*buf));
295                 buf->buf_offset = buf_offset;
296                 buf->raw_offset = hammer_xlate_to_phys(volume->ondisk,
297                                                         buf_offset);
298                 buf->volume = volume;
299                 buf->ondisk = malloc(HAMMER_BUFSIZE);
300                 if (isnew <= 0) {
301                         if (readhammerbuf(buf) == -1) {
302                                 err(1, "get_buffer: %s:%016jx "
303                                     "Read failed at offset %016jx",
304                                     volume->name,
305                                     (intmax_t)buf->buf_offset,
306                                     (intmax_t)buf->raw_offset);
307                         }
308                 }
309
310                 hi = buffer_hash(buf_offset);
311                 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
312                 buf->cache.buffer = buf;
313                 hammer_cache_add(&buf->cache);
314                 dora = (isnew == 0);
315         } else {
316                 assert(buf->ondisk != NULL);
317                 assert(isnew != -1);
318                 hammer_cache_used(&buf->cache);
319         }
320
321         ++buf->cache.refs;
322         hammer_cache_flush();
323
324         if (isnew > 0) {
325                 assert(buf->cache.modified == 0);
326                 bzero(buf->ondisk, HAMMER_BUFSIZE);
327                 buf->cache.modified = 1;
328         }
329         if (dora)
330                 get_buffer_readahead(buf);
331         return(buf);
332 }
333
334 static void
335 get_buffer_readahead(struct buffer_info *base)
336 {
337         struct buffer_info *buf;
338         struct volume_info *vol;
339         hammer_off_t buf_offset;
340         int64_t raw_offset;
341         int ri = UseReadBehind;
342         int re = UseReadAhead;
343
344         raw_offset = base->raw_offset + ri * HAMMER_BUFSIZE;
345         vol = base->volume;
346
347         while (ri < re) {
348                 if (raw_offset >= vol->ondisk->vol_buf_end)
349                         break;
350                 if (raw_offset < vol->ondisk->vol_buf_beg || ri == 0) {
351                         ++ri;
352                         raw_offset += HAMMER_BUFSIZE;
353                         continue;
354                 }
355                 buf_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no,
356                         raw_offset - vol->ondisk->vol_buf_beg);
357                 buf = find_buffer(vol, buf_offset);
358                 if (buf == NULL) {
359                         buf = get_buffer(buf_offset, -1);
360                         rel_buffer(buf);
361                 }
362                 ++ri;
363                 raw_offset += HAMMER_BUFSIZE;
364         }
365 }
366
367 void
368 rel_buffer(struct buffer_info *buffer)
369 {
370         struct volume_info *volume;
371         int hi;
372
373         if (buffer == NULL)
374                 return;
375         assert(buffer->cache.refs > 0);
376         if (--buffer->cache.refs == 0) {
377                 if (buffer->cache.delete) {
378                         hi = buffer_hash(buffer->buf_offset);
379                         volume = buffer->volume;
380                         if (buffer->cache.modified)
381                                 flush_buffer(buffer);
382                         TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
383                         hammer_cache_del(&buffer->cache);
384                         free(buffer->ondisk);
385                         free(buffer);
386                 }
387         }
388 }
389
390 /*
391  * Retrieve a pointer to a buffer data given a buffer offset.  The underlying
392  * bufferp is freed if isnew or the offset is out of range of the cached data.
393  * If bufferp is freed a referenced buffer is loaded into it.
394  */
395 void *
396 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
397                 int isnew)
398 {
399         if (*bufferp != NULL) {
400                 if (isnew > 0 ||
401                     (((*bufferp)->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
402                         rel_buffer(*bufferp);
403                         *bufferp = NULL;
404                 }
405         }
406         return(get_ondisk(buf_offset, bufferp, isnew));
407 }
408
409 /*
410  * Retrieve a pointer to a B-Tree node given a zone offset.  The underlying
411  * bufferp is freed if non-NULL and a referenced buffer is loaded into it.
412  */
413 hammer_node_ondisk_t
414 get_node(hammer_off_t node_offset, struct buffer_info **bufferp)
415 {
416         if (*bufferp != NULL) {
417                 rel_buffer(*bufferp);
418                 *bufferp = NULL;
419         }
420         return(get_ondisk(node_offset, bufferp, 0));
421 }
422
423 /*
424  * Return a pointer to a buffer data given a buffer offset.
425  * If *bufferp is NULL acquire the buffer otherwise use that buffer.
426  */
427 static void *
428 get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew)
429 {
430         struct buffer_info *buffer;
431
432         buffer = *bufferp;
433         if (buffer == NULL) {
434                 buffer = *bufferp = get_buffer(buf_offset, isnew);
435                 if (buffer == NULL)
436                         return(NULL);
437         }
438
439         return((char *)buffer->ondisk +
440                 ((int32_t)buf_offset & HAMMER_BUFMASK));
441 }
442
443 /*
444  * Allocate HAMMER elements - B-Tree nodes
445  */
446 void *
447 alloc_btree_element(hammer_off_t *offp, struct buffer_info **data_bufferp)
448 {
449         hammer_node_ondisk_t node;
450
451         node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
452                               offp, data_bufferp);
453         bzero(node, sizeof(*node));
454         return (node);
455 }
456
457 /*
458  * Allocate HAMMER elements - meta data (inode, direntry, PFS, etc)
459  */
460 void *
461 alloc_meta_element(hammer_off_t *offp, int32_t data_len,
462                    struct buffer_info **data_bufferp)
463 {
464         void *data;
465
466         data = alloc_blockmap(HAMMER_ZONE_META_INDEX, data_len,
467                               offp, data_bufferp);
468         bzero(data, data_len);
469         return (data);
470 }
471
472 /*
473  * Allocate HAMMER elements - data storage
474  *
475  * The only data_len supported by HAMMER userspace for large data zone
476  * (zone 10) is HAMMER_BUFSIZE which is 16KB.  >16KB data does not fit
477  * in a buffer allocated by get_buffer().  Also alloc_blockmap() does
478  * not consider >16KB buffer size.
479  */
480 void *
481 alloc_data_element(hammer_off_t *offp, int32_t data_len,
482                    struct buffer_info **data_bufferp)
483 {
484         void *data;
485         int zone;
486
487         if (data_len == 0)
488                 return(NULL);
489
490         zone = hammer_data_zone_index(data_len);
491         assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
492         assert(zone == HAMMER_ZONE_LARGE_DATA_INDEX ||
493                zone == HAMMER_ZONE_SMALL_DATA_INDEX);
494
495         data = alloc_blockmap(zone, data_len, offp, data_bufferp);
496         bzero(data, data_len);
497         return(data);
498 }
499
500 /*
501  * Format a new blockmap.  This is mostly a degenerate case because
502  * all allocations are now actually done from the freemap.
503  */
504 void
505 format_blockmap(struct volume_info *root_vol, int zone, hammer_off_t offset)
506 {
507         hammer_blockmap_t blockmap;
508         hammer_off_t zone_base;
509
510         /* Only root volume needs formatting */
511         assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
512
513         assert(hammer_is_zone2_mapped_index(zone));
514
515         blockmap = &root_vol->ondisk->vol0_blockmap[zone];
516         zone_base = HAMMER_ZONE_ENCODE(zone, offset);
517
518         bzero(blockmap, sizeof(*blockmap));
519         blockmap->phys_offset = 0;
520         blockmap->first_offset = zone_base;
521         blockmap->next_offset = zone_base;
522         blockmap->alloc_offset = HAMMER_ENCODE(zone, 255, -1);
523         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
524 }
525
526 /*
527  * Format a new freemap.  Set all layer1 entries to UNAVAIL.  The initialize
528  * code will load each volume's freemap.
529  */
530 void
531 format_freemap(struct volume_info *root_vol)
532 {
533         struct buffer_info *buffer = NULL;
534         hammer_off_t layer1_offset;
535         hammer_blockmap_t blockmap;
536         struct hammer_blockmap_layer1 *layer1;
537         int i, isnew;
538
539         /* Only root volume needs formatting */
540         assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
541
542         layer1_offset = alloc_bigblock(root_vol, HAMMER_ZONE_FREEMAP_INDEX);
543         for (i = 0; i < HAMMER_BIGBLOCK_SIZE; i += sizeof(*layer1)) {
544                 isnew = ((i % HAMMER_BUFSIZE) == 0);
545                 layer1 = get_buffer_data(layer1_offset + i, &buffer, isnew);
546                 bzero(layer1, sizeof(*layer1));
547                 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
548                 layer1->blocks_free = 0;
549                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
550         }
551         assert(i == HAMMER_BIGBLOCK_SIZE);
552         rel_buffer(buffer);
553
554         blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
555         bzero(blockmap, sizeof(*blockmap));
556         blockmap->phys_offset = layer1_offset;
557         blockmap->first_offset = 0;
558         blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
559         blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
560         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
561 }
562
563 /*
564  * Load the volume's remaining free space into the freemap.
565  *
566  * Returns the number of big-blocks available.
567  */
568 int64_t
569 initialize_freemap(struct volume_info *vol)
570 {
571         struct volume_info *root_vol;
572         struct buffer_info *buffer1 = NULL;
573         struct buffer_info *buffer2 = NULL;
574         struct hammer_blockmap_layer1 *layer1;
575         struct hammer_blockmap_layer2 *layer2;
576         hammer_off_t layer1_offset;
577         hammer_off_t layer2_offset;
578         hammer_off_t phys_offset;
579         hammer_off_t block_offset;
580         hammer_off_t aligned_vol_free_end;
581         hammer_blockmap_t freemap;
582         int64_t count = 0;
583         int64_t layer1_count = 0;
584
585         root_vol = get_root_volume();
586         aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
587                                 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
588
589         printf("initialize freemap volume %d\n", vol->vol_no);
590
591         /*
592          * Initialize the freemap.  First preallocate the big-blocks required
593          * to implement layer2.   This preallocation is a bootstrap allocation
594          * using blocks from the target volume.
595          */
596         freemap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
597
598         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
599              phys_offset < aligned_vol_free_end;
600              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
601                 layer1_offset = freemap->phys_offset +
602                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
603                 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
604                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
605                         layer1->phys_offset = alloc_bigblock(vol,
606                                                 HAMMER_ZONE_FREEMAP_INDEX);
607                         layer1->blocks_free = 0;
608                         buffer1->cache.modified = 1;
609                         layer1->layer1_crc = crc32(layer1,
610                                                    HAMMER_LAYER1_CRCSIZE);
611                 }
612         }
613
614         /*
615          * Now fill everything in.
616          */
617         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
618              phys_offset < aligned_vol_free_end;
619              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
620                 layer1_count = 0;
621                 layer1_offset = freemap->phys_offset +
622                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
623                 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
624                 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
625
626                 for (block_offset = 0;
627                      block_offset < HAMMER_BLOCKMAP_LAYER2;
628                      block_offset += HAMMER_BIGBLOCK_SIZE) {
629                         layer2_offset = layer1->phys_offset +
630                                         HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
631                         layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
632                         bzero(layer2, sizeof(*layer2));
633
634                         if (phys_offset + block_offset < vol->vol_free_off) {
635                                 /*
636                                  * Fixups XXX - big-blocks already allocated as part
637                                  * of the freemap bootstrap.
638                                  */
639                                 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
640                                 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
641                                 layer2->bytes_free = 0;
642                         } else if (phys_offset + block_offset < vol->vol_free_end) {
643                                 layer2->zone = 0;
644                                 layer2->append_off = 0;
645                                 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
646                                 ++count;
647                                 ++layer1_count;
648                         } else {
649                                 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
650                                 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
651                                 layer2->bytes_free = 0;
652                         }
653                         layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
654                         buffer2->cache.modified = 1;
655                 }
656
657                 layer1->blocks_free += layer1_count;
658                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
659                 buffer1->cache.modified = 1;
660         }
661
662         rel_buffer(buffer1);
663         rel_buffer(buffer2);
664         return(count);
665 }
666
667 /*
668  * Returns the number of big-blocks available for filesystem data and undos
669  * without formatting.
670  */
671 int64_t
672 count_freemap(struct volume_info *vol)
673 {
674         hammer_off_t phys_offset;
675         hammer_off_t vol_free_off;
676         hammer_off_t aligned_vol_free_end;
677         int64_t count = 0;
678
679         vol_free_off = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
680         aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
681                                 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
682
683         if (vol->vol_no == HAMMER_ROOT_VOLNO)
684                 vol_free_off += HAMMER_BIGBLOCK_SIZE;
685
686         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
687              phys_offset < aligned_vol_free_end;
688              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
689                 vol_free_off += HAMMER_BIGBLOCK_SIZE;
690         }
691
692         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
693              phys_offset < aligned_vol_free_end;
694              phys_offset += HAMMER_BIGBLOCK_SIZE) {
695                 if (phys_offset < vol_free_off) {
696                         ;
697                 } else if (phys_offset < vol->vol_free_end) {
698                         ++count;
699                 }
700         }
701
702         return(count);
703 }
704
705 /*
706  * Format the undomap for the root volume.
707  */
708 void
709 format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size)
710 {
711         const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
712         hammer_off_t undo_limit;
713         hammer_blockmap_t blockmap;
714         struct hammer_volume_ondisk *ondisk;
715         struct buffer_info *buffer = NULL;
716         hammer_off_t scan;
717         int n;
718         int limit_index;
719         uint32_t seqno;
720
721         /* Only root volume needs formatting */
722         assert(root_vol->vol_no == HAMMER_ROOT_VOLNO);
723         ondisk = root_vol->ondisk;
724
725         /*
726          * Size the undo buffer in multiples of HAMMER_BIGBLOCK_SIZE,
727          * up to HAMMER_UNDO_LAYER2 big-blocks.  Size to approximately
728          * 0.1% of the disk.
729          *
730          * The minimum UNDO fifo size is 500MB, or approximately 1% of
731          * the recommended 50G disk.
732          *
733          * Changing this minimum is rather dangerous as complex filesystem
734          * operations can cause the UNDO FIFO to fill up otherwise.
735          */
736         undo_limit = *undo_buffer_size;
737         if (undo_limit == 0) {
738                 undo_limit = HAMMER_VOL_BUF_SIZE(ondisk) / 1000;
739                 if (undo_limit < 500*1024*1024)
740                         undo_limit = 500*1024*1024;
741         }
742         undo_limit = (undo_limit + HAMMER_BIGBLOCK_MASK64) &
743                      ~HAMMER_BIGBLOCK_MASK64;
744         if (undo_limit < HAMMER_BIGBLOCK_SIZE)
745                 undo_limit = HAMMER_BIGBLOCK_SIZE;
746         if (undo_limit > HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2)
747                 undo_limit = HAMMER_BIGBLOCK_SIZE * HAMMER_UNDO_LAYER2;
748         *undo_buffer_size = undo_limit;
749
750         blockmap = &ondisk->vol0_blockmap[undo_zone];
751         bzero(blockmap, sizeof(*blockmap));
752         blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
753         blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
754         blockmap->next_offset = blockmap->first_offset;
755         blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
756         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
757
758         limit_index = undo_limit / HAMMER_BIGBLOCK_SIZE;
759         assert(limit_index <= HAMMER_UNDO_LAYER2);
760
761         for (n = 0; n < limit_index; ++n) {
762                 ondisk->vol0_undo_array[n] = alloc_bigblock(root_vol,
763                                                         HAMMER_ZONE_UNDO_INDEX);
764         }
765         while (n < HAMMER_UNDO_LAYER2) {
766                 ondisk->vol0_undo_array[n++] = HAMMER_BLOCKMAP_UNAVAIL;
767         }
768
769         /*
770          * Pre-initialize the UNDO blocks (HAMMER version 4+)
771          */
772         printf("initializing the undo map (%jd MB)\n",
773                 (intmax_t)(blockmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
774                 (1024 * 1024));
775
776         scan = blockmap->first_offset;
777         seqno = 0;
778
779         while (scan < blockmap->alloc_offset) {
780                 hammer_fifo_head_t head;
781                 hammer_fifo_tail_t tail;
782                 int isnew;
783                 int bytes = HAMMER_UNDO_ALIGN;
784
785                 isnew = ((scan & HAMMER_BUFMASK64) == 0);
786                 head = get_buffer_data(scan, &buffer, isnew);
787                 buffer->cache.modified = 1;
788                 tail = (void *)((char *)head + bytes - sizeof(*tail));
789
790                 bzero(head, bytes);
791                 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
792                 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
793                 head->hdr_size = bytes;
794                 head->hdr_seq = seqno++;
795
796                 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
797                 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
798                 tail->tail_size = bytes;
799
800                 head->hdr_crc = crc32(head, HAMMER_FIFO_HEAD_CRCOFF) ^
801                                 crc32(head + 1, bytes - sizeof(*head));
802
803                 scan += bytes;
804         }
805         rel_buffer(buffer);
806 }
807
808 /*
809  * Flush various tracking structures to disk
810  */
811 void
812 flush_all_volumes(void)
813 {
814         struct volume_info *vol;
815
816         TAILQ_FOREACH(vol, &VolList, entry)
817                 flush_volume(vol);
818 }
819
820 void
821 flush_volume(struct volume_info *volume)
822 {
823         struct buffer_info *buffer;
824         int i;
825
826         for (i = 0; i < HAMMER_BUFLISTS; ++i) {
827                 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
828                         flush_buffer(buffer);
829         }
830         if (writehammervol(volume) == -1)
831                 err(1, "Write volume %d (%s)", volume->vol_no, volume->name);
832 }
833
834 void
835 flush_buffer(struct buffer_info *buffer)
836 {
837         struct volume_info *vol;
838
839         vol = buffer->volume;
840         if (writehammerbuf(buffer) == -1)
841                 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
842         buffer->cache.modified = 0;
843 }
844
845 /*
846  * Core I/O operations
847  */
848 static int
849 __read(struct volume_info *vol, void *data, int64_t offset, int size)
850 {
851         ssize_t n;
852
853         n = pread(vol->fd, data, size, offset);
854         if (n != size)
855                 return(-1);
856         return(0);
857 }
858
859 static __inline int
860 readhammervol(struct volume_info *vol)
861 {
862         return(__read(vol, vol->ondisk, 0, HAMMER_BUFSIZE));
863 }
864
865 static __inline int
866 readhammerbuf(struct buffer_info *buf)
867 {
868         return(__read(buf->volume, buf->ondisk, buf->raw_offset, HAMMER_BUFSIZE));
869 }
870
871 static int
872 __write(struct volume_info *vol, const void *data, int64_t offset, int size)
873 {
874         ssize_t n;
875
876         if (vol->rdonly)
877                 return(0);
878
879         n = pwrite(vol->fd, data, size, offset);
880         if (n != size)
881                 return(-1);
882         return(0);
883 }
884
885 static __inline int
886 writehammervol(struct volume_info *vol)
887 {
888         return(__write(vol, vol->ondisk, 0, HAMMER_BUFSIZE));
889 }
890
891 static __inline int
892 writehammerbuf(struct buffer_info *buf)
893 {
894         return(__write(buf->volume, buf->ondisk, buf->raw_offset, HAMMER_BUFSIZE));
895 }
896
897 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size)
898 {
899         if (value == 0) {
900                 value = HAMMER_BOOT_NOMBYTES;
901                 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
902                         value >>= 1;
903                 if (value < HAMMER_BOOT_MINBYTES)
904                         value = 0;
905         } else if (value < HAMMER_BOOT_MINBYTES) {
906                 value = HAMMER_BOOT_MINBYTES;
907         }
908
909         return(value);
910 }
911
912 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size)
913 {
914         if (value == 0) {
915                 value = HAMMER_MEM_NOMBYTES;
916                 while (value > avg_vol_size / HAMMER_MAX_VOLUMES)
917                         value >>= 1;
918                 if (value < HAMMER_MEM_MINBYTES)
919                         value = 0;
920         } else if (value < HAMMER_MEM_MINBYTES) {
921                 value = HAMMER_MEM_MINBYTES;
922         }
923
924         return(value);
925 }