73d6cdbe2fa023c2e67835c5af0a6dac1459711e
[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
39 static int
40 hammer_format_volume_header(struct hammer_mount *hmp,
41         struct hammer_volume_ondisk *ondisk,
42         const char *vol_name, int vol_no, int vol_count,
43         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size);
44
45 static int
46 hammer_update_volumes_header(hammer_transaction_t trans,
47         int64_t total_bigblocks, int64_t empty_bigblocks);
48
49 static int
50 hammer_do_reblock(hammer_transaction_t trans, hammer_inode_t ip);
51
52 static int
53 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume);
54
55 static int
56 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume);
57
58 static int
59 hammer_count_bigblocks(hammer_mount_t hmp, hammer_volume_t volume,
60         int64_t *total_bigblocks, int64_t *empty_bigblocks);
61
62 int
63 hammer_ioc_volume_add(hammer_transaction_t trans, hammer_inode_t ip,
64                 struct hammer_ioc_volume *ioc)
65 {
66         struct hammer_mount *hmp = trans->hmp;
67         struct mount *mp = hmp->mp;
68         struct hammer_volume_ondisk ondisk;
69         hammer_volume_t volume;
70         int64_t total_bigblocks, empty_bigblocks;
71         int free_vol_no = 0;
72         int error;
73
74         if (mp->mnt_flag & MNT_RDONLY) {
75                 hmkprintf(hmp, "Cannot add volume to read-only HAMMER filesystem\n");
76                 return (EINVAL);
77         }
78
79         if (hmp->nvolumes >= HAMMER_MAX_VOLUMES) {
80                 hmkprintf(hmp, "Max number of HAMMER volumes exceeded\n");
81                 return (EINVAL);
82         }
83
84         if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
85                 hmkprintf(hmp, "Another volume operation is in progress!\n");
86                 return (EAGAIN);
87         }
88
89         /*
90          * Find an unused volume number.
91          */
92         while (free_vol_no < HAMMER_MAX_VOLUMES &&
93                 HAMMER_VOLUME_NUMBER_IS_SET(hmp, free_vol_no)) {
94                 ++free_vol_no;
95         }
96         if (free_vol_no >= HAMMER_MAX_VOLUMES) {
97                 hmkprintf(hmp, "Max number of HAMMER volumes exceeded\n");
98                 error = EINVAL;
99                 goto end;
100         }
101
102         error = hammer_format_volume_header(
103                 hmp,
104                 &ondisk,
105                 hmp->rootvol->ondisk->vol_name,
106                 free_vol_no,
107                 hmp->nvolumes+1,
108                 ioc->vol_size,
109                 ioc->boot_area_size,
110                 ioc->mem_area_size);
111         if (error)
112                 goto end;
113
114         error = hammer_install_volume(hmp, ioc->device_name, NULL, &ondisk);
115         if (error)
116                 goto end;
117
118         hammer_sync_lock_sh(trans);
119         hammer_lock_ex(&hmp->blkmap_lock);
120
121         volume = hammer_get_volume(hmp, free_vol_no, &error);
122         KKASSERT(volume != NULL && error == 0);
123
124         error = hammer_format_freemap(trans, volume);
125         KKASSERT(error == 0);
126
127         error = hammer_count_bigblocks(hmp, volume,
128                         &total_bigblocks, &empty_bigblocks);
129         KKASSERT(error == 0);
130         KKASSERT(total_bigblocks == empty_bigblocks);
131
132         hammer_rel_volume(volume, 0);
133
134         ++hmp->nvolumes;
135         error = hammer_update_volumes_header(trans,
136                         total_bigblocks, empty_bigblocks);
137         KKASSERT(error == 0);
138
139         hammer_unlock(&hmp->blkmap_lock);
140         hammer_sync_unlock(trans);
141
142         KKASSERT(error == 0);
143 end:
144         hammer_unlock(&hmp->volume_lock);
145         if (error)
146                 hmkprintf(hmp, "An error occurred: %d\n", error);
147         return (error);
148 }
149
150
151 /*
152  * Remove a volume.
153  */
154 int
155 hammer_ioc_volume_del(hammer_transaction_t trans, hammer_inode_t ip,
156                 struct hammer_ioc_volume *ioc)
157 {
158         struct hammer_mount *hmp = trans->hmp;
159         struct mount *mp = hmp->mp;
160         struct hammer_volume_ondisk ondisk;
161         hammer_volume_t volume;
162         int64_t total_bigblocks, empty_bigblocks;
163         int vol_no;
164         int error = 0;
165
166         if (mp->mnt_flag & MNT_RDONLY) {
167                 hmkprintf(hmp, "Cannot del volume from read-only HAMMER filesystem\n");
168                 return (EINVAL);
169         }
170
171         if (hmp->nvolumes <= 1) {
172                 hmkprintf(hmp, "No HAMMER volume to delete\n");
173                 return (EINVAL);
174         }
175
176         if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
177                 hmkprintf(hmp, "Another volume operation is in progress!\n");
178                 return (EAGAIN);
179         }
180
181         /*
182          * find volume by volname
183          */
184         volume = NULL;
185         HAMMER_VOLUME_NUMBER_FOREACH(hmp, vol_no) {
186                 volume = hammer_get_volume(hmp, vol_no, &error);
187                 KKASSERT(volume != NULL && error == 0);
188                 if (strcmp(volume->vol_name, ioc->device_name) == 0) {
189                         break;
190                 }
191                 hammer_rel_volume(volume, 0);
192                 volume = NULL;
193         }
194
195         if (volume == NULL) {
196                 hmkprintf(hmp, "Couldn't find volume\n");
197                 error = EINVAL;
198                 goto end;
199         }
200
201         if (volume == trans->rootvol) {
202                 hmkprintf(hmp, "Cannot remove root-volume\n");
203                 hammer_rel_volume(volume, 0);
204                 error = EINVAL;
205                 goto end;
206         }
207
208         /*
209          * Reblock filesystem if the volume is not empty
210          */
211         hmp->volume_to_remove = volume->vol_no;
212
213         error = hammer_count_bigblocks(hmp, volume,
214                         &total_bigblocks, &empty_bigblocks);
215         KKASSERT(error == 0);
216
217         if (total_bigblocks == empty_bigblocks) {
218                 hmkprintf(hmp, "%s is already empty\n", volume->vol_name);
219         } else {
220                 error = hammer_do_reblock(trans, ip);
221                 if (error) {
222                         hmp->volume_to_remove = -1;
223                         hammer_rel_volume(volume, 0);
224                         goto end;
225                 }
226         }
227
228         /*
229          * Sync filesystem
230          */
231         hammer_flush_dirty(hmp, 30);
232
233         hammer_sync_lock_sh(trans);
234         hammer_lock_ex(&hmp->blkmap_lock);
235
236         error = hammer_count_bigblocks(hmp, volume,
237                         &total_bigblocks, &empty_bigblocks);
238         KKASSERT(error == 0);
239
240         error = hammer_free_freemap(trans, volume);
241         if (error) {
242                 hmkprintf(hmp, "Failed to free volume: ");
243                 if (error == EBUSY)
244                         kprintf("Volume %d not empty\n", volume->vol_no);
245                 else
246                         kprintf("%d\n", error);
247                 hmp->volume_to_remove = -1;
248                 hammer_rel_volume(volume, 0);
249                 goto end1;
250         }
251         hammer_rel_volume(volume, 0);
252
253         /*
254          * XXX: Temporary solution for
255          * http://lists.dragonflybsd.org/pipermail/kernel/2015-August/175027.html
256          */
257         hammer_unlock(&hmp->blkmap_lock);
258         hammer_sync_unlock(trans);
259         hammer_flusher_sync(hmp); /* 1 */
260         hammer_flusher_sync(hmp); /* 2 */
261         hammer_flusher_sync(hmp); /* 3 */
262         hammer_sync_lock_sh(trans);
263         hammer_lock_ex(&hmp->blkmap_lock);
264
265         /*
266          * Unload buffers
267          */
268         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
269                 hammer_unload_buffer, volume);
270
271         bzero(&ondisk, sizeof(ondisk));
272         error = hammer_unload_volume(volume, &ondisk);
273         if (error == -1) {
274                 hmkprintf(hmp, "Failed to unload volume\n");
275                 goto end1;
276         }
277
278         --hmp->nvolumes;
279         error = hammer_update_volumes_header(trans,
280                         -total_bigblocks, -empty_bigblocks);
281         KKASSERT(error == 0);
282         hmp->volume_to_remove = -1;
283
284 end1:
285         hammer_unlock(&hmp->blkmap_lock);
286         hammer_sync_unlock(trans);
287
288 end:
289         hammer_unlock(&hmp->volume_lock);
290         if (error)
291                 hmkprintf(hmp, "An error occurred: %d\n", error);
292         return (error);
293 }
294
295
296 int
297 hammer_ioc_volume_list(hammer_transaction_t trans, hammer_inode_t ip,
298     struct hammer_ioc_volume_list *ioc)
299 {
300         struct hammer_mount *hmp = trans->hmp;
301         hammer_volume_t volume;
302         int error = 0;
303         int i, len, cnt = 0;
304
305         if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
306                 hmkprintf(hmp, "Another volume operation is in progress!\n");
307                 return (EAGAIN);
308         }
309
310         HAMMER_VOLUME_NUMBER_FOREACH(hmp, i) {
311                 if (cnt >= ioc->nvols)
312                         break;
313                 volume = hammer_get_volume(hmp, i, &error);
314                 KKASSERT(volume != NULL && error == 0);
315
316                 len = strlen(volume->vol_name) + 1;
317                 KKASSERT(len <= MAXPATHLEN);
318
319                 error = copyout(volume->vol_name, ioc->vols[cnt].device_name,
320                                 len);
321                 hammer_rel_volume(volume, 0);
322                 if (error)
323                         goto end;
324                 cnt++;
325         }
326         ioc->nvols = cnt;
327
328 end:
329         hammer_unlock(&hmp->volume_lock);
330         return (error);
331 }
332
333 static
334 int
335 hammer_do_reblock(hammer_transaction_t trans, hammer_inode_t ip)
336 {
337         struct hammer_mount *hmp = trans->hmp;
338         int error;
339         int vol_no;
340
341         struct hammer_ioc_reblock reblock;
342         bzero(&reblock, sizeof(reblock));
343
344         vol_no = trans->hmp->volume_to_remove;
345         KKASSERT(vol_no != -1);
346
347         reblock.key_beg.localization = HAMMER_MIN_LOCALIZATION;
348         reblock.key_beg.obj_id = HAMMER_MIN_OBJID;
349         reblock.key_end.localization = HAMMER_MAX_LOCALIZATION;
350         reblock.key_end.obj_id = HAMMER_MAX_OBJID;
351         reblock.head.flags = HAMMER_IOC_DO_FLAGS;
352         reblock.free_level = 0; /* reblock all big-blocks */
353         reblock.allpfs = 1;     /* reblock all PFS */
354         reblock.vol_no = vol_no;
355
356         hmkprintf(hmp, "reblock started\n");
357         error = hammer_ioc_reblock(trans, ip, &reblock);
358
359         if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) {
360                 error = EINTR;
361         }
362
363         if (error) {
364                 if (error == EINTR) {
365                         hmkprintf(hmp, "reblock was interrupted\n");
366                 } else {
367                         hmkprintf(hmp, "reblock failed: %d\n", error);
368                 }
369                 return(error);
370         }
371
372         return(0);
373 }
374
375 static int
376 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume)
377 {
378         struct hammer_mount *hmp = trans->hmp;
379         struct hammer_volume_ondisk *ondisk;
380         hammer_blockmap_t freemap;
381         hammer_off_t alloc_offset;
382         hammer_off_t phys_offset;
383         hammer_off_t block_offset;
384         hammer_off_t layer1_offset;
385         hammer_off_t layer2_offset;
386         hammer_off_t vol_free_end;
387         hammer_off_t aligned_vol_free_end;
388         struct hammer_blockmap_layer1 *layer1;
389         struct hammer_blockmap_layer2 *layer2;
390         hammer_buffer_t buffer1 = NULL;
391         hammer_buffer_t buffer2 = NULL;
392         int64_t vol_buf_size;
393         int64_t layer1_count = 0;
394         int error = 0;
395
396         KKASSERT(volume->vol_no != HAMMER_ROOT_VOLNO);
397
398         ondisk = volume->ondisk;
399         vol_buf_size = ondisk->vol_buf_end - ondisk->vol_buf_beg;
400         vol_free_end = HAMMER_ENCODE_RAW_BUFFER(ondisk->vol_no,
401                         vol_buf_size & ~HAMMER_BIGBLOCK_MASK64);
402         aligned_vol_free_end = (vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
403                         & ~HAMMER_BLOCKMAP_LAYER2_MASK;
404
405         freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
406         alloc_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
407
408         hmkprintf(hmp, "Initialize freemap volume %d\n", volume->vol_no);
409
410         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
411              phys_offset < aligned_vol_free_end;
412              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
413                 layer1_offset = freemap->phys_offset +
414                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
415                 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
416                 if (error)
417                         goto end;
418                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
419                         hammer_modify_buffer(trans, buffer1, layer1, sizeof(*layer1));
420                         bzero(layer1, sizeof(*layer1));
421                         layer1->phys_offset = alloc_offset;
422                         layer1->blocks_free = 0;
423                         layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
424                         hammer_modify_buffer_done(buffer1);
425                         alloc_offset += HAMMER_BIGBLOCK_SIZE;
426                 }
427         }
428
429         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
430              phys_offset < aligned_vol_free_end;
431              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
432                 layer1_count = 0;
433                 layer1_offset = freemap->phys_offset +
434                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
435                 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
436                 if (error)
437                         goto end;
438                 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
439
440                 for (block_offset = 0;
441                      block_offset < HAMMER_BLOCKMAP_LAYER2;
442                      block_offset += HAMMER_BIGBLOCK_SIZE) {
443                         layer2_offset = layer1->phys_offset +
444                                         HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
445                         layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
446                         if (error)
447                                 goto end;
448
449                         hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
450                         bzero(layer2, sizeof(*layer2));
451
452                         if (phys_offset + block_offset < alloc_offset) {
453                                 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
454                                 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
455                                 layer2->bytes_free = 0;
456                         } else if (phys_offset + block_offset < vol_free_end) {
457                                 layer2->zone = 0;
458                                 layer2->append_off = 0;
459                                 layer2->bytes_free = HAMMER_BIGBLOCK_SIZE;
460                                 ++layer1_count;
461                         } else {
462                                 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
463                                 layer2->append_off = HAMMER_BIGBLOCK_SIZE;
464                                 layer2->bytes_free = 0;
465                         }
466
467                         layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
468                         hammer_modify_buffer_done(buffer2);
469                 }
470
471                 hammer_modify_buffer(trans, buffer1, layer1, sizeof(*layer1));
472                 layer1->blocks_free += layer1_count;
473                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
474                 hammer_modify_buffer_done(buffer1);
475         }
476
477 end:
478         if (buffer1)
479                 hammer_rel_buffer(buffer1, 0);
480         if (buffer2)
481                 hammer_rel_buffer(buffer2, 0);
482
483         return error;
484 }
485
486 static int
487 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume)
488 {
489         struct hammer_mount *hmp = trans->hmp;
490         struct hammer_volume_ondisk *ondisk;
491         hammer_blockmap_t freemap;
492         hammer_off_t phys_offset;
493         hammer_off_t block_offset;
494         hammer_off_t layer1_offset;
495         hammer_off_t layer2_offset;
496         hammer_off_t vol_free_end;
497         hammer_off_t aligned_vol_free_end;
498         struct hammer_blockmap_layer1 *layer1;
499         struct hammer_blockmap_layer2 *layer2;
500         hammer_buffer_t buffer1 = NULL;
501         hammer_buffer_t buffer2 = NULL;
502         int64_t vol_buf_size;
503         int64_t layer1_count = 0;
504         int error = 0;
505
506         KKASSERT(volume->vol_no != HAMMER_ROOT_VOLNO);
507
508         ondisk = volume->ondisk;
509         vol_buf_size = ondisk->vol_buf_end - ondisk->vol_buf_beg;
510         vol_free_end = HAMMER_ENCODE_RAW_BUFFER(ondisk->vol_no,
511                         vol_buf_size & ~HAMMER_BIGBLOCK_MASK64);
512         aligned_vol_free_end = (vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
513                         & ~HAMMER_BLOCKMAP_LAYER2_MASK;
514
515         freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
516
517         hmkprintf(hmp, "Free freemap volume %d\n", volume->vol_no);
518
519         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
520              phys_offset < aligned_vol_free_end;
521              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
522                 layer1_count = 0;
523                 layer1_offset = freemap->phys_offset +
524                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
525                 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
526                 if (error)
527                         goto end;
528                 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
529
530                 for (block_offset = 0;
531                      block_offset < HAMMER_BLOCKMAP_LAYER2;
532                      block_offset += HAMMER_BIGBLOCK_SIZE) {
533                         layer2_offset = layer1->phys_offset +
534                                         HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
535                         layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
536                         if (error)
537                                 goto end;
538
539                         switch (layer2->zone) {
540                         case HAMMER_ZONE_UNDO_INDEX:
541                                 KKASSERT(0);
542                         case HAMMER_ZONE_FREEMAP_INDEX:
543                         case HAMMER_ZONE_UNAVAIL_INDEX:
544                                 continue;
545                         default:
546                                 KKASSERT(phys_offset + block_offset < aligned_vol_free_end);
547                                 if (layer2->append_off == 0 &&
548                                     layer2->bytes_free == HAMMER_BIGBLOCK_SIZE)
549                                         continue;
550                                 break;
551                         }
552                         return EBUSY;  /* Not empty */
553                 }
554         }
555
556         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no, 0);
557              phys_offset < aligned_vol_free_end;
558              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
559                 layer1_count = 0;
560                 layer1_offset = freemap->phys_offset +
561                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
562                 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
563                 if (error)
564                         goto end;
565                 KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
566
567                 for (block_offset = 0;
568                      block_offset < HAMMER_BLOCKMAP_LAYER2;
569                      block_offset += HAMMER_BIGBLOCK_SIZE) {
570                         layer2_offset = layer1->phys_offset +
571                                         HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
572                         layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
573                         if (error)
574                                 goto end;
575
576                         switch (layer2->zone) {
577                         case HAMMER_ZONE_UNDO_INDEX:
578                                 KKASSERT(0);
579                         default:
580                                 KKASSERT(phys_offset + block_offset < aligned_vol_free_end);
581                                 hammer_modify_buffer(trans, buffer2, layer2, sizeof(*layer2));
582                                 bzero(layer2, sizeof(*layer2));
583                                 hammer_modify_buffer_done(buffer2);
584                                 break;
585                         }
586                 }
587
588                 hammer_modify_buffer(trans, buffer1, layer1, sizeof(*layer1));
589                 bzero(layer1, sizeof(*layer1));
590                 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
591                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
592                 hammer_modify_buffer_done(buffer1);
593         }
594
595 end:
596         if (buffer1)
597                 hammer_rel_buffer(buffer1, 0);
598         if (buffer2)
599                 hammer_rel_buffer(buffer2, 0);
600
601         return error;
602 }
603
604 static int
605 hammer_format_volume_header(struct hammer_mount *hmp,
606         struct hammer_volume_ondisk *ondisk,
607         const char *vol_name, int vol_no, int vol_count,
608         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
609 {
610         int64_t vol_alloc;
611
612         KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
613
614         bzero(ondisk, sizeof(struct hammer_volume_ondisk));
615         ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
616         ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
617         ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
618         ondisk->vol_fsid = hmp->fsid;
619         ondisk->vol_rootvol = hmp->rootvol->vol_no;
620         ondisk->vol_no = vol_no;
621         ondisk->vol_count = vol_count;
622         ondisk->vol_version = hmp->version;
623
624         /*
625          * Reserve space for (future) header junk, copy volume relative
626          * offset from the existing root volume.
627          */
628         vol_alloc = hmp->rootvol->ondisk->vol_bot_beg;
629         ondisk->vol_bot_beg = vol_alloc;
630         vol_alloc += boot_area_size;
631         ondisk->vol_mem_beg = vol_alloc;
632         vol_alloc += mem_area_size;
633
634         /*
635          * The remaining area is the zone 2 buffer allocation area.
636          */
637         ondisk->vol_buf_beg = vol_alloc;
638         ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
639
640         if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
641                 hmkprintf(hmp, "volume %d %s is too small to hold the volume header\n",
642                      ondisk->vol_no, ondisk->vol_name);
643                 return(EFTYPE);
644         }
645
646         ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
647                               HAMMER_BUFSIZE;
648         ondisk->vol_blocksize = HAMMER_BUFSIZE;
649         return(0);
650 }
651
652 static int
653 hammer_update_volumes_header(hammer_transaction_t trans,
654         int64_t total_bigblocks, int64_t empty_bigblocks)
655 {
656         struct hammer_mount *hmp = trans->hmp;
657         struct mount *mp = hmp->mp;
658         hammer_volume_t volume;
659         int vol_no;
660         int error = 0;
661
662         /*
663          * Set each volume's new value of the vol_count field.
664          */
665         HAMMER_VOLUME_NUMBER_FOREACH(hmp, vol_no) {
666                 volume = hammer_get_volume(hmp, vol_no, &error);
667                 KKASSERT(volume != NULL && error == 0);
668                 hammer_modify_volume_field(trans, volume, vol_count);
669                 volume->ondisk->vol_count = hmp->nvolumes;
670                 hammer_modify_volume_done(volume);
671
672                 /*
673                  * Only changes to the header of the root volume
674                  * are automatically flushed to disk. For all
675                  * other volumes that we modify we do it here.
676                  *
677                  * No interlock is needed, volume buffers are not
678                  * messed with by bioops.
679                  */
680                 if (volume != trans->rootvol && volume->io.modified) {
681                         hammer_crc_set_volume(volume->ondisk);
682                         hammer_io_flush(&volume->io, 0);
683                 }
684
685                 hammer_rel_volume(volume, 0);
686         }
687
688         /*
689          * Update the total number of big-blocks.
690          */
691         hammer_modify_volume_field(trans, trans->rootvol, vol0_stat_bigblocks);
692         trans->rootvol->ondisk->vol0_stat_bigblocks += total_bigblocks;
693         hammer_modify_volume_done(trans->rootvol);
694
695         /*
696          * Big-block count changed so recompute the total number of blocks.
697          */
698         mp->mnt_stat.f_blocks = trans->rootvol->ondisk->vol0_stat_bigblocks *
699                                 HAMMER_BUFFERS_PER_BIGBLOCK;
700         mp->mnt_vstat.f_blocks = trans->rootvol->ondisk->vol0_stat_bigblocks *
701                                 HAMMER_BUFFERS_PER_BIGBLOCK;
702
703         /*
704          * Update the total number of free big-blocks.
705          */
706         hammer_modify_volume_field(trans, trans->rootvol,
707                 vol0_stat_freebigblocks);
708         trans->rootvol->ondisk->vol0_stat_freebigblocks += empty_bigblocks;
709         hammer_modify_volume_done(trans->rootvol);
710
711         /*
712          * Update the copy in hmp.
713          */
714         hmp->copy_stat_freebigblocks =
715                 trans->rootvol->ondisk->vol0_stat_freebigblocks;
716
717         return(error);
718 }
719
720 /*
721  * Count total big-blocks and empty big-blocks within the volume.
722  * The volume must be a non-root volume.
723  *
724  * Note that total big-blocks doesn't include big-blocks for layer2
725  * (and obviously layer1 and undomap).  This is requirement of the
726  * volume header and this function is to retrieve that information.
727  */
728 static int
729 hammer_count_bigblocks(hammer_mount_t hmp, hammer_volume_t volume,
730         int64_t *total_bigblocks, int64_t *empty_bigblocks)
731 {
732         struct hammer_volume_ondisk *ondisk;
733         hammer_blockmap_t freemap;
734         hammer_off_t phys_offset;
735         hammer_off_t block_offset;
736         hammer_off_t layer1_offset;
737         hammer_off_t layer2_offset;
738         hammer_off_t vol_free_end;
739         hammer_off_t aligned_vol_free_end;
740         struct hammer_blockmap_layer1 *layer1;
741         struct hammer_blockmap_layer2 *layer2;
742         hammer_buffer_t buffer1 = NULL;
743         hammer_buffer_t buffer2 = NULL;
744         int64_t vol_buf_size;
745         int64_t total = 0;
746         int64_t empty = 0;
747         int error = 0;
748
749         KKASSERT(volume->vol_no != HAMMER_ROOT_VOLNO);
750
751         ondisk = volume->ondisk;
752         vol_buf_size = ondisk->vol_buf_end - ondisk->vol_buf_beg;
753         vol_free_end = HAMMER_ENCODE_RAW_BUFFER(ondisk->vol_no,
754                         vol_buf_size & ~HAMMER_BIGBLOCK_MASK64);
755         aligned_vol_free_end = (vol_free_end + HAMMER_BLOCKMAP_LAYER2_MASK)
756                         & ~HAMMER_BLOCKMAP_LAYER2_MASK;
757
758         freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
759
760         for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
761              phys_offset < aligned_vol_free_end;
762              phys_offset += HAMMER_BLOCKMAP_LAYER2) {
763                 layer1_offset = freemap->phys_offset +
764                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
765                 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
766                 if (error)
767                         goto end;
768
769                 for (block_offset = 0;
770                      block_offset < HAMMER_BLOCKMAP_LAYER2;
771                      block_offset += HAMMER_BIGBLOCK_SIZE) {
772                         layer2_offset = layer1->phys_offset +
773                                         HAMMER_BLOCKMAP_LAYER2_OFFSET(block_offset);
774                         layer2 = hammer_bread(hmp, layer2_offset, &error, &buffer2);
775                         if (error)
776                                 goto end;
777
778                         switch (layer2->zone) {
779                         case HAMMER_ZONE_UNDO_INDEX:
780                                 KKASSERT(0);
781                         case HAMMER_ZONE_FREEMAP_INDEX:
782                         case HAMMER_ZONE_UNAVAIL_INDEX:
783                                 continue;
784                         default:
785                                 KKASSERT(phys_offset + block_offset < aligned_vol_free_end);
786                                 total++;
787                                 if (layer2->append_off == 0 &&
788                                     layer2->bytes_free == HAMMER_BIGBLOCK_SIZE)
789                                         empty++;
790                                 break;
791                         }
792                 }
793         }
794
795         hmkprintf(hmp, "big-blocks total=%jd empty=%jd\n", total, empty);
796         *total_bigblocks = total;
797         *empty_bigblocks = empty;
798 end:
799         if (buffer1)
800                 hammer_rel_buffer(buffer1, 0);
801         if (buffer2)
802                 hammer_rel_buffer(buffer2, 0);
803
804         return error;
805 }