HAMMER - Stabilize and refactor volume removal
[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 "hammer.h"
38 #include <sys/fcntl.h>
39 #include <sys/nlookup.h>
40 #include <sys/buf.h>
41
42 static int
43 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly);
44
45 static void
46 hammer_close_device(struct vnode **devvpp, int ronly);
47
48 static int
49 hammer_format_volume_header(struct hammer_mount *hmp, struct vnode *devvp,
50         const char *vol_name, int vol_no, int vol_count,
51         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size);
52
53 static int
54 hammer_clear_volume_header(struct vnode *devvp);
55
56 static uint64_t
57 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume);
58
59 static int
60 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume);
61
62
63 int
64 hammer_ioc_volume_add(hammer_transaction_t trans, hammer_inode_t ip,
65                 struct hammer_ioc_volume *ioc)
66 {
67         struct hammer_mount *hmp = trans->hmp;
68         struct mount *mp = hmp->mp;
69         hammer_volume_t volume;
70         int error;
71
72         if (mp->mnt_flag & MNT_RDONLY) {
73                 kprintf("Cannot add volume to read-only HAMMER filesystem\n");
74                 return (EINVAL);
75         }
76
77         if (hmp->nvolumes + 1 >= HAMMER_MAX_VOLUMES) {
78                 kprintf("Max number of HAMMER volumes exceeded\n");
79                 return (EINVAL);
80         }
81
82         /*
83          * Find an unused volume number.
84          */
85         int free_vol_no = 0;
86         while (free_vol_no < HAMMER_MAX_VOLUMES &&
87                RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, free_vol_no)) {
88                 ++free_vol_no;
89         }
90         if (free_vol_no >= HAMMER_MAX_VOLUMES) {
91                 kprintf("Max number of HAMMER volumes exceeded\n");
92                 return (EINVAL);
93         }
94
95         struct vnode *devvp = NULL;
96         error = hammer_setup_device(&devvp, ioc->device_name, 0);
97         if (error)
98                 goto end;
99         KKASSERT(devvp);
100         error = hammer_format_volume_header(
101                 hmp,
102                 devvp,
103                 hmp->rootvol->ondisk->vol_name,
104                 free_vol_no,
105                 hmp->nvolumes+1,
106                 ioc->vol_size,
107                 ioc->boot_area_size,
108                 ioc->mem_area_size);
109         hammer_close_device(&devvp, 0);
110         if (error)
111                 goto end;
112
113         error = hammer_install_volume(hmp, ioc->device_name, NULL);
114         if (error)
115                 goto end;
116
117         hammer_sync_lock_sh(trans);
118         hammer_lock_ex(&hmp->blkmap_lock);
119
120         ++hmp->nvolumes;
121
122         /*
123          * Set each volumes new value of the vol_count field.
124          */
125         for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
126                 volume = hammer_get_volume(hmp, vol_no, &error);
127                 if (volume == NULL && error == ENOENT) {
128                         /*
129                          * Skip unused volume numbers
130                          */
131                         error = 0;
132                         continue;
133                 }
134                 KKASSERT(volume != NULL && error == 0);
135                 hammer_modify_volume_field(trans, volume, vol_count);
136                 volume->ondisk->vol_count = hmp->nvolumes;
137                 hammer_modify_volume_done(volume);
138
139                 /*
140                  * Only changes to the header of the root volume
141                  * are automatically flushed to disk. For all
142                  * other volumes that we modify we do it here.
143                  */
144                 if (volume != trans->rootvol && volume->io.modified) {
145                         hammer_crc_set_volume(volume->ondisk);
146                         hammer_io_flush(&volume->io, 0);
147                 }
148
149                 hammer_rel_volume(volume, 0);
150         }
151
152         volume = hammer_get_volume(hmp, free_vol_no, &error);
153         KKASSERT(volume != NULL && error == 0);
154
155         uint64_t total_free_bigblocks =
156                 hammer_format_freemap(trans, volume);
157
158         /*
159          * Increase the total number of bigblocks
160          */
161         hammer_modify_volume_field(trans, trans->rootvol,
162                 vol0_stat_bigblocks);
163         trans->rootvol->ondisk->vol0_stat_bigblocks += total_free_bigblocks;
164         hammer_modify_volume_done(trans->rootvol);
165
166         /*
167          * Increase the number of free bigblocks
168          * (including the copy in hmp)
169          */
170         hammer_modify_volume_field(trans, trans->rootvol,
171                 vol0_stat_freebigblocks);
172         trans->rootvol->ondisk->vol0_stat_freebigblocks += total_free_bigblocks;
173         hmp->copy_stat_freebigblocks =
174                 trans->rootvol->ondisk->vol0_stat_freebigblocks;
175         hammer_modify_volume_done(trans->rootvol);
176
177         hammer_rel_volume(volume, 0);
178
179         hammer_unlock(&hmp->blkmap_lock);
180         hammer_sync_unlock(trans);
181
182 end:
183         if (error)
184                 kprintf("An error occurred: %d\n", error);
185         return (error);
186 }
187
188
189 /*
190  * Remove a volume.
191  */
192 int
193 hammer_ioc_volume_del(hammer_transaction_t trans, hammer_inode_t ip,
194                 struct hammer_ioc_volume *ioc)
195 {
196         struct hammer_mount *hmp = trans->hmp;
197         struct mount *mp = hmp->mp;
198         hammer_volume_t volume;
199         int error = 0;
200
201         if (mp->mnt_flag & MNT_RDONLY) {
202                 kprintf("Cannot del volume from read-only HAMMER filesystem\n");
203                 return (EINVAL);
204         }
205
206
207         volume = NULL;
208
209         /*
210          * find volume by volname
211          */
212         for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
213                 volume = hammer_get_volume(hmp, vol_no, &error);
214                 if (volume == NULL && error == ENOENT) {
215                         /*
216                          * Skip unused volume numbers
217                          */
218                         error = 0;
219                         continue;
220                 }
221                 KKASSERT(volume != NULL && error == 0);
222                 if (strcmp(volume->vol_name, ioc->device_name) == 0) {
223                         break;
224                 }
225                 hammer_rel_volume(volume, 0);
226                 volume = NULL;
227         }
228
229         if (volume == NULL) {
230                 kprintf("Couldn't find volume\n");
231                 return (EINVAL);
232         }
233
234         if (volume == trans->rootvol) {
235                 kprintf("Cannot remove root-volume\n");
236                 hammer_rel_volume(volume, 0);
237                 return (EINVAL);
238         }
239
240         /*
241          *
242          */
243
244         hmp->volume_to_remove = volume->vol_no;
245
246         struct hammer_ioc_reblock reblock;
247         bzero(&reblock, sizeof(reblock));
248
249         reblock.key_beg.localization = HAMMER_MIN_LOCALIZATION;
250         reblock.key_beg.obj_id = HAMMER_MIN_OBJID;
251         reblock.key_end.localization = HAMMER_MAX_LOCALIZATION;
252         reblock.key_end.obj_id = HAMMER_MAX_OBJID;
253         reblock.head.flags = HAMMER_IOC_DO_FLAGS;
254         reblock.free_level = 0;
255
256         error = hammer_ioc_reblock(trans, ip, &reblock);
257
258         if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) {
259                 error = EINTR;
260         }
261
262         if (error) {
263                 if (error == EINTR) {
264                         kprintf("reblock was interrupted\n");
265                 } else {
266                         kprintf("reblock failed: %d\n", error);
267                 }
268                 hmp->volume_to_remove = -1;
269                 hammer_rel_volume(volume, 0);
270                 return (error);
271         }
272
273         /*
274          * Sync filesystem
275          */
276         int count = 0;
277         while (hammer_flusher_haswork(hmp)) {
278                 hammer_flusher_sync(hmp);
279                 ++count;
280                 if (count >= 5) {
281                         if (count == 5)
282                                 kprintf("HAMMER: flushing.");
283                         else
284                                 kprintf(".");
285                         tsleep(&count, 0, "hmrufl", hz);
286                 }
287                 if (count == 30) {
288                         kprintf("giving up");
289                         break;
290                 }
291         }
292         kprintf("\n");
293
294         hammer_sync_lock_sh(trans);
295         hammer_lock_ex(&hmp->blkmap_lock);
296
297         error = hammer_free_freemap(trans, volume);
298         if (error) {
299                 kprintf("Failed to free volume. Volume not empty!\n");
300                 hmp->volume_to_remove = -1;
301                 hammer_rel_volume(volume, 0);
302                 hammer_unlock(&hmp->blkmap_lock);
303                 hammer_sync_unlock(trans);
304                 return (error);
305         }
306
307         hmp->volume_to_remove = -1;
308
309         hammer_rel_volume(volume, 0);
310
311         /*
312          * Unload buffers
313          */
314         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
315                 hammer_unload_buffer, volume);
316
317         error = hammer_unload_volume(volume, NULL);
318         if (error == -1) {
319                 kprintf("Failed to unload volume\n");
320                 hammer_unlock(&hmp->blkmap_lock);
321                 hammer_sync_unlock(trans);
322                 return (error);
323         }
324
325         volume = NULL;
326         --hmp->nvolumes;
327
328         /*
329          * Set each volume's new value of the vol_count field.
330          */
331         for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
332                 volume = hammer_get_volume(hmp, vol_no, &error);
333                 if (volume == NULL && error == ENOENT) {
334                         /*
335                          * Skip unused volume numbers
336                          */
337                         error = 0;
338                         continue;
339                 }
340
341                 KKASSERT(volume != NULL && error == 0);
342                 hammer_modify_volume_field(trans, volume, vol_count);
343                 volume->ondisk->vol_count = hmp->nvolumes;
344                 hammer_modify_volume_done(volume);
345
346                 /*
347                  * Only changes to the header of the root volume
348                  * are automatically flushed to disk. For all
349                  * other volumes that we modify we do it here.
350                  */
351                 if (volume != trans->rootvol && volume->io.modified) {
352                         hammer_crc_set_volume(volume->ondisk);
353                         hammer_io_flush(&volume->io, 0);
354                 }
355
356                 hammer_rel_volume(volume, 0);
357         }
358
359         hammer_unlock(&hmp->blkmap_lock);
360         hammer_sync_unlock(trans);
361
362         /*
363          * Erase the volume header of the removed device.
364          *
365          * This is to not accidentally mount the volume again.
366          */
367         struct vnode *devvp = NULL;
368         error = hammer_setup_device(&devvp, ioc->device_name, 0);
369         if (error) {
370                 kprintf("Failed to open device: %s\n", ioc->device_name);
371                 return error;
372         }
373         KKASSERT(devvp);
374         error = hammer_clear_volume_header(devvp);
375         if (error) {
376                 kprintf("Failed to clear volume header of device: %s\n",
377                         ioc->device_name);
378                 return error;
379         }
380         hammer_close_device(&devvp, 0);
381
382         return (0);
383 }
384
385
386 /*
387  * Iterate over all usable L1 entries of the volume and
388  * the corresponding L2 entries.
389  */
390 static int
391 hammer_iterate_l1l2_entries(hammer_transaction_t trans, hammer_volume_t volume,
392         int (*callback)(hammer_transaction_t, hammer_volume_t, hammer_buffer_t*,
393                 struct hammer_blockmap_layer1*, struct hammer_blockmap_layer2*,
394                 hammer_off_t, hammer_off_t, void*),
395         void *data)
396 {
397         struct hammer_mount *hmp = trans->hmp;
398         hammer_blockmap_t freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
399         hammer_buffer_t buffer = NULL;
400         int error = 0;
401
402         hammer_off_t phys_off;
403         hammer_off_t block_off;
404         hammer_off_t layer1_off;
405         hammer_off_t layer2_off;
406         hammer_off_t aligned_buf_end_off;
407         struct hammer_blockmap_layer1 *layer1;
408         struct hammer_blockmap_layer2 *layer2;
409
410         /*
411          * Calculate the usable size of the volume, which
412          * must be aligned at a bigblock (8 MB) boundary.
413          */
414         aligned_buf_end_off = (HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
415                 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
416                 & ~HAMMER_LARGEBLOCK_MASK64));
417
418         /*
419          * Iterate the volume's address space in chunks of 4 TB, where each
420          * chunk consists of at least one physically available 8 MB bigblock.
421          *
422          * For each chunk we need one L1 entry and one L2 bigblock.
423          * We use the first bigblock of each chunk as L2 block.
424          */
425         for (phys_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
426              phys_off < aligned_buf_end_off;
427              phys_off += HAMMER_BLOCKMAP_LAYER2) {
428                 for (block_off = 0;
429                      block_off < HAMMER_BLOCKMAP_LAYER2;
430                      block_off += HAMMER_LARGEBLOCK_SIZE) {
431                         layer2_off = phys_off +
432                                 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_off);
433                         layer2 = hammer_bread(hmp, layer2_off, &error, &buffer);
434                         if (error)
435                                 goto end;
436
437                         error = callback(trans, volume, &buffer, NULL,
438                                          layer2, phys_off, block_off, data);
439                         if (error)
440                                 goto end;
441                 }
442
443                 layer1_off = freemap->phys_offset +
444                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_off);
445                 layer1 = hammer_bread(hmp, layer1_off, &error, &buffer);
446                 if (error)
447                         goto end;
448
449                 error = callback(trans, volume, &buffer, layer1, NULL,
450                                  phys_off, 0, data);
451                 if (error)
452                         goto end;
453         }
454
455 end:
456         if (buffer) {
457                 hammer_rel_buffer(buffer, 0);
458                 buffer = NULL;
459         }
460
461         return error;
462 }
463
464 struct format_bigblock_stat {
465         uint64_t total_free_bigblocks;
466         uint64_t free_bigblocks;
467 };
468
469 static int
470 format_callback(hammer_transaction_t trans, hammer_volume_t volume,
471         hammer_buffer_t *bufferp,
472         struct hammer_blockmap_layer1 *layer1,
473         struct hammer_blockmap_layer2 *layer2,
474         hammer_off_t phys_off,
475         hammer_off_t block_off,
476         void *data)
477 {
478         struct format_bigblock_stat *stat = (struct format_bigblock_stat*)data;
479
480         /*
481          * Calculate the usable size of the volume, which must be aligned
482          * at a bigblock (8 MB) boundary.
483          */
484         hammer_off_t aligned_buf_end_off;
485         aligned_buf_end_off = (HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
486                 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
487                 & ~HAMMER_LARGEBLOCK_MASK64));
488
489         if (layer1) {
490                 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
491
492                 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
493                 bzero(layer1, sizeof(layer1));
494                 layer1->phys_offset = phys_off;
495                 layer1->blocks_free = stat->free_bigblocks;
496                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
497                 hammer_modify_buffer_done(*bufferp);
498
499                 stat->total_free_bigblocks += stat->free_bigblocks;
500                 stat->free_bigblocks = 0; /* reset */
501         } else if (layer2) {
502                 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
503                 bzero(layer2, sizeof(*layer2));
504
505                 if (block_off == 0) {
506                         /*
507                          * The first entry represents the L2 bigblock itself.
508                          */
509                         layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
510                         layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
511                         layer2->bytes_free = 0;
512                 } else if (phys_off + block_off < aligned_buf_end_off) {
513                         /*
514                          * Available bigblock
515                          */
516                         layer2->zone = 0;
517                         layer2->append_off = 0;
518                         layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
519                         ++stat->free_bigblocks;
520                 } else {
521                         /*
522                          * Bigblock outside of physically available
523                          * space
524                          */
525                         layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
526                         layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
527                         layer2->bytes_free = 0;
528                 }
529
530                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
531                 hammer_modify_buffer_done(*bufferp);
532         } else {
533                 KKASSERT(0);
534         }
535
536         return 0;
537 }
538
539 static uint64_t
540 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume)
541 {
542         int error = 0;
543
544         struct format_bigblock_stat stat;
545         stat.total_free_bigblocks = 0;
546         stat.free_bigblocks = 0;
547
548         error = hammer_iterate_l1l2_entries(trans, volume, format_callback,
549                                         (void*)&stat);
550         KKASSERT(error == 0);
551
552         return stat.total_free_bigblocks;
553 }
554
555 static int
556 free_callback(hammer_transaction_t trans, hammer_volume_t volume __unused,
557         hammer_buffer_t *bufferp,
558         struct hammer_blockmap_layer1 *layer1,
559         struct hammer_blockmap_layer2 *layer2,
560         hammer_off_t phys_off,
561         hammer_off_t block_off __unused,
562         void *data)
563 {
564         /*
565          * No modifications to ondisk structures
566          */
567         int testonly = (data != NULL);
568
569         if (layer1) {
570                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
571                         /*
572                          * This layer1 entry is already free.
573                          */
574                         return 0;
575                 }
576
577                 KKASSERT((int)HAMMER_VOL_DECODE(layer1->phys_offset) ==
578                         trans->hmp->volume_to_remove);
579
580                 if (testonly)
581                         return 0;
582
583                 /*
584                  * Free the L1 entry
585                  */
586                 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
587                 bzero(layer1, sizeof(layer1));
588                 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
589                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
590                 hammer_modify_buffer_done(*bufferp);
591
592                 return 0;
593         } else if (layer2) {
594                 if (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX ||
595                     layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX)
596                         return 0;
597
598                 if (layer2->append_off == 0 &&
599                     layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
600                         return 0;
601                 /*
602                  * We found a layer2 entry that is not empty!
603                  */
604                 return EBUSY;
605         } else {
606                 KKASSERT(0);
607         }
608
609         return EINVAL;
610 }
611
612 static int
613 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume)
614 {
615         int error;
616         error = hammer_iterate_l1l2_entries(trans, volume, free_callback,
617                                             (void*)1);
618         if (error)
619                 return error;
620
621         error = hammer_iterate_l1l2_entries(trans, volume, free_callback, NULL);
622         return error;
623 }
624
625 /************************************************************************
626  *                              MISC                                    *
627  ************************************************************************
628  */
629
630 static int
631 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly)
632 {
633         int error;
634         struct nlookupdata nd;
635
636         /*
637          * Get the device vnode
638          */
639         if (*devvpp == NULL) {
640                 error = nlookup_init(&nd, dev_path, UIO_SYSSPACE, NLC_FOLLOW);
641                 if (error == 0)
642                         error = nlookup(&nd);
643                 if (error == 0)
644                         error = cache_vref(&nd.nl_nch, nd.nl_cred, devvpp);
645                 nlookup_done(&nd);
646         } else {
647                 error = 0;
648         }
649
650         if (error == 0) {
651                 if (vn_isdisk(*devvpp, &error)) {
652                         error = vfs_mountedon(*devvpp);
653                 }
654         }
655         if (error == 0 && vcount(*devvpp) > 0)
656                 error = EBUSY;
657         if (error == 0) {
658                 vn_lock(*devvpp, LK_EXCLUSIVE | LK_RETRY);
659                 error = vinvalbuf(*devvpp, V_SAVE, 0, 0);
660                 if (error == 0) {
661                         error = VOP_OPEN(*devvpp,
662                                          (ronly ? FREAD : FREAD|FWRITE),
663                                          FSCRED, NULL);
664                 }
665                 vn_unlock(*devvpp);
666         }
667         if (error && *devvpp) {
668                 vrele(*devvpp);
669                 *devvpp = NULL;
670         }
671         return (error);
672 }
673
674 static void
675 hammer_close_device(struct vnode **devvpp, int ronly)
676 {
677         VOP_CLOSE(*devvpp, (ronly ? FREAD : FREAD|FWRITE));
678         if (*devvpp) {
679                 vinvalbuf(*devvpp, ronly ? 0 : V_SAVE, 0, 0);
680                 vrele(*devvpp);
681                 *devvpp = NULL;
682         }
683 }
684
685 static int
686 hammer_format_volume_header(struct hammer_mount *hmp, struct vnode *devvp,
687         const char *vol_name, int vol_no, int vol_count,
688         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
689 {
690         struct buf *bp = NULL;
691         struct hammer_volume_ondisk *ondisk;
692         int error;
693
694         /*
695          * Extract the volume number from the volume header and do various
696          * sanity checks.
697          */
698         KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
699         error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
700         if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
701                 goto late_failure;
702
703         ondisk = (struct hammer_volume_ondisk*) bp->b_data;
704
705         /*
706          * Note that we do NOT allow to use a device that contains
707          * a valid HAMMER signature. It has to be cleaned up with dd
708          * before.
709          */
710         if (ondisk->vol_signature == HAMMER_FSBUF_VOLUME) {
711                 kprintf("hammer_volume_add: Formatting of valid HAMMER volume "
712                         "%s denied. Erase with dd!\n", vol_name);
713                 error = EFTYPE;
714                 goto late_failure;
715         }
716
717         bzero(ondisk, sizeof(struct hammer_volume_ondisk));
718         ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
719         ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
720         ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
721         ondisk->vol_fsid = hmp->fsid;
722         ondisk->vol_rootvol = hmp->rootvol->vol_no;
723         ondisk->vol_no = vol_no;
724         ondisk->vol_count = vol_count;
725         ondisk->vol_version = hmp->version;
726
727         /*
728          * Reserve space for (future) header junk, setup our poor-man's
729          * bigblock allocator.
730          */
731         int64_t vol_alloc = HAMMER_BUFSIZE * 16;
732
733         ondisk->vol_bot_beg = vol_alloc;
734         vol_alloc += boot_area_size;
735         ondisk->vol_mem_beg = vol_alloc;
736         vol_alloc += mem_area_size;
737
738         /*
739          * The remaining area is the zone 2 buffer allocation area.  These
740          * buffers
741          */
742         ondisk->vol_buf_beg = vol_alloc;
743         ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
744
745         if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
746                 kprintf("volume %d %s is too small to hold the volume header",
747                      ondisk->vol_no, ondisk->vol_name);
748                 error = EFTYPE;
749                 goto late_failure;
750         }
751
752         ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
753                               HAMMER_BUFSIZE;
754         ondisk->vol_blocksize = HAMMER_BUFSIZE;
755
756         /*
757          * Write volume header to disk
758          */
759         error = bwrite(bp);
760         bp = NULL;
761
762 late_failure:
763         if (bp)
764                 brelse(bp);
765         return (error);
766 }
767
768 /*
769  * Invalidates the volume header. Used by volume-del.
770  */
771 static int
772 hammer_clear_volume_header(struct vnode *devvp)
773 {
774         struct buf *bp = NULL;
775         struct hammer_volume_ondisk *ondisk;
776         int error;
777
778         /*
779          * Extract the volume number from the volume header and do various
780          * sanity checks.
781          */
782         KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
783         error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
784         if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
785                 goto late_failure;
786
787         ondisk = (struct hammer_volume_ondisk*) bp->b_data;
788         bzero(ondisk, sizeof(struct hammer_volume_ondisk));
789
790         /*
791          * Write volume header to disk
792          */
793         error = bwrite(bp);
794         bp = NULL;
795
796 late_failure:
797         if (bp)
798                 brelse(bp);
799         return (error);
800 }