1359a541345b1600d705eee87e0129f6d29f49a1
[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_format_volume_header(struct hammer_mount *hmp, const char *dev_path,
44         const char *vol_name, int vol_no, int vol_count,
45         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size,
46         uint64_t *num_layer1_entries_p, uint64_t *layer1_free_blocks);
47
48 int
49 hammer_ioc_expand(hammer_transaction_t trans, hammer_inode_t ip,
50                 struct hammer_ioc_expand *expand)
51 {
52         struct hammer_mount *hmp = trans->hmp;
53         struct mount *mp = hmp->mp;
54         int error;
55
56         if (mp->mnt_flag & MNT_RDONLY) {
57                 kprintf("Cannot expand read-only HAMMER filesystem\n");
58                 return (EINVAL);
59         }
60
61         if (hmp->nvolumes + 1 >= HAMMER_MAX_VOLUMES) {
62                 kprintf("Max number of HAMMER volumes exceeded\n");
63                 return (EINVAL);
64         }
65
66         /*
67          * Find an unused volume number.
68          */
69         int free_vol_no = 0;
70         while (free_vol_no < HAMMER_MAX_VOLUMES &&
71                RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, free_vol_no)) {
72                 ++free_vol_no;
73         }
74         if (free_vol_no >= HAMMER_MAX_VOLUMES) {
75                 kprintf("Max number of HAMMER volumes exceeded\n");
76                 return (EINVAL);
77         }
78
79         uint64_t num_layer1_entries = 0;
80         uint64_t *layer1_free_blocks =
81                 kmalloc(1024 * sizeof(uint64_t), M_TEMP, M_WAITOK|M_ZERO);
82
83         error = hammer_format_volume_header(
84                 hmp,
85                 expand->device_name,
86                 hmp->rootvol->ondisk->vol_name,
87                 free_vol_no,
88                 hmp->nvolumes+1,
89                 expand->vol_size,
90                 expand->boot_area_size,
91                 expand->mem_area_size,
92                 &num_layer1_entries /* out param */,
93                 layer1_free_blocks);
94         KKASSERT(num_layer1_entries < 1024);
95         if (error)
96                 goto end;
97
98         error = hammer_install_volume(hmp, expand->device_name, NULL);
99         if (error)
100                 goto end;
101
102         ++hmp->nvolumes;
103
104         hammer_sync_lock_sh(trans);
105         hammer_lock_ex(&hmp->blkmap_lock);
106
107         /*
108          * Set each volumes new value of the vol_count field.
109          */
110         for (int vol_no = 0; vol_no < HAMMER_MAX_VOLUMES; ++vol_no) {
111                 hammer_volume_t volume;
112                 volume = hammer_get_volume(hmp, vol_no, &error);
113                 if (volume == NULL && error == ENOENT) {
114                         /*
115                          * Skip unused volume numbers
116                          */
117                         error = 0;
118                         continue;
119                 }
120                 KKASSERT(error == 0);
121                 hammer_modify_volume_field(trans, volume, vol_count);
122                 volume->ondisk->vol_count = hmp->nvolumes;
123                 hammer_modify_volume_done(volume);
124                 hammer_rel_volume(volume, 0);
125         }
126
127         /*
128          * Assign Layer1 entries
129          */
130
131         hammer_volume_t root_volume = NULL;
132         hammer_blockmap_t freemap;
133
134         freemap = &hmp->blockmap[HAMMER_ZONE_FREEMAP_INDEX];
135         root_volume = hammer_get_root_volume(hmp, &error);
136         KKASSERT(root_volume && error == 0);
137
138         for (uint64_t i_layer1 = 0; i_layer1 < num_layer1_entries; i_layer1++) {
139                 hammer_buffer_t buffer1 = NULL;
140                 struct hammer_blockmap_layer1 *layer1;
141                 hammer_off_t layer1_offset;
142
143                 layer1_offset = freemap->phys_offset +
144                         (free_vol_no * 1024L) *
145                         sizeof(struct hammer_blockmap_layer1) + i_layer1;
146
147                 layer1 = hammer_bread(hmp, layer1_offset, &error, &buffer1);
148                 KKASSERT(layer1 != NULL && error == 0);
149                 KKASSERT(layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL);
150
151                 hammer_modify_buffer(trans, buffer1, layer1, sizeof(*layer1));
152                 bzero(layer1, sizeof(*layer1));
153                 layer1->phys_offset = HAMMER_ENCODE_RAW_BUFFER(free_vol_no,
154                         i_layer1 * HAMMER_LARGEBLOCK_SIZE);
155
156                 layer1->blocks_free = layer1_free_blocks[i_layer1];
157                 layer1->layer1_crc = crc32(layer1, HAMMER_LAYER1_CRCSIZE);
158
159                 hammer_modify_buffer_done(buffer1);
160                 if (buffer1)
161                         hammer_rel_buffer(buffer1, 0);
162
163                 hammer_modify_volume_field(trans, root_volume,
164                         vol0_stat_freebigblocks);
165
166                 root_volume->ondisk->vol0_stat_freebigblocks +=
167                         layer1_free_blocks[i_layer1];
168                 hmp->copy_stat_freebigblocks =
169                         root_volume->ondisk->vol0_stat_freebigblocks;
170                 hammer_modify_volume_done(root_volume);
171         } /* for */
172
173         hammer_rel_volume(root_volume, 0);
174
175         hammer_unlock(&hmp->blkmap_lock);
176         hammer_sync_unlock(trans);
177
178 end:
179         if (error) {
180                 kprintf("An error occured: %d\n", error);
181         }
182         if (layer1_free_blocks)
183                 kfree(layer1_free_blocks, M_TEMP);
184         return (error);
185 }
186
187 static int
188 hammer_format_volume_header(struct hammer_mount *hmp, const char *dev_path,
189         const char *vol_name, int vol_no, int vol_count,
190         int64_t vol_size, int64_t boot_area_size, int64_t mem_area_size,
191         uint64_t *num_layer1_entries_p, uint64_t *layer1_free_blocks)
192 {
193         struct vnode *devvp = NULL;
194         struct buf *bp = NULL;
195         struct nlookupdata nd;
196         struct hammer_volume_ondisk *ondisk;
197         int error;
198
199         /*
200          * Get the device vnode
201          */
202         error = nlookup_init(&nd, dev_path, UIO_SYSSPACE, NLC_FOLLOW);
203         if (error == 0)
204                 error = nlookup(&nd);
205         if (error == 0)
206                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
207         nlookup_done(&nd);
208
209         if (error == 0) {
210                 if (vn_isdisk(devvp, &error)) {
211                         error = vfs_mountedon(devvp);
212                 }
213         }
214         if (error == 0 &&
215             count_udev(devvp->v_umajor, devvp->v_uminor) > 0) {
216                 error = EBUSY;
217         }
218         if (error == 0) {
219                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
220                 error = vinvalbuf(devvp, V_SAVE, 0, 0);
221                 if (error == 0) {
222                         error = VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
223                 }
224                 vn_unlock(devvp);
225         }
226         if (error) {
227                 if (devvp)
228                         vrele(devvp);
229                 return (error);
230         }
231
232         /*
233          * Extract the volume number from the volume header and do various
234          * sanity checks.
235          */
236         KKASSERT(HAMMER_BUFSIZE >= sizeof(struct hammer_volume_ondisk));
237         error = bread(devvp, 0LL, HAMMER_BUFSIZE, &bp);
238         if (error || bp->b_bcount < sizeof(struct hammer_volume_ondisk))
239                 goto late_failure;
240
241         ondisk = (struct hammer_volume_ondisk*) bp->b_data;
242
243         /*
244          * Note that we do NOT allow to use a device that contains
245          * a valid HAMMER signature. It has to be cleaned up with dd
246          * before.
247          */
248         if (ondisk->vol_signature == HAMMER_FSBUF_VOLUME) {
249                 kprintf("hammer_expand: Formatting of valid HAMMER volume "
250                         "%s denied. Erase with dd!\n", vol_name);
251                 error = EFTYPE;
252                 goto late_failure;
253         }
254
255         bzero(ondisk, sizeof(struct hammer_volume_ondisk));
256         ksnprintf(ondisk->vol_name, sizeof(ondisk->vol_name), "%s", vol_name);
257         ondisk->vol_fstype = hmp->rootvol->ondisk->vol_fstype;
258         ondisk->vol_signature = HAMMER_FSBUF_VOLUME;
259         ondisk->vol_fsid = hmp->fsid;
260         ondisk->vol_rootvol = hmp->rootvol->vol_no;
261         ondisk->vol_no = vol_no;
262         ondisk->vol_count = vol_count;
263         ondisk->vol_version = hmp->version;
264
265         /*
266          * Reserve space for (future) header junk, setup our poor-man's
267          * bigblock allocator.
268          */
269         int64_t vol_alloc = HAMMER_BUFSIZE * 16;
270
271         ondisk->vol_bot_beg = vol_alloc;
272         vol_alloc += boot_area_size;
273         ondisk->vol_mem_beg = vol_alloc;
274         vol_alloc += mem_area_size;
275
276         /*
277          * The remaining area is the zone 2 buffer allocation area.  These
278          * buffers
279          */
280         ondisk->vol_buf_beg = vol_alloc;
281         ondisk->vol_buf_end = vol_size & ~(int64_t)HAMMER_BUFMASK;
282
283         if (ondisk->vol_buf_end < ondisk->vol_buf_beg) {
284                 kprintf("volume %d %s is too small to hold the volume header",
285                      ondisk->vol_no, ondisk->vol_name);
286                 error = EFTYPE;
287                 goto late_failure;
288         }
289
290         ondisk->vol_nblocks = (ondisk->vol_buf_end - ondisk->vol_buf_beg) /
291                               HAMMER_BUFSIZE;
292         ondisk->vol_blocksize = HAMMER_BUFSIZE;
293
294         /*
295          * Write volume header to disk
296          */
297         error = bwrite(bp);
298         bp = NULL;
299
300         /*
301          * Initialize layer2 freemap
302          */
303
304         /*
305          * Determine the number of L1 entries we need to represent the
306          * space of the whole volume. Each L1 entry covers 4 TB of space
307          * (8MB * 2**19) and we need one L2 big block for each L1 entry.
308          * L1 entries are stored in the root volume.
309          */
310         hammer_off_t off_end = (ondisk->vol_buf_end - ondisk->vol_buf_beg)
311                 & ~HAMMER_LARGEBLOCK_MASK64;
312         uint64_t num_layer1_entries = (off_end / HAMMER_BLOCKMAP_LAYER2) +
313                 ((off_end & HAMMER_BLOCKMAP_LAYER2_MASK) == 0 ? 0 : 1);
314         *num_layer1_entries_p = num_layer1_entries;
315
316         /*
317          * We allocate all L2 big blocks sequentially from the start of
318          * the volume.
319          */
320         KKASSERT(off_end / HAMMER_LARGEBLOCK_SIZE >= num_layer1_entries);
321
322         hammer_off_t layer2_end = num_layer1_entries * HAMMER_LARGEBLOCK_SIZE;
323         hammer_off_t off = 0;
324         while (off < layer2_end) {
325                 error = bread(devvp, ondisk->vol_buf_beg + off,
326                               HAMMER_BUFSIZE, &bp);
327                 if (error || bp->b_bcount != HAMMER_BUFSIZE)
328                         goto late_failure;
329                 struct hammer_blockmap_layer2 *layer2 = (void*)bp->b_data;
330
331                 for (int i = 0; i < HAMMER_BUFSIZE / sizeof(*layer2); ++i) {
332
333                         /* the bigblock described by the layer2 entry */
334                         hammer_off_t bigblock_off = HAMMER_LARGEBLOCK_SIZE *
335                                 (off / sizeof(*layer2));
336
337                         /*
338                          * To which layer1 entry does the current layer2
339                          * big block belong?
340                          *
341                          * We need this to calculate the free bigblocks
342                          * which is required for the layer1.
343                          */
344                         uint64_t i_layer1 = HAMMER_BLOCKMAP_LAYER1_OFFSET(off) /
345                                         sizeof(struct hammer_blockmap_layer1);
346                         KKASSERT(i_layer1 < 1024);
347
348                         bzero(layer2, sizeof(*layer2));
349
350                         if ((off & HAMMER_LARGEBLOCK_SIZE) == bigblock_off) {
351                                 /*
352                                  * Bigblock is part of the layer2 freemap
353                                  */
354                                 layer2->zone = HAMMER_ZONE_FREEMAP_INDEX;
355                                 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
356                                 layer2->bytes_free = 0;
357                         } else if (bigblock_off < off_end) {
358                                 layer2->zone = 0;
359                                 layer2->append_off = 0;
360                                 layer2->bytes_free = HAMMER_LARGEBLOCK_SIZE;
361                                 ++layer1_free_blocks[i_layer1];
362                         } else {
363                                 layer2->zone = HAMMER_ZONE_UNAVAIL_INDEX;
364                                 layer2->append_off = HAMMER_LARGEBLOCK_SIZE;
365                                 layer2->bytes_free = 0;
366                         }
367                         layer2->entry_crc = crc32(layer2, HAMMER_LAYER2_CRCSIZE);
368                         off += sizeof(*layer2);
369                         ++layer2;
370                 }
371
372                 error = bwrite(bp);
373                 bp = NULL;
374                 if (error)
375                         goto late_failure;
376         }
377
378 late_failure:
379         if (bp)
380                 brelse(bp);
381         VOP_CLOSE(devvp, FREAD|FWRITE);
382         if (devvp)
383                 vrele(devvp);
384         return (error);
385 }