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