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