84f70405d3e3435311f46cdef2e80b1a06edae1e
[lwext4.git] / lwext4 / ext4_mkfs.c
1 /*
2  * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /** @addtogroup lwext4
30  * @{
31  */
32 /**
33  * @file  ext4_mkfs.c
34  * @brief
35  */
36
37 #include "ext4_config.h"
38 #include "ext4_super.h"
39 #include "ext4_debug.h"
40 #include "ext4_mkfs.h"
41
42 #include <inttypes.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
47 #define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
48
49 struct fs_aux_info {
50         struct ext4_sblock *sb;
51         struct ext4_bgroup *bg_desc;
52         struct xattr_list_element *xattrs;
53         uint32_t first_data_block;
54         uint64_t len_blocks;
55         uint32_t inode_table_blocks;
56         uint32_t groups;
57         uint32_t bg_desc_blocks;
58         uint32_t default_i_flags;
59         uint32_t blocks_per_ind;
60         uint32_t blocks_per_dind;
61         uint32_t blocks_per_tind;
62 };
63
64 static inline int log_2(int j)
65 {
66         int i;
67
68         for (i = 0; j > 0; i++)
69                 j >>= 1;
70
71         return i - 1;
72 }
73
74 static int sb2info(struct ext4_sblock *sb, struct ext4_mkfs_info *info)
75 {
76         if (to_le16(sb->magic) != EXT4_SUPERBLOCK_MAGIC)
77                 return EINVAL;
78
79         info->block_size = 1024 << to_le32(sb->log_block_size);
80         info->blocks_per_group = to_le32(sb->blocks_per_group);
81         info->inodes_per_group = to_le32(sb->inodes_per_group);
82         info->inode_size = to_le16(sb->inode_size);
83         info->inodes = to_le32(sb->inodes_count);
84         info->feat_ro_compat = to_le32(sb->features_read_only);
85         info->feat_compat = to_le32(sb->features_compatible);
86         info->feat_incompat = to_le32(sb->features_incompatible);
87         info->bg_desc_reserve_blocks = to_le16(sb->s_reserved_gdt_blocks);
88         info->label = sb->volume_name;
89         info->len = (uint64_t)info->block_size * ext4_sb_get_blocks_cnt(sb);
90
91         return EOK;
92 }
93
94 static uint32_t compute_blocks_per_group(struct ext4_mkfs_info *info)
95 {
96         return info->block_size * 8;
97 }
98
99 static uint32_t compute_inodes(struct ext4_mkfs_info *info)
100 {
101         return DIV_ROUND_UP(info->len, info->block_size) / 4;
102 }
103
104 static uint32_t compute_inodes_per_group(struct ext4_mkfs_info *info)
105 {
106         uint32_t blocks = DIV_ROUND_UP(info->len, info->block_size);
107         uint32_t block_groups = DIV_ROUND_UP(blocks, info->blocks_per_group);
108         uint32_t inodes = DIV_ROUND_UP(info->inodes, block_groups);
109         inodes = EXT4_ALIGN(inodes, (info->block_size / info->inode_size));
110
111         /* After properly rounding up the number of inodes/group,
112          * make sure to update the total inodes field in the info struct.
113          */
114         info->inodes = inodes * block_groups;
115
116         return inodes;
117 }
118
119 static uint32_t compute_bg_desc_reserve_blocks(struct ext4_mkfs_info *info)
120 {
121         uint32_t blocks = DIV_ROUND_UP(info->len, info->block_size);
122         uint32_t block_groups = DIV_ROUND_UP(blocks, info->blocks_per_group);
123         uint32_t bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext4_bgroup),
124                         info->block_size);
125
126         uint32_t bg_desc_reserve_blocks =
127                         DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext4_bgroup),
128                                         info->block_size) - bg_desc_blocks;
129
130         if (bg_desc_reserve_blocks > info->block_size / sizeof(uint32_t))
131                 bg_desc_reserve_blocks = info->block_size / sizeof(uint32_t);
132
133         return bg_desc_reserve_blocks;
134 }
135
136 static uint32_t compute_journal_blocks(struct ext4_mkfs_info *info)
137 {
138         uint32_t journal_blocks = DIV_ROUND_UP(info->len, info->block_size) / 64;
139         if (journal_blocks < 1024)
140                 journal_blocks = 1024;
141         if (journal_blocks > 32768)
142                 journal_blocks = 32768;
143         return journal_blocks;
144 }
145
146 static bool has_superblock(struct ext4_mkfs_info *info, uint32_t bgid)
147 {
148         if (!(info->feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER))
149                 return true;
150
151         return ext4_sb_sparse(bgid);
152 }
153
154 static int create_fs_aux_info(struct fs_aux_info *aux_info, struct ext4_mkfs_info *info)
155 {
156         aux_info->first_data_block = (info->block_size > 1024) ? 0 : 1;
157         aux_info->len_blocks = info->len / info->block_size;
158         aux_info->inode_table_blocks = DIV_ROUND_UP(info->inodes_per_group * info->inode_size,
159                 info->block_size);
160         aux_info->groups = DIV_ROUND_UP(aux_info->len_blocks - aux_info->first_data_block,
161                 info->blocks_per_group);
162         aux_info->blocks_per_ind = info->block_size / sizeof(uint32_t);
163         aux_info->blocks_per_dind = aux_info->blocks_per_ind * aux_info->blocks_per_ind;
164         aux_info->blocks_per_tind = aux_info->blocks_per_dind * aux_info->blocks_per_dind;
165
166         aux_info->bg_desc_blocks =
167                 DIV_ROUND_UP(aux_info->groups * sizeof(struct ext4_bgroup),
168                         info->block_size);
169
170         aux_info->default_i_flags = EXT4_INODE_FLAG_NOATIME;
171
172         uint32_t last_group_size = aux_info->len_blocks % info->blocks_per_group;
173         uint32_t last_header_size = 2 + aux_info->inode_table_blocks;
174         if (has_superblock(info, aux_info->groups - 1))
175                 last_header_size += 1 + aux_info->bg_desc_blocks +
176                         info->bg_desc_reserve_blocks;
177
178         if (last_group_size > 0 && last_group_size < last_header_size) {
179                 aux_info->groups--;
180                 aux_info->len_blocks -= last_group_size;
181         }
182
183         aux_info->sb = calloc(1, sizeof(struct ext4_sblock));
184         if (!aux_info->sb)
185                 return ENOMEM;
186
187         aux_info->bg_desc = calloc(sizeof(struct ext4_bgroup),
188                         aux_info->bg_desc_blocks);
189         if (!aux_info->bg_desc)
190                 return ENOMEM;
191
192         aux_info->xattrs = NULL;
193         return EOK;
194 }
195
196 static void release_fs_aux_info(struct fs_aux_info *aux_info)
197 {
198         if (aux_info->sb)
199                 free(aux_info->sb);
200         if (aux_info->sb)
201                 free(aux_info->bg_desc);
202 }
203
204
205 /* Fill in the superblock memory buffer based on the filesystem parameters */
206 static void fill_in_sb(struct fs_aux_info *aux_info, struct ext4_mkfs_info *info)
207 {
208         unsigned int i;
209         struct ext4_sblock *sb = aux_info->sb;
210
211         sb->inodes_count = info->inodes_per_group * aux_info->groups;
212         sb->blocks_count_lo = aux_info->len_blocks;
213         sb->reserved_blocks_count_lo = 0;
214         sb->free_blocks_count_lo = 0;
215         sb->free_inodes_count = 0;
216         sb->first_data_block = aux_info->first_data_block;
217         sb->log_block_size = log_2(info->block_size / 1024);
218         sb->log_cluster_size = log_2(info->block_size / 1024);
219         sb->blocks_per_group = info->blocks_per_group;
220         sb->frags_per_group = info->blocks_per_group;
221         sb->inodes_per_group = info->inodes_per_group;
222         sb->mount_time = 0;
223         sb->write_time = 0;
224         sb->mount_count = 0;
225         sb->max_mount_count = 0xFFFF;
226         sb->magic = EXT4_SUPERBLOCK_MAGIC;
227         sb->state = EXT4_SUPERBLOCK_STATE_VALID_FS;
228         sb->errors = EXT4_SUPERBLOCK_ERRORS_RO;
229         sb->minor_rev_level = 0;
230         sb->last_check_time = 0;
231         sb->check_interval = 0;
232         sb->creator_os = EXT4_SUPERBLOCK_OS_LINUX;
233         sb->rev_level = 1;
234         sb->def_resuid = 0;
235         sb->def_resgid = 0;
236
237         sb->first_inode = EXT4_GOOD_OLD_FIRST_INO;
238         sb->inode_size = info->inode_size;
239         sb->block_group_index = 0;
240         sb->features_compatible = info->feat_compat;
241         sb->features_incompatible = info->feat_incompat;
242         sb->features_read_only = info->feat_ro_compat;
243
244         memset(sb->uuid, 0, sizeof(sb->uuid));
245
246         memset(sb->volume_name, 0, sizeof(sb->volume_name));
247         strncpy(sb->volume_name, info->label, sizeof(sb->volume_name));
248         memset(sb->last_mounted, 0, sizeof(sb->last_mounted));
249         sb->algorithm_usage_bitmap = 0;
250
251         sb->s_reserved_gdt_blocks = info->bg_desc_reserve_blocks;
252         sb->s_prealloc_blocks = 0;
253         sb->s_prealloc_dir_blocks = 0;
254
255         //memcpy(sb->journal_uuid, sb->uuid, sizeof(sb->journal_uuid));
256         if (info->feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
257                 sb->journal_inode_number = EXT4_JOURNAL_INO;
258         sb->journal_dev = 0;
259         sb->last_orphan = 0;
260         sb->hash_seed[0] = 0; /* FIXME */
261         sb->default_hash_version = EXT2_HTREE_HALF_MD4;
262         sb->reserved_char_pad = 1;
263         sb->desc_size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE;
264         sb->default_mount_opts = 0; /* FIXME */
265         sb->first_meta_bg = 0;
266         sb->mkfs_time = 0;
267         //sb->jnl_blocks[17]; /* FIXME */
268
269         sb->blocks_count_hi = aux_info->len_blocks >> 32;
270         sb->reserved_blocks_count_hi = 0;
271         sb->free_blocks_count_hi = 0;
272         sb->min_extra_isize = sizeof(struct ext4_inode) -
273                 EXT4_GOOD_OLD_INODE_SIZE;
274         sb->want_extra_isize = sizeof(struct ext4_inode) -
275                 EXT4_GOOD_OLD_INODE_SIZE;
276         sb->flags = 2;
277         sb->raid_stride = 0;
278         sb->mmp_interval = 0;
279         sb->mmp_block = 0;
280         sb->raid_stripe_width = 0;
281         sb->log_groups_per_flex = 0;
282         sb->kbytes_written = 0;
283
284         sb->block_group_index = 0;
285
286         for (i = 0; i < aux_info->groups; i++) {
287
288                 uint64_t group_start_block = aux_info->first_data_block + i *
289                         info->blocks_per_group;
290                 uint32_t header_size = 0;
291
292                 if (has_superblock(info, i))
293                         header_size = 1 + aux_info->bg_desc_blocks + info->bg_desc_reserve_blocks;
294
295
296                 aux_info->bg_desc[i].block_bitmap_lo = group_start_block + header_size;
297                 aux_info->bg_desc[i].inode_bitmap_lo = group_start_block + header_size + 1;
298                 aux_info->bg_desc[i].inode_table_first_block_lo = group_start_block + header_size + 2;
299
300                 aux_info->bg_desc[i].free_blocks_count_lo = sb->blocks_per_group;
301                 aux_info->bg_desc[i].free_inodes_count_lo = sb->inodes_per_group;
302                 aux_info->bg_desc[i].used_dirs_count_lo = 0;
303         }
304 }
305
306 static int bdev_write_sb(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
307                           struct ext4_mkfs_info *info)
308 {
309         uint64_t offset;
310         uint32_t i;
311         int r;
312
313         /* write out the backup superblocks */
314         for (i = 1; i < aux_info->groups; i++) {
315                 if (has_superblock(info, i)) {
316                         offset = info->block_size * (aux_info->first_data_block
317                                 + i * info->blocks_per_group);
318
319                         aux_info->sb->block_group_index = i;
320                         r = ext4_block_writebytes(bd, offset, aux_info->sb, 1024);
321                         if (r != EOK)
322                                 return r;
323                 }
324         }
325
326         /* write out the primary superblock */
327         aux_info->sb->block_group_index = 0;
328         return ext4_block_writebytes(bd, 1024, aux_info->sb, 1024);
329 }
330
331
332 int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
333 {
334         int r;
335         struct ext4_sblock *sb = NULL;
336         r = ext4_block_init(bd);
337         if (r != EOK)
338                 return r;
339
340         sb = malloc(sizeof(struct ext4_sblock));
341         if (!sb)
342                 goto Finish;
343
344
345         r = ext4_sb_read(bd, sb);
346         if (r != EOK)
347                 goto Finish;
348
349         r = sb2info(sb, info);
350
351 Finish:
352         if (sb)
353                 free(sb);
354         ext4_block_fini(bd);
355         return r;
356 }
357
358 int ext4_mkfs(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
359 {
360         int r;
361         r = ext4_block_init(bd);
362         if (r != EOK)
363                 return r;
364
365         if (info->len == 0)
366                 info->len = bd->ph_bcnt * bd->ph_bsize;
367
368         if (info->block_size == 0)
369                 info->block_size = 4096; /*Set block size to default value*/
370
371         /* Round down the filesystem length to be a multiple of the block size */
372         info->len &= ~((uint64_t)info->block_size - 1);
373
374         if (info->journal_blocks == 0)
375                 info->journal_blocks = compute_journal_blocks(info);
376
377         if (info->blocks_per_group == 0)
378                 info->blocks_per_group = compute_blocks_per_group(info);
379
380         if (info->inodes == 0)
381                 info->inodes = compute_inodes(info);
382
383         if (info->inode_size == 0)
384                 info->inode_size = 256;
385
386         if (info->label == NULL)
387                 info->label = "";
388
389         info->inodes_per_group = compute_inodes_per_group(info);
390
391         info->feat_compat = CONFIG_FEATURE_COMPAT_SUPP;
392         info->feat_ro_compat = CONFIG_FEATURE_RO_COMPAT_SUPP;
393         info->feat_incompat = CONFIG_FEATURE_INCOMPAT_SUPP;
394
395         if (info->no_journal == 0)
396                 info->feat_compat |= EXT4_FEATURE_COMPAT_HAS_JOURNAL;
397
398         info->bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks(info);
399
400         ext4_dbg(DEBUG_MKFS, DBG_INFO "Creating filesystem with parameters:\n");
401         ext4_dbg(DEBUG_MKFS, DBG_NONE "Size: %llu\n",
402                         (long long unsigned int)info->len);
403         ext4_dbg(DEBUG_MKFS, DBG_NONE "Block size: %"PRIu32"\n", info->block_size);
404         ext4_dbg(DEBUG_MKFS, DBG_NONE "Blocks per group: %"PRIu32"\n",
405                         info->blocks_per_group);
406         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes per group: %"PRIu32"\n",
407                         info->inodes_per_group);
408         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inode size: %"PRIu32"\n", info->inode_size);
409         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes: %"PRIu32"\n", info->inodes);
410         ext4_dbg(DEBUG_MKFS, DBG_NONE "Journal blocks: %"PRIu32"\n",
411                         info->journal_blocks);
412         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features ro_compat: 0x%x\n",
413                         info->feat_ro_compat);
414         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features compat: 0x%x\n",
415                         info->feat_compat);
416         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features incompat: 0x%x\n",
417                         info->feat_incompat);
418         ext4_dbg(DEBUG_MKFS, DBG_NONE "BG desc reserve: %"PRIu32"\n",
419                         info->bg_desc_reserve_blocks);
420         ext4_dbg(DEBUG_MKFS, DBG_NONE "journal: %s\n",
421                         !info->no_journal ? "yes" : "no");
422         ext4_dbg(DEBUG_MKFS, DBG_NONE "Label: %s\n", info->label);
423
424         struct fs_aux_info aux_info;
425         memset(&aux_info, 0, sizeof(struct fs_aux_info));
426
427         r = create_fs_aux_info(&aux_info, info);
428         if (r != EOK)
429                 goto Finish;
430
431         fill_in_sb(&aux_info, info);
432
433
434         r = bdev_write_sb(bd, &aux_info, info);
435         if (r != EOK)
436                 goto Finish;
437
438         Finish:
439         release_fs_aux_info(&aux_info);
440         ext4_block_fini(bd);
441         return r;
442 }
443
444 /**
445  * @}
446  */