HAMMER 43/Many: Remove records from the media format, plus other stuff
[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  * $DragonFly: src/sbin/hammer/ondisk.c,v 1.19 2008/05/12 21:17:16 dillon Exp $
35  */
36
37 #include <sys/types.h>
38 #include <assert.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdarg.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <stddef.h>
45 #include <err.h>
46 #include <fcntl.h>
47 #include "hammer_util.h"
48
49 static void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
50                         struct buffer_info **bufferp);
51 static hammer_off_t alloc_bigblock(struct volume_info *volume,
52                         hammer_off_t owner);
53 #if 0
54 static void init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type);
55 static hammer_off_t hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
56                         struct buffer_info **bufp, u_int16_t hdr_type);
57 static void readhammerbuf(struct volume_info *vol, void *data,
58                         int64_t offset);
59 #endif
60 static void writehammerbuf(struct volume_info *vol, const void *data,
61                         int64_t offset);
62
63 int DebugOpt;
64
65 uuid_t Hammer_FSType;
66 uuid_t Hammer_FSId;
67 int64_t BootAreaSize;
68 int64_t MemAreaSize;
69 int64_t UndoBufferSize;
70 int     UsingSuperClusters;
71 int     NumVolumes;
72 int     RootVolNo = -1;
73 struct volume_list VolList = TAILQ_HEAD_INITIALIZER(VolList);
74
75 static __inline
76 int
77 buffer_hash(hammer_off_t buf_offset)
78 {
79         int hi;
80
81         hi = (int)(buf_offset / HAMMER_BUFSIZE) & HAMMER_BUFLISTMASK;
82         return(hi);
83 }
84
85 /*
86  * Lookup the requested information structure and related on-disk buffer.
87  * Missing structures are created.
88  */
89 struct volume_info *
90 setup_volume(int32_t vol_no, const char *filename, int isnew, int oflags)
91 {
92         struct volume_info *vol;
93         struct volume_info *scan;
94         struct hammer_volume_ondisk *ondisk;
95         int i, n;
96
97         /*
98          * Allocate the volume structure
99          */
100         vol = malloc(sizeof(*vol));
101         bzero(vol, sizeof(*vol));
102         for (i = 0; i < HAMMER_BUFLISTS; ++i)
103                 TAILQ_INIT(&vol->buffer_lists[i]);
104         vol->name = strdup(filename);
105         vol->fd = open(filename, oflags);
106         if (vol->fd < 0) {
107                 free(vol->name);
108                 free(vol);
109                 err(1, "setup_volume: %s: Open failed", filename);
110         }
111
112         /*
113          * Read or initialize the volume header
114          */
115         vol->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
116         if (isnew) {
117                 bzero(ondisk, HAMMER_BUFSIZE);
118         } else {
119                 n = pread(vol->fd, ondisk, HAMMER_BUFSIZE, 0);
120                 if (n != HAMMER_BUFSIZE) {
121                         err(1, "setup_volume: %s: Read failed at offset 0",
122                             filename);
123                 }
124                 vol_no = ondisk->vol_no;
125                 if (RootVolNo < 0) {
126                         RootVolNo = ondisk->vol_rootvol;
127                 } else if (RootVolNo != (int)ondisk->vol_rootvol) {
128                         errx(1, "setup_volume: %s: root volume disagreement: "
129                                 "%d vs %d",
130                                 vol->name, RootVolNo, ondisk->vol_rootvol);
131                 }
132
133                 if (bcmp(&Hammer_FSType, &ondisk->vol_fstype, sizeof(Hammer_FSType)) != 0) {
134                         errx(1, "setup_volume: %s: Header does not indicate "
135                                 "that this is a hammer volume", vol->name);
136                 }
137                 if (TAILQ_EMPTY(&VolList)) {
138                         Hammer_FSId = vol->ondisk->vol_fsid;
139                 } else if (bcmp(&Hammer_FSId, &ondisk->vol_fsid, sizeof(Hammer_FSId)) != 0) {
140                         errx(1, "setup_volume: %s: FSId does match other "
141                                 "volumes!", vol->name);
142                 }
143         }
144         vol->vol_no = vol_no;
145
146         if (isnew) {
147                 /*init_fifo_head(&ondisk->head, HAMMER_HEAD_TYPE_VOL);*/
148                 vol->cache.modified = 1;
149         }
150
151         /*
152          * Link the volume structure in
153          */
154         TAILQ_FOREACH(scan, &VolList, entry) {
155                 if (scan->vol_no == vol_no) {
156                         errx(1, "setup_volume %s: Duplicate volume number %d "
157                                 "against %s", filename, vol_no, scan->name);
158                 }
159         }
160         TAILQ_INSERT_TAIL(&VolList, vol, entry);
161         return(vol);
162 }
163
164 struct volume_info *
165 get_volume(int32_t vol_no)
166 {
167         struct volume_info *vol;
168
169         TAILQ_FOREACH(vol, &VolList, entry) {
170                 if (vol->vol_no == vol_no)
171                         break;
172         }
173         if (vol == NULL)
174                 errx(1, "get_volume: Volume %d does not exist!", vol_no);
175         ++vol->cache.refs;
176         /* not added to or removed from hammer cache */
177         return(vol);
178 }
179
180 void
181 rel_volume(struct volume_info *volume)
182 {
183         /* not added to or removed from hammer cache */
184         --volume->cache.refs;
185 }
186
187 /*
188  * Acquire the specified buffer.
189  */
190 struct buffer_info *
191 get_buffer(hammer_off_t buf_offset, int isnew)
192 {
193         void *ondisk;
194         struct buffer_info *buf;
195         struct volume_info *volume;
196         hammer_off_t orig_offset = buf_offset;
197         int vol_no;
198         int zone;
199         int hi, n;
200
201         zone = HAMMER_ZONE_DECODE(buf_offset);
202         if (zone > HAMMER_ZONE_RAW_BUFFER_INDEX) {
203                 buf_offset = blockmap_lookup(buf_offset, NULL, NULL);
204         }
205         assert((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
206         vol_no = HAMMER_VOL_DECODE(buf_offset);
207         volume = get_volume(vol_no);
208         buf_offset &= ~HAMMER_BUFMASK64;
209
210         hi = buffer_hash(buf_offset);
211
212         TAILQ_FOREACH(buf, &volume->buffer_lists[hi], entry) {
213                 if (buf->buf_offset == buf_offset)
214                         break;
215         }
216         if (buf == NULL) {
217                 buf = malloc(sizeof(*buf));
218                 bzero(buf, sizeof(*buf));
219                 if (DebugOpt) {
220                         fprintf(stderr, "get_buffer %016llx %016llx\n",
221                                 orig_offset, buf_offset);
222                 }
223                 buf->buf_offset = buf_offset;
224                 buf->buf_disk_offset = volume->ondisk->vol_buf_beg +
225                                         (buf_offset & HAMMER_OFF_SHORT_MASK);
226                 buf->volume = volume;
227                 TAILQ_INSERT_TAIL(&volume->buffer_lists[hi], buf, entry);
228                 ++volume->cache.refs;
229                 buf->cache.u.buffer = buf;
230                 hammer_cache_add(&buf->cache, ISBUFFER);
231         }
232         ++buf->cache.refs;
233         hammer_cache_flush();
234         if ((ondisk = buf->ondisk) == NULL) {
235                 buf->ondisk = ondisk = malloc(HAMMER_BUFSIZE);
236                 if (isnew == 0) {
237                         n = pread(volume->fd, ondisk, HAMMER_BUFSIZE,
238                                   buf->buf_disk_offset);
239                         if (n != HAMMER_BUFSIZE) {
240                                 err(1, "get_buffer: %s:%016llx Read failed at "
241                                        "offset %lld",
242                                     volume->name, buf->buf_offset,
243                                     buf->buf_disk_offset);
244                         }
245                 }
246         }
247         if (isnew) {
248                 bzero(ondisk, HAMMER_BUFSIZE);
249                 buf->cache.modified = 1;
250         }
251         return(buf);
252 }
253
254 void
255 rel_buffer(struct buffer_info *buffer)
256 {
257         struct volume_info *volume;
258         int hi;
259
260         assert(buffer->cache.refs > 0);
261         if (--buffer->cache.refs == 0) {
262                 if (buffer->cache.delete) {
263                         hi = buffer_hash(buffer->buf_offset);
264                         volume = buffer->volume;
265                         if (buffer->cache.modified)
266                                 flush_buffer(buffer);
267                         TAILQ_REMOVE(&volume->buffer_lists[hi], buffer, entry);
268                         hammer_cache_del(&buffer->cache);
269                         free(buffer->ondisk);
270                         free(buffer);
271                         rel_volume(volume);
272                 }
273         }
274 }
275
276 void *
277 get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
278                 int isnew)
279 {
280         struct buffer_info *buffer;
281
282         if ((buffer = *bufferp) != NULL) {
283                 if (isnew || 
284                     ((buffer->buf_offset ^ buf_offset) & ~HAMMER_BUFMASK64)) {
285                         rel_buffer(buffer);
286                         buffer = *bufferp = NULL;
287                 }
288         }
289         if (buffer == NULL)
290                 buffer = *bufferp = get_buffer(buf_offset, isnew);
291         return((char *)buffer->ondisk + ((int32_t)buf_offset & HAMMER_BUFMASK));
292 }
293
294 /*
295  * Retrieve a pointer to a B-Tree node given a cluster offset.  The underlying
296  * bufp is freed if non-NULL and a referenced buffer is loaded into it.
297  */
298 hammer_node_ondisk_t
299 get_node(hammer_off_t node_offset, struct buffer_info **bufp)
300 {
301         struct buffer_info *buf;
302
303         if (*bufp)
304                 rel_buffer(*bufp);
305         *bufp = buf = get_buffer(node_offset, 0);
306         return((void *)((char *)buf->ondisk +
307                         (int32_t)(node_offset & HAMMER_BUFMASK)));
308 }
309
310 /*
311  * Allocate HAMMER elements - btree nodes, data storage, and record elements
312  *
313  * NOTE: hammer_alloc_fifo() initializes the fifo header for the returned
314  * item and zero's out the remainder, so don't bzero() it.
315  */
316 void *
317 alloc_btree_element(hammer_off_t *offp)
318 {
319         struct buffer_info *buffer = NULL;
320         hammer_node_ondisk_t node;
321
322         node = alloc_blockmap(HAMMER_ZONE_BTREE_INDEX, sizeof(*node),
323                               offp, &buffer);
324         bzero(node, sizeof(*node));
325         /* XXX buffer not released, pointer remains valid */
326         return(node);
327 }
328
329 void *
330 alloc_data_element(hammer_off_t *offp, int32_t data_len,
331                    struct buffer_info **data_bufferp)
332 {
333         void *data;
334
335         if (data_len >= HAMMER_BUFSIZE) {
336                 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
337                 data = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
338                                       offp, data_bufferp);
339                 bzero(data, data_len);
340         } else if (data_len) {
341                 data = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
342                                       offp, data_bufferp);
343                 bzero(data, data_len);
344         } else {
345                 data = NULL;
346         }
347         return (data);
348 }
349
350 /*
351  * Format a new freemap.  Set all layer1 entries to UNAVAIL.  The initialize
352  * code will load each volume's freemap.
353  */
354 void
355 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
356 {
357         struct buffer_info *buffer = NULL;
358         hammer_off_t layer1_offset;
359         struct hammer_blockmap_layer1 *layer1;
360         int i, isnew;
361
362         layer1_offset = alloc_bigblock(root_vol, 0);
363         for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
364                 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
365                 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
366                                          &buffer, isnew);
367                 bzero(layer1, sizeof(*layer1));
368                 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
369                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
370         }
371         rel_buffer(buffer);
372
373         blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
374         blockmap->phys_offset = layer1_offset;
375         blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
376         blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
377         blockmap->reserved01 = 0;
378         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
379         root_vol->cache.modified = 1;
380 }
381
382 /*
383  * Load the volume's remaining free space into the freemap.  If this is
384  * the root volume, initialize the freemap owner for the layer1 bigblock.
385  *
386  * Returns the number of bigblocks available.
387  */
388 int64_t
389 initialize_freemap(struct volume_info *vol)
390 {
391         struct volume_info *root_vol;
392         struct buffer_info *buffer1 = NULL;
393         struct buffer_info *buffer2 = NULL;
394         struct hammer_blockmap_layer1 *layer1;
395         struct hammer_blockmap_layer2 *layer2;
396         hammer_off_t layer1_base;
397         hammer_off_t layer1_offset;
398         hammer_off_t layer2_offset;
399         hammer_off_t phys_offset;
400         hammer_off_t aligned_vol_free_end;
401         int64_t count = 0;
402         int modified1 = 0;
403
404         root_vol = get_volume(RootVolNo);
405         aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
406                                 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
407
408         printf("initialize freemap volume %d\n", vol->vol_no);
409
410         /*
411          * Initialize the freemap.  First preallocate the bigblocks required
412          * to implement layer2.   This preallocation is a bootstrap allocation
413          * using blocks from the target volume.
414          */
415         layer1_base = root_vol->ondisk->vol0_blockmap[
416                                         HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
417         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
418              phys_offset < aligned_vol_free_end;
419              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
420                 layer1_offset = layer1_base +
421                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
422                 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
423                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
424                         layer1->phys_offset = alloc_bigblock(vol, 0);
425                         layer1->blocks_free = 0;
426                         buffer1->cache.modified = 1;
427                         layer1->layer1_crc = crc32(layer1,
428                                                    HAMMER_LAYER1_CRCSIZE);
429                 }
430         }
431
432         /*
433          * Now fill everything in.
434          */
435         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
436              phys_offset < aligned_vol_free_end;
437              phys_offset += HAMMER_LARGEBLOCK_SIZE) {
438                 modified1 = 0;
439                 layer1_offset = layer1_base +
440                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
441                 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
442
443                 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
444                 layer2_offset = layer1->phys_offset +
445                                 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
446
447                 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
448                 if (phys_offset < vol->vol_free_off) {
449                         /*
450                          * Fixups XXX - bigblocks already allocated as part
451                          * of the freemap bootstrap.
452                          */
453                         layer2->u.owner = HAMMER_ENCODE_FREEMAP(0, 0); /* XXX */
454                 } else if (phys_offset < vol->vol_free_end) {
455                         ++layer1->blocks_free;
456                         buffer1->cache.modified = 1;
457                         layer2->u.owner = HAMMER_BLOCKMAP_FREE;
458                         ++count;
459                         modified1 = 1;
460                 } else {
461                         layer2->u.owner = HAMMER_BLOCKMAP_UNAVAIL;
462                 }
463                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
464                 buffer2->cache.modified = 1;
465
466                 /*
467                  * Finish-up layer 1
468                  */
469                 if (modified1) {
470                         layer1->layer1_crc = crc32(layer1,
471                                                    HAMMER_LAYER1_CRCSIZE);
472                         buffer1->cache.modified = 1;
473                 }
474         }
475         rel_buffer(buffer1);
476         rel_buffer(buffer2);
477         rel_volume(root_vol);
478         return(count);
479 }
480
481 /*
482  * Allocate big-blocks using our poor-man's volume->vol_free_off and
483  * update the freemap if owner != 0.
484  */
485 hammer_off_t
486 alloc_bigblock(struct volume_info *volume, hammer_off_t owner)
487 {
488         struct buffer_info *buffer = NULL;
489         struct volume_info *root_vol;
490         hammer_off_t result_offset;
491         hammer_off_t layer_offset;
492         struct hammer_blockmap_layer1 *layer1;
493         struct hammer_blockmap_layer2 *layer2;
494         int didget;
495
496         if (volume == NULL) {
497                 volume = get_volume(RootVolNo);
498                 didget = 1;
499         } else {
500                 didget = 0;
501         }
502         result_offset = volume->vol_free_off;
503         if (result_offset >= volume->vol_free_end)
504                 panic("alloc_bigblock: Ran out of room, filesystem too small");
505         volume->vol_free_off += HAMMER_LARGEBLOCK_SIZE;
506
507         /*
508          * Update the freemap
509          */
510         if (owner) {
511                 root_vol = get_volume(RootVolNo);
512                 layer_offset = root_vol->ondisk->vol0_blockmap[
513                                         HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
514                 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
515                 layer1 = get_buffer_data(layer_offset, &buffer, 0);
516                 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
517                 --layer1->blocks_free;
518                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
519                 buffer->cache.modified = 1;
520                 layer_offset = layer1->phys_offset +
521                                HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
522                 layer2 = get_buffer_data(layer_offset, &buffer, 0);
523                 assert(layer2->u.owner == HAMMER_BLOCKMAP_FREE);
524                 layer2->u.owner = owner;
525                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
526                 buffer->cache.modified = 1;
527
528                 rel_buffer(buffer);
529                 rel_volume(root_vol);
530         }
531
532         if (didget)
533                 rel_volume(volume);
534         return(result_offset);
535 }
536
537 /*
538  * Format the undo-map for the root volume.
539  */
540 void
541 format_undomap(hammer_volume_ondisk_t ondisk)
542 {
543         const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
544         hammer_off_t undo_limit;
545         hammer_blockmap_t blockmap;
546         hammer_off_t scan;
547         struct hammer_blockmap_layer2 *layer2;
548         int n;
549         int limit_index;
550
551         /*
552          * Size the undo buffer in multiples of HAMMER_LARGEBLOCK_SIZE,
553          * up to HAMMER_UNDO_LAYER2 large blocks.  Size to approximately
554          * 0.1% of the disk.
555          */
556         undo_limit = UndoBufferSize;
557         if (undo_limit == 0)
558                 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
559         undo_limit = (undo_limit + HAMMER_LARGEBLOCK_MASK64) &
560                      ~HAMMER_LARGEBLOCK_MASK64;
561         if (undo_limit < HAMMER_LARGEBLOCK_SIZE)
562                 undo_limit = HAMMER_LARGEBLOCK_SIZE;
563         if (undo_limit > HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2)
564                 undo_limit = HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2;
565         UndoBufferSize = undo_limit;
566
567         blockmap = &ondisk->vol0_blockmap[undo_zone];
568         bzero(blockmap, sizeof(*blockmap));
569         blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
570         blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
571         blockmap->next_offset = blockmap->first_offset;
572         blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
573         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
574
575         layer2 = &ondisk->vol0_undo_array[0];
576         n = 0;
577         scan = blockmap->next_offset;
578         limit_index = undo_limit / HAMMER_LARGEBLOCK_SIZE;
579
580         assert(limit_index <= HAMMER_UNDO_LAYER2);
581
582         for (n = 0; n < limit_index; ++n) {
583                 layer2->u.phys_offset = alloc_bigblock(NULL, scan);
584                 layer2->bytes_free = -1;        /* not used */
585                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
586
587                 scan += HAMMER_LARGEBLOCK_SIZE;
588                 ++layer2;
589         }
590         while (n < HAMMER_UNDO_LAYER2) {
591                 layer2->u.phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
592                 layer2->bytes_free = -1;
593                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
594                 ++layer2;
595                 ++n;
596         }
597 }
598
599 /*
600  * Format a new blockmap.  Set the owner to the base of the blockmap
601  * (meaning either the blockmap layer1 bigblock, layer2 bigblock, or
602  * target bigblock).
603  */
604 void
605 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_off)
606 {
607         blockmap->phys_offset = alloc_bigblock(NULL, zone_off);
608         blockmap->alloc_offset = zone_off;
609         blockmap->first_offset = zone_off;
610         blockmap->next_offset = zone_off;
611         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
612 }
613
614 static
615 void *
616 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
617                struct buffer_info **bufferp)
618 {
619         struct buffer_info *buffer1 = NULL;
620         struct buffer_info *buffer2 = NULL;
621         struct volume_info *volume;
622         hammer_blockmap_t rootmap;
623         struct hammer_blockmap_layer1 *layer1;
624         struct hammer_blockmap_layer2 *layer2;
625         hammer_off_t layer1_offset;
626         hammer_off_t layer2_offset;
627         hammer_off_t bigblock_offset;
628         void *ptr;
629
630         volume = get_volume(RootVolNo);
631
632         rootmap = &volume->ondisk->vol0_blockmap[zone];
633
634         /*
635          * Alignment and buffer-boundary issues
636          */
637         bytes = (bytes + 7) & ~7;
638         if ((rootmap->phys_offset ^ (rootmap->phys_offset + bytes - 1)) &
639             ~HAMMER_BUFMASK64) {
640                 volume->cache.modified = 1;
641                 rootmap->phys_offset = (rootmap->phys_offset + bytes) &
642                                        ~HAMMER_BUFMASK64;
643         }
644
645         /*
646          * Dive layer 1
647          */
648         layer1_offset = rootmap->phys_offset +
649                         HAMMER_BLOCKMAP_LAYER1_OFFSET(rootmap->alloc_offset);
650
651         layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
652         if ((rootmap->alloc_offset & HAMMER_BLOCKMAP_LAYER2_MASK) == 0) {
653                 buffer1->cache.modified = 1;
654                 bzero(layer1, sizeof(*layer1));
655                 layer1->blocks_free = HAMMER_BLOCKMAP_RADIX2;
656                 layer1->phys_offset = alloc_bigblock(NULL,
657                                                      rootmap->alloc_offset);
658         }
659
660         /*
661          * Dive layer 2
662          */
663         layer2_offset = layer1->phys_offset +
664                         HAMMER_BLOCKMAP_LAYER2_OFFSET(rootmap->alloc_offset);
665
666         layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
667
668         if ((rootmap->alloc_offset & HAMMER_LARGEBLOCK_MASK64) == 0) {
669                 buffer2->cache.modified = 1;
670                 bzero(layer2, sizeof(*layer2));
671                 layer2->u.phys_offset = alloc_bigblock(NULL,
672                                                        rootmap->alloc_offset);
673                 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
674                 --layer1->blocks_free;
675         }
676
677         buffer1->cache.modified = 1;
678         buffer2->cache.modified = 1;
679         volume->cache.modified = 1;
680         layer2->bytes_free -= bytes;
681         *result_offp = rootmap->alloc_offset;
682         rootmap->alloc_offset += bytes;
683         rootmap->next_offset = rootmap->alloc_offset;
684
685         bigblock_offset = layer2->u.phys_offset + 
686                           (*result_offp & HAMMER_LARGEBLOCK_MASK);
687
688         layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
689         layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
690
691         ptr = get_buffer_data(bigblock_offset, bufferp, 0);
692         (*bufferp)->cache.modified = 1;
693
694         if (buffer1)
695                 rel_buffer(buffer1);
696         if (buffer2)
697                 rel_buffer(buffer2);
698
699         rel_volume(volume);
700         return(ptr);
701 }
702
703 #if 0
704 /*
705  * Reserve space from the FIFO.  Make sure that bytes does not cross a 
706  * record boundary.
707  *
708  * Zero out base_bytes and initialize the fifo head and tail.  The
709  * data area is not zerod.
710  */
711 static
712 hammer_off_t
713 hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
714                   struct buffer_info **bufp, u_int16_t hdr_type)
715 {
716         struct buffer_info *buf;
717         struct volume_info *volume;
718         hammer_fifo_head_t head;
719         hammer_fifo_tail_t tail;
720         hammer_off_t off;
721         int32_t aligned_bytes;
722
723         aligned_bytes = (base_bytes + ext_bytes + HAMMER_TAIL_ONDISK_SIZE +
724                          HAMMER_HEAD_ALIGN_MASK) & ~HAMMER_HEAD_ALIGN_MASK;
725
726         volume = get_volume(RootVolNo);
727         off = volume->ondisk->vol0_fifo_end;
728
729         /*
730          * For now don't deal with transitions across buffer boundaries,
731          * only newfs_hammer uses this function.
732          */
733         assert((off & ~HAMMER_BUFMASK64) ==
734                 ((off + aligned_bytes) & ~HAMMER_BUFMASK));
735
736         *bufp = buf = get_buffer(off, 0);
737
738         buf->cache.modified = 1;
739         volume->cache.modified = 1;
740
741         head = (void *)((char *)buf->ondisk + ((int32_t)off & HAMMER_BUFMASK));
742         bzero(head, base_bytes);
743
744         head->hdr_signature = HAMMER_HEAD_SIGNATURE;
745         head->hdr_type = hdr_type;
746         head->hdr_size = aligned_bytes;
747         head->hdr_seq = volume->ondisk->vol0_next_seq++;
748
749         tail = (void*)((char *)head + aligned_bytes - HAMMER_TAIL_ONDISK_SIZE);
750         tail->tail_signature = HAMMER_TAIL_SIGNATURE;
751         tail->tail_type = hdr_type;
752         tail->tail_size = aligned_bytes;
753
754         volume->ondisk->vol0_fifo_end += aligned_bytes;
755         volume->cache.modified = 1;
756
757         rel_volume(volume);
758
759         return(off);
760 }
761
762 #endif
763
764 /*
765  * Flush various tracking structures to disk
766  */
767
768 /*
769  * Flush various tracking structures to disk
770  */
771 void
772 flush_all_volumes(void)
773 {
774         struct volume_info *vol;
775
776         TAILQ_FOREACH(vol, &VolList, entry)
777                 flush_volume(vol);
778 }
779
780 void
781 flush_volume(struct volume_info *volume)
782 {
783         struct buffer_info *buffer;
784         int i;
785
786         for (i = 0; i < HAMMER_BUFLISTS; ++i) {
787                 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
788                         flush_buffer(buffer);
789         }
790         writehammerbuf(volume, volume->ondisk, 0);
791         volume->cache.modified = 0;
792 }
793
794 void
795 flush_buffer(struct buffer_info *buffer)
796 {
797         writehammerbuf(buffer->volume, buffer->ondisk, buffer->buf_disk_offset);
798         buffer->cache.modified = 0;
799 }
800
801 #if 0
802 /*
803  * Generic buffer initialization
804  */
805 static void
806 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
807 {
808         head->hdr_signature = HAMMER_HEAD_SIGNATURE;
809         head->hdr_type = hdr_type;
810         head->hdr_size = 0;
811         head->hdr_crc = 0;
812         head->hdr_seq = 0;
813 }
814
815 #endif
816
817 #if 0
818 /*
819  * Core I/O operations
820  */
821 static void
822 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
823 {
824         ssize_t n;
825
826         n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
827         if (n != HAMMER_BUFSIZE)
828                 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
829 }
830
831 #endif
832
833 static void
834 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
835 {
836         ssize_t n;
837
838         n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
839         if (n != HAMMER_BUFSIZE)
840                 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
841 }
842
843 void
844 panic(const char *ctl, ...)
845 {
846         va_list va;
847
848         va_start(va, ctl);
849         vfprintf(stderr, ctl, va);
850         va_end(va);
851         fprintf(stderr, "\n");
852         exit(1);
853 }
854