Tone logging down a bit.
[lwext4.git] / src / 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_types.h>
39 #include <ext4_misc.h>
40 #include <ext4_errno.h>
41 #include <ext4_debug.h>
42
43 #include <ext4_super.h>
44 #include <ext4_block_group.h>
45 #include <ext4_dir.h>
46 #include <ext4_dir_idx.h>
47 #include <ext4_fs.h>
48 #include <ext4_inode.h>
49 #include <ext4_ialloc.h>
50 #include <ext4_mkfs.h>
51
52 #include <inttypes.h>
53 #include <string.h>
54 #include <stdlib.h>
55
56 struct fs_aux_info {
57         struct ext4_sblock *sb;
58         uint8_t *bg_desc_blk;
59         struct xattr_list_element *xattrs;
60         uint32_t first_data_block;
61         uint64_t len_blocks;
62         uint32_t inode_table_blocks;
63         uint32_t groups;
64         uint32_t bg_desc_blocks;
65         uint32_t default_i_flags;
66         uint32_t blocks_per_ind;
67         uint32_t blocks_per_dind;
68         uint32_t blocks_per_tind;
69 };
70
71 static inline int log_2(int j)
72 {
73         int i;
74
75         for (i = 0; j > 0; i++)
76                 j >>= 1;
77
78         return i - 1;
79 }
80
81 static int sb2info(struct ext4_sblock *sb, struct ext4_mkfs_info *info)
82 {
83         if (to_le16(sb->magic) != EXT4_SUPERBLOCK_MAGIC)
84                 return EINVAL;
85
86         info->block_size = 1024 << to_le32(sb->log_block_size);
87         info->blocks_per_group = to_le32(sb->blocks_per_group);
88         info->inodes_per_group = to_le32(sb->inodes_per_group);
89         info->inode_size = to_le16(sb->inode_size);
90         info->inodes = to_le32(sb->inodes_count);
91         info->feat_ro_compat = to_le32(sb->features_read_only);
92         info->feat_compat = to_le32(sb->features_compatible);
93         info->feat_incompat = to_le32(sb->features_incompatible);
94         info->bg_desc_reserve_blocks = to_le16(sb->s_reserved_gdt_blocks);
95         info->label = sb->volume_name;
96         info->len = (uint64_t)info->block_size * ext4_sb_get_blocks_cnt(sb);
97         info->dsc_size = to_le16(sb->desc_size);
98         memcpy(info->uuid, sb->uuid, UUID_SIZE);
99
100         return EOK;
101 }
102
103 static uint32_t compute_blocks_per_group(struct ext4_mkfs_info *info)
104 {
105         return info->block_size * 8;
106 }
107
108 static uint32_t compute_inodes(struct ext4_mkfs_info *info)
109 {
110         return (uint32_t)EXT4_DIV_ROUND_UP(info->len, info->block_size) / 4;
111 }
112
113 static uint32_t compute_inodes_per_group(struct ext4_mkfs_info *info)
114 {
115         uint32_t blocks = (uint32_t)EXT4_DIV_ROUND_UP(info->len, info->block_size);
116         uint32_t block_groups = EXT4_DIV_ROUND_UP(blocks, info->blocks_per_group);
117         uint32_t inodes = EXT4_DIV_ROUND_UP(info->inodes, block_groups);
118         inodes = EXT4_ALIGN(inodes, (info->block_size / info->inode_size));
119
120         /* After properly rounding up the number of inodes/group,
121          * make sure to update the total inodes field in the info struct.
122          */
123         info->inodes = inodes * block_groups;
124
125         return inodes;
126 }
127
128
129 static uint32_t compute_journal_blocks(struct ext4_mkfs_info *info)
130 {
131         uint32_t journal_blocks = (uint32_t)EXT4_DIV_ROUND_UP(info->len,
132                                                  info->block_size) / 64;
133         if (journal_blocks < 1024)
134                 journal_blocks = 1024;
135         if (journal_blocks > 32768)
136                 journal_blocks = 32768;
137         return journal_blocks;
138 }
139
140 static bool has_superblock(struct ext4_mkfs_info *info, uint32_t bgid)
141 {
142         if (!(info->feat_ro_compat & EXT4_FRO_COM_SPARSE_SUPER))
143                 return true;
144
145         return ext4_sb_sparse(bgid);
146 }
147
148 static int create_fs_aux_info(struct fs_aux_info *aux_info,
149                               struct ext4_mkfs_info *info)
150 {
151         aux_info->first_data_block = (info->block_size > 1024) ? 0 : 1;
152         aux_info->len_blocks = info->len / info->block_size;
153         aux_info->inode_table_blocks = EXT4_DIV_ROUND_UP(info->inodes_per_group *
154                         info->inode_size, info->block_size);
155         aux_info->groups = (uint32_t)EXT4_DIV_ROUND_UP(aux_info->len_blocks -
156                         aux_info->first_data_block, info->blocks_per_group);
157         aux_info->blocks_per_ind = info->block_size / sizeof(uint32_t);
158         aux_info->blocks_per_dind =
159                         aux_info->blocks_per_ind * aux_info->blocks_per_ind;
160         aux_info->blocks_per_tind =
161                         aux_info->blocks_per_dind * aux_info->blocks_per_dind;
162
163         aux_info->bg_desc_blocks =
164                 EXT4_DIV_ROUND_UP(aux_info->groups * info->dsc_size,
165                         info->block_size);
166
167         aux_info->default_i_flags = EXT4_INODE_FLAG_NOATIME;
168
169         uint32_t last_group_size = aux_info->len_blocks % info->blocks_per_group;
170         uint32_t last_header_size = 2 + aux_info->inode_table_blocks;
171         if (has_superblock(info, aux_info->groups - 1))
172                 last_header_size += 1 + aux_info->bg_desc_blocks +
173                         info->bg_desc_reserve_blocks;
174
175         if (last_group_size > 0 && last_group_size < last_header_size) {
176                 aux_info->groups--;
177                 aux_info->len_blocks -= last_group_size;
178         }
179
180         aux_info->sb = ext4_calloc(1, EXT4_SUPERBLOCK_SIZE);
181         if (!aux_info->sb)
182                 return ENOMEM;
183
184         aux_info->bg_desc_blk = ext4_calloc(1, info->block_size);
185         if (!aux_info->bg_desc_blk)
186                 return ENOMEM;
187
188         aux_info->xattrs = NULL;
189
190
191         ext4_dbg(DEBUG_MKFS, DBG_INFO "create_fs_aux_info\n");
192         ext4_dbg(DEBUG_MKFS, DBG_NONE "first_data_block: %"PRIu32"\n",
193                         aux_info->first_data_block);
194         ext4_dbg(DEBUG_MKFS, DBG_NONE "len_blocks: %"PRIu64"\n",
195                         aux_info->len_blocks);
196         ext4_dbg(DEBUG_MKFS, DBG_NONE "inode_table_blocks: %"PRIu32"\n",
197                         aux_info->inode_table_blocks);
198         ext4_dbg(DEBUG_MKFS, DBG_NONE "groups: %"PRIu32"\n",
199                         aux_info->groups);
200         ext4_dbg(DEBUG_MKFS, DBG_NONE "bg_desc_blocks: %"PRIu32"\n",
201                         aux_info->bg_desc_blocks);
202         ext4_dbg(DEBUG_MKFS, DBG_NONE "default_i_flags: %"PRIu32"\n",
203                         aux_info->default_i_flags);
204         ext4_dbg(DEBUG_MKFS, DBG_NONE "blocks_per_ind: %"PRIu32"\n",
205                         aux_info->blocks_per_ind);
206         ext4_dbg(DEBUG_MKFS, DBG_NONE "blocks_per_dind: %"PRIu32"\n",
207                         aux_info->blocks_per_dind);
208         ext4_dbg(DEBUG_MKFS, DBG_NONE "blocks_per_tind: %"PRIu32"\n",
209                         aux_info->blocks_per_tind);
210
211         return EOK;
212 }
213
214 static void release_fs_aux_info(struct fs_aux_info *aux_info)
215 {
216         if (aux_info->sb)
217                 ext4_free(aux_info->sb);
218         if (aux_info->bg_desc_blk)
219                 ext4_free(aux_info->bg_desc_blk);
220 }
221
222
223 /* Fill in the superblock memory buffer based on the filesystem parameters */
224 static void fill_sb(struct fs_aux_info *aux_info, struct ext4_mkfs_info *info)
225 {
226         struct ext4_sblock *sb = aux_info->sb;
227
228         sb->inodes_count = to_le32(info->inodes_per_group * aux_info->groups);
229
230         ext4_sb_set_blocks_cnt(sb, aux_info->len_blocks);
231         ext4_sb_set_free_blocks_cnt(sb, aux_info->len_blocks);
232         sb->free_inodes_count = to_le32(info->inodes_per_group * aux_info->groups);
233
234         sb->reserved_blocks_count_lo = to_le32(0);
235         sb->first_data_block = to_le32(aux_info->first_data_block);
236         sb->log_block_size = to_le32(log_2(info->block_size / 1024));
237         sb->log_cluster_size = to_le32(log_2(info->block_size / 1024));
238         sb->blocks_per_group = to_le32(info->blocks_per_group);
239         sb->frags_per_group = to_le32(info->blocks_per_group);
240         sb->inodes_per_group = to_le32(info->inodes_per_group);
241         sb->mount_time = to_le32(0);
242         sb->write_time = to_le32(0);
243         sb->mount_count = to_le16(0);
244         sb->max_mount_count = to_le16(0xFFFF);
245         sb->magic = to_le16(EXT4_SUPERBLOCK_MAGIC);
246         sb->state = to_le16(EXT4_SUPERBLOCK_STATE_VALID_FS);
247         sb->errors = to_le16(EXT4_SUPERBLOCK_ERRORS_RO);
248         sb->minor_rev_level = to_le16(0);
249         sb->last_check_time = to_le32(0);
250         sb->check_interval = to_le32(0);
251         sb->creator_os = to_le32(EXT4_SUPERBLOCK_OS_LINUX);
252         sb->rev_level = to_le32(1);
253         sb->def_resuid = to_le16(0);
254         sb->def_resgid = to_le16(0);
255
256         sb->first_inode = to_le32(EXT4_GOOD_OLD_FIRST_INO);
257         sb->inode_size = to_le16(info->inode_size);
258         sb->block_group_index = to_le16(0);
259
260         sb->features_compatible = to_le32(info->feat_compat);
261         sb->features_incompatible = to_le32(info->feat_incompat);
262         sb->features_read_only = to_le32(info->feat_ro_compat);
263
264         memcpy(sb->uuid, info->uuid, UUID_SIZE);
265
266         memset(sb->volume_name, 0, sizeof(sb->volume_name));
267         strncpy(sb->volume_name, info->label, sizeof(sb->volume_name) - 1);
268         memset(sb->last_mounted, 0, sizeof(sb->last_mounted));
269
270         sb->algorithm_usage_bitmap = to_le32(0);
271         sb->s_prealloc_blocks = 0;
272         sb->s_prealloc_dir_blocks = 0;
273         sb->s_reserved_gdt_blocks = to_le16(info->bg_desc_reserve_blocks);
274
275         if (info->feat_compat & EXT4_FCOM_HAS_JOURNAL)
276                 sb->journal_inode_number = to_le32(EXT4_JOURNAL_INO);
277
278         sb->journal_backup_type = 1;
279         sb->journal_dev = to_le32(0);
280         sb->last_orphan = to_le32(0);
281         sb->hash_seed[0] = to_le32(0x11111111);
282         sb->hash_seed[1] = to_le32(0x22222222);
283         sb->hash_seed[2] = to_le32(0x33333333);
284         sb->hash_seed[3] = to_le32(0x44444444);
285         sb->default_hash_version = EXT2_HTREE_HALF_MD4;
286         sb->checksum_type = 1;
287         sb->desc_size = to_le16(info->dsc_size);
288         sb->default_mount_opts = to_le32(0);
289         sb->first_meta_bg = to_le32(0);
290         sb->mkfs_time = to_le32(0);
291
292         sb->reserved_blocks_count_hi = to_le32(0);
293         sb->min_extra_isize = to_le32(sizeof(struct ext4_inode) -
294                 EXT4_GOOD_OLD_INODE_SIZE);
295         sb->want_extra_isize = to_le32(sizeof(struct ext4_inode) -
296                 EXT4_GOOD_OLD_INODE_SIZE);
297         sb->flags = to_le32(EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH);
298 }
299
300
301 static int write_bgroups(struct ext4_fs *fs, struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
302                          struct ext4_mkfs_info *info, ext4_progress progress, void* progress_context)
303 {
304         /* size of the group descriptor */
305         uint32_t dsc_size = ext4_sb_get_desc_size(aux_info->sb);
306         uint32_t block_size = ext4_sb_get_block_size(aux_info->sb);
307
308         /* Calculate the group descriptors chunk that will be written to each block group */
309
310         /* The whole set of group descriptors */
311         void* all_bg_desc = ext4_calloc(1, aux_info->groups * dsc_size);
312         if (!all_bg_desc) {
313                 return ENOMEM;
314         }
315
316         uint32_t bg_free_blk = 0;
317         uint64_t sb_free_blk = 0;
318
319         for (uint32_t i = 0; i < aux_info->groups; i++) {
320                 ext4_dbg(DEBUG_MKFS, DBG_NONE "Writing aux_info group %d of %d", i, aux_info->groups);
321                 uint64_t bg_start_block = aux_info->first_data_block +
322                         aux_info->first_data_block + i * info->blocks_per_group;
323                 uint32_t blk_off = 0;
324
325                 struct ext4_bgroup* bg_desc = (struct ext4_bgroup *) (((char *) all_bg_desc) + i * dsc_size);
326
327                 uint32_t blocks_in_group = info->blocks_per_group;
328                 if (i == aux_info->groups - 1) {
329                         blocks_in_group = (uint32_t)
330                                 (ext4_sb_get_blocks_cnt(aux_info->sb) - ext4_get32(aux_info->sb, first_data_block) - info->blocks_per_group * (ext4_block_group_cnt(aux_info->sb) - 1));
331                 }
332
333                 bg_free_blk = blocks_in_group - aux_info->inode_table_blocks;
334
335                 bg_free_blk -= 2;
336                 blk_off += aux_info->bg_desc_blocks;
337
338                 if (i == (aux_info->groups - 1)) {
339                         bg_free_blk -= aux_info->first_data_block;
340                 }
341
342                 if (has_superblock(info, i)) {
343                         bg_start_block++;
344                         blk_off += info->bg_desc_reserve_blocks;
345                         bg_free_blk -= info->bg_desc_reserve_blocks + 1;
346                         bg_free_blk -= aux_info->bg_desc_blocks;
347                 }
348
349                 ext4_bg_set_block_bitmap(bg_desc, aux_info->sb,
350                                          bg_start_block + blk_off + 1);
351
352                 ext4_bg_set_inode_bitmap(bg_desc, aux_info->sb,
353                                          bg_start_block + blk_off + 2);
354
355                 ext4_bg_set_inode_table_first_block(bg_desc,
356                                                 aux_info->sb,
357                                                 bg_start_block + blk_off + 3);
358
359                 ext4_bg_set_free_blocks_count(bg_desc, aux_info->sb,
360                                               bg_free_blk);
361
362                 ext4_bg_set_free_inodes_count(bg_desc,
363                                 aux_info->sb, to_le32(aux_info->sb->inodes_per_group));
364
365                 ext4_bg_set_used_dirs_count(bg_desc, aux_info->sb, 0);
366
367                 ext4_bg_set_flag(bg_desc,
368                                  EXT4_BLOCK_GROUP_BLOCK_UNINIT |
369                                  EXT4_BLOCK_GROUP_INODE_UNINIT);
370
371                 sb_free_blk += bg_free_blk;
372         }
373
374         /* Write the block group headers */
375
376         int r = EOK;
377
378         for (uint32_t i = 0; i < aux_info->groups; i++) {
379                 ext4_dbg(DEBUG_MKFS, DBG_NONE "Writing block group header %d of %d", i, aux_info->groups);
380                 if (progress) {
381                         progress(progress_context, (float) i / aux_info->groups);
382                 }
383
384                 uint64_t bg_start_block = aux_info->first_data_block +
385                         aux_info->first_data_block + i * info->blocks_per_group;
386                 uint32_t blk_off = 0;
387
388                 if (has_superblock(info, i)) {
389                         bg_start_block++;
390                         blk_off += info->bg_desc_reserve_blocks;
391                 }
392
393                 /* Group descriptors */
394                 ext4_block_writebytes(bd, (bg_start_block + blk_off) * block_size, all_bg_desc, aux_info->groups * dsc_size);
395
396                 blk_off += aux_info->bg_desc_blocks;
397
398                 struct ext4_block_group_ref ref;
399                 r = ext4_fs_get_block_group_ref(fs, i, &ref);
400                 if (r != EOK) {
401                         free(all_bg_desc);
402                         break;
403                 }
404
405                 r = ext4_fs_put_block_group_ref(&ref);
406                 if (r != EOK) {
407                         free(all_bg_desc);
408                         break;
409                 }
410         }
411
412         free(all_bg_desc);
413
414         ext4_sb_set_free_blocks_cnt(aux_info->sb, sb_free_blk);
415         return r;
416 }
417
418 static int write_sblocks(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
419                           struct ext4_mkfs_info *info)
420 {
421         uint64_t offset;
422         uint32_t i;
423         int r;
424
425         /* write out the backup superblocks */
426         for (i = 1; i < aux_info->groups; i++) {
427                 if (has_superblock(info, i)) {
428                         offset = info->block_size * (aux_info->first_data_block
429                                 + i * info->blocks_per_group);
430
431                         aux_info->sb->block_group_index = to_le16(i);
432                         r = ext4_block_writebytes(bd, offset, aux_info->sb,
433                                                   EXT4_SUPERBLOCK_SIZE);
434                         if (r != EOK)
435                                 return r;
436                 }
437         }
438
439         /* write out the primary superblock */
440         aux_info->sb->block_group_index = to_le16(0);
441         return ext4_block_writebytes(bd, 1024, aux_info->sb,
442                         EXT4_SUPERBLOCK_SIZE);
443 }
444
445
446 int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
447 {
448         int r;
449         struct ext4_sblock *sb = NULL;
450         r = ext4_block_init(bd);
451         if (r != EOK)
452                 return r;
453
454         sb = ext4_malloc(EXT4_SUPERBLOCK_SIZE);
455         if (!sb)
456                 goto Finish;
457
458
459         r = ext4_sb_read(bd, sb);
460         if (r != EOK)
461                 goto Finish;
462
463         r = sb2info(sb, info);
464
465 Finish:
466         if (sb)
467                 ext4_free(sb);
468         ext4_block_fini(bd);
469         return r;
470 }
471
472 static int mkfs_init(struct ext4_fs *fs, struct ext4_blockdev *bd, struct ext4_mkfs_info *info, ext4_progress progress, void* progress_context)
473 {
474         int r;
475         struct fs_aux_info aux_info;
476         memset(&aux_info, 0, sizeof(struct fs_aux_info));
477
478         r = create_fs_aux_info(&aux_info, info);
479         if (r != EOK)
480                 goto Finish;
481
482         fill_sb(&aux_info, info);
483         memcpy(&fs->sb, aux_info.sb, sizeof(struct ext4_sblock));
484
485         ext4_dbg(DEBUG_MKFS, DBG_NONE "mkfs_init: write_bgroups");
486         r = write_bgroups(fs, bd, &aux_info, info, progress, progress_context);
487         if (r != EOK)
488                 goto Finish;
489
490         ext4_dbg(DEBUG_MKFS, DBG_NONE "mkfs_init: write_sblocks");
491         r = write_sblocks(bd, &aux_info, info);
492         if (r != EOK)
493                 goto Finish;
494
495         Finish:
496         release_fs_aux_info(&aux_info);
497         return r;
498 }
499
500 static int alloc_inodes(struct ext4_fs *fs)
501 {
502         int r = EOK;
503         int i;
504         struct ext4_inode_ref inode_ref;
505         for (i = 1; i < 12; ++i) {
506                 int filetype = EXT4_DE_REG_FILE;
507
508                 switch (i) {
509                 case EXT4_ROOT_INO:
510                 case EXT4_GOOD_OLD_FIRST_INO:
511                         filetype = EXT4_DE_DIR;
512                         break;
513                 default:
514                         break;
515                 }
516
517                 r = ext4_fs_alloc_inode(fs, &inode_ref, filetype);
518                 if (r != EOK)
519                         return r;
520
521                 ext4_inode_set_mode(&fs->sb, inode_ref.inode, 0);
522
523                 switch (i) {
524                 case EXT4_ROOT_INO:
525                 case EXT4_JOURNAL_INO:
526                         ext4_fs_inode_blocks_init(fs, &inode_ref);
527                         break;
528                 }
529
530                 ext4_fs_put_inode_ref(&inode_ref);
531         }
532
533         return r;
534 }
535
536 static int create_dirs(struct ext4_fs *fs)
537 {
538         int r = EOK;
539         struct ext4_inode_ref root;
540         struct ext4_inode_ref child;
541
542         r = ext4_fs_get_inode_ref(fs, EXT4_ROOT_INO, &root);
543         if (r != EOK)
544                 return r;
545
546         r = ext4_fs_get_inode_ref(fs, EXT4_GOOD_OLD_FIRST_INO, &child);
547         if (r != EOK)
548                 return r;
549
550         ext4_inode_set_mode(&fs->sb, child.inode,
551                         EXT4_INODE_MODE_DIRECTORY | 0777);
552
553         ext4_inode_set_mode(&fs->sb, root.inode,
554                         EXT4_INODE_MODE_DIRECTORY | 0777);
555
556 #if CONFIG_DIR_INDEX_ENABLE
557         /* Initialize directory index if supported */
558         if (ext4_sb_feature_com(&fs->sb, EXT4_FCOM_DIR_INDEX)) {
559                 r = ext4_dir_dx_init(&root, &root);
560                 if (r != EOK)
561                         return r;
562
563                 r = ext4_dir_dx_init(&child, &root);
564                 if (r != EOK)
565                         return r;
566
567                 ext4_inode_set_flag(root.inode, EXT4_INODE_FLAG_INDEX);
568                 ext4_inode_set_flag(child.inode, EXT4_INODE_FLAG_INDEX);
569         } else
570 #endif
571         {
572                 r = ext4_dir_add_entry(&root, ".", strlen("."), &root);
573                 if (r != EOK)
574                         return r;
575
576                 r = ext4_dir_add_entry(&root, "..", strlen(".."), &root);
577                 if (r != EOK)
578                         return r;
579
580                 r = ext4_dir_add_entry(&child, ".", strlen("."), &child);
581                 if (r != EOK)
582                         return r;
583
584                 r = ext4_dir_add_entry(&child, "..", strlen(".."), &root);
585                 if (r != EOK)
586                         return r;
587         }
588
589         r = ext4_dir_add_entry(&root, "lost+found", strlen("lost+found"), &child);
590         if (r != EOK)
591                 return r;
592
593         ext4_inode_set_links_cnt(root.inode, 3);
594         ext4_inode_set_links_cnt(child.inode, 2);
595
596         child.dirty = true;
597         root.dirty = true;
598         ext4_fs_put_inode_ref(&child);
599         ext4_fs_put_inode_ref(&root);
600         return r;
601 }
602
603 static int create_journal_inode(struct ext4_fs *fs,
604                                 struct ext4_mkfs_info *info)
605 {
606         int ret;
607         struct ext4_inode_ref inode_ref;
608         uint64_t blocks_count;
609
610         if (!info->journal)
611                 return EOK;
612
613         ret = ext4_fs_get_inode_ref(fs, EXT4_JOURNAL_INO, &inode_ref);
614         if (ret != EOK)
615                 return ret;
616
617         struct ext4_inode *inode = inode_ref.inode;
618
619         ext4_inode_set_mode(&fs->sb, inode, EXT4_INODE_MODE_FILE | 0600);
620         ext4_inode_set_links_cnt(inode, 1);
621
622         blocks_count = ext4_inode_get_blocks_count(&fs->sb, inode);
623
624         while (blocks_count++ < info->journal_blocks)
625         {
626                 ext4_fsblk_t fblock;
627                 ext4_lblk_t iblock;
628                 struct ext4_block blk;
629
630                 ret = ext4_fs_append_inode_dblk(&inode_ref, &fblock, &iblock);
631                 if (ret != EOK)
632                         goto Finish;
633
634                 if (iblock != 0)
635                         continue;
636
637                 ret = ext4_block_get(fs->bdev, &blk, fblock);
638                 if (ret != EOK)
639                         goto Finish;
640
641
642                 struct jbd_sb * jbd_sb = (struct jbd_sb * )blk.data;
643                 memset(jbd_sb, 0, sizeof(struct jbd_sb));
644
645                 jbd_sb->header.magic = to_be32(JBD_MAGIC_NUMBER);
646                 jbd_sb->header.blocktype = to_be32(JBD_SUPERBLOCK_V2);
647                 jbd_sb->blocksize = to_be32(info->block_size);
648                 jbd_sb->maxlen = to_be32(info->journal_blocks);
649                 jbd_sb->nr_users = to_be32(1);
650                 jbd_sb->first = to_be32(1);
651                 jbd_sb->sequence = to_be32(1);
652
653                 ext4_bcache_set_dirty(blk.buf);
654                 ret = ext4_block_set(fs->bdev, &blk);
655                 if (ret != EOK)
656                         goto Finish;
657         }
658
659         memcpy(fs->sb.journal_blocks, inode->blocks, sizeof(inode->blocks));
660
661         Finish:
662         ext4_fs_put_inode_ref(&inode_ref);
663
664         return ret;
665 }
666
667 int ext4_mkfs(struct ext4_fs *fs, struct ext4_blockdev *bd,
668               struct ext4_mkfs_info *info, int fs_type, ext4_progress progress, void* progress_context)
669 {
670         int r;
671
672         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_mkfs start");
673
674         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_block_init");
675
676         r = ext4_block_init(bd);
677         if (r != EOK)
678                 return r;
679
680         bd->fs = fs;
681
682         if (info->len == 0)
683                 info->len = bd->part_size;
684
685         if (info->block_size == 0)
686                 info->block_size = 4096; /*Set block size to default value*/
687
688         /* Round down the filesystem length to be a multiple of the block size */
689         info->len &= ~((uint64_t)info->block_size - 1);
690
691         if (info->journal_blocks == 0)
692                 info->journal_blocks = compute_journal_blocks(info);
693
694         if (info->blocks_per_group == 0)
695                 info->blocks_per_group = compute_blocks_per_group(info);
696
697         if (info->inodes == 0)
698                 info->inodes = compute_inodes(info);
699
700         if (info->inode_size == 0)
701                 info->inode_size = 256;
702
703         if (info->label == NULL)
704                 info->label = "";
705
706         info->inodes_per_group = compute_inodes_per_group(info);
707
708         switch (fs_type) {
709         case F_SET_EXT2:
710                 info->feat_compat = EXT2_SUPPORTED_FCOM;
711                 info->feat_ro_compat = EXT2_SUPPORTED_FRO_COM;
712                 info->feat_incompat = EXT2_SUPPORTED_FINCOM;
713                 break;
714         case F_SET_EXT3:
715                 info->feat_compat = EXT3_SUPPORTED_FCOM;
716                 info->feat_ro_compat = EXT3_SUPPORTED_FRO_COM;
717                 info->feat_incompat = EXT3_SUPPORTED_FINCOM;
718                 break;
719         case F_SET_EXT4:
720                 info->feat_compat = EXT4_SUPPORTED_FCOM;
721                 info->feat_ro_compat = EXT4_SUPPORTED_FRO_COM;
722                 info->feat_incompat = EXT4_SUPPORTED_FINCOM;
723                 break;
724         }
725
726         /*TODO: handle this features some day...*/
727         info->feat_incompat &= ~EXT4_FINCOM_META_BG;
728         info->feat_incompat &= ~EXT4_FINCOM_FLEX_BG;
729         info->feat_incompat &= ~EXT4_FINCOM_64BIT;
730
731         info->feat_ro_compat &= ~EXT4_FRO_COM_METADATA_CSUM;
732         info->feat_ro_compat &= ~EXT4_FRO_COM_GDT_CSUM;
733         info->feat_ro_compat &= ~EXT4_FRO_COM_DIR_NLINK;
734         info->feat_ro_compat &= ~EXT4_FRO_COM_EXTRA_ISIZE;
735         info->feat_ro_compat &= ~EXT4_FRO_COM_HUGE_FILE;
736
737         if (info->journal)
738                 info->feat_compat |= EXT4_FCOM_HAS_JOURNAL;
739
740         if (info->dsc_size == 0) {
741
742                 if (info->feat_incompat & EXT4_FINCOM_64BIT)
743                         info->dsc_size = EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE;
744                 else
745                         info->dsc_size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE;
746         }
747
748         info->bg_desc_reserve_blocks = 0;
749
750         ext4_dbg(DEBUG_MKFS, DBG_INFO "Creating filesystem with parameters:\n");
751         ext4_dbg(DEBUG_MKFS, DBG_NONE "Size: %"PRIu64"\n", info->len);
752         ext4_dbg(DEBUG_MKFS, DBG_NONE "Block size: %"PRIu32"\n",
753                         info->block_size);
754         ext4_dbg(DEBUG_MKFS, DBG_NONE "Blocks per group: %"PRIu32"\n",
755                         info->blocks_per_group);
756         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes per group: %"PRIu32"\n",
757                         info->inodes_per_group);
758         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inode size: %"PRIu32"\n",
759                         info->inode_size);
760         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes: %"PRIu32"\n", info->inodes);
761         ext4_dbg(DEBUG_MKFS, DBG_NONE "Journal blocks: %"PRIu32"\n",
762                         info->journal_blocks);
763         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features ro_compat: 0x%x\n",
764                         info->feat_ro_compat);
765         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features compat: 0x%x\n",
766                         info->feat_compat);
767         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features incompat: 0x%x\n",
768                         info->feat_incompat);
769         ext4_dbg(DEBUG_MKFS, DBG_NONE "BG desc reserve: %"PRIu32"\n",
770                         info->bg_desc_reserve_blocks);
771         ext4_dbg(DEBUG_MKFS, DBG_NONE "Descriptor size: %"PRIu16"\n",
772                         info->dsc_size);
773         ext4_dbg(DEBUG_MKFS, DBG_NONE "journal: %s\n",
774                         info->journal ? "yes" : "no");
775         ext4_dbg(DEBUG_MKFS, DBG_NONE "Label: %s\n", info->label);
776
777         struct ext4_bcache bc;
778
779         memset(&bc, 0, sizeof(struct ext4_bcache));
780         ext4_block_set_lb_size(bd, info->block_size);
781
782         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_bcache_init_dynamic");
783
784         r = ext4_bcache_init_dynamic(&bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
785                                       info->block_size);
786         if (r != EOK)
787                 goto block_fini;
788
789         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_block_bind_bcache");
790
791         /*Bind block cache to block device*/
792         r = ext4_block_bind_bcache(bd, &bc);
793         if (r != EOK)
794                 goto cache_fini;
795
796         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_block_cache_write_back");
797
798         r = ext4_block_cache_write_back(bd, 1);
799         if (r != EOK)
800                 goto cache_fini;
801
802         fs->bdev = bd;
803         fs->read_only = false;
804
805         ext4_dbg(DEBUG_MKFS, DBG_NONE "mkfs_init");
806
807         r = mkfs_init(fs, bd, info, progress, progress_context);
808         if (r != EOK)
809                 goto cache_fini;
810
811         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_fs_init");
812
813         r = ext4_fs_init(fs, bd, false);
814         if (r != EOK)
815                 goto cache_fini;
816
817         ext4_dbg(DEBUG_MKFS, DBG_NONE "alloc_inodes");
818
819         r = alloc_inodes(fs);
820         if (r != EOK)
821                 goto fs_fini;
822
823         ext4_dbg(DEBUG_MKFS, DBG_NONE "create_dirs");
824
825         r = create_dirs(fs);
826         if (r != EOK)
827                 goto fs_fini;
828
829         ext4_dbg(DEBUG_MKFS, DBG_NONE "create_journal_inode");
830
831         r = create_journal_inode(fs, info);
832         if (r != EOK)
833                 goto fs_fini;
834
835         fs_fini:
836         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_fs_fini");
837         ext4_fs_fini(fs);
838
839         cache_fini:
840         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_block_cache_write_back");
841         ext4_block_cache_write_back(bd, 0);
842
843         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_bcache_fini_dynamic");
844         ext4_bcache_fini_dynamic(&bc);
845
846         block_fini:
847         ext4_dbg(DEBUG_MKFS, DBG_NONE "ext4_block_fini");
848         ext4_block_fini(bd);
849
850         return r;
851 }
852
853 /**
854  * @}
855  */