HAMMER - Implement volume-list command
[dragonfly.git] / sys / vfs / hammer / hammer_volume.c
CommitLineData
e27700cf
MN
1/*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
90ecab35
MN
5 * by Matthew Dillon <dillon@backplane.com> and
6 * Michael Neumann <mneumann@ntecs.de>
e27700cf
MN
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"
90ecab35
MN
38#include <sys/fcntl.h>
39#include <sys/nlookup.h>
40#include <sys/buf.h>
41
42static int
114d4836
MN
43hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly);
44
45static void
46hammer_close_device(struct vnode **devvpp, int ronly);
47
48static int
49hammer_format_volume_header(struct hammer_mount *hmp, struct vnode *devvp,
2c794fb2 50 const char *vol_name, int vol_no, int vol_count,
114d4836
MN
51 int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size);
52
c47d84e8
MN
53static int
54hammer_clear_volume_header(struct vnode *devvp);
55
16b533e8
MN
56struct bigblock_stat {
57 uint64_t total_bigblocks;
58 uint64_t total_free_bigblocks;
59 uint64_t counter;
60};
114d4836 61
865c9609 62static int
16b533e8
MN
63hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume,
64 struct bigblock_stat *stat);
114d4836 65
16b533e8
MN
66static int
67hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume,
68 struct bigblock_stat *stat);
e27700cf
MN
69
70int
d121f61c 71hammer_ioc_volume_add(hammer_transaction_t trans, hammer_inode_t ip,
865c9609 72 struct hammer_ioc_volume *ioc)
e27700cf 73{
90ecab35
MN
74 struct hammer_mount *hmp = trans->hmp;
75 struct mount *mp = hmp->mp;
114d4836 76 hammer_volume_t volume;
90ecab35
MN
77 int error;
78
79 if (mp->mnt_flag & MNT_RDONLY) {
d121f61c 80 kprintf("Cannot add volume to read-only HAMMER filesystem\n");
90ecab35
MN
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
52e547e3
MN
89 if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
90 kprintf("Another volume operation is in progress!\n");
91 return (EAGAIN);
92 }
93
93d839df
MN
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");
52e547e3 104 hammer_unlock(&hmp->volume_lock);
93d839df
MN
105 return (EINVAL);
106 }
107
114d4836 108 struct vnode *devvp = NULL;
d121f61c 109 error = hammer_setup_device(&devvp, ioc->device_name, 0);
114d4836
MN
110 if (error)
111 goto end;
112 KKASSERT(devvp);
90ecab35
MN
113 error = hammer_format_volume_header(
114 hmp,
114d4836 115 devvp,
90ecab35 116 hmp->rootvol->ondisk->vol_name,
93d839df 117 free_vol_no,
90ecab35 118 hmp->nvolumes+1,
d121f61c
MN
119 ioc->vol_size,
120 ioc->boot_area_size,
121 ioc->mem_area_size);
114d4836 122 hammer_close_device(&devvp, 0);
2c794fb2
MN
123 if (error)
124 goto end;
90ecab35 125
d121f61c 126 error = hammer_install_volume(hmp, ioc->device_name, NULL);
2c794fb2
MN
127 if (error)
128 goto end;
90ecab35 129
2c794fb2
MN
130 hammer_sync_lock_sh(trans);
131 hammer_lock_ex(&hmp->blkmap_lock);
132
114d4836
MN
133 ++hmp->nvolumes;
134
2c794fb2
MN
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) {
2c794fb2
MN
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;
90ecab35 146 }
5ba58ea0 147 KKASSERT(volume != NULL && error == 0);
2c794fb2
MN
148 hammer_modify_volume_field(trans, volume, vol_count);
149 volume->ondisk->vol_count = hmp->nvolumes;
150 hammer_modify_volume_done(volume);
ebc29cdc
MN
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.
77912481
MD
156 *
157 * No interlock is needed, volume buffers are not
158 * messed with by bioops.
ebc29cdc
MN
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
2c794fb2
MN
165 hammer_rel_volume(volume, 0);
166 }
90ecab35 167
114d4836
MN
168 volume = hammer_get_volume(hmp, free_vol_no, &error);
169 KKASSERT(volume != NULL && error == 0);
69e6d11c 170
16b533e8
MN
171 struct bigblock_stat stat;
172 error = hammer_format_freemap(trans, volume, &stat);
173 KKASSERT(error == 0);
e6e0a973
MN
174
175 /*
176 * Increase the total number of bigblocks
177 */
865c9609 178 hammer_modify_volume_field(trans, trans->rootvol,
e6e0a973 179 vol0_stat_bigblocks);
16b533e8 180 trans->rootvol->ondisk->vol0_stat_bigblocks += stat.total_bigblocks;
865c9609 181 hammer_modify_volume_done(trans->rootvol);
e6e0a973
MN
182
183 /*
184 * Increase the number of free bigblocks
185 * (including the copy in hmp)
186 */
865c9609 187 hammer_modify_volume_field(trans, trans->rootvol,
e6e0a973 188 vol0_stat_freebigblocks);
16b533e8 189 trans->rootvol->ondisk->vol0_stat_freebigblocks += stat.total_free_bigblocks;
e6e0a973 190 hmp->copy_stat_freebigblocks =
865c9609
MN
191 trans->rootvol->ondisk->vol0_stat_freebigblocks;
192 hammer_modify_volume_done(trans->rootvol);
69e6d11c 193
114d4836 194 hammer_rel_volume(volume, 0);
69e6d11c 195
114d4836
MN
196 hammer_unlock(&hmp->blkmap_lock);
197 hammer_sync_unlock(trans);
69e6d11c 198
52e547e3 199 KKASSERT(error == 0);
114d4836 200end:
52e547e3 201 hammer_unlock(&hmp->volume_lock);
114d4836 202 if (error)
1de3c21d 203 kprintf("An error occurred: %d\n", error);
114d4836
MN
204 return (error);
205}
69e6d11c 206
865c9609
MN
207
208/*
209 * Remove a volume.
210 */
211int
212hammer_ioc_volume_del(hammer_transaction_t trans, hammer_inode_t ip,
213 struct hammer_ioc_volume *ioc)
114d4836 214{
865c9609
MN
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
52e547e3
MN
225 if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
226 kprintf("Another volume operation is in progress!\n");
227 return (EAGAIN);
228 }
865c9609
MN
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 }
07be83b8 248 hammer_rel_volume(volume, 0);
865c9609
MN
249 volume = NULL;
250 }
251
3c6039be 252 if (volume == NULL) {
865c9609 253 kprintf("Couldn't find volume\n");
52e547e3
MN
254 error = EINVAL;
255 goto end;
865c9609
MN
256 }
257
258 if (volume == trans->rootvol) {
259 kprintf("Cannot remove root-volume\n");
260 hammer_rel_volume(volume, 0);
52e547e3
MN
261 error = EINVAL;
262 goto end;
865c9609
MN
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;
bbda1970 278 reblock.head.flags = HAMMER_IOC_DO_FLAGS;
865c9609
MN
279 reblock.free_level = 0;
280
281 error = hammer_ioc_reblock(trans, ip, &reblock);
282
bbda1970
MN
283 if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) {
284 error = EINTR;
285 }
286
865c9609 287 if (error) {
bbda1970
MN
288 if (error == EINTR) {
289 kprintf("reblock was interrupted\n");
290 } else {
291 kprintf("reblock failed: %d\n", error);
292 }
865c9609
MN
293 hmp->volume_to_remove = -1;
294 hammer_rel_volume(volume, 0);
52e547e3 295 goto end;
865c9609
MN
296 }
297
bbda1970
MN
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
865c9609
MN
319 hammer_sync_lock_sh(trans);
320 hammer_lock_ex(&hmp->blkmap_lock);
321
16b533e8
MN
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);
865c9609 327 if (error) {
c47d84e8 328 kprintf("Failed to free volume. Volume not empty!\n");
865c9609
MN
329 hmp->volume_to_remove = -1;
330 hammer_rel_volume(volume, 0);
331 hammer_unlock(&hmp->blkmap_lock);
332 hammer_sync_unlock(trans);
52e547e3 333 goto end;
865c9609
MN
334 }
335
336 hmp->volume_to_remove = -1;
865c9609 337
07be83b8
MN
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);
865c9609
MN
347 if (error == -1) {
348 kprintf("Failed to unload volume\n");
349 hammer_unlock(&hmp->blkmap_lock);
350 hammer_sync_unlock(trans);
52e547e3 351 goto end;
07be83b8 352 }
865c9609 353
07be83b8 354 volume = NULL;
865c9609
MN
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 }
07be83b8 369
865c9609
MN
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);
ebc29cdc
MN
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.
77912481
MD
379 *
380 * No interlock is needed, volume buffers are not
381 * messed with by bioops.
ebc29cdc
MN
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
865c9609
MN
388 hammer_rel_volume(volume, 0);
389 }
390
16b533e8
MN
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
865c9609
MN
411 hammer_unlock(&hmp->blkmap_lock);
412 hammer_sync_unlock(trans);
413
c47d84e8
MN
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);
52e547e3 423 goto end;
c47d84e8
MN
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);
52e547e3 430 goto end;
c47d84e8
MN
431 }
432 hammer_close_device(&devvp, 0);
433
52e547e3
MN
434 KKASSERT(error == 0);
435end:
436 hammer_unlock(&hmp->volume_lock);
437 return (error);
865c9609
MN
438}
439
440
e914c91d
SK
441int
442hammer_ioc_volume_list(hammer_transaction_t trans, hammer_inode_t ip,
443 struct hammer_ioc_volume_list *ioc)
444{
445 struct hammer_mount *hmp = trans->hmp;
446 hammer_volume_t volume;
447 int error = 0;
448 int i, cnt, len;
449
450 for (i = 0, cnt = 0; i < HAMMER_MAX_VOLUMES && cnt < ioc->nvols; i++) {
451 volume = hammer_get_volume(hmp, i, &error);
452 if (volume == NULL && error == ENOENT) {
453 error = 0;
454 continue;
455 }
456 KKASSERT(volume != NULL && error == 0);
457
458 len = strlen(volume->vol_name) + 1;
459 KKASSERT(len <= MAXPATHLEN);
460
461 error = copyout(volume->vol_name, ioc->vols[cnt].device_name,
462 len);
463 if (error) {
464 hammer_rel_volume(volume, 0);
465 return (error);
466 }
467 cnt++;
468 hammer_rel_volume(volume, 0);
469 }
470 ioc->nvols = cnt;
471
472 return (error);
473}
474
865c9609
MN
475/*
476 * Iterate over all usable L1 entries of the volume and
477 * the corresponding L2 entries.
478 */
479static int
480hammer_iterate_l1l2_entries(hammer_transaction_t trans, hammer_volume_t volume,
c47d84e8
MN
481 int (*callback)(hammer_transaction_t, hammer_volume_t, hammer_buffer_t*,
482 struct hammer_blockmap_layer1*, struct hammer_blockmap_layer2*,
483 hammer_off_t, hammer_off_t, void*),
865c9609
MN
484 void *data)
485{
486 struct hammer_mount *hmp = trans->hmp;
487 hammer_blockmap_t freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
114d4836 488 hammer_buffer_t buffer = NULL;
114d4836 489 int error = 0;
69e6d11c 490
865c9609
MN
491 hammer_off_t phys_off;
492 hammer_off_t block_off;
493 hammer_off_t layer1_off;
494 hammer_off_t layer2_off;
495 hammer_off_t aligned_buf_end_off;
496 struct hammer_blockmap_layer1 *layer1;
497 struct hammer_blockmap_layer2 *layer2;
aace59d0 498
114d4836 499 /*
865c9609 500 * Calculate the usable size of the volume, which
114d4836
MN
501 * must be aligned at a bigblock (8 MB) boundary.
502 */
865c9609 503 aligned_buf_end_off = (HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
114d4836 504 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
865c9609 505 & ~HAMMER_LARGEBLOCK_MASK64));
114d4836
MN
506
507 /*
865c9609
MN
508 * Iterate the volume's address space in chunks of 4 TB, where each
509 * chunk consists of at least one physically available 8 MB bigblock.
114d4836
MN
510 *
511 * For each chunk we need one L1 entry and one L2 bigblock.
512 * We use the first bigblock of each chunk as L2 block.
513 */
865c9609
MN
514 for (phys_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
515 phys_off < aligned_buf_end_off;
516 phys_off += HAMMER_BLOCKMAP_LAYER2) {
517 for (block_off = 0;
518 block_off < HAMMER_BLOCKMAP_LAYER2;
519 block_off += HAMMER_LARGEBLOCK_SIZE) {
520 layer2_off = phys_off +
521 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_off);
c47d84e8 522 layer2 = hammer_bread(hmp, layer2_off, &error, &buffer);
865c9609
MN
523 if (error)
524 goto end;
525
c47d84e8
MN
526 error = callback(trans, volume, &buffer, NULL,
527 layer2, phys_off, block_off, data);
865c9609
MN
528 if (error)
529 goto end;
530 }
114d4836 531
865c9609
MN
532 layer1_off = freemap->phys_offset +
533 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_off);
534 layer1 = hammer_bread(hmp, layer1_off, &error, &buffer);
535 if (error)
536 goto end;
69e6d11c 537
c47d84e8
MN
538 error = callback(trans, volume, &buffer, layer1, NULL,
539 phys_off, 0, data);
865c9609
MN
540 if (error)
541 goto end;
114d4836 542 }
69e6d11c 543
865c9609 544end:
114d4836
MN
545 if (buffer) {
546 hammer_rel_buffer(buffer, 0);
547 buffer = NULL;
548 }
90ecab35 549
865c9609 550 return error;
114d4836 551}
2c794fb2 552
865c9609
MN
553
554static int
c47d84e8
MN
555format_callback(hammer_transaction_t trans, hammer_volume_t volume,
556 hammer_buffer_t *bufferp,
865c9609
MN
557 struct hammer_blockmap_layer1 *layer1,
558 struct hammer_blockmap_layer2 *layer2,
559 hammer_off_t phys_off,
c47d84e8 560 hammer_off_t block_off,
865c9609 561 void *data)
114d4836 562{
16b533e8 563 struct bigblock_stat *stat = (struct bigblock_stat*)data;
114d4836 564
c47d84e8
MN
565 /*
566 * Calculate the usable size of the volume, which must be aligned
567 * at a bigblock (8 MB) boundary.
568 */
569 hammer_off_t aligned_buf_end_off;
570 aligned_buf_end_off = (HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
571 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
572 & ~HAMMER_LARGEBLOCK_MASK64));
573
865c9609
MN
574 if (layer1) {
575 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
114d4836 576
865c9609
MN
577 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
578 bzero(layer1, sizeof(layer1));
579 layer1->phys_offset = phys_off;
16b533e8 580 layer1->blocks_free = stat->counter;
865c9609
MN
581 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
582 hammer_modify_buffer_done(*bufferp);
114d4836 583
16b533e8
MN
584 stat->total_free_bigblocks += stat->counter;
585 stat->counter = 0; /* reset */
865c9609 586 } else if (layer2) {
114d4836
MN
587 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
588 bzero(layer2, sizeof(*layer2));
589
c47d84e8 590 if (block_off == 0) {
114d4836
MN
591 /*
592 * The first entry represents the L2 bigblock itself.
593 */
c47d84e8 594 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
114d4836
MN
595 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
596 layer2->bytes_free = 0;
16b533e8 597 ++stat->total_bigblocks;
c47d84e8 598 } else if (phys_off + block_off < aligned_buf_end_off) {
865c9609
MN
599 /*
600 * Available bigblock
601 */
c47d84e8 602 layer2->zone = 0;
114d4836
MN
603 layer2->append_off = 0;
604 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
16b533e8
MN
605 ++stat->total_bigblocks;
606 ++stat->counter;
c47d84e8 607 } else {
114d4836 608 /*
c47d84e8
MN
609 * Bigblock outside of physically available
610 * space
114d4836 611 */
c47d84e8 612 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
114d4836
MN
613 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
614 layer2->bytes_free = 0;
615 }
865c9609 616
114d4836 617 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
865c9609
MN
618 hammer_modify_buffer_done(*bufferp);
619 } else {
620 KKASSERT(0);
621 }
622
623 return 0;
624}
114d4836 625
16b533e8
MN
626static int
627hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume,
628 struct bigblock_stat *stat)
865c9609 629{
16b533e8
MN
630 stat->total_bigblocks = 0;
631 stat->total_free_bigblocks = 0;
632 stat->counter = 0;
633 return hammer_iterate_l1l2_entries(trans, volume, format_callback, stat);
865c9609
MN
634}
635
636static int
c47d84e8
MN
637free_callback(hammer_transaction_t trans, hammer_volume_t volume __unused,
638 hammer_buffer_t *bufferp,
865c9609
MN
639 struct hammer_blockmap_layer1 *layer1,
640 struct hammer_blockmap_layer2 *layer2,
641 hammer_off_t phys_off,
c47d84e8 642 hammer_off_t block_off __unused,
3c6039be 643 void *data)
865c9609 644{
16b533e8
MN
645 struct bigblock_stat *stat = (struct bigblock_stat*)data;
646
3c6039be
MN
647 /*
648 * No modifications to ondisk structures
649 */
16b533e8 650 int testonly = (stat == NULL);
3c6039be 651
865c9609 652 if (layer1) {
c47d84e8
MN
653 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
654 /*
655 * This layer1 entry is already free.
656 */
657 return 0;
658 }
659
bbda1970
MN
660 KKASSERT((int)HAMMER_VOL_DECODE(layer1->phys_offset) ==
661 trans->hmp->volume_to_remove);
662
3c6039be
MN
663 if (testonly)
664 return 0;
665
865c9609
MN
666 /*
667 * Free the L1 entry
668 */
669 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
670 bzero(layer1, sizeof(layer1));
671 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
672 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
114d4836 673 hammer_modify_buffer_done(*bufferp);
865c9609
MN
674
675 return 0;
676 } else if (layer2) {
16b533e8 677 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
865c9609 678 return 0;
16b533e8
MN
679 }
680
681 if (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX) {
682 if (stat) {
683 ++stat->total_bigblocks;
684 }
685 return 0;
686 }
c47d84e8
MN
687
688 if (layer2->append_off == 0 &&
16b533e8
MN
689 layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
690 if (stat) {
691 ++stat->total_bigblocks;
692 ++stat->total_free_bigblocks;
693 }
c47d84e8 694 return 0;
16b533e8
MN
695 }
696
c47d84e8
MN
697 /*
698 * We found a layer2 entry that is not empty!
699 */
700 return EBUSY;
865c9609
MN
701 } else {
702 KKASSERT(0);
90ecab35 703 }
114d4836 704
865c9609 705 return EINVAL;
114d4836
MN
706}
707
865c9609 708static int
16b533e8
MN
709hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume,
710 struct bigblock_stat *stat)
114d4836 711{
3c6039be 712 int error;
16b533e8
MN
713
714 stat->total_bigblocks = 0;
715 stat->total_free_bigblocks = 0;
716 stat->counter = 0;
717
718 error = hammer_iterate_l1l2_entries(trans, volume, free_callback, NULL);
3c6039be
MN
719 if (error)
720 return error;
721
16b533e8 722 error = hammer_iterate_l1l2_entries(trans, volume, free_callback, stat);
3c6039be 723 return error;
90ecab35
MN
724}
725
865c9609
MN
726/************************************************************************
727 * MISC *
728 ************************************************************************
729 */
730
90ecab35 731static int
114d4836 732hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly)
90ecab35 733{
90ecab35 734 int error;
114d4836 735 struct nlookupdata nd;
90ecab35
MN
736
737 /*
738 * Get the device vnode
739 */
114d4836
MN
740 if (*devvpp == NULL) {
741 error = nlookup_init(&nd, dev_path, UIO_SYSSPACE, NLC_FOLLOW);
742 if (error == 0)
743 error = nlookup(&nd);
744 if (error == 0)
745 error = cache_vref(&nd.nl_nch, nd.nl_cred, devvpp);
746 nlookup_done(&nd);
747 } else {
748 error = 0;
749 }
90ecab35
MN
750
751 if (error == 0) {
114d4836
MN
752 if (vn_isdisk(*devvpp, &error)) {
753 error = vfs_mountedon(*devvpp);
90ecab35
MN
754 }
755 }
8be7edad 756 if (error == 0 && vcount(*devvpp) > 0)
90ecab35 757 error = EBUSY;
90ecab35 758 if (error == 0) {
114d4836
MN
759 vn_lock(*devvpp, LK_EXCLUSIVE | LK_RETRY);
760 error = vinvalbuf(*devvpp, V_SAVE, 0, 0);
90ecab35 761 if (error == 0) {
114d4836
MN
762 error = VOP_OPEN(*devvpp,
763 (ronly ? FREAD : FREAD|FWRITE),
764 FSCRED, NULL);
90ecab35 765 }
114d4836 766 vn_unlock(*devvpp);
90ecab35 767 }
114d4836
MN
768 if (error && *devvpp) {
769 vrele(*devvpp);
770 *devvpp = NULL;
90ecab35 771 }
114d4836
MN
772 return (error);
773}
774
775static void
776hammer_close_device(struct vnode **devvpp, int ronly)
777{
778 VOP_CLOSE(*devvpp, (ronly ? FREAD : FREAD|FWRITE));
779 if (*devvpp) {
780 vinvalbuf(*devvpp, ronly ? 0 : V_SAVE, 0, 0);
781 vrele(*devvpp);
782 *devvpp = NULL;
783 }
784}
785
786static int
787hammer_format_volume_header(struct hammer_mount *hmp, struct vnode *devvp,
788 const char *vol_name, int vol_no, int vol_count,
789 int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
790{
791 struct buf *bp = NULL;
792 struct hammer_volume_ondisk *ondisk;
793 int error;
90ecab35
MN
794
795 /*
796 * Extract the volume number from the volume header and do various
797 * sanity checks.
798 */
799 KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
800 error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
801 if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
802 goto late_failure;
803
804 ondisk = (struct hammer_volume_ondisk*) bp->b_data;
805
806 /*
807 * Note that we do NOT allow to use a device that contains
808 * a valid HAMMER signature. It has to be cleaned up with dd
809 * before.
810 */
811 if (ondisk->vol_signature == HAMMER_FSBUF_VOLUME) {
d121f61c 812 kprintf("hammer_volume_add: Formatting of valid HAMMER volume "
90ecab35
MN
813 "%s denied. Erase with dd!\n", vol_name);
814 error = EFTYPE;
815 goto late_failure;
816 }
817
818 bzero(ondisk, sizeof(struct hammer_volume_ondisk));
819 ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
820 ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
821 ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
822 ondisk->vol_fsid = hmp->fsid;
823 ondisk->vol_rootvol = hmp->rootvol->vol_no;
824 ondisk->vol_no = vol_no;
825 ondisk->vol_count = vol_count;
826 ondisk->vol_version = hmp->version;
827
828 /*
829 * Reserve space for (future) header junk, setup our poor-man's
830 * bigblock allocator.
831 */
832 int64_t vol_alloc = HAMMER_BUFSIZE * 16;
833
834 ondisk->vol_bot_beg = vol_alloc;
835 vol_alloc += boot_area_size;
836 ondisk->vol_mem_beg = vol_alloc;
837 vol_alloc += mem_area_size;
838
839 /*
840 * The remaining area is the zone 2 buffer allocation area. These
841 * buffers
842 */
843 ondisk->vol_buf_beg = vol_alloc;
844 ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
845
846 if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
847 kprintf("volume %d %s is too small to hold the volume header",
848 ondisk->vol_no, ondisk->vol_name);
849 error = EFTYPE;
850 goto late_failure;
851 }
852
853 ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
854 HAMMER_BUFSIZE;
855 ondisk->vol_blocksize = HAMMER_BUFSIZE;
856
857 /*
858 * Write volume header to disk
859 */
860 error = bwrite(bp);
861 bp = NULL;
862
863late_failure:
864 if (bp)
865 brelse(bp);
90ecab35 866 return (error);
e27700cf 867}
c47d84e8
MN
868
869/*
870 * Invalidates the volume header. Used by volume-del.
871 */
872static int
873hammer_clear_volume_header(struct vnode *devvp)
874{
875 struct buf *bp = NULL;
876 struct hammer_volume_ondisk *ondisk;
877 int error;
878
c47d84e8
MN
879 KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
880 error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
881 if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
882 goto late_failure;
883
884 ondisk = (struct hammer_volume_ondisk*) bp->b_data;
885 bzero(ondisk, sizeof(struct hammer_volume_ondisk));
886
c47d84e8
MN
887 error = bwrite(bp);
888 bp = NULL;
889
890late_failure:
891 if (bp)
892 brelse(bp);
893 return (error);
894}