HAMMER Utilities: Cleanup.
[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.18 2008/05/12 05:13:48 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 hammer_record_ondisk_t
330 alloc_record_element(hammer_off_t *offp, int32_t data_len, void **datap)
331 {
332         struct buffer_info *record_buffer = NULL;
333         struct buffer_info *data_buffer = NULL;
334         hammer_record_ondisk_t rec;
335
336         rec = alloc_blockmap(HAMMER_ZONE_RECORD_INDEX, sizeof(*rec),
337                              offp, &record_buffer);
338         bzero(rec, sizeof(*rec));
339
340         if (data_len >= HAMMER_BUFSIZE) {
341                 assert(data_len <= HAMMER_BUFSIZE); /* just one buffer */
342                 *datap = alloc_blockmap(HAMMER_ZONE_LARGE_DATA_INDEX, data_len,
343                                         &rec->base.data_off, &data_buffer);
344                 rec->base.data_len = data_len;
345                 bzero(*datap, data_len);
346         } else if (data_len) {
347                 *datap = alloc_blockmap(HAMMER_ZONE_SMALL_DATA_INDEX, data_len,
348                                         &rec->base.data_off, &data_buffer);
349                 rec->base.data_len = data_len;
350                 bzero(*datap, data_len);
351         } else {
352                 *datap = NULL;
353         }
354         /* XXX buf not released, ptr remains valid */
355         return(rec);
356 }
357
358 /*
359  * Format a new freemap.  Set all layer1 entries to UNAVAIL.  The initialize
360  * code will load each volume's freemap.
361  */
362 void
363 format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap)
364 {
365         struct buffer_info *buffer = NULL;
366         hammer_off_t layer1_offset;
367         struct hammer_blockmap_layer1 *layer1;
368         int i, isnew;
369
370         layer1_offset = alloc_bigblock(root_vol, 0);
371         for (i = 0; i < (int)HAMMER_BLOCKMAP_RADIX1; ++i) {
372                 isnew = ((i % HAMMER_BLOCKMAP_RADIX1_PERBUFFER) == 0);
373                 layer1 = get_buffer_data(layer1_offset + i * sizeof(*layer1),
374                                          &buffer, isnew);
375                 bzero(layer1, sizeof(*layer1));
376                 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
377                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
378         }
379         rel_buffer(buffer);
380
381         blockmap = &root_vol->ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
382         blockmap->phys_offset = layer1_offset;
383         blockmap->alloc_offset = HAMMER_ENCODE_RAW_BUFFER(255, -1);
384         blockmap->next_offset = HAMMER_ENCODE_RAW_BUFFER(0, 0);
385         blockmap->reserved01 = 0;
386         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
387         root_vol->cache.modified = 1;
388 }
389
390 /*
391  * Load the volume's remaining free space into the freemap.  If this is
392  * the root volume, initialize the freemap owner for the layer1 bigblock.
393  *
394  * Returns the number of bigblocks available.
395  */
396 int64_t
397 initialize_freemap(struct volume_info *vol)
398 {
399         struct volume_info *root_vol;
400         struct buffer_info *buffer1 = NULL;
401         struct buffer_info *buffer2 = NULL;
402         struct hammer_blockmap_layer1 *layer1;
403         struct hammer_blockmap_layer2 *layer2;
404         hammer_off_t layer1_base;
405         hammer_off_t layer1_offset;
406         hammer_off_t layer2_offset;
407         hammer_off_t phys_offset;
408         hammer_off_t aligned_vol_free_end;
409         int64_t count = 0;
410         int modified1 = 0;
411
412         root_vol = get_volume(RootVolNo);
413         aligned_vol_free_end = (vol->vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
414                                 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
415
416         printf("initialize freemap volume %d\n", vol->vol_no);
417
418         /*
419          * Initialize the freemap.  First preallocate the bigblocks required
420          * to implement layer2.   This preallocation is a bootstrap allocation
421          * using blocks from the target volume.
422          */
423         layer1_base = root_vol->ondisk->vol0_blockmap[
424                                         HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
425         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
426              phys_offset < aligned_vol_free_end;
427              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
428                 layer1_offset = layer1_base +
429                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
430                 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
431                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
432                         layer1->phys_offset = alloc_bigblock(vol, 0);
433                         layer1->blocks_free = 0;
434                         buffer1->cache.modified = 1;
435                         layer1->layer1_crc = crc32(layer1,
436                                                    HAMMER_LAYER1_CRCSIZE);
437                 }
438         }
439
440         /*
441          * Now fill everything in.
442          */
443         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(vol->vol_no, 0);
444              phys_offset < aligned_vol_free_end;
445              phys_offset += HAMMER_LARGEBLOCK_SIZE) {
446                 modified1 = 0;
447                 layer1_offset = layer1_base +
448                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
449                 layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
450
451                 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
452                 layer2_offset = layer1->phys_offset +
453                                 HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
454
455                 layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
456                 if (phys_offset < vol->vol_free_off) {
457                         /*
458                          * Fixups XXX - bigblocks already allocated as part
459                          * of the freemap bootstrap.
460                          */
461                         layer2->u.owner = HAMMER_ENCODE_FREEMAP(0, 0); /* XXX */
462                 } else if (phys_offset < vol->vol_free_end) {
463                         ++layer1->blocks_free;
464                         buffer1->cache.modified = 1;
465                         layer2->u.owner = HAMMER_BLOCKMAP_FREE;
466                         ++count;
467                         modified1 = 1;
468                 } else {
469                         layer2->u.owner = HAMMER_BLOCKMAP_UNAVAIL;
470                 }
471                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
472                 buffer2->cache.modified = 1;
473
474                 /*
475                  * Finish-up layer 1
476                  */
477                 if (modified1) {
478                         layer1->layer1_crc = crc32(layer1,
479                                                    HAMMER_LAYER1_CRCSIZE);
480                         buffer1->cache.modified = 1;
481                 }
482         }
483         rel_buffer(buffer1);
484         rel_buffer(buffer2);
485         rel_volume(root_vol);
486         return(count);
487 }
488
489 /*
490  * Allocate big-blocks using our poor-man's volume->vol_free_off and
491  * update the freemap if owner != 0.
492  */
493 hammer_off_t
494 alloc_bigblock(struct volume_info *volume, hammer_off_t owner)
495 {
496         struct buffer_info *buffer = NULL;
497         struct volume_info *root_vol;
498         hammer_off_t result_offset;
499         hammer_off_t layer_offset;
500         struct hammer_blockmap_layer1 *layer1;
501         struct hammer_blockmap_layer2 *layer2;
502         int didget;
503
504         if (volume == NULL) {
505                 volume = get_volume(RootVolNo);
506                 didget = 1;
507         } else {
508                 didget = 0;
509         }
510         result_offset = volume->vol_free_off;
511         if (result_offset >= volume->vol_free_end)
512                 panic("alloc_bigblock: Ran out of room, filesystem too small");
513         volume->vol_free_off += HAMMER_LARGEBLOCK_SIZE;
514
515         /*
516          * Update the freemap
517          */
518         if (owner) {
519                 root_vol = get_volume(RootVolNo);
520                 layer_offset = root_vol->ondisk->vol0_blockmap[
521                                         HAMMER_ZONE_FREEMAP_INDEX].phys_offset;
522                 layer_offset += HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
523                 layer1 = get_buffer_data(layer_offset, &buffer, 0);
524                 assert(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
525                 --layer1->blocks_free;
526                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
527                 buffer->cache.modified = 1;
528                 layer_offset = layer1->phys_offset +
529                                HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
530                 layer2 = get_buffer_data(layer_offset, &buffer, 0);
531                 assert(layer2->u.owner == HAMMER_BLOCKMAP_FREE);
532                 layer2->u.owner = owner;
533                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
534                 buffer->cache.modified = 1;
535
536                 rel_buffer(buffer);
537                 rel_volume(root_vol);
538         }
539
540         if (didget)
541                 rel_volume(volume);
542         return(result_offset);
543 }
544
545 /*
546  * Format the undo-map for the root volume.
547  */
548 void
549 format_undomap(hammer_volume_ondisk_t ondisk)
550 {
551         const int undo_zone = HAMMER_ZONE_UNDO_INDEX;
552         hammer_off_t undo_limit;
553         hammer_blockmap_t blockmap;
554         hammer_off_t scan;
555         struct hammer_blockmap_layer2 *layer2;
556         int n;
557         int limit_index;
558
559         /*
560          * Size the undo buffer in multiples of HAMMER_LARGEBLOCK_SIZE,
561          * up to HAMMER_UNDO_LAYER2 large blocks.  Size to approximately
562          * 0.1% of the disk.
563          */
564         undo_limit = UndoBufferSize;
565         if (undo_limit == 0)
566                 undo_limit = (ondisk->vol_buf_end - ondisk->vol_buf_beg) / 1000;
567         undo_limit = (undo_limit + HAMMER_LARGEBLOCK_MASK64) &
568                      ~HAMMER_LARGEBLOCK_MASK64;
569         if (undo_limit < HAMMER_LARGEBLOCK_SIZE)
570                 undo_limit = HAMMER_LARGEBLOCK_SIZE;
571         if (undo_limit > HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2)
572                 undo_limit = HAMMER_LARGEBLOCK_SIZE * HAMMER_UNDO_LAYER2;
573         UndoBufferSize = undo_limit;
574
575         blockmap = &ondisk->vol0_blockmap[undo_zone];
576         bzero(blockmap, sizeof(*blockmap));
577         blockmap->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
578         blockmap->first_offset = HAMMER_ZONE_ENCODE(undo_zone, 0);
579         blockmap->next_offset = blockmap->first_offset;
580         blockmap->alloc_offset = HAMMER_ZONE_ENCODE(undo_zone, undo_limit);
581         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
582
583         layer2 = &ondisk->vol0_undo_array[0];
584         n = 0;
585         scan = blockmap->next_offset;
586         limit_index = undo_limit / HAMMER_LARGEBLOCK_SIZE;
587
588         assert(limit_index <= HAMMER_UNDO_LAYER2);
589
590         for (n = 0; n < limit_index; ++n) {
591                 layer2->u.phys_offset = alloc_bigblock(NULL, scan);
592                 layer2->bytes_free = -1;        /* not used */
593                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
594
595                 scan += HAMMER_LARGEBLOCK_SIZE;
596                 ++layer2;
597         }
598         while (n < HAMMER_UNDO_LAYER2) {
599                 layer2->u.phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
600                 layer2->bytes_free = -1;
601                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
602                 ++layer2;
603                 ++n;
604         }
605 }
606
607 /*
608  * Format a new blockmap.  Set the owner to the base of the blockmap
609  * (meaning either the blockmap layer1 bigblock, layer2 bigblock, or
610  * target bigblock).
611  */
612 void
613 format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_off)
614 {
615         blockmap->phys_offset = alloc_bigblock(NULL, zone_off);
616         blockmap->alloc_offset = zone_off;
617         blockmap->first_offset = zone_off;
618         blockmap->next_offset = zone_off;
619         blockmap->entry_crc = crc32(blockmap, HAMMER_BLOCKMAP_CRCSIZE);
620 }
621
622 static
623 void *
624 alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
625                struct buffer_info **bufferp)
626 {
627         struct buffer_info *buffer1 = NULL;
628         struct buffer_info *buffer2 = NULL;
629         struct volume_info *volume;
630         hammer_blockmap_t rootmap;
631         struct hammer_blockmap_layer1 *layer1;
632         struct hammer_blockmap_layer2 *layer2;
633         hammer_off_t layer1_offset;
634         hammer_off_t layer2_offset;
635         hammer_off_t bigblock_offset;
636         void *ptr;
637
638         volume = get_volume(RootVolNo);
639
640         rootmap = &volume->ondisk->vol0_blockmap[zone];
641
642         /*
643          * Alignment and buffer-boundary issues
644          */
645         bytes = (bytes + 7) & ~7;
646         if ((rootmap->phys_offset ^ (rootmap->phys_offset + bytes - 1)) &
647             ~HAMMER_BUFMASK64) {
648                 volume->cache.modified = 1;
649                 rootmap->phys_offset = (rootmap->phys_offset + bytes) &
650                                        ~HAMMER_BUFMASK64;
651         }
652
653         /*
654          * Dive layer 1
655          */
656         layer1_offset = rootmap->phys_offset +
657                         HAMMER_BLOCKMAP_LAYER1_OFFSET(rootmap->alloc_offset);
658
659         layer1 = get_buffer_data(layer1_offset, &buffer1, 0);
660         if ((rootmap->alloc_offset & HAMMER_BLOCKMAP_LAYER2_MASK) == 0) {
661                 buffer1->cache.modified = 1;
662                 bzero(layer1, sizeof(*layer1));
663                 layer1->blocks_free = HAMMER_BLOCKMAP_RADIX2;
664                 layer1->phys_offset = alloc_bigblock(NULL,
665                                                      rootmap->alloc_offset);
666         }
667
668         /*
669          * Dive layer 2
670          */
671         layer2_offset = layer1->phys_offset +
672                         HAMMER_BLOCKMAP_LAYER2_OFFSET(rootmap->alloc_offset);
673
674         layer2 = get_buffer_data(layer2_offset, &buffer2, 0);
675
676         if ((rootmap->alloc_offset & HAMMER_LARGEBLOCK_MASK64) == 0) {
677                 buffer2->cache.modified = 1;
678                 bzero(layer2, sizeof(*layer2));
679                 layer2->u.phys_offset = alloc_bigblock(NULL,
680                                                        rootmap->alloc_offset);
681                 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
682                 --layer1->blocks_free;
683         }
684
685         buffer1->cache.modified = 1;
686         buffer2->cache.modified = 1;
687         volume->cache.modified = 1;
688         layer2->bytes_free -= bytes;
689         *result_offp = rootmap->alloc_offset;
690         rootmap->alloc_offset += bytes;
691         rootmap->next_offset = rootmap->alloc_offset;
692
693         bigblock_offset = layer2->u.phys_offset + 
694                           (*result_offp & HAMMER_LARGEBLOCK_MASK);
695
696         layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
697         layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
698
699         ptr = get_buffer_data(bigblock_offset, bufferp, 0);
700         (*bufferp)->cache.modified = 1;
701
702         if (buffer1)
703                 rel_buffer(buffer1);
704         if (buffer2)
705                 rel_buffer(buffer2);
706
707         rel_volume(volume);
708         return(ptr);
709 }
710
711 #if 0
712 /*
713  * Reserve space from the FIFO.  Make sure that bytes does not cross a 
714  * record boundary.
715  *
716  * Zero out base_bytes and initialize the fifo head and tail.  The
717  * data area is not zerod.
718  */
719 static
720 hammer_off_t
721 hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes,
722                   struct buffer_info **bufp, u_int16_t hdr_type)
723 {
724         struct buffer_info *buf;
725         struct volume_info *volume;
726         hammer_fifo_head_t head;
727         hammer_fifo_tail_t tail;
728         hammer_off_t off;
729         int32_t aligned_bytes;
730
731         aligned_bytes = (base_bytes + ext_bytes + HAMMER_TAIL_ONDISK_SIZE +
732                          HAMMER_HEAD_ALIGN_MASK) & ~HAMMER_HEAD_ALIGN_MASK;
733
734         volume = get_volume(RootVolNo);
735         off = volume->ondisk->vol0_fifo_end;
736
737         /*
738          * For now don't deal with transitions across buffer boundaries,
739          * only newfs_hammer uses this function.
740          */
741         assert((off & ~HAMMER_BUFMASK64) ==
742                 ((off + aligned_bytes) & ~HAMMER_BUFMASK));
743
744         *bufp = buf = get_buffer(off, 0);
745
746         buf->cache.modified = 1;
747         volume->cache.modified = 1;
748
749         head = (void *)((char *)buf->ondisk + ((int32_t)off & HAMMER_BUFMASK));
750         bzero(head, base_bytes);
751
752         head->hdr_signature = HAMMER_HEAD_SIGNATURE;
753         head->hdr_type = hdr_type;
754         head->hdr_size = aligned_bytes;
755         head->hdr_seq = volume->ondisk->vol0_next_seq++;
756
757         tail = (void*)((char *)head + aligned_bytes - HAMMER_TAIL_ONDISK_SIZE);
758         tail->tail_signature = HAMMER_TAIL_SIGNATURE;
759         tail->tail_type = hdr_type;
760         tail->tail_size = aligned_bytes;
761
762         volume->ondisk->vol0_fifo_end += aligned_bytes;
763         volume->cache.modified = 1;
764
765         rel_volume(volume);
766
767         return(off);
768 }
769
770 #endif
771
772 /*
773  * Flush various tracking structures to disk
774  */
775
776 /*
777  * Flush various tracking structures to disk
778  */
779 void
780 flush_all_volumes(void)
781 {
782         struct volume_info *vol;
783
784         TAILQ_FOREACH(vol, &VolList, entry)
785                 flush_volume(vol);
786 }
787
788 void
789 flush_volume(struct volume_info *volume)
790 {
791         struct buffer_info *buffer;
792         int i;
793
794         for (i = 0; i < HAMMER_BUFLISTS; ++i) {
795                 TAILQ_FOREACH(buffer, &volume->buffer_lists[i], entry)
796                         flush_buffer(buffer);
797         }
798         writehammerbuf(volume, volume->ondisk, 0);
799         volume->cache.modified = 0;
800 }
801
802 void
803 flush_buffer(struct buffer_info *buffer)
804 {
805         writehammerbuf(buffer->volume, buffer->ondisk, buffer->buf_disk_offset);
806         buffer->cache.modified = 0;
807 }
808
809 #if 0
810 /*
811  * Generic buffer initialization
812  */
813 static void
814 init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type)
815 {
816         head->hdr_signature = HAMMER_HEAD_SIGNATURE;
817         head->hdr_type = hdr_type;
818         head->hdr_size = 0;
819         head->hdr_crc = 0;
820         head->hdr_seq = 0;
821 }
822
823 #endif
824
825 #if 0
826 /*
827  * Core I/O operations
828  */
829 static void
830 readhammerbuf(struct volume_info *vol, void *data, int64_t offset)
831 {
832         ssize_t n;
833
834         n = pread(vol->fd, data, HAMMER_BUFSIZE, offset);
835         if (n != HAMMER_BUFSIZE)
836                 err(1, "Read volume %d (%s)", vol->vol_no, vol->name);
837 }
838
839 #endif
840
841 static void
842 writehammerbuf(struct volume_info *vol, const void *data, int64_t offset)
843 {
844         ssize_t n;
845
846         n = pwrite(vol->fd, data, HAMMER_BUFSIZE, offset);
847         if (n != HAMMER_BUFSIZE)
848                 err(1, "Write volume %d (%s)", vol->vol_no, vol->name);
849 }
850
851 void
852 panic(const char *ctl, ...)
853 {
854         va_list va;
855
856         va_start(va, ctl);
857         vfprintf(stderr, ctl, va);
858         va_end(va);
859         fprintf(stderr, "\n");
860         exit(1);
861 }
862