2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
38 #include <sys/fcntl.h>
39 #include <sys/nlookup.h>
43 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly);
46 hammer_close_device(struct vnode **devvpp, int ronly);
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);
54 hammer_clear_volume_header(struct vnode *devvp);
57 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume);
60 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume);
64 hammer_ioc_volume_add(hammer_transaction_t trans, hammer_inode_t ip,
65 struct hammer_ioc_volume *ioc)
67 struct hammer_mount *hmp = trans->hmp;
68 struct mount *mp = hmp->mp;
69 hammer_volume_t volume;
72 if (mp->mnt_flag & MNT_RDONLY) {
73 kprintf("Cannot add volume to read-only HAMMER filesystem\n");
77 if (hmp->nvolumes + 1 >= HAMMER_MAX_VOLUMES) {
78 kprintf("Max number of HAMMER volumes exceeded\n");
83 * Find an unused volume number.
86 while (free_vol_no < HAMMER_MAX_VOLUMES &&
87 RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, free_vol_no)) {
90 if (free_vol_no >= HAMMER_MAX_VOLUMES) {
91 kprintf("Max number of HAMMER volumes exceeded\n");
95 struct vnode *devvp = NULL;
96 error = hammer_setup_device(&devvp, ioc->device_name, 0);
100 error = hammer_format_volume_header(
103 hmp->rootvol->ondisk->vol_name,
109 hammer_close_device(&devvp, 0);
113 error = hammer_install_volume(hmp, ioc->device_name, NULL);
117 hammer_sync_lock_sh(trans);
118 hammer_lock_ex(&hmp->blkmap_lock);
123 * Set each volumes new value of the vol_count field.
125 for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
126 volume = hammer_get_volume(hmp, vol_no, &error);
127 if (volume == NULL && error == ENOENT) {
129 * Skip unused volume numbers
134 KKASSERT(volume != NULL && error == 0);
135 hammer_modify_volume_field(trans, volume, vol_count);
136 volume->ondisk->vol_count = hmp->nvolumes;
137 hammer_modify_volume_done(volume);
140 * Only changes to the header of the root volume
141 * are automatically flushed to disk. For all
142 * other volumes that we modify we do it here.
144 if (volume != trans->rootvol && volume->io.modified) {
145 hammer_crc_set_volume(volume->ondisk);
146 hammer_io_flush(&volume->io, 0);
149 hammer_rel_volume(volume, 0);
152 volume = hammer_get_volume(hmp, free_vol_no, &error);
153 KKASSERT(volume != NULL && error == 0);
155 uint64_t total_free_bigblocks =
156 hammer_format_freemap(trans, volume);
159 * Increase the total number of bigblocks
161 hammer_modify_volume_field(trans, trans->rootvol,
162 vol0_stat_bigblocks);
163 trans->rootvol->ondisk->vol0_stat_bigblocks += total_free_bigblocks;
164 hammer_modify_volume_done(trans->rootvol);
167 * Increase the number of free bigblocks
168 * (including the copy in hmp)
170 hammer_modify_volume_field(trans, trans->rootvol,
171 vol0_stat_freebigblocks);
172 trans->rootvol->ondisk->vol0_stat_freebigblocks += total_free_bigblocks;
173 hmp->copy_stat_freebigblocks =
174 trans->rootvol->ondisk->vol0_stat_freebigblocks;
175 hammer_modify_volume_done(trans->rootvol);
177 hammer_rel_volume(volume, 0);
179 hammer_unlock(&hmp->blkmap_lock);
180 hammer_sync_unlock(trans);
184 kprintf("An error occurred: %d\n", error);
193 hammer_ioc_volume_del(hammer_transaction_t trans, hammer_inode_t ip,
194 struct hammer_ioc_volume *ioc)
196 struct hammer_mount *hmp = trans->hmp;
197 struct mount *mp = hmp->mp;
198 hammer_volume_t volume;
201 if (mp->mnt_flag & MNT_RDONLY) {
202 kprintf("Cannot del volume from read-only HAMMER filesystem\n");
210 * find volume by volname
212 for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
213 volume = hammer_get_volume(hmp, vol_no, &error);
214 if (volume == NULL && error == ENOENT) {
216 * Skip unused volume numbers
221 KKASSERT(volume != NULL && error == 0);
222 if (strcmp(volume->vol_name, ioc->device_name) == 0) {
225 hammer_rel_volume(volume, 0);
229 if (volume == NULL) {
230 kprintf("Couldn't find volume\n");
234 if (volume == trans->rootvol) {
235 kprintf("Cannot remove root-volume\n");
236 hammer_rel_volume(volume, 0);
244 hmp->volume_to_remove = volume->vol_no;
246 struct hammer_ioc_reblock reblock;
247 bzero(&reblock, sizeof(reblock));
249 reblock.key_beg.localization = HAMMER_MIN_LOCALIZATION;
250 reblock.key_beg.obj_id = HAMMER_MIN_OBJID;
251 reblock.key_end.localization = HAMMER_MAX_LOCALIZATION;
252 reblock.key_end.obj_id = HAMMER_MAX_OBJID;
253 reblock.head.flags = HAMMER_IOC_DO_FLAGS;
254 reblock.free_level = 0;
256 error = hammer_ioc_reblock(trans, ip, &reblock);
258 if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) {
263 if (error == EINTR) {
264 kprintf("reblock was interrupted\n");
266 kprintf("reblock failed: %d\n", error);
268 hmp->volume_to_remove = -1;
269 hammer_rel_volume(volume, 0);
277 while (hammer_flusher_haswork(hmp)) {
278 hammer_flusher_sync(hmp);
282 kprintf("HAMMER: flushing.");
285 tsleep(&count, 0, "hmrufl", hz);
288 kprintf("giving up");
294 hammer_sync_lock_sh(trans);
295 hammer_lock_ex(&hmp->blkmap_lock);
297 error = hammer_free_freemap(trans, volume);
299 kprintf("Failed to free volume. Volume not empty!\n");
300 hmp->volume_to_remove = -1;
301 hammer_rel_volume(volume, 0);
302 hammer_unlock(&hmp->blkmap_lock);
303 hammer_sync_unlock(trans);
307 hmp->volume_to_remove = -1;
309 hammer_rel_volume(volume, 0);
314 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
315 hammer_unload_buffer, volume);
317 error = hammer_unload_volume(volume, NULL);
319 kprintf("Failed to unload volume\n");
320 hammer_unlock(&hmp->blkmap_lock);
321 hammer_sync_unlock(trans);
329 * Set each volume's new value of the vol_count field.
331 for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
332 volume = hammer_get_volume(hmp, vol_no, &error);
333 if (volume == NULL && error == ENOENT) {
335 * Skip unused volume numbers
341 KKASSERT(volume != NULL && error == 0);
342 hammer_modify_volume_field(trans, volume, vol_count);
343 volume->ondisk->vol_count = hmp->nvolumes;
344 hammer_modify_volume_done(volume);
347 * Only changes to the header of the root volume
348 * are automatically flushed to disk. For all
349 * other volumes that we modify we do it here.
351 if (volume != trans->rootvol && volume->io.modified) {
352 hammer_crc_set_volume(volume->ondisk);
353 hammer_io_flush(&volume->io, 0);
356 hammer_rel_volume(volume, 0);
359 hammer_unlock(&hmp->blkmap_lock);
360 hammer_sync_unlock(trans);
363 * Erase the volume header of the removed device.
365 * This is to not accidentally mount the volume again.
367 struct vnode *devvp = NULL;
368 error = hammer_setup_device(&devvp, ioc->device_name, 0);
370 kprintf("Failed to open device: %s\n", ioc->device_name);
374 error = hammer_clear_volume_header(devvp);
376 kprintf("Failed to clear volume header of device: %s\n",
380 hammer_close_device(&devvp, 0);
387 * Iterate over all usable L1 entries of the volume and
388 * the corresponding L2 entries.
391 hammer_iterate_l1l2_entries(hammer_transaction_t trans, hammer_volume_t volume,
392 int (*callback)(hammer_transaction_t, hammer_volume_t, hammer_buffer_t*,
393 struct hammer_blockmap_layer1*, struct hammer_blockmap_layer2*,
394 hammer_off_t, hammer_off_t, void*),
397 struct hammer_mount *hmp = trans->hmp;
398 hammer_blockmap_t freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
399 hammer_buffer_t buffer = NULL;
402 hammer_off_t phys_off;
403 hammer_off_t block_off;
404 hammer_off_t layer1_off;
405 hammer_off_t layer2_off;
406 hammer_off_t aligned_buf_end_off;
407 struct hammer_blockmap_layer1 *layer1;
408 struct hammer_blockmap_layer2 *layer2;
411 * Calculate the usable size of the volume, which
412 * must be aligned at a bigblock (8 MB) boundary.
414 aligned_buf_end_off = (HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
415 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
416 & ~HAMMER_LARGEBLOCK_MASK64));
419 * Iterate the volume's address space in chunks of 4 TB, where each
420 * chunk consists of at least one physically available 8 MB bigblock.
422 * For each chunk we need one L1 entry and one L2 bigblock.
423 * We use the first bigblock of each chunk as L2 block.
425 for (phys_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
426 phys_off < aligned_buf_end_off;
427 phys_off += HAMMER_BLOCKMAP_LAYER2) {
429 block_off < HAMMER_BLOCKMAP_LAYER2;
430 block_off += HAMMER_LARGEBLOCK_SIZE) {
431 layer2_off = phys_off +
432 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_off);
433 layer2 = hammer_bread(hmp, layer2_off, &error, &buffer);
437 error = callback(trans, volume, &buffer, NULL,
438 layer2, phys_off, block_off, data);
443 layer1_off = freemap->phys_offset +
444 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_off);
445 layer1 = hammer_bread(hmp, layer1_off, &error, &buffer);
449 error = callback(trans, volume, &buffer, layer1, NULL,
457 hammer_rel_buffer(buffer, 0);
464 struct format_bigblock_stat {
465 uint64_t total_free_bigblocks;
466 uint64_t free_bigblocks;
470 format_callback(hammer_transaction_t trans, hammer_volume_t volume,
471 hammer_buffer_t *bufferp,
472 struct hammer_blockmap_layer1 *layer1,
473 struct hammer_blockmap_layer2 *layer2,
474 hammer_off_t phys_off,
475 hammer_off_t block_off,
478 struct format_bigblock_stat *stat = (struct format_bigblock_stat*)data;
481 * Calculate the usable size of the volume, which must be aligned
482 * at a bigblock (8 MB) boundary.
484 hammer_off_t aligned_buf_end_off;
485 aligned_buf_end_off = (HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
486 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
487 & ~HAMMER_LARGEBLOCK_MASK64));
490 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
492 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
493 bzero(layer1, sizeof(layer1));
494 layer1->phys_offset = phys_off;
495 layer1->blocks_free = stat->free_bigblocks;
496 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
497 hammer_modify_buffer_done(*bufferp);
499 stat->total_free_bigblocks += stat->free_bigblocks;
500 stat->free_bigblocks = 0; /* reset */
502 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
503 bzero(layer2, sizeof(*layer2));
505 if (block_off == 0) {
507 * The first entry represents the L2 bigblock itself.
509 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
510 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
511 layer2->bytes_free = 0;
512 } else if (phys_off + block_off < aligned_buf_end_off) {
517 layer2->append_off = 0;
518 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
519 ++stat->free_bigblocks;
522 * Bigblock outside of physically available
525 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
526 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
527 layer2->bytes_free = 0;
530 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
531 hammer_modify_buffer_done(*bufferp);
540 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume)
544 struct format_bigblock_stat stat;
545 stat.total_free_bigblocks = 0;
546 stat.free_bigblocks = 0;
548 error = hammer_iterate_l1l2_entries(trans, volume, format_callback,
550 KKASSERT(error == 0);
552 return stat.total_free_bigblocks;
556 free_callback(hammer_transaction_t trans, hammer_volume_t volume __unused,
557 hammer_buffer_t *bufferp,
558 struct hammer_blockmap_layer1 *layer1,
559 struct hammer_blockmap_layer2 *layer2,
560 hammer_off_t phys_off,
561 hammer_off_t block_off __unused,
565 * No modifications to ondisk structures
567 int testonly = (data != NULL);
570 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
572 * This layer1 entry is already free.
577 KKASSERT((int)HAMMER_VOL_DECODE(layer1->phys_offset) ==
578 trans->hmp->volume_to_remove);
586 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
587 bzero(layer1, sizeof(layer1));
588 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
589 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
590 hammer_modify_buffer_done(*bufferp);
594 if (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX ||
595 layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX)
598 if (layer2->append_off == 0 &&
599 layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE)
602 * We found a layer2 entry that is not empty!
613 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume)
616 error = hammer_iterate_l1l2_entries(trans, volume, free_callback,
621 error = hammer_iterate_l1l2_entries(trans, volume, free_callback, NULL);
625 /************************************************************************
627 ************************************************************************
631 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly)
634 struct nlookupdata nd;
637 * Get the device vnode
639 if (*devvpp == NULL) {
640 error = nlookup_init(&nd, dev_path, UIO_SYSSPACE, NLC_FOLLOW);
642 error = nlookup(&nd);
644 error = cache_vref(&nd.nl_nch, nd.nl_cred, devvpp);
651 if (vn_isdisk(*devvpp, &error)) {
652 error = vfs_mountedon(*devvpp);
655 if (error == 0 && vcount(*devvpp) > 0)
658 vn_lock(*devvpp, LK_EXCLUSIVE | LK_RETRY);
659 error = vinvalbuf(*devvpp, V_SAVE, 0, 0);
661 error = VOP_OPEN(*devvpp,
662 (ronly ? FREAD : FREAD|FWRITE),
667 if (error && *devvpp) {
675 hammer_close_device(struct vnode **devvpp, int ronly)
677 VOP_CLOSE(*devvpp, (ronly ? FREAD : FREAD|FWRITE));
679 vinvalbuf(*devvpp, ronly ? 0 : V_SAVE, 0, 0);
686 hammer_format_volume_header(struct hammer_mount *hmp, struct vnode *devvp,
687 const char *vol_name, int vol_no, int vol_count,
688 int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
690 struct buf *bp = NULL;
691 struct hammer_volume_ondisk *ondisk;
695 * Extract the volume number from the volume header and do various
698 KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
699 error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
700 if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
703 ondisk = (struct hammer_volume_ondisk*) bp->b_data;
706 * Note that we do NOT allow to use a device that contains
707 * a valid HAMMER signature. It has to be cleaned up with dd
710 if (ondisk->vol_signature == HAMMER_FSBUF_VOLUME) {
711 kprintf("hammer_volume_add: Formatting of valid HAMMER volume "
712 "%s denied. Erase with dd!\n", vol_name);
717 bzero(ondisk, sizeof(struct hammer_volume_ondisk));
718 ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
719 ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
720 ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
721 ondisk->vol_fsid = hmp->fsid;
722 ondisk->vol_rootvol = hmp->rootvol->vol_no;
723 ondisk->vol_no = vol_no;
724 ondisk->vol_count = vol_count;
725 ondisk->vol_version = hmp->version;
728 * Reserve space for (future) header junk, setup our poor-man's
729 * bigblock allocator.
731 int64_t vol_alloc = HAMMER_BUFSIZE * 16;
733 ondisk->vol_bot_beg = vol_alloc;
734 vol_alloc += boot_area_size;
735 ondisk->vol_mem_beg = vol_alloc;
736 vol_alloc += mem_area_size;
739 * The remaining area is the zone 2 buffer allocation area. These
742 ondisk->vol_buf_beg = vol_alloc;
743 ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
745 if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
746 kprintf("volume %d %s is too small to hold the volume header",
747 ondisk->vol_no, ondisk->vol_name);
752 ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
754 ondisk->vol_blocksize = HAMMER_BUFSIZE;
757 * Write volume header to disk
769 * Invalidates the volume header. Used by volume-del.
772 hammer_clear_volume_header(struct vnode *devvp)
774 struct buf *bp = NULL;
775 struct hammer_volume_ondisk *ondisk;
779 * Extract the volume number from the volume header and do various
782 KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
783 error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
784 if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
787 ondisk = (struct hammer_volume_ondisk*) bp->b_data;
788 bzero(ondisk, sizeof(struct hammer_volume_ondisk));
791 * Write volume header to disk