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