sys/vfs/hammer: Add hammer_flush_dirty()
[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 error = 0;
168
169         if (mp->mnt_flag & MNT_RDONLY) {
170                 kprintf("Cannot del volume from read-only HAMMER filesystem\n");
171                 return (EINVAL);
172         }
173
174         if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
175                 kprintf("Another volume operation is in progress!\n");
176                 return (EAGAIN);
177         }
178
179         /*
180          * find volume by volname
181          */
182         volume = NULL;
183         HAMMER_VOLUME_NUMBER_FOREACH(hmp, vol_no) {
184                 volume = hammer_get_volume(hmp, vol_no, &error);
185                 KKASSERT(volume != NULL && error == 0);
186                 if (strcmp(volume->vol_name, ioc->device_name) == 0) {
187                         break;
188                 }
189                 hammer_rel_volume(volume, 0);
190                 volume = NULL;
191         }
192
193         if (volume == NULL) {
194                 kprintf("Couldn't find volume\n");
195                 error = EINVAL;
196                 goto end;
197         }
198
199         if (volume == trans->rootvol) {
200                 kprintf("Cannot remove root-volume\n");
201                 hammer_rel_volume(volume, 0);
202                 error = EINVAL;
203                 goto end;
204         }
205
206         /*
207          * Reblock filesystem if the volume is not empty
208          */
209         hmp->volume_to_remove = volume->vol_no;
210
211         if (hammer_test_free_freemap(trans, volume)) {
212                 error = hammer_do_reblock(trans, ip);
213                 if (error) {
214                         hmp->volume_to_remove = -1;
215                         hammer_rel_volume(volume, 0);
216                         goto end;
217                 }
218         }
219
220         /*
221          * Sync filesystem
222          */
223         hammer_flush_dirty(hmp, 30);
224
225         hammer_sync_lock_sh(trans);
226         hammer_lock_ex(&hmp->blkmap_lock);
227
228         /*
229          * We use stat later to update rootvol's big-block stats
230          */
231         error = hammer_free_freemap(trans, volume, &stat);
232         if (error) {
233                 kprintf("Failed to free volume: ");
234                 if (error == EBUSY)
235                         kprintf("Volume %d not empty\n", volume->vol_no);
236                 else
237                         kprintf("%d\n", error);
238                 hmp->volume_to_remove = -1;
239                 hammer_rel_volume(volume, 0);
240                 goto end1;
241         }
242
243         hmp->volume_to_remove = -1;
244         hammer_rel_volume(volume, 0);
245
246         /*
247          * Unload buffers
248          */
249         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
250                 hammer_unload_buffer, volume);
251
252         bzero(&ondisk, sizeof(ondisk));
253         error = hammer_unload_volume(volume, &ondisk);
254         if (error == -1) {
255                 kprintf("Failed to unload volume\n");
256                 goto end1;
257         }
258
259         --hmp->nvolumes;
260         error = hammer_update_volumes_header(trans, &stat);
261         KKASSERT(error == 0);
262
263 end1:
264         hammer_unlock(&hmp->blkmap_lock);
265         hammer_sync_unlock(trans);
266
267 end:
268         hammer_unlock(&hmp->volume_lock);
269         if (error)
270                 kprintf("An error occurred: %d\n", error);
271         return (error);
272 }
273
274
275 int
276 hammer_ioc_volume_list(hammer_transaction_t trans, hammer_inode_t ip,
277     struct hammer_ioc_volume_list *ioc)
278 {
279         struct hammer_mount *hmp = trans->hmp;
280         hammer_volume_t volume;
281         int error = 0;
282         int i, len, cnt = 0;
283
284         if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
285                 kprintf("Another volume operation is in progress!\n");
286                 return (EAGAIN);
287         }
288
289         HAMMER_VOLUME_NUMBER_FOREACH(hmp, i) {
290                 if (cnt >= ioc->nvols)
291                         break;
292                 volume = hammer_get_volume(hmp, i, &error);
293                 KKASSERT(volume != NULL && error == 0);
294
295                 len = strlen(volume->vol_name) + 1;
296                 KKASSERT(len <= MAXPATHLEN);
297
298                 error = copyout(volume->vol_name, ioc->vols[cnt].device_name,
299                                 len);
300                 hammer_rel_volume(volume, 0);
301                 if (error)
302                         goto end;
303                 cnt++;
304         }
305         ioc->nvols = cnt;
306
307 end:
308         hammer_unlock(&hmp->volume_lock);
309         return (error);
310 }
311
312 static
313 int
314 hammer_do_reblock(hammer_transaction_t trans, hammer_inode_t ip)
315 {
316         int error;
317
318         struct hammer_ioc_reblock reblock;
319         bzero(&reblock, sizeof(reblock));
320
321         reblock.key_beg.localization = HAMMER_MIN_LOCALIZATION;
322         reblock.key_beg.obj_id = HAMMER_MIN_OBJID;
323         reblock.key_end.localization = HAMMER_MAX_LOCALIZATION;
324         reblock.key_end.obj_id = HAMMER_MAX_OBJID;
325         reblock.head.flags = HAMMER_IOC_DO_FLAGS;
326         reblock.free_level = 0;
327         reblock.allpfs = 1;
328
329         kprintf("reblock started\n");
330         error = hammer_ioc_reblock(trans, ip, &reblock);
331
332         if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) {
333                 error = EINTR;
334         }
335
336         if (error) {
337                 if (error == EINTR) {
338                         kprintf("reblock was interrupted\n");
339                 } else {
340                         kprintf("reblock failed: %d\n", error);
341                 }
342                 return(error);
343         }
344
345         return(0);
346 }
347
348 /*
349  * Iterate over all usable L1 entries of the volume and
350  * the corresponding L2 entries.
351  */
352 static int
353 hammer_iterate_l1l2_entries(hammer_transaction_t trans, hammer_volume_t volume,
354         int (*callback)(hammer_transaction_t, hammer_volume_t, hammer_buffer_t*,
355                 struct hammer_blockmap_layer1*, struct hammer_blockmap_layer2*,
356                 hammer_off_t, hammer_off_t, void*),
357         void *data)
358 {
359         struct hammer_mount *hmp = trans->hmp;
360         hammer_blockmap_t freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
361         int error = 0;
362         hammer_off_t phys_off;
363         hammer_off_t block_off;
364         hammer_off_t layer1_off;
365         hammer_off_t layer2_off;
366         hammer_off_t aligned_buf_end_off;
367         hammer_off_t aligned_vol_end_off;
368         struct hammer_blockmap_layer1 *layer1;
369         struct hammer_blockmap_layer2 *layer2;
370         hammer_buffer_t buffer1 = NULL;
371         hammer_buffer_t buffer2 = NULL;
372
373         /*
374          * Calculate the usable size of the volume, which
375          * must be aligned at a big-block (8 MB) boundary.
376          */
377         aligned_buf_end_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
378                 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
379                 & ~HAMMER_BIGBLOCK_MASK64);
380         aligned_vol_end_off = (aligned_buf_end_off + HAMMER_BLOCKMAP_LAYER2_MASK)
381                 & ~HAMMER_BLOCKMAP_LAYER2_MASK;
382
383         /*
384          * Iterate the volume's address space in chunks of 4 TB, where each
385          * chunk consists of at least one physically available 8 MB big-block.
386          *
387          * For each chunk we need one L1 entry and one L2 big-block.
388          * We use the first big-block of each chunk as L2 block.
389          */
390         for (phys_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
391              phys_off < aligned_vol_end_off;
392              phys_off += HAMMER_BLOCKMAP_LAYER2) {
393                 for (block_off = 0;
394                      block_off < HAMMER_BLOCKMAP_LAYER2;
395                      block_off += HAMMER_BIGBLOCK_SIZE) {
396                         layer2_off = phys_off +
397                                 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_off);
398                         layer2 = hammer_bread(hmp, layer2_off, &error, &buffer2);
399                         if (error)
400                                 goto end;
401
402                         error = callback(trans, volume, &buffer2, NULL,
403                                          layer2, phys_off, block_off, data);
404                         if (error)
405                                 goto end;
406                 }
407
408                 layer1_off = freemap->phys_offset +
409                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_off);
410                 layer1 = hammer_bread(hmp, layer1_off, &error, &buffer1);
411                 if (error)
412                         goto end;
413
414                 error = callback(trans, volume, &buffer1, layer1, NULL,
415                                  phys_off, 0, data);
416                 if (error)
417                         goto end;
418         }
419
420 end:
421         if (buffer1)
422                 hammer_rel_buffer(buffer1, 0);
423         if (buffer2)
424                 hammer_rel_buffer(buffer2, 0);
425
426         return error;
427 }
428
429
430 static int
431 format_callback(hammer_transaction_t trans, hammer_volume_t volume,
432         hammer_buffer_t *bufferp,
433         struct hammer_blockmap_layer1 *layer1,
434         struct hammer_blockmap_layer2 *layer2,
435         hammer_off_t phys_off,
436         hammer_off_t block_off,
437         void *data)
438 {
439         struct bigblock_stat *stat = (struct bigblock_stat*)data;
440
441         /*
442          * Calculate the usable size of the volume, which must be aligned
443          * at a big-block (8 MB) boundary.
444          */
445         hammer_off_t aligned_buf_end_off;
446         aligned_buf_end_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
447                 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
448                 & ~HAMMER_BIGBLOCK_MASK64);
449
450         if (layer1) {
451                 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
452
453                 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
454                 bzero(layer1, sizeof(*layer1));
455                 layer1->phys_offset = phys_off;
456                 layer1->blocks_free = stat->counter;
457                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
458                 hammer_modify_buffer_done(*bufferp);
459                 stat->counter = 0; /* reset */
460         } else if (layer2) {
461                 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
462                 bzero(layer2, sizeof(*layer2));
463
464                 if (block_off == 0) {
465                         /*
466                          * The first entry represents the L2 big-block itself.
467                          * Note that the first entry represents the L1 big-block
468                          * and the second entry represents the L2 big-block for
469                          * root volume, but this function assumes the volume is
470                          * non-root given that we can't add a new root volume.
471                          */
472                         KKASSERT(trans->rootvol && trans->rootvol != volume);
473                         layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
474                         layer2->append_off = HAMMER_BIGBLOCK_SIZE;
475                         layer2->bytes_free = 0;
476                 } else if (phys_off + block_off < aligned_buf_end_off) {
477                         /*
478                          * Available big-block
479                          */
480                         layer2->zone = 0;
481                         layer2->append_off = 0;
482                         layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
483                         ++stat->total_bigblocks;
484                         ++stat->total_free_bigblocks;
485                         ++stat->counter;
486                 } else {
487                         /*
488                          * Big-block outside of physically available
489                          * space
490                          */
491                         layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
492                         layer2->append_off = HAMMER_BIGBLOCK_SIZE;
493                         layer2->bytes_free = 0;
494                 }
495
496                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
497                 hammer_modify_buffer_done(*bufferp);
498         } else {
499                 KKASSERT(0);
500         }
501
502         return 0;
503 }
504
505 static int
506 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume,
507         struct bigblock_stat *stat)
508 {
509         stat->total_bigblocks = 0;
510         stat->total_free_bigblocks = 0;
511         stat->counter = 0;
512         return hammer_iterate_l1l2_entries(trans, volume, format_callback, stat);
513 }
514
515 static int
516 free_callback(hammer_transaction_t trans, hammer_volume_t volume __unused,
517         hammer_buffer_t *bufferp,
518         struct hammer_blockmap_layer1 *layer1,
519         struct hammer_blockmap_layer2 *layer2,
520         hammer_off_t phys_off,
521         hammer_off_t block_off __unused,
522         void *data)
523 {
524         struct bigblock_stat *stat = (struct bigblock_stat*)data;
525
526         if (layer1) {
527                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
528                         /*
529                          * This layer1 entry is already free.
530                          */
531                         return 0;
532                 }
533
534                 KKASSERT((int)HAMMER_VOL_DECODE(layer1->phys_offset) ==
535                         trans->hmp->volume_to_remove);
536
537                 /*
538                  * Free the L1 entry
539                  */
540                 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
541                 bzero(layer1, sizeof(*layer1));
542                 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
543                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
544                 hammer_modify_buffer_done(*bufferp);
545
546                 return 0;
547         } else if (layer2) {
548                 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
549                         return 0;
550                 }
551
552                 if (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX) {
553                         return 0;
554                 }
555
556                 if (layer2->append_off == 0 &&
557                     layer2->bytes_free == HAMMER_BIGBLOCK_SIZE) {
558                         --stat->total_bigblocks;
559                         --stat->total_free_bigblocks;
560                         return 0;
561                 }
562
563                 /*
564                  * We found a layer2 entry that is not empty!
565                  */
566                 return EBUSY;
567         } else {
568                 KKASSERT(0);
569         }
570
571         return EINVAL;
572 }
573
574 /*
575  * Non-zero return value means we can't free the volume.
576  */
577 static int
578 test_free_callback(hammer_transaction_t trans, hammer_volume_t volume __unused,
579         hammer_buffer_t *bufferp,
580         struct hammer_blockmap_layer1 *layer1,
581         struct hammer_blockmap_layer2 *layer2,
582         hammer_off_t phys_off,
583         hammer_off_t block_off __unused,
584         void *data)
585 {
586         if (layer2 == NULL) {
587                 return(0);  /* only layer2 needs to be tested */
588         }
589
590         if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
591                 return(0);  /* beyond physically available space */
592         }
593         if (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX) {
594                 return(0);  /* big-block for layer1/2 */
595         }
596         if (layer2->append_off == 0 &&
597             layer2->bytes_free == HAMMER_BIGBLOCK_SIZE) {
598                 return(0);  /* big-block is 0% used */
599         }
600
601         return(EBUSY);  /* big-block has data */
602 }
603
604 static int
605 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume,
606         struct bigblock_stat *stat)
607 {
608         int error;
609
610         error = hammer_test_free_freemap(trans, volume);
611         if (error)
612                 return error;  /* not ready to free */
613
614         stat->total_bigblocks = 0;
615         stat->total_free_bigblocks = 0;
616         stat->counter = 0;
617         return hammer_iterate_l1l2_entries(trans, volume, free_callback, stat);
618 }
619
620 static int
621 hammer_test_free_freemap(hammer_transaction_t trans, hammer_volume_t volume)
622 {
623         return hammer_iterate_l1l2_entries(trans, volume, test_free_callback, NULL);
624 }
625
626 static int
627 hammer_format_volume_header(struct hammer_mount *hmp,
628         struct hammer_volume_ondisk *ondisk,
629         const char *vol_name, int vol_no, int vol_count,
630         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
631 {
632         int64_t vol_alloc;
633
634         KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
635
636         bzero(ondisk, sizeof(struct hammer_volume_ondisk));
637         ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
638         ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
639         ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
640         ondisk->vol_fsid = hmp->fsid;
641         ondisk->vol_rootvol = hmp->rootvol->vol_no;
642         ondisk->vol_no = vol_no;
643         ondisk->vol_count = vol_count;
644         ondisk->vol_version = hmp->version;
645
646         /*
647          * Reserve space for (future) header junk, setup our poor-man's
648          * big-block allocator.
649          */
650         vol_alloc = HAMMER_BUFSIZE * 16;
651         ondisk->vol_bot_beg = vol_alloc;
652         vol_alloc += boot_area_size;
653         ondisk->vol_mem_beg = vol_alloc;
654         vol_alloc += mem_area_size;
655
656         /*
657          * The remaining area is the zone 2 buffer allocation area.  These
658          * buffers
659          */
660         ondisk->vol_buf_beg = vol_alloc;
661         ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
662
663         if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
664                 kprintf("volume %d %s is too small to hold the volume header\n",
665                      ondisk->vol_no, ondisk->vol_name);
666                 return(EFTYPE);
667         }
668
669         ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
670                               HAMMER_BUFSIZE;
671         ondisk->vol_blocksize = HAMMER_BUFSIZE;
672         return(0);
673 }
674
675 static int
676 hammer_update_volumes_header(hammer_transaction_t trans,
677         struct bigblock_stat *stat)
678 {
679         struct hammer_mount *hmp = trans->hmp;
680         struct mount *mp = hmp->mp;
681         hammer_volume_t volume;
682         int vol_no;
683         int error = 0;
684
685         /*
686          * Set each volume's new value of the vol_count field.
687          */
688         HAMMER_VOLUME_NUMBER_FOREACH(hmp, vol_no) {
689                 volume = hammer_get_volume(hmp, vol_no, &error);
690                 KKASSERT(volume != NULL && error == 0);
691                 hammer_modify_volume_field(trans, volume, vol_count);
692                 volume->ondisk->vol_count = hmp->nvolumes;
693                 hammer_modify_volume_done(volume);
694
695                 /*
696                  * Only changes to the header of the root volume
697                  * are automatically flushed to disk. For all
698                  * other volumes that we modify we do it here.
699                  *
700                  * No interlock is needed, volume buffers are not
701                  * messed with by bioops.
702                  */
703                 if (volume != trans->rootvol && volume->io.modified) {
704                         hammer_crc_set_volume(volume->ondisk);
705                         hammer_io_flush(&volume->io, 0);
706                 }
707
708                 hammer_rel_volume(volume, 0);
709         }
710
711         /*
712          * Update the total number of big-blocks.
713          */
714         hammer_modify_volume_field(trans, trans->rootvol, vol0_stat_bigblocks);
715         trans->rootvol->ondisk->vol0_stat_bigblocks += stat->total_bigblocks;
716         hammer_modify_volume_done(trans->rootvol);
717
718         /*
719          * Big-block count changed so recompute the total number of blocks.
720          */
721         mp->mnt_stat.f_blocks = trans->rootvol->ondisk->vol0_stat_bigblocks *
722                                 HAMMER_BUFFERS_PER_BIGBLOCK;
723         mp->mnt_vstat.f_blocks = trans->rootvol->ondisk->vol0_stat_bigblocks *
724                                 HAMMER_BUFFERS_PER_BIGBLOCK;
725
726         /*
727          * Update the total number of free big-blocks.
728          */
729         hammer_modify_volume_field(trans, trans->rootvol,
730                 vol0_stat_freebigblocks);
731         trans->rootvol->ondisk->vol0_stat_freebigblocks +=
732                 stat->total_free_bigblocks;
733         hammer_modify_volume_done(trans->rootvol);
734
735         /*
736          * Update the copy in hmp.
737          */
738         hmp->copy_stat_freebigblocks =
739                 trans->rootvol->ondisk->vol0_stat_freebigblocks;
740
741         return(error);
742 }