fc93392442bb51bda809577aa21380099409dd9d
[dragonfly.git] / sys / vfs / hammer / hammer_expand.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
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>
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"
38 #include <sys/fcntl.h>
39 #include <sys/nlookup.h>
40 #include <sys/buf.h>
41
42 static int
43 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly);
44
45 static void
46 hammer_close_device(struct vnode **devvpp, int ronly);
47
48 static int
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);
52
53 static int
54 hammer_format_freemap(struct hammer_mount *hmp,
55         hammer_transaction_t trans,
56         hammer_volume_t volume,
57         hammer_volume_t root_volume);
58
59 static uint64_t
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,
65         int *errorp);
66
67 static void
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,
74         int *errorp);
75
76 int
77 hammer_ioc_expand(hammer_transaction_t trans, hammer_inode_t ip,
78                 struct hammer_ioc_expand *expand)
79 {
80         struct hammer_mount *hmp = trans->hmp;
81         struct mount *mp = hmp->mp;
82         hammer_volume_t volume;
83         hammer_volume_t root_volume;
84         int error;
85
86         if (mp->mnt_flag & MNT_RDONLY) {
87                 kprintf("Cannot expand read-only HAMMER filesystem\n");
88                 return (EINVAL);
89         }
90
91         if (hmp->nvolumes + 1 >= HAMMER_MAX_VOLUMES) {
92                 kprintf("Max number of HAMMER volumes exceeded\n");
93                 return (EINVAL);
94         }
95
96         /*
97          * Find an unused volume number.
98          */
99         int free_vol_no = 0;
100         while (free_vol_no < HAMMER_MAX_VOLUMES &&
101                RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, free_vol_no)) {
102                 ++free_vol_no;
103         }
104         if (free_vol_no >= HAMMER_MAX_VOLUMES) {
105                 kprintf("Max number of HAMMER volumes exceeded\n");
106                 return (EINVAL);
107         }
108
109         struct vnode *devvp = NULL;
110         error = hammer_setup_device(&devvp, expand->device_name, 0);
111         if (error)
112                 goto end;
113         KKASSERT(devvp);
114         error = hammer_format_volume_header(
115                 hmp,
116                 devvp,
117                 hmp->rootvol->ondisk->vol_name,
118                 free_vol_no,
119                 hmp->nvolumes+1,
120                 expand->vol_size,
121                 expand->boot_area_size,
122                 expand->mem_area_size);
123         hammer_close_device(&devvp, 0);
124         if (error)
125                 goto end;
126
127         error = hammer_install_volume(hmp, expand->device_name, NULL);
128         if (error)
129                 goto end;
130
131         hammer_sync_lock_sh(trans);
132         hammer_lock_ex(&hmp->blkmap_lock);
133
134         ++hmp->nvolumes;
135
136         /*
137          * Set each volumes new value of the vol_count field.
138          */
139         for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
140                 if (vol_no == free_vol_no)
141                         continue;
142
143                 volume = hammer_get_volume(hmp, vol_no, &error);
144                 if (volume == NULL && error == ENOENT) {
145                         /*
146                          * Skip unused volume numbers
147                          */
148                         error = 0;
149                         continue;
150                 }
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);
156         }
157
158         volume = hammer_get_volume(hmp, free_vol_no, &error);
159         KKASSERT(volume != NULL && error == 0);
160
161         root_volume = hammer_get_root_volume(hmp, &error);
162         KKASSERT(root_volume != NULL && error == 0);
163
164         error = hammer_format_freemap(hmp, trans, volume, root_volume);
165
166         hammer_rel_volume(root_volume, 0);
167         hammer_rel_volume(volume, 0);
168
169         hammer_unlock(&hmp->blkmap_lock);
170         hammer_sync_unlock(trans);
171
172 end:
173         if (error)
174                 kprintf("An error occurred: %d\n", error);
175         return (error);
176 }
177
178 static int
179 hammer_format_freemap(struct hammer_mount *hmp,
180         hammer_transaction_t trans,
181         hammer_volume_t volume,
182         hammer_volume_t root_volume)
183 {
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;
190         int error = 0;
191
192         total_free_bigblocks = 0;
193
194         /*
195          * Calculate the usable size of the new volume, which
196          * must be aligned at a bigblock (8 MB) boundary.
197          */
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);
201
202         freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
203
204         /*
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.
208          *
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.
211          */
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) {
215
216                 free_bigblocks = hammer_format_layer2_chunk(hmp, trans,
217                         phys_offset, aligned_buf_end_off, &buffer, &error);
218                 KKASSERT(error == 0);
219
220                 hammer_set_layer1_entry(hmp, trans, phys_offset,
221                         free_bigblocks, freemap, &buffer, &error);
222                 KKASSERT(error == 0);
223
224                 total_free_bigblocks += free_bigblocks;
225         }
226
227         /*
228          * Increase the total number of bigblocks
229          */
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);
234
235         /*
236          * Increase the number of free bigblocks
237          * (including the copy in hmp)
238          */
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);
245
246         if (buffer) {
247                 hammer_rel_buffer(buffer, 0);
248                 buffer = NULL;
249         }
250
251         return (error);
252 }
253
254 /*
255  * Format the L2 bigblock representing a 4 TB chunk.
256  *
257  * Returns the number of free bigblocks.
258  */
259 static uint64_t
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,
265         int *errorp)
266 {
267         uint64_t free_bigblocks = 0;
268         hammer_off_t block_off;
269         hammer_off_t layer2_offset;
270         struct hammer_blockmap_layer2 *layer2;
271
272         for (block_off = 0;
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);
278                 if (*errorp)
279                         return free_bigblocks;
280
281                 KKASSERT(layer2);
282
283                 hammer_modify_buffer(trans, *bufferp, layer2, sizeof(*layer2));
284                 bzero(layer2, sizeof(*layer2));
285
286                 if (block_off == 0) {
287                         /*
288                          * The first entry represents the L2 bigblock itself.
289                          */
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) {
294                         layer2->zone = 0;
295                         layer2->append_off = 0;
296                         layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
297                         ++free_bigblocks;
298                 } else {
299                         /*
300                          * Bigblock outside of physically available space
301                          */
302                         layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
303                         layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
304                         layer2->bytes_free = 0;
305                 }
306                 layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
307
308                 hammer_modify_buffer_done(*bufferp);
309         }
310
311         return free_bigblocks;
312 }
313
314 static void
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,
321         int *errorp)
322 {
323         struct hammer_blockmap_layer1 *layer1;
324         hammer_off_t layer1_offset;
325
326         layer1_offset = freemap->phys_offset +
327                         HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
328         layer1 = hammer_bread(hmp, layer1_offset, errorp, bufferp);
329         if (*errorp)
330                 return;
331         KKASSERT(layer1);
332         KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
333
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);
339
340         hammer_modify_buffer_done(*bufferp);
341 }
342
343 static int
344 hammer_setup_device(struct vnode **devvpp, const char *dev_path, int ronly)
345 {
346         int error;
347         struct nlookupdata nd;
348
349         /*
350          * Get the device vnode
351          */
352         if (*devvpp == NULL) {
353                 error = nlookup_init(&nd, dev_path, UIO_SYSSPACE, NLC_FOLLOW);
354                 if (error == 0)
355                         error = nlookup(&nd);
356                 if (error == 0)
357                         error = cache_vref(&nd.nl_nch, nd.nl_cred, devvpp);
358                 nlookup_done(&nd);
359         } else {
360                 error = 0;
361         }
362
363         if (error == 0) {
364                 if (vn_isdisk(*devvpp, &error)) {
365                         error = vfs_mountedon(*devvpp);
366                 }
367         }
368         if (error == 0 && vcount(*devvpp) > 0)
369                 error = EBUSY;
370         if (error == 0) {
371                 vn_lock(*devvpp, LK_EXCLUSIVE | LK_RETRY);
372                 error = vinvalbuf(*devvpp, V_SAVE, 0, 0);
373                 if (error == 0) {
374                         error = VOP_OPEN(*devvpp,
375                                          (ronly ? FREAD : FREAD|FWRITE),
376                                          FSCRED, NULL);
377                 }
378                 vn_unlock(*devvpp);
379         }
380         if (error && *devvpp) {
381                 vrele(*devvpp);
382                 *devvpp = NULL;
383         }
384         return (error);
385 }
386
387 static void
388 hammer_close_device(struct vnode **devvpp, int ronly)
389 {
390         VOP_CLOSE(*devvpp, (ronly ? FREAD : FREAD|FWRITE));
391         if (*devvpp) {
392                 vinvalbuf(*devvpp, ronly ? 0 : V_SAVE, 0, 0);
393                 vrele(*devvpp);
394                 *devvpp = NULL;
395         }
396 }
397
398 static int
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)
402 {
403         struct buf *bp = NULL;
404         struct hammer_volume_ondisk *ondisk;
405         int error;
406
407         /*
408          * Extract the volume number from the volume header and do various
409          * sanity checks.
410          */
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))
414                 goto late_failure;
415
416         ondisk = (struct hammer_volume_ondisk*) bp->b_data;
417
418         /*
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
421          * before.
422          */
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);
426                 error = EFTYPE;
427                 goto late_failure;
428         }
429
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;
439
440         /*
441          * Reserve space for (future) header junk, setup our poor-man's
442          * bigblock allocator.
443          */
444         int64_t vol_alloc = HAMMER_BUFSIZE * 16;
445
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;
450
451         /*
452          * The remaining area is the zone 2 buffer allocation area.  These
453          * buffers
454          */
455         ondisk->vol_buf_beg = vol_alloc;
456         ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
457
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);
461                 error = EFTYPE;
462                 goto late_failure;
463         }
464
465         ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
466                               HAMMER_BUFSIZE;
467         ondisk->vol_blocksize = HAMMER_BUFSIZE;
468
469         /*
470          * Write volume header to disk
471          */
472         error = bwrite(bp);
473         bp = NULL;
474
475 late_failure:
476         if (bp)
477                 brelse(bp);
478         return (error);
479 }