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);
56 struct bigblock_stat {
57 uint64_t total_bigblocks;
58 uint64_t total_free_bigblocks;
63 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume,
64 struct bigblock_stat *stat);
67 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume,
68 struct bigblock_stat *stat);
71 hammer_ioc_volume_add(hammer_transaction_t trans, hammer_inode_t ip,
72 struct hammer_ioc_volume *ioc)
74 struct hammer_mount *hmp = trans->hmp;
75 struct mount *mp = hmp->mp;
76 hammer_volume_t volume;
79 if (mp->mnt_flag & MNT_RDONLY) {
80 kprintf("Cannot add volume to read-only HAMMER filesystem\n");
84 if (hmp->nvolumes + 1 >= HAMMER_MAX_VOLUMES) {
85 kprintf("Max number of HAMMER volumes exceeded\n");
89 if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
90 kprintf("Another volume operation is in progress!\n");
95 * Find an unused volume number.
98 while (free_vol_no < HAMMER_MAX_VOLUMES &&
99 RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, free_vol_no)) {
102 if (free_vol_no >= HAMMER_MAX_VOLUMES) {
103 kprintf("Max number of HAMMER volumes exceeded\n");
104 hammer_unlock(&hmp->volume_lock);
108 struct vnode *devvp = NULL;
109 error = hammer_setup_device(&devvp, ioc->device_name, 0);
113 error = hammer_format_volume_header(
116 hmp->rootvol->ondisk->vol_name,
122 hammer_close_device(&devvp, 0);
126 error = hammer_install_volume(hmp, ioc->device_name, NULL);
130 hammer_sync_lock_sh(trans);
131 hammer_lock_ex(&hmp->blkmap_lock);
136 * Set each volumes new value of the vol_count field.
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) {
142 * Skip unused volume numbers
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);
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.
157 if (volume != trans->rootvol && volume->io.modified) {
158 hammer_crc_set_volume(volume->ondisk);
159 hammer_io_flush(&volume->io, 0);
162 hammer_rel_volume(volume, 0);
165 volume = hammer_get_volume(hmp, free_vol_no, &error);
166 KKASSERT(volume != NULL && error == 0);
168 struct bigblock_stat stat;
169 error = hammer_format_freemap(trans, volume, &stat);
170 KKASSERT(error == 0);
173 * Increase the total number of bigblocks
175 hammer_modify_volume_field(trans, trans->rootvol,
176 vol0_stat_bigblocks);
177 trans->rootvol->ondisk->vol0_stat_bigblocks += stat.total_bigblocks;
178 hammer_modify_volume_done(trans->rootvol);
181 * Increase the number of free bigblocks
182 * (including the copy in hmp)
184 hammer_modify_volume_field(trans, trans->rootvol,
185 vol0_stat_freebigblocks);
186 trans->rootvol->ondisk->vol0_stat_freebigblocks += stat.total_free_bigblocks;
187 hmp->copy_stat_freebigblocks =
188 trans->rootvol->ondisk->vol0_stat_freebigblocks;
189 hammer_modify_volume_done(trans->rootvol);
191 hammer_rel_volume(volume, 0);
193 hammer_unlock(&hmp->blkmap_lock);
194 hammer_sync_unlock(trans);
196 KKASSERT(error == 0);
198 hammer_unlock(&hmp->volume_lock);
200 kprintf("An error occurred: %d\n", error);
209 hammer_ioc_volume_del(hammer_transaction_t trans, hammer_inode_t ip,
210 struct hammer_ioc_volume *ioc)
212 struct hammer_mount *hmp = trans->hmp;
213 struct mount *mp = hmp->mp;
214 hammer_volume_t volume;
217 if (mp->mnt_flag & MNT_RDONLY) {
218 kprintf("Cannot del volume from read-only HAMMER filesystem\n");
222 if (hammer_lock_ex_try(&hmp->volume_lock) != 0) {
223 kprintf("Another volume operation is in progress!\n");
230 * find volume by volname
232 for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
233 volume = hammer_get_volume(hmp, vol_no, &error);
234 if (volume == NULL && error == ENOENT) {
236 * Skip unused volume numbers
241 KKASSERT(volume != NULL && error == 0);
242 if (strcmp(volume->vol_name, ioc->device_name) == 0) {
245 hammer_rel_volume(volume, 0);
249 if (volume == NULL) {
250 kprintf("Couldn't find volume\n");
255 if (volume == trans->rootvol) {
256 kprintf("Cannot remove root-volume\n");
257 hammer_rel_volume(volume, 0);
266 hmp->volume_to_remove = volume->vol_no;
268 struct hammer_ioc_reblock reblock;
269 bzero(&reblock, sizeof(reblock));
271 reblock.key_beg.localization = HAMMER_MIN_LOCALIZATION;
272 reblock.key_beg.obj_id = HAMMER_MIN_OBJID;
273 reblock.key_end.localization = HAMMER_MAX_LOCALIZATION;
274 reblock.key_end.obj_id = HAMMER_MAX_OBJID;
275 reblock.head.flags = HAMMER_IOC_DO_FLAGS;
276 reblock.free_level = 0;
278 error = hammer_ioc_reblock(trans, ip, &reblock);
280 if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) {
285 if (error == EINTR) {
286 kprintf("reblock was interrupted\n");
288 kprintf("reblock failed: %d\n", error);
290 hmp->volume_to_remove = -1;
291 hammer_rel_volume(volume, 0);
299 while (hammer_flusher_haswork(hmp)) {
300 hammer_flusher_sync(hmp);
304 kprintf("HAMMER: flushing.");
307 tsleep(&count, 0, "hmrufl", hz);
310 kprintf("giving up");
316 hammer_sync_lock_sh(trans);
317 hammer_lock_ex(&hmp->blkmap_lock);
320 * We use stat later to update rootvol's bigblock stats
322 struct bigblock_stat stat;
323 error = hammer_free_freemap(trans, volume, &stat);
325 kprintf("Failed to free volume. Volume not empty!\n");
326 hmp->volume_to_remove = -1;
327 hammer_rel_volume(volume, 0);
328 hammer_unlock(&hmp->blkmap_lock);
329 hammer_sync_unlock(trans);
333 hmp->volume_to_remove = -1;
335 hammer_rel_volume(volume, 0);
340 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
341 hammer_unload_buffer, volume);
343 error = hammer_unload_volume(volume, NULL);
345 kprintf("Failed to unload volume\n");
346 hammer_unlock(&hmp->blkmap_lock);
347 hammer_sync_unlock(trans);
355 * Set each volume's new value of the vol_count field.
357 for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
358 volume = hammer_get_volume(hmp, vol_no, &error);
359 if (volume == NULL && error == ENOENT) {
361 * Skip unused volume numbers
367 KKASSERT(volume != NULL && error == 0);
368 hammer_modify_volume_field(trans, volume, vol_count);
369 volume->ondisk->vol_count = hmp->nvolumes;
370 hammer_modify_volume_done(volume);
373 * Only changes to the header of the root volume
374 * are automatically flushed to disk. For all
375 * other volumes that we modify we do it here.
377 if (volume != trans->rootvol && volume->io.modified) {
378 hammer_crc_set_volume(volume->ondisk);
379 hammer_io_flush(&volume->io, 0);
382 hammer_rel_volume(volume, 0);
386 * Update the total number of bigblocks
388 hammer_modify_volume_field(trans, trans->rootvol,
389 vol0_stat_bigblocks);
390 trans->rootvol->ondisk->vol0_stat_bigblocks -= stat.total_bigblocks;
391 hammer_modify_volume_done(trans->rootvol);
394 * Update the number of free bigblocks
395 * (including the copy in hmp)
397 hammer_modify_volume_field(trans, trans->rootvol,
398 vol0_stat_freebigblocks);
399 trans->rootvol->ondisk->vol0_stat_freebigblocks -= stat.total_free_bigblocks;
400 hmp->copy_stat_freebigblocks =
401 trans->rootvol->ondisk->vol0_stat_freebigblocks;
402 hammer_modify_volume_done(trans->rootvol);
405 hammer_unlock(&hmp->blkmap_lock);
406 hammer_sync_unlock(trans);
409 * Erase the volume header of the removed device.
411 * This is to not accidentally mount the volume again.
413 struct vnode *devvp = NULL;
414 error = hammer_setup_device(&devvp, ioc->device_name, 0);
416 kprintf("Failed to open device: %s\n", ioc->device_name);
420 error = hammer_clear_volume_header(devvp);
422 kprintf("Failed to clear volume header of device: %s\n",
426 hammer_close_device(&devvp, 0);
428 KKASSERT(error == 0);
430 hammer_unlock(&hmp->volume_lock);
436 * Iterate over all usable L1 entries of the volume and
437 * the corresponding L2 entries.
440 hammer_iterate_l1l2_entries(hammer_transaction_t trans, hammer_volume_t volume,
441 int (*callback)(hammer_transaction_t, hammer_volume_t, hammer_buffer_t*,
442 struct hammer_blockmap_layer1*, struct hammer_blockmap_layer2*,
443 hammer_off_t, hammer_off_t, void*),
446 struct hammer_mount *hmp = trans->hmp;
447 hammer_blockmap_t freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
448 hammer_buffer_t buffer = NULL;
451 hammer_off_t phys_off;
452 hammer_off_t block_off;
453 hammer_off_t layer1_off;
454 hammer_off_t layer2_off;
455 hammer_off_t aligned_buf_end_off;
456 struct hammer_blockmap_layer1 *layer1;
457 struct hammer_blockmap_layer2 *layer2;
460 * Calculate the usable size of the volume, which
461 * must be aligned at a bigblock (8 MB) boundary.
463 aligned_buf_end_off = (HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
464 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
465 & ~HAMMER_LARGEBLOCK_MASK64));
468 * Iterate the volume's address space in chunks of 4 TB, where each
469 * chunk consists of at least one physically available 8 MB bigblock.
471 * For each chunk we need one L1 entry and one L2 bigblock.
472 * We use the first bigblock of each chunk as L2 block.
474 for (phys_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
475 phys_off < aligned_buf_end_off;
476 phys_off += HAMMER_BLOCKMAP_LAYER2) {
478 block_off < HAMMER_BLOCKMAP_LAYER2;
479 block_off += HAMMER_LARGEBLOCK_SIZE) {
480 layer2_off = phys_off +
481 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_off);
482 layer2 = hammer_bread(hmp, layer2_off, &error, &buffer);
486 error = callback(trans, volume, &buffer, NULL,
487 layer2, phys_off, block_off, data);
492 layer1_off = freemap->phys_offset +
493 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_off);
494 layer1 = hammer_bread(hmp, layer1_off, &error, &buffer);
498 error = callback(trans, volume, &buffer, layer1, NULL,
506 hammer_rel_buffer(buffer, 0);
515 format_callback(hammer_transaction_t trans, hammer_volume_t volume,
516 hammer_buffer_t *bufferp,
517 struct hammer_blockmap_layer1 *layer1,
518 struct hammer_blockmap_layer2 *layer2,
519 hammer_off_t phys_off,
520 hammer_off_t block_off,
523 struct bigblock_stat *stat = (struct bigblock_stat*)data;
526 * Calculate the usable size of the volume, which must be aligned
527 * at a bigblock (8 MB) boundary.
529 hammer_off_t aligned_buf_end_off;
530 aligned_buf_end_off = (HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
531 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
532 & ~HAMMER_LARGEBLOCK_MASK64));
535 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
537 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
538 bzero(layer1, sizeof(layer1));
539 layer1->phys_offset = phys_off;
540 layer1->blocks_free = stat->counter;
541 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
542 hammer_modify_buffer_done(*bufferp);
544 stat->total_free_bigblocks += stat->counter;
545 stat->counter = 0; /* reset */
547 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
548 bzero(layer2, sizeof(*layer2));
550 if (block_off == 0) {
552 * The first entry represents the L2 bigblock itself.
554 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
555 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
556 layer2->bytes_free = 0;
557 ++stat->total_bigblocks;
558 } else if (phys_off + block_off < aligned_buf_end_off) {
563 layer2->append_off = 0;
564 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
565 ++stat->total_bigblocks;
569 * Bigblock outside of physically available
572 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
573 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
574 layer2->bytes_free = 0;
577 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
578 hammer_modify_buffer_done(*bufferp);
587 hammer_format_freemap(hammer_transaction_t trans, hammer_volume_t volume,
588 struct bigblock_stat *stat)
590 stat->total_bigblocks = 0;
591 stat->total_free_bigblocks = 0;
593 return hammer_iterate_l1l2_entries(trans, volume, format_callback, stat);
597 free_callback(hammer_transaction_t trans, hammer_volume_t volume __unused,
598 hammer_buffer_t *bufferp,
599 struct hammer_blockmap_layer1 *layer1,
600 struct hammer_blockmap_layer2 *layer2,
601 hammer_off_t phys_off,
602 hammer_off_t block_off __unused,
605 struct bigblock_stat *stat = (struct bigblock_stat*)data;
608 * No modifications to ondisk structures
610 int testonly = (stat == NULL);
613 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
615 * This layer1 entry is already free.
620 KKASSERT((int)HAMMER_VOL_DECODE(layer1->phys_offset) ==
621 trans->hmp->volume_to_remove);
629 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
630 bzero(layer1, sizeof(layer1));
631 layer1->phys_offset = HAMMER_BLOCKMAP_UNAVAIL;
632 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
633 hammer_modify_buffer_done(*bufferp);
637 if (layer2->zone == HAMMER_ZONE_UNAVAIL_INDEX) {
641 if (layer2->zone == HAMMER_ZONE_FREEMAP_INDEX) {
643 ++stat->total_bigblocks;
648 if (layer2->append_off == 0 &&
649 layer2->bytes_free == HAMMER_LARGEBLOCK_SIZE) {
651 ++stat->total_bigblocks;
652 ++stat->total_free_bigblocks;
658 * We found a layer2 entry that is not empty!
669 hammer_free_freemap(hammer_transaction_t trans, hammer_volume_t volume,
670 struct bigblock_stat *stat)
674 stat->total_bigblocks = 0;
675 stat->total_free_bigblocks = 0;
678 error = hammer_iterate_l1l2_entries(trans, volume, free_callback, NULL);
682 error = hammer_iterate_l1l2_entries(trans, volume, free_callback, stat);
686 /************************************************************************
688 ************************************************************************
692 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly)
695 struct nlookupdata nd;
698 * Get the device vnode
700 if (*devvpp == NULL) {
701 error = nlookup_init(&nd, dev_path, UIO_SYSSPACE, NLC_FOLLOW);
703 error = nlookup(&nd);
705 error = cache_vref(&nd.nl_nch, nd.nl_cred, devvpp);
712 if (vn_isdisk(*devvpp, &error)) {
713 error = vfs_mountedon(*devvpp);
716 if (error == 0 && vcount(*devvpp) > 0)
719 vn_lock(*devvpp, LK_EXCLUSIVE | LK_RETRY);
720 error = vinvalbuf(*devvpp, V_SAVE, 0, 0);
722 error = VOP_OPEN(*devvpp,
723 (ronly ? FREAD : FREAD|FWRITE),
728 if (error && *devvpp) {
736 hammer_close_device(struct vnode **devvpp, int ronly)
738 VOP_CLOSE(*devvpp, (ronly ? FREAD : FREAD|FWRITE));
740 vinvalbuf(*devvpp, ronly ? 0 : V_SAVE, 0, 0);
747 hammer_format_volume_header(struct hammer_mount *hmp, struct vnode *devvp,
748 const char *vol_name, int vol_no, int vol_count,
749 int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
751 struct buf *bp = NULL;
752 struct hammer_volume_ondisk *ondisk;
756 * Extract the volume number from the volume header and do various
759 KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
760 error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
761 if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
764 ondisk = (struct hammer_volume_ondisk*) bp->b_data;
767 * Note that we do NOT allow to use a device that contains
768 * a valid HAMMER signature. It has to be cleaned up with dd
771 if (ondisk->vol_signature == HAMMER_FSBUF_VOLUME) {
772 kprintf("hammer_volume_add: Formatting of valid HAMMER volume "
773 "%s denied. Erase with dd!\n", vol_name);
778 bzero(ondisk, sizeof(struct hammer_volume_ondisk));
779 ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
780 ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
781 ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
782 ondisk->vol_fsid = hmp->fsid;
783 ondisk->vol_rootvol = hmp->rootvol->vol_no;
784 ondisk->vol_no = vol_no;
785 ondisk->vol_count = vol_count;
786 ondisk->vol_version = hmp->version;
789 * Reserve space for (future) header junk, setup our poor-man's
790 * bigblock allocator.
792 int64_t vol_alloc = HAMMER_BUFSIZE * 16;
794 ondisk->vol_bot_beg = vol_alloc;
795 vol_alloc += boot_area_size;
796 ondisk->vol_mem_beg = vol_alloc;
797 vol_alloc += mem_area_size;
800 * The remaining area is the zone 2 buffer allocation area. These
803 ondisk->vol_buf_beg = vol_alloc;
804 ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
806 if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
807 kprintf("volume %d %s is too small to hold the volume header",
808 ondisk->vol_no, ondisk->vol_name);
813 ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
815 ondisk->vol_blocksize = HAMMER_BUFSIZE;
818 * Write volume header to disk
830 * Invalidates the volume header. Used by volume-del.
833 hammer_clear_volume_header(struct vnode *devvp)
835 struct buf *bp = NULL;
836 struct hammer_volume_ondisk *ondisk;
839 KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
840 error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
841 if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
844 ondisk = (struct hammer_volume_ondisk*) bp->b_data;
845 bzero(ondisk, sizeof(struct hammer_volume_ondisk));