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