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_format_freemap(struct hammer_mount *hmp,
55 hammer_transaction_t trans,
56 hammer_volume_t volume,
57 hammer_volume_t root_volume);
60 hammer_format_layer2_chunk(struct hammer_mount *hmp,
61 hammer_transaction_t trans,
62 hammer_off_t phys_offset,
63 hammer_off_t aligned_buf_end_off,
64 hammer_buffer_t *bufferp,
68 hammer_set_layer1_entry(struct hammer_mount *hmp,
69 hammer_transaction_t trans,
70 hammer_off_t phys_offset,
71 uint64_t free_bigblocks,
72 hammer_blockmap_t freemap,
73 hammer_buffer_t *bufferp,
77 hammer_ioc_expand(hammer_transaction_t trans, hammer_inode_t ip,
78 struct hammer_ioc_expand *expand)
80 struct hammer_mount *hmp = trans->hmp;
81 struct mount *mp = hmp->mp;
82 hammer_volume_t volume;
83 hammer_volume_t root_volume;
86 if (mp->mnt_flag & MNT_RDONLY) {
87 kprintf("Cannot expand read-only HAMMER filesystem\n");
91 if (hmp->nvolumes + 1 >= HAMMER_MAX_VOLUMES) {
92 kprintf("Max number of HAMMER volumes exceeded\n");
97 * Find an unused volume number.
100 while (free_vol_no < HAMMER_MAX_VOLUMES &&
101 RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, free_vol_no)) {
104 if (free_vol_no >= HAMMER_MAX_VOLUMES) {
105 kprintf("Max number of HAMMER volumes exceeded\n");
109 struct vnode *devvp = NULL;
110 error = hammer_setup_device(&devvp, expand->device_name, 0);
114 error = hammer_format_volume_header(
117 hmp->rootvol->ondisk->vol_name,
121 expand->boot_area_size,
122 expand->mem_area_size);
123 hammer_close_device(&devvp, 0);
127 error = hammer_install_volume(hmp, expand->device_name, NULL);
131 hammer_sync_lock_sh(trans);
132 hammer_lock_ex(&hmp->blkmap_lock);
137 * Set each volumes new value of the vol_count field.
139 for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
140 volume = hammer_get_volume(hmp, vol_no, &error);
141 if (volume == NULL && error == ENOENT) {
143 * Skip unused volume numbers
148 KKASSERT(error == 0);
149 hammer_modify_volume_field(trans, volume, vol_count);
150 volume->ondisk->vol_count = hmp->nvolumes;
151 hammer_modify_volume_done(volume);
152 hammer_rel_volume(volume, 0);
155 volume = hammer_get_volume(hmp, free_vol_no, &error);
156 KKASSERT(volume != NULL && error == 0);
158 root_volume = hammer_get_root_volume(hmp, &error);
159 KKASSERT(root_volume && error == 0);
161 error = hammer_format_freemap(hmp, trans, volume, root_volume);
163 hammer_rel_volume(root_volume, 0);
164 hammer_rel_volume(volume, 0);
166 hammer_unlock(&hmp->blkmap_lock);
167 hammer_sync_unlock(trans);
171 kprintf("An error occurred: %d\n", error);
176 hammer_format_freemap(struct hammer_mount *hmp,
177 hammer_transaction_t trans,
178 hammer_volume_t volume,
179 hammer_volume_t root_volume)
181 hammer_off_t phys_offset;
182 hammer_buffer_t buffer = NULL;
183 hammer_blockmap_t freemap;
184 hammer_off_t aligned_buf_end_off;
185 uint64_t free_bigblocks;
189 * Calculate the usable size of the new volume, which
190 * must be aligned at a bigblock (8 MB) boundary.
192 aligned_buf_end_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
193 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
194 & ~HAMMER_LARGEBLOCK_MASK64);
196 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
199 * Iterate the volume's address space in chunks of 4 TB,
200 * where each chunk consists of at least one physically
201 * available 8 MB bigblock.
203 * For each chunk we need one L1 entry and one L2 bigblock.
204 * We use the first bigblock of each chunk as L2 block.
206 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
207 phys_offset < aligned_buf_end_off;
208 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
210 free_bigblocks = hammer_format_layer2_chunk(hmp, trans,
211 phys_offset, aligned_buf_end_off, &buffer, &error);
212 KKASSERT(error == 0);
214 hammer_set_layer1_entry(hmp, trans, phys_offset,
215 free_bigblocks, freemap, &buffer, &error);
216 KKASSERT(error == 0);
219 * Increase the total number of bigblocks
221 hammer_modify_volume_field(trans, root_volume,
222 vol0_stat_bigblocks);
223 root_volume->ondisk->vol0_stat_bigblocks += free_bigblocks;
224 hammer_modify_volume_done(root_volume);
227 * Increase the number of free bigblocks
228 * (including the copy in hmp)
230 hammer_modify_volume_field(trans, root_volume,
231 vol0_stat_freebigblocks);
232 root_volume->ondisk->vol0_stat_freebigblocks += free_bigblocks;
233 hmp->copy_stat_freebigblocks =
234 root_volume->ondisk->vol0_stat_freebigblocks;
235 hammer_modify_volume_done(root_volume);
239 hammer_rel_buffer(buffer, 0);
247 * Format the L2 bigblock representing a 4 TB chunk.
249 * Returns the number of free bigblocks.
252 hammer_format_layer2_chunk(struct hammer_mount *hmp,
253 hammer_transaction_t trans,
254 hammer_off_t phys_offset,
255 hammer_off_t aligned_buf_end_off,
256 hammer_buffer_t *bufferp,
259 uint64_t free_bigblocks = 0;
260 hammer_off_t block_off;
261 hammer_off_t layer2_offset;
262 struct hammer_blockmap_layer2 *layer2;
265 block_off < HAMMER_BLOCKMAP_LAYER2;
266 block_off += HAMMER_LARGEBLOCK_SIZE) {
267 layer2_offset = phys_offset +
268 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_off);
269 layer2 = hammer_bread(hmp, layer2_offset, errorp, bufferp);
271 return free_bigblocks;
275 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
276 bzero(layer2, sizeof(*layer2));
278 if (block_off == 0) {
280 * The first entry represents the L2 bigblock itself.
282 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
283 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
284 layer2->bytes_free = 0;
285 } else if (phys_offset + block_off < aligned_buf_end_off) {
287 layer2->append_off = 0;
288 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
292 * Bigblock outside of physically available space
294 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
295 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
296 layer2->bytes_free = 0;
298 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
300 hammer_modify_buffer_done(*bufferp);
303 return free_bigblocks;
307 hammer_set_layer1_entry(struct hammer_mount *hmp,
308 hammer_transaction_t trans,
309 hammer_off_t phys_offset,
310 uint64_t free_bigblocks,
311 hammer_blockmap_t freemap,
312 hammer_buffer_t *bufferp,
315 struct hammer_blockmap_layer1 *layer1;
316 hammer_off_t layer1_offset;
318 layer1_offset = freemap->phys_offset +
319 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
320 layer1 = hammer_bread(hmp, layer1_offset, errorp, bufferp);
324 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
326 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
327 bzero(layer1, sizeof(*layer1));
328 layer1->phys_offset = phys_offset;
329 layer1->blocks_free = free_bigblocks;
330 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
332 hammer_modify_buffer_done(*bufferp);
336 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly)
339 struct nlookupdata nd;
342 * Get the device vnode
344 if (*devvpp == NULL) {
345 error = nlookup_init(&nd, dev_path, UIO_SYSSPACE, NLC_FOLLOW);
347 error = nlookup(&nd);
349 error = cache_vref(&nd.nl_nch, nd.nl_cred, devvpp);
356 if (vn_isdisk(*devvpp, &error)) {
357 error = vfs_mountedon(*devvpp);
360 if (error == 0 && vcount(*devvpp) > 0)
363 vn_lock(*devvpp, LK_EXCLUSIVE | LK_RETRY);
364 error = vinvalbuf(*devvpp, V_SAVE, 0, 0);
366 error = VOP_OPEN(*devvpp,
367 (ronly ? FREAD : FREAD|FWRITE),
372 if (error && *devvpp) {
380 hammer_close_device(struct vnode **devvpp, int ronly)
382 VOP_CLOSE(*devvpp, (ronly ? FREAD : FREAD|FWRITE));
384 vinvalbuf(*devvpp, ronly ? 0 : V_SAVE, 0, 0);
391 hammer_format_volume_header(struct hammer_mount *hmp, struct vnode *devvp,
392 const char *vol_name, int vol_no, int vol_count,
393 int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
395 struct buf *bp = NULL;
396 struct hammer_volume_ondisk *ondisk;
400 * Extract the volume number from the volume header and do various
403 KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
404 error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
405 if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
408 ondisk = (struct hammer_volume_ondisk*) bp->b_data;
411 * Note that we do NOT allow to use a device that contains
412 * a valid HAMMER signature. It has to be cleaned up with dd
415 if (ondisk->vol_signature == HAMMER_FSBUF_VOLUME) {
416 kprintf("hammer_expand: Formatting of valid HAMMER volume "
417 "%s denied. Erase with dd!\n", vol_name);
422 bzero(ondisk, sizeof(struct hammer_volume_ondisk));
423 ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
424 ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
425 ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
426 ondisk->vol_fsid = hmp->fsid;
427 ondisk->vol_rootvol = hmp->rootvol->vol_no;
428 ondisk->vol_no = vol_no;
429 ondisk->vol_count = vol_count;
430 ondisk->vol_version = hmp->version;
433 * Reserve space for (future) header junk, setup our poor-man's
434 * bigblock allocator.
436 int64_t vol_alloc = HAMMER_BUFSIZE * 16;
438 ondisk->vol_bot_beg = vol_alloc;
439 vol_alloc += boot_area_size;
440 ondisk->vol_mem_beg = vol_alloc;
441 vol_alloc += mem_area_size;
444 * The remaining area is the zone 2 buffer allocation area. These
447 ondisk->vol_buf_beg = vol_alloc;
448 ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
450 if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
451 kprintf("volume %d %s is too small to hold the volume header",
452 ondisk->vol_no, ondisk->vol_name);
457 ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
459 ondisk->vol_blocksize = HAMMER_BUFSIZE;
462 * Write volume header to disk