sys/vfs/hammer: Cleanups
[dragonfly.git] / sys / vfs / hammer / hammer_volume.c
1 /*
2  * Copyright (c) 2009 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> and
6  * Michael Neumann <mneumann@ntecs.de>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36
37 #include <sys/fcntl.h>
38 #include <sys/nlookup.h>
39
40 #include "hammer.h"
41
42 struct bigblock_stat {
43         int64_t total_bigblocks;
44         int64_t total_free_bigblocks;
45         int64_t counter;
46 };
47
48 static int
49 hammer_format_volume_header(struct hammer_mount *hmp,
50         struct hammer_volume_ondisk *ondisk,
51         const char *vol_name, int vol_no, int vol_count,
52         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size);
53
54 static int
55 hammer_update_volumes_header(hammer_transaction_t trans,
56         struct bigblock_stat *stat);
57
58 static int
59 hammer_do_reblock(hammer_transaction_t trans, hammer_inode_t ip);
60
61 static int
62 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume,
63         struct bigblock_stat *stat);
64
65 static int
66 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume,
67         struct bigblock_stat *stat);
68
69 static int
70 hammer_test_free_freemap(hammer_transaction_t trans, hammer_volume_t volume);
71
72 int
73 hammer_ioc_volume_add(hammer_transaction_t trans, hammer_inode_t ip,
74                 struct hammer_ioc_volume *ioc)
75 {
76         struct hammer_mount *hmp = trans->hmp;
77         struct mount *mp = hmp->mp;
78         struct hammer_volume_ondisk ondisk;
79         struct bigblock_stat stat;
80         hammer_volume_t volume;
81         int free_vol_no = 0;
82         int error;
83
84         if (mp->mnt_flag & MNT_RDONLY) {
85                 kprintf("Cannot add volume to read-only HAMMER filesystem\n");
86                 return (EINVAL);
87         }
88
89         if (hmp->nvolumes >= HAMMER_MAX_VOLUMES) {
90                 kprintf("Max number of HAMMER volumes exceeded\n");
91                 return (EINVAL);
92         }
93
94         if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
95                 kprintf("Another volume operation is in progress!\n");
96                 return (EAGAIN);
97         }
98
99         /*
100          * Find an unused volume number.
101          */
102         while (free_vol_no < HAMMER_MAX_VOLUMES &&
103                 HAMMER_VOLUME_NUMBER_IS_SET(hmp, free_vol_no)) {
104                 ++free_vol_no;
105         }
106         if (free_vol_no >= HAMMER_MAX_VOLUMES) {
107                 kprintf("Max number of HAMMER volumes exceeded\n");
108                 error = EINVAL;
109                 goto end;
110         }
111
112         error = hammer_format_volume_header(
113                 hmp,
114                 &ondisk,
115                 hmp->rootvol->ondisk->vol_name,
116                 free_vol_no,
117                 hmp->nvolumes+1,
118                 ioc->vol_size,
119                 ioc->boot_area_size,
120                 ioc->mem_area_size);
121         if (error)
122                 goto end;
123
124         error = hammer_install_volume(hmp, ioc->device_name, NULL, &ondisk);
125         if (error)
126                 goto end;
127
128         hammer_sync_lock_sh(trans);
129         hammer_lock_ex(&hmp->blkmap_lock);
130
131         volume = hammer_get_volume(hmp, free_vol_no, &error);
132         KKASSERT(volume != NULL && error == 0);
133
134         error = hammer_format_freemap(trans, volume, &stat);
135         KKASSERT(error == 0);
136         hammer_rel_volume(volume, 0);
137
138         ++hmp->nvolumes;
139         error = hammer_update_volumes_header(trans, &stat);
140         KKASSERT(error == 0);
141
142         hammer_unlock(&hmp->blkmap_lock);
143         hammer_sync_unlock(trans);
144
145         KKASSERT(error == 0);
146 end:
147         hammer_unlock(&hmp->volume_lock);
148         if (error)
149                 kprintf("An error occurred: %d\n", error);
150         return (error);
151 }
152
153
154 /*
155  * Remove a volume.
156  */
157 int
158 hammer_ioc_volume_del(hammer_transaction_t trans, hammer_inode_t ip,
159                 struct hammer_ioc_volume *ioc)
160 {
161         struct hammer_mount *hmp = trans->hmp;
162         struct mount *mp = hmp->mp;
163         struct hammer_volume_ondisk *ondisk;
164         struct bigblock_stat stat;
165         hammer_volume_t volume;
166         int vol_no;
167         int count = 0;
168         int error = 0;
169
170         if (mp->mnt_flag & MNT_RDONLY) {
171                 kprintf("Cannot del volume from read-only HAMMER filesystem\n");
172                 return (EINVAL);
173         }
174
175         if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
176                 kprintf("Another volume operation is in progress!\n");
177                 return (EAGAIN);
178         }
179
180         /*
181          * find volume by volname
182          */
183         volume = NULL;
184         HAMMER_VOLUME_NUMBER_FOREACH(hmp, vol_no) {
185                 volume = hammer_get_volume(hmp, vol_no, &error);
186                 KKASSERT(volume != NULL && error == 0);
187                 if (strcmp(volume->vol_name, ioc->device_name) == 0) {
188                         break;
189                 }
190                 hammer_rel_volume(volume, 0);
191                 volume = NULL;
192         }
193
194         if (volume == NULL) {
195                 kprintf("Couldn't find volume\n");
196                 error = EINVAL;
197                 goto end;
198         }
199
200         if (volume == trans->rootvol) {
201                 kprintf("Cannot remove root-volume\n");
202                 hammer_rel_volume(volume, 0);
203                 error = EINVAL;
204                 goto end;
205         }
206
207         /*
208          * Reblock filesystem if the volume is not empty
209          */
210         hmp->volume_to_remove = volume->vol_no;
211
212         if (hammer_test_free_freemap(trans, volume)) {
213                 error = hammer_do_reblock(trans, ip);
214                 if (error) {
215                         hmp->volume_to_remove = -1;
216                         hammer_rel_volume(volume, 0);
217                         goto end;
218                 }
219         }
220
221         /*
222          * Sync filesystem
223          */
224         while (hammer_flusher_haswork(hmp)) {
225                 hammer_flusher_sync(hmp);
226                 ++count;
227                 if (count >= 5) {
228                         if (count == 5)
229                                 kprintf("HAMMER: flushing.");
230                         else
231                                 kprintf(".");
232                         tsleep(&count, 0, "hmrufl", hz);
233                 }
234                 if (count == 30) {
235                         kprintf("giving up");
236                         break;
237                 }
238         }
239         if (count >= 5)
240                 kprintf("\n");
241
242         hammer_sync_lock_sh(trans);
243         hammer_lock_ex(&hmp->blkmap_lock);
244
245         /*
246          * We use stat later to update rootvol's big-block stats
247          */
248         error = hammer_free_freemap(trans, volume, &stat);
249         if (error) {
250                 kprintf("Failed to free volume: ");
251                 if (error == EBUSY)
252                         kprintf("Volume %d not empty\n", volume->vol_no);
253                 else
254                         kprintf("%d\n", error);
255                 hmp->volume_to_remove = -1;
256                 hammer_rel_volume(volume, 0);
257                 goto end1;
258         }
259
260         hmp->volume_to_remove = -1;
261         hammer_rel_volume(volume, 0);
262
263         /*
264          * Unload buffers
265          */
266         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
267                 hammer_unload_buffer, volume);
268
269         bzero(&ondisk, sizeof(ondisk));
270         error = hammer_unload_volume(volume, &ondisk);
271         if (error == -1) {
272                 kprintf("Failed to unload volume\n");
273                 goto end1;
274         }
275
276         --hmp->nvolumes;
277         error = hammer_update_volumes_header(trans, &stat);
278         KKASSERT(error == 0);
279
280 end1:
281         hammer_unlock(&hmp->blkmap_lock);
282         hammer_sync_unlock(trans);
283
284 end:
285         hammer_unlock(&hmp->volume_lock);
286         if (error)
287                 kprintf("An error occurred: %d\n", error);
288         return (error);
289 }
290
291
292 int
293 hammer_ioc_volume_list(hammer_transaction_t trans, hammer_inode_t ip,
294     struct hammer_ioc_volume_list *ioc)
295 {
296         struct hammer_mount *hmp = trans->hmp;
297         hammer_volume_t volume;
298         int error = 0;
299         int i, len, cnt = 0;
300
301         if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
302                 kprintf("Another volume operation is in progress!\n");
303                 return (EAGAIN);
304         }
305
306         HAMMER_VOLUME_NUMBER_FOREACH(hmp, i) {
307                 if (cnt >= ioc->nvols)
308                         break;
309                 volume = hammer_get_volume(hmp, i, &error);
310                 KKASSERT(volume != NULL && error == 0);
311
312                 len = strlen(volume->vol_name) + 1;
313                 KKASSERT(len <= MAXPATHLEN);
314
315                 error = copyout(volume->vol_name, ioc->vols[cnt].device_name,
316                                 len);
317                 hammer_rel_volume(volume, 0);
318                 if (error)
319                         goto end;
320                 cnt++;
321         }
322         ioc->nvols = cnt;
323
324 end:
325         hammer_unlock(&hmp->volume_lock);
326         return (error);
327 }
328
329 static
330 int
331 hammer_do_reblock(hammer_transaction_t trans, hammer_inode_t ip)
332 {
333         int error;
334
335         struct hammer_ioc_reblock reblock;
336         bzero(&reblock, sizeof(reblock));
337
338         reblock.key_beg.localization = HAMMER_MIN_LOCALIZATION;
339         reblock.key_beg.obj_id = HAMMER_MIN_OBJID;
340         reblock.key_end.localization = HAMMER_MAX_LOCALIZATION;
341         reblock.key_end.obj_id = HAMMER_MAX_OBJID;
342         reblock.head.flags = HAMMER_IOC_DO_FLAGS;
343         reblock.free_level = 0;
344         reblock.allpfs = 1;
345
346         kprintf("reblock started\n");
347         error = hammer_ioc_reblock(trans, ip, &reblock);
348
349         if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) {
350                 error = EINTR;
351         }
352
353         if (error) {
354                 if (error == EINTR) {
355                         kprintf("reblock was interrupted\n");
356                 } else {
357                         kprintf("reblock failed: %d\n", error);
358                 }
359                 return(error);
360         }
361
362         return(0);
363 }
364
365 /*
366  * Iterate over all usable L1 entries of the volume and
367  * the corresponding L2 entries.
368  */
369 static int
370 hammer_iterate_l1l2_entries(hammer_transaction_t trans, hammer_volume_t volume,
371         int (*callback)(hammer_transaction_t, hammer_volume_t, hammer_buffer_t*,
372                 struct hammer_blockmap_layer1*, struct hammer_blockmap_layer2*,
373                 hammer_off_t, hammer_off_t, void*),
374         void *data)
375 {
376         struct hammer_mount *hmp = trans->hmp;
377         hammer_blockmap_t freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
378         int error = 0;
379         hammer_off_t phys_off;
380         hammer_off_t block_off;
381         hammer_off_t layer1_off;
382         hammer_off_t layer2_off;
383         hammer_off_t aligned_buf_end_off;
384         hammer_off_t aligned_vol_end_off;
385         struct hammer_blockmap_layer1 *layer1;
386         struct hammer_blockmap_layer2 *layer2;
387         hammer_buffer_t buffer1 = NULL;
388         hammer_buffer_t buffer2 = NULL;
389
390         /*
391          * Calculate the usable size of the volume, which
392          * must be aligned at a big-block (8 MB) boundary.
393          */
394         aligned_buf_end_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
395                 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
396                 & ~HAMMER_BIGBLOCK_MASK64);
397         aligned_vol_end_off = (aligned_buf_end_off + HAMMER_BLOCKMAP_LAYER2_MASK)
398                 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
399
400         /*
401          * Iterate the volume's address space in chunks of 4 TB, where each
402          * chunk consists of at least one physically available 8 MB big-block.
403          *
404          * For each chunk we need one L1 entry and one L2 big-block.
405          * We use the first big-block of each chunk as L2 block.
406          */
407         for (phys_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
408              phys_off < aligned_vol_end_off;
409              phys_off += HAMMER_BLOCKMAP_LAYER2) {
410                 for (block_off = 0;
411                      block_off < HAMMER_BLOCKMAP_LAYER2;
412                      block_off += HAMMER_BIGBLOCK_SIZE) {
413                         layer2_off = phys_off +
414                                 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_off);
415                         layer2 = hammer_bread(hmp, layer2_off, &error, &buffer2);
416                         if (error)
417                                 goto end;
418
419                         error = callback(trans, volume, &buffer2, NULL,
420                                          layer2, phys_off, block_off, data);
421                         if (error)
422                                 goto end;
423                 }
424
425                 layer1_off = freemap->phys_offset +
426                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_off);
427                 layer1 = hammer_bread(hmp, layer1_off, &error, &buffer1);
428                 if (error)
429                         goto end;
430
431                 error = callback(trans, volume, &buffer1, layer1, NULL,
432                                  phys_off, 0, data);
433                 if (error)
434                         goto end;
435         }
436
437 end:
438         if (buffer1)
439                 hammer_rel_buffer(buffer1, 0);
440         if (buffer2)
441                 hammer_rel_buffer(buffer2, 0);
442
443         return error;
444 }
445
446
447 static int
448 format_callback(hammer_transaction_t trans, hammer_volume_t volume,
449         hammer_buffer_t *bufferp,
450         struct hammer_blockmap_layer1 *layer1,
451         struct hammer_blockmap_layer2 *layer2,
452         hammer_off_t phys_off,
453         hammer_off_t block_off,
454         void *data)
455 {
456         struct bigblock_stat *stat = (struct bigblock_stat*)data;
457
458         /*
459          * Calculate the usable size of the volume, which must be aligned
460          * at a big-block (8 MB) boundary.
461          */
462         hammer_off_t aligned_buf_end_off;
463         aligned_buf_end_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
464                 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
465                 & ~HAMMER_BIGBLOCK_MASK64);
466
467         if (layer1) {
468                 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
469
470                 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
471                 bzero(layer1, sizeof(*layer1));
472                 layer1->phys_offset = phys_off;
473                 layer1->blocks_free = stat->counter;
474                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
475                 hammer_modify_buffer_done(*bufferp);
476                 stat->counter = 0; /* reset */
477         } else if (layer2) {
478                 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
479                 bzero(layer2, sizeof(*layer2));
480
481                 if (block_off == 0) {
482                         /*
483                          * The first entry represents the L2 big-block itself.
484                          * Note that the first entry represents the L1 big-block
485                          * and the second entry represents the L2 big-block for
486                          * root volume, but this function assumes the volume is
487                          * non-root given that we can't add a new root volume.
488                          */
489                         KKASSERT(trans->rootvol && trans->rootvol != volume);
490                         layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
491                         layer2->append_off = HAMMER_BIGBLOCK_SIZE;
492                         layer2->bytes_free = 0;
493                 } else if (phys_off + block_off < aligned_buf_end_off) {
494                         /*
495                          * Available big-block
496                          */
497                         layer2->zone = 0;
498                         layer2->append_off = 0;
499                         layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
500                         ++stat->total_bigblocks;
501                         ++stat->total_free_bigblocks;
502                         ++stat->counter;
503                 } else {
504                         /*
505                          * Big-block outside of physically available
506                          * space
507                          */
508                         layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
509                         layer2->append_off = HAMMER_BIGBLOCK_SIZE;
510                         layer2->bytes_free = 0;
511                 }
512
513                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
514                 hammer_modify_buffer_done(*bufferp);
515         } else {
516                 KKASSERT(0);
517         }
518
519         return 0;
520 }
521
522 static int
523 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume,
524         struct bigblock_stat *stat)
525 {
526         stat->total_bigblocks = 0;
527         stat->total_free_bigblocks = 0;
528         stat->counter = 0;
529         return hammer_iterate_l1l2_entries(trans, volume, format_callback, stat);
530 }
531
532 static int
533 free_callback(hammer_transaction_t trans, hammer_volume_t volume __unused,
534         hammer_buffer_t *bufferp,
535         struct hammer_blockmap_layer1 *layer1,
536         struct hammer_blockmap_layer2 *layer2,
537         hammer_off_t phys_off,
538         hammer_off_t block_off __unused,
539         void *data)
540 {
541         struct bigblock_stat *stat = (struct bigblock_stat*)data;
542
543         if (layer1) {
544                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
545                         /*
546                          * This layer1 entry is already free.
547                          */
548                         return 0;
549                 }
550
551                 KKASSERT((int)HAMMER_VOL_DECODE(layer1->phys_offset) ==
552                         trans->hmp->volume_to_remove);
553
554                 /*
555                  * Free the L1 entry
556                  */
557                 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
558                 bzero(layer1, sizeof(*layer1));
559                 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
560                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
561                 hammer_modify_buffer_done(*bufferp);
562
563                 return 0;
564         } else if (layer2) {
565                 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
566                         return 0;
567                 }
568
569                 if (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX) {
570                         return 0;
571                 }
572
573                 if (layer2->append_off == 0 &&
574                     layer2->bytes_free == HAMMER_BIGBLOCK_SIZE) {
575                         --stat->total_bigblocks;
576                         --stat->total_free_bigblocks;
577                         return 0;
578                 }
579
580                 /*
581                  * We found a layer2 entry that is not empty!
582                  */
583                 return EBUSY;
584         } else {
585                 KKASSERT(0);
586         }
587
588         return EINVAL;
589 }
590
591 /*
592  * Non-zero return value means we can't free the volume.
593  */
594 static int
595 test_free_callback(hammer_transaction_t trans, hammer_volume_t volume __unused,
596         hammer_buffer_t *bufferp,
597         struct hammer_blockmap_layer1 *layer1,
598         struct hammer_blockmap_layer2 *layer2,
599         hammer_off_t phys_off,
600         hammer_off_t block_off __unused,
601         void *data)
602 {
603         if (layer2 == NULL) {
604                 return(0);  /* only layer2 needs to be tested */
605         }
606
607         if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
608                 return(0);  /* beyond physically available space */
609         }
610         if (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX) {
611                 return(0);  /* big-block for layer1/2 */
612         }
613         if (layer2->append_off == 0 &&
614             layer2->bytes_free == HAMMER_BIGBLOCK_SIZE) {
615                 return(0);  /* big-block is 0% used */
616         }
617
618         return(EBUSY);  /* big-block has data */
619 }
620
621 static int
622 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume,
623         struct bigblock_stat *stat)
624 {
625         int error;
626
627         error = hammer_test_free_freemap(trans, volume);
628         if (error)
629                 return error;  /* not ready to free */
630
631         stat->total_bigblocks = 0;
632         stat->total_free_bigblocks = 0;
633         stat->counter = 0;
634         return hammer_iterate_l1l2_entries(trans, volume, free_callback, stat);
635 }
636
637 static int
638 hammer_test_free_freemap(hammer_transaction_t trans, hammer_volume_t volume)
639 {
640         return hammer_iterate_l1l2_entries(trans, volume, test_free_callback, NULL);
641 }
642
643 static int
644 hammer_format_volume_header(struct hammer_mount *hmp,
645         struct hammer_volume_ondisk *ondisk,
646         const char *vol_name, int vol_no, int vol_count,
647         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
648 {
649         int64_t vol_alloc;
650
651         KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
652
653         bzero(ondisk, sizeof(struct hammer_volume_ondisk));
654         ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
655         ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
656         ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
657         ondisk->vol_fsid = hmp->fsid;
658         ondisk->vol_rootvol = hmp->rootvol->vol_no;
659         ondisk->vol_no = vol_no;
660         ondisk->vol_count = vol_count;
661         ondisk->vol_version = hmp->version;
662
663         /*
664          * Reserve space for (future) header junk, setup our poor-man's
665          * big-block allocator.
666          */
667         vol_alloc = HAMMER_BUFSIZE * 16;
668         ondisk->vol_bot_beg = vol_alloc;
669         vol_alloc += boot_area_size;
670         ondisk->vol_mem_beg = vol_alloc;
671         vol_alloc += mem_area_size;
672
673         /*
674          * The remaining area is the zone 2 buffer allocation area.  These
675          * buffers
676          */
677         ondisk->vol_buf_beg = vol_alloc;
678         ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
679
680         if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
681                 kprintf("volume %d %s is too small to hold the volume header\n",
682                      ondisk->vol_no, ondisk->vol_name);
683                 return(EFTYPE);
684         }
685
686         ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
687                               HAMMER_BUFSIZE;
688         ondisk->vol_blocksize = HAMMER_BUFSIZE;
689         return(0);
690 }
691
692 static int
693 hammer_update_volumes_header(hammer_transaction_t trans,
694         struct bigblock_stat *stat)
695 {
696         struct hammer_mount *hmp = trans->hmp;
697         struct mount *mp = hmp->mp;
698         hammer_volume_t volume;
699         int vol_no;
700         int error = 0;
701
702         /*
703          * Set each volume's new value of the vol_count field.
704          */
705         HAMMER_VOLUME_NUMBER_FOREACH(hmp, vol_no) {
706                 volume = hammer_get_volume(hmp, vol_no, &error);
707                 KKASSERT(volume != NULL && error == 0);
708                 hammer_modify_volume_field(trans, volume, vol_count);
709                 volume->ondisk->vol_count = hmp->nvolumes;
710                 hammer_modify_volume_done(volume);
711
712                 /*
713                  * Only changes to the header of the root volume
714                  * are automatically flushed to disk. For all
715                  * other volumes that we modify we do it here.
716                  *
717                  * No interlock is needed, volume buffers are not
718                  * messed with by bioops.
719                  */
720                 if (volume != trans->rootvol && volume->io.modified) {
721                         hammer_crc_set_volume(volume->ondisk);
722                         hammer_io_flush(&volume->io, 0);
723                 }
724
725                 hammer_rel_volume(volume, 0);
726         }
727
728         /*
729          * Update the total number of big-blocks.
730          */
731         hammer_modify_volume_field(trans, trans->rootvol, vol0_stat_bigblocks);
732         trans->rootvol->ondisk->vol0_stat_bigblocks += stat->total_bigblocks;
733         hammer_modify_volume_done(trans->rootvol);
734
735         /*
736          * Big-block count changed so recompute the total number of blocks.
737          */
738         mp->mnt_stat.f_blocks = trans->rootvol->ondisk->vol0_stat_bigblocks *
739                                 HAMMER_BUFFERS_PER_BIGBLOCK;
740         mp->mnt_vstat.f_blocks = trans->rootvol->ondisk->vol0_stat_bigblocks *
741                                 HAMMER_BUFFERS_PER_BIGBLOCK;
742
743         /*
744          * Update the total number of free big-blocks.
745          */
746         hammer_modify_volume_field(trans, trans->rootvol,
747                 vol0_stat_freebigblocks);
748         trans->rootvol->ondisk->vol0_stat_freebigblocks +=
749                 stat->total_free_bigblocks;
750         hammer_modify_volume_done(trans->rootvol);
751
752         /*
753          * Update the copy in hmp.
754          */
755         hmp->copy_stat_freebigblocks =
756                 trans->rootvol->ondisk->vol0_stat_freebigblocks;
757
758         return(error);
759 }