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