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 if (vol_no == free_vol_no)
143 volume = hammer_get_volume(hmp, vol_no, &error);
144 if (volume == NULL && error == ENOENT) {
146 * Skip unused volume numbers
151 KKASSERT(volume != NULL && error == 0);
152 hammer_modify_volume_field(trans, volume, vol_count);
153 volume->ondisk->vol_count = hmp->nvolumes;
154 hammer_modify_volume_done(volume);
155 hammer_rel_volume(volume, 0);
158 volume = hammer_get_volume(hmp, free_vol_no, &error);
159 KKASSERT(volume != NULL && error == 0);
161 root_volume = hammer_get_root_volume(hmp, &error);
162 KKASSERT(root_volume != NULL && error == 0);
164 error = hammer_format_freemap(hmp, trans, volume, root_volume);
166 hammer_rel_volume(root_volume, 0);
167 hammer_rel_volume(volume, 0);
169 hammer_unlock(&hmp->blkmap_lock);
170 hammer_sync_unlock(trans);
174 kprintf("An error occurred: %d\n", error);
179 hammer_format_freemap(struct hammer_mount *hmp,
180 hammer_transaction_t trans,
181 hammer_volume_t volume,
182 hammer_volume_t root_volume)
184 hammer_off_t phys_offset;
185 hammer_buffer_t buffer = NULL;
186 hammer_blockmap_t freemap;
187 hammer_off_t aligned_buf_end_off;
188 uint64_t free_bigblocks;
189 uint64_t total_free_bigblocks;
192 total_free_bigblocks = 0;
195 * Calculate the usable size of the new volume, which
196 * must be aligned at a bigblock (8 MB) boundary.
198 aligned_buf_end_off = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no,
199 (volume->ondisk->vol_buf_end - volume->ondisk->vol_buf_beg)
200 & ~HAMMER_LARGEBLOCK_MASK64);
202 freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
205 * Iterate the volume's address space in chunks of 4 TB,
206 * where each chunk consists of at least one physically
207 * available 8 MB bigblock.
209 * For each chunk we need one L1 entry and one L2 bigblock.
210 * We use the first bigblock of each chunk as L2 block.
212 for (phys_offset = HAMMER_ENCODE_RAW_BUFFER(volume->ondisk->vol_no, 0);
213 phys_offset < aligned_buf_end_off;
214 phys_offset += HAMMER_BLOCKMAP_LAYER2) {
216 free_bigblocks = hammer_format_layer2_chunk(hmp, trans,
217 phys_offset, aligned_buf_end_off, &buffer, &error);
218 KKASSERT(error == 0);
220 hammer_set_layer1_entry(hmp, trans, phys_offset,
221 free_bigblocks, freemap, &buffer, &error);
222 KKASSERT(error == 0);
224 total_free_bigblocks += free_bigblocks;
228 * Increase the total number of bigblocks
230 hammer_modify_volume_field(trans, root_volume,
231 vol0_stat_bigblocks);
232 root_volume->ondisk->vol0_stat_bigblocks += total_free_bigblocks;
233 hammer_modify_volume_done(root_volume);
236 * Increase the number of free bigblocks
237 * (including the copy in hmp)
239 hammer_modify_volume_field(trans, root_volume,
240 vol0_stat_freebigblocks);
241 root_volume->ondisk->vol0_stat_freebigblocks += total_free_bigblocks;
242 hmp->copy_stat_freebigblocks =
243 root_volume->ondisk->vol0_stat_freebigblocks;
244 hammer_modify_volume_done(root_volume);
247 hammer_rel_buffer(buffer, 0);
255 * Format the L2 bigblock representing a 4 TB chunk.
257 * Returns the number of free bigblocks.
260 hammer_format_layer2_chunk(struct hammer_mount *hmp,
261 hammer_transaction_t trans,
262 hammer_off_t phys_offset,
263 hammer_off_t aligned_buf_end_off,
264 hammer_buffer_t *bufferp,
267 uint64_t free_bigblocks = 0;
268 hammer_off_t block_off;
269 hammer_off_t layer2_offset;
270 struct hammer_blockmap_layer2 *layer2;
273 block_off < HAMMER_BLOCKMAP_LAYER2;
274 block_off += HAMMER_LARGEBLOCK_SIZE) {
275 layer2_offset = phys_offset +
276 HAMMER_BLOCKMAP_LAYER2_OFFSET(block_off);
277 layer2 = hammer_bread(hmp, layer2_offset, errorp, bufferp);
279 return free_bigblocks;
283 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
284 bzero(layer2, sizeof(*layer2));
286 if (block_off == 0) {
288 * The first entry represents the L2 bigblock itself.
290 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
291 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
292 layer2->bytes_free = 0;
293 } else if (phys_offset + block_off < aligned_buf_end_off) {
295 layer2->append_off = 0;
296 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
300 * Bigblock outside of physically available space
302 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
303 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
304 layer2->bytes_free = 0;
306 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
308 hammer_modify_buffer_done(*bufferp);
311 return free_bigblocks;
315 hammer_set_layer1_entry(struct hammer_mount *hmp,
316 hammer_transaction_t trans,
317 hammer_off_t phys_offset,
318 uint64_t free_bigblocks,
319 hammer_blockmap_t freemap,
320 hammer_buffer_t *bufferp,
323 struct hammer_blockmap_layer1 *layer1;
324 hammer_off_t layer1_offset;
326 layer1_offset = freemap->phys_offset +
327 HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
328 layer1 = hammer_bread(hmp, layer1_offset, errorp, bufferp);
332 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
334 hammer_modify_buffer(trans, *bufferp, layer1, sizeof(*layer1));
335 bzero(layer1, sizeof(*layer1));
336 layer1->phys_offset = phys_offset;
337 layer1->blocks_free = free_bigblocks;
338 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
340 hammer_modify_buffer_done(*bufferp);
344 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly)
347 struct nlookupdata nd;
350 * Get the device vnode
352 if (*devvpp == NULL) {
353 error = nlookup_init(&nd, dev_path, UIO_SYSSPACE, NLC_FOLLOW);
355 error = nlookup(&nd);
357 error = cache_vref(&nd.nl_nch, nd.nl_cred, devvpp);
364 if (vn_isdisk(*devvpp, &error)) {
365 error = vfs_mountedon(*devvpp);
368 if (error == 0 && vcount(*devvpp) > 0)
371 vn_lock(*devvpp, LK_EXCLUSIVE | LK_RETRY);
372 error = vinvalbuf(*devvpp, V_SAVE, 0, 0);
374 error = VOP_OPEN(*devvpp,
375 (ronly ? FREAD : FREAD|FWRITE),
380 if (error && *devvpp) {
388 hammer_close_device(struct vnode **devvpp, int ronly)
390 VOP_CLOSE(*devvpp, (ronly ? FREAD : FREAD|FWRITE));
392 vinvalbuf(*devvpp, ronly ? 0 : V_SAVE, 0, 0);
399 hammer_format_volume_header(struct hammer_mount *hmp, struct vnode *devvp,
400 const char *vol_name, int vol_no, int vol_count,
401 int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size)
403 struct buf *bp = NULL;
404 struct hammer_volume_ondisk *ondisk;
408 * Extract the volume number from the volume header and do various
411 KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
412 error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
413 if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
416 ondisk = (struct hammer_volume_ondisk*) bp->b_data;
419 * Note that we do NOT allow to use a device that contains
420 * a valid HAMMER signature. It has to be cleaned up with dd
423 if (ondisk->vol_signature == HAMMER_FSBUF_VOLUME) {
424 kprintf("hammer_expand: Formatting of valid HAMMER volume "
425 "%s denied. Erase with dd!\n", vol_name);
430 bzero(ondisk, sizeof(struct hammer_volume_ondisk));
431 ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
432 ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
433 ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
434 ondisk->vol_fsid = hmp->fsid;
435 ondisk->vol_rootvol = hmp->rootvol->vol_no;
436 ondisk->vol_no = vol_no;
437 ondisk->vol_count = vol_count;
438 ondisk->vol_version = hmp->version;
441 * Reserve space for (future) header junk, setup our poor-man's
442 * bigblock allocator.
444 int64_t vol_alloc = HAMMER_BUFSIZE * 16;
446 ondisk->vol_bot_beg = vol_alloc;
447 vol_alloc += boot_area_size;
448 ondisk->vol_mem_beg = vol_alloc;
449 vol_alloc += mem_area_size;
452 * The remaining area is the zone 2 buffer allocation area. These
455 ondisk->vol_buf_beg = vol_alloc;
456 ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
458 if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
459 kprintf("volume %d %s is too small to hold the volume header",
460 ondisk->vol_no, ondisk->vol_name);
465 ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
467 ondisk->vol_blocksize = HAMMER_BUFSIZE;
470 * Write volume header to disk