61a574253354ceea18e7268a5fcbf962de3979ff
[lwext4.git] / lwext4 / ext4_mkfs.c
1 /*
2  * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /** @addtogroup lwext4
30  * @{
31  */
32 /**
33  * @file  ext4_mkfs.c
34  * @brief
35  */
36
37 #include "ext4_config.h"
38 #include "ext4_super.h"
39 #include "ext4_block_group.h"
40 #include "ext4_dir.h"
41 #include "ext4_dir_idx.h"
42 #include "ext4_fs.h"
43 #include "ext4_inode.h"
44 #include "ext4_debug.h"
45 #include "ext4_ialloc.h"
46 #include "ext4_mkfs.h"
47
48 #include <inttypes.h>
49 #include <string.h>
50 #include <stdlib.h>
51
52 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
53 #define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
54
55 struct fs_aux_info {
56         struct ext4_sblock *sb;
57         struct ext4_bgroup *bg_desc;
58         struct xattr_list_element *xattrs;
59         uint32_t first_data_block;
60         uint64_t len_blocks;
61         uint32_t inode_table_blocks;
62         uint32_t groups;
63         uint32_t bg_desc_blocks;
64         uint32_t default_i_flags;
65         uint32_t blocks_per_ind;
66         uint32_t blocks_per_dind;
67         uint32_t blocks_per_tind;
68 };
69
70 static inline int log_2(int j)
71 {
72         int i;
73
74         for (i = 0; j > 0; i++)
75                 j >>= 1;
76
77         return i - 1;
78 }
79
80 static int sb2info(struct ext4_sblock *sb, struct ext4_mkfs_info *info)
81 {
82         if (to_le16(sb->magic) != EXT4_SUPERBLOCK_MAGIC)
83                 return EINVAL;
84
85         info->block_size = 1024 << to_le32(sb->log_block_size);
86         info->blocks_per_group = to_le32(sb->blocks_per_group);
87         info->inodes_per_group = to_le32(sb->inodes_per_group);
88         info->inode_size = to_le16(sb->inode_size);
89         info->inodes = to_le32(sb->inodes_count);
90         info->feat_ro_compat = to_le32(sb->features_read_only);
91         info->feat_compat = to_le32(sb->features_compatible);
92         info->feat_incompat = to_le32(sb->features_incompatible);
93         info->bg_desc_reserve_blocks = to_le16(sb->s_reserved_gdt_blocks);
94         info->label = sb->volume_name;
95         info->len = (uint64_t)info->block_size * ext4_sb_get_blocks_cnt(sb);
96
97         return EOK;
98 }
99
100 static uint32_t compute_blocks_per_group(struct ext4_mkfs_info *info)
101 {
102         return info->block_size * 8;
103 }
104
105 static uint32_t compute_inodes(struct ext4_mkfs_info *info)
106 {
107         return DIV_ROUND_UP(info->len, info->block_size) / 4;
108 }
109
110 static uint32_t compute_inodes_per_group(struct ext4_mkfs_info *info)
111 {
112         uint32_t blocks = DIV_ROUND_UP(info->len, info->block_size);
113         uint32_t block_groups = DIV_ROUND_UP(blocks, info->blocks_per_group);
114         uint32_t inodes = DIV_ROUND_UP(info->inodes, block_groups);
115         inodes = EXT4_ALIGN(inodes, (info->block_size / info->inode_size));
116
117         /* After properly rounding up the number of inodes/group,
118          * make sure to update the total inodes field in the info struct.
119          */
120         info->inodes = inodes * block_groups;
121
122         return inodes;
123 }
124
125
126 static uint32_t compute_journal_blocks(struct ext4_mkfs_info *info)
127 {
128         uint32_t journal_blocks = DIV_ROUND_UP(info->len, info->block_size) / 64;
129         if (journal_blocks < 1024)
130                 journal_blocks = 1024;
131         if (journal_blocks > 32768)
132                 journal_blocks = 32768;
133         return journal_blocks;
134 }
135
136 static bool has_superblock(struct ext4_mkfs_info *info, uint32_t bgid)
137 {
138         if (!(info->feat_ro_compat & EXT4_FRO_COM_SPARSE_SUPER))
139                 return true;
140
141         return ext4_sb_sparse(bgid);
142 }
143
144 static int create_fs_aux_info(struct fs_aux_info *aux_info,
145                               struct ext4_mkfs_info *info)
146 {
147         aux_info->first_data_block = (info->block_size > 1024) ? 0 : 1;
148         aux_info->len_blocks = info->len / info->block_size;
149         aux_info->inode_table_blocks = DIV_ROUND_UP(info->inodes_per_group *
150                         info->inode_size, info->block_size);
151         aux_info->groups = DIV_ROUND_UP(aux_info->len_blocks -
152                         aux_info->first_data_block, info->blocks_per_group);
153         aux_info->blocks_per_ind = info->block_size / sizeof(uint32_t);
154         aux_info->blocks_per_dind =
155                         aux_info->blocks_per_ind * aux_info->blocks_per_ind;
156         aux_info->blocks_per_tind =
157                         aux_info->blocks_per_dind * aux_info->blocks_per_dind;
158
159         aux_info->bg_desc_blocks =
160                 DIV_ROUND_UP(aux_info->groups * sizeof(struct ext4_bgroup),
161                         info->block_size);
162
163         aux_info->default_i_flags = EXT4_INODE_FLAG_NOATIME;
164
165         uint32_t last_group_size = aux_info->len_blocks % info->blocks_per_group;
166         uint32_t last_header_size = 2 + aux_info->inode_table_blocks;
167         if (has_superblock(info, aux_info->groups - 1))
168                 last_header_size += 1 + aux_info->bg_desc_blocks +
169                         info->bg_desc_reserve_blocks;
170
171         if (last_group_size > 0 && last_group_size < last_header_size) {
172                 aux_info->groups--;
173                 aux_info->len_blocks -= last_group_size;
174         }
175
176         aux_info->sb = calloc(1, EXT4_SUPERBLOCK_SIZE);
177         if (!aux_info->sb)
178                 return ENOMEM;
179
180         aux_info->bg_desc = calloc(aux_info->groups, sizeof(struct ext4_bgroup));
181         if (!aux_info->bg_desc)
182                 return ENOMEM;
183
184         aux_info->xattrs = NULL;
185         return EOK;
186 }
187
188 static void release_fs_aux_info(struct fs_aux_info *aux_info)
189 {
190         if (aux_info->sb)
191                 free(aux_info->sb);
192         if (aux_info->bg_desc)
193                 free(aux_info->bg_desc);
194 }
195
196
197 /* Fill in the superblock memory buffer based on the filesystem parameters */
198 static void fill_in_sb(struct fs_aux_info *aux_info, struct ext4_mkfs_info *info)
199 {
200         struct ext4_sblock *sb = aux_info->sb;
201
202         sb->inodes_count = to_le32(info->inodes_per_group * aux_info->groups);
203
204         ext4_sb_set_blocks_cnt(sb, aux_info->len_blocks);
205         ext4_sb_set_free_blocks_cnt(sb, aux_info->len_blocks);
206         sb->free_inodes_count = to_le32(info->inodes_per_group * aux_info->groups);
207
208         sb->reserved_blocks_count_lo = to_le32(0);
209         sb->first_data_block = to_le32(aux_info->first_data_block);
210         sb->log_block_size = to_le32(log_2(info->block_size / 1024));
211         sb->log_cluster_size = to_le32(log_2(info->block_size / 1024));
212         sb->blocks_per_group = to_le32(info->blocks_per_group);
213         sb->frags_per_group = to_le32(info->blocks_per_group);
214         sb->inodes_per_group = to_le32(info->inodes_per_group);
215         sb->mount_time = to_le32(0);
216         sb->write_time = to_le32(0);
217         sb->mount_count = to_le16(0);
218         sb->max_mount_count = to_le16(0xFFFF);
219         sb->magic = to_le16(EXT4_SUPERBLOCK_MAGIC);
220         sb->state = to_le16(EXT4_SUPERBLOCK_STATE_VALID_FS);
221         sb->errors = to_le16(EXT4_SUPERBLOCK_ERRORS_RO);
222         sb->minor_rev_level = to_le16(0);
223         sb->last_check_time = to_le32(0);
224         sb->check_interval = to_le32(0);
225         sb->creator_os = to_le32(EXT4_SUPERBLOCK_OS_LINUX);
226         sb->rev_level = to_le32(1);
227         sb->def_resuid = to_le16(0);
228         sb->def_resgid = to_le16(0);
229
230         sb->first_inode = to_le32(EXT4_GOOD_OLD_FIRST_INO);
231         sb->inode_size = to_le16(info->inode_size);
232         sb->block_group_index = to_le16(0);
233
234         sb->features_compatible = to_le32(info->feat_compat);
235         sb->features_incompatible = to_le32(info->feat_incompat);
236         sb->features_read_only = to_le32(info->feat_ro_compat);
237
238         memset(sb->uuid, 0, sizeof(sb->uuid));
239
240         memset(sb->volume_name, 0, sizeof(sb->volume_name));
241         strncpy(sb->volume_name, info->label, sizeof(sb->volume_name));
242         memset(sb->last_mounted, 0, sizeof(sb->last_mounted));
243
244         sb->algorithm_usage_bitmap = to_le32(0);
245         sb->s_prealloc_blocks = 0;
246         sb->s_prealloc_dir_blocks = 0;
247         sb->s_reserved_gdt_blocks = to_le16(info->bg_desc_reserve_blocks);
248
249         if (info->feat_compat & EXT4_FCOM_HAS_JOURNAL)
250                 sb->journal_inode_number = to_le32(EXT4_JOURNAL_INO);
251         sb->journal_dev = to_le32(0);
252         sb->last_orphan = to_le32(0);
253         sb->hash_seed[0] = to_le32(0x11111111);
254         sb->hash_seed[1] = to_le32(0x22222222);
255         sb->hash_seed[2] = to_le32(0x33333333);
256         sb->hash_seed[3] = to_le32(0x44444444);
257         sb->default_hash_version = EXT2_HTREE_HALF_MD4;
258         sb->checksum_type = 1;
259         sb->desc_size = to_le16(info->dsc_size);
260         sb->default_mount_opts = to_le32(0);
261         sb->first_meta_bg = to_le32(0);
262         sb->mkfs_time = to_le32(0);
263
264         sb->reserved_blocks_count_hi = to_le32(0);
265         sb->min_extra_isize = to_le32(sizeof(struct ext4_inode) -
266                 EXT4_GOOD_OLD_INODE_SIZE);
267         sb->want_extra_isize = to_le32(sizeof(struct ext4_inode) -
268                 EXT4_GOOD_OLD_INODE_SIZE);
269         sb->flags = to_le32(EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH);
270 }
271
272 static void fill_bgroups(struct fs_aux_info *aux_info,
273                          struct ext4_mkfs_info *info)
274 {
275         uint32_t i;
276
277         uint64_t bg_free_blk = 0;
278         uint64_t sb_free_blk = 0;
279
280         for (i = 0; i < aux_info->groups; i++) {
281
282                 uint64_t bg_start_block = aux_info->first_data_block +
283                         aux_info->first_data_block + i * info->blocks_per_group;
284                 uint32_t blk_off = 0;
285
286                 bg_free_blk = info->blocks_per_group -
287                         (aux_info->inode_table_blocks + aux_info->bg_desc_blocks);
288
289                 bg_free_blk -= 2;
290                 blk_off += aux_info->bg_desc_blocks;
291
292                 if (has_superblock(info, i)) {
293                         bg_start_block++;
294                         blk_off += info->bg_desc_reserve_blocks;
295                         bg_free_blk -= info->bg_desc_reserve_blocks + 1;
296                 } else {
297                         bg_free_blk++;
298                 }
299
300                 ext4_bg_set_block_bitmap(&aux_info->bg_desc[i], aux_info->sb,
301                                 bg_start_block + blk_off + 1);
302
303                 ext4_bg_set_inode_bitmap(&aux_info->bg_desc[i], aux_info->sb,
304                                 bg_start_block + blk_off + 2);
305
306                 ext4_bg_set_inode_table_first_block(&aux_info->bg_desc[i],
307                                 aux_info->sb,
308                                 bg_start_block + blk_off + 3);
309
310                 ext4_bg_set_free_blocks_count(&aux_info->bg_desc[i],
311                                 aux_info->sb, bg_free_blk);
312
313                 ext4_bg_set_free_inodes_count(&aux_info->bg_desc[i],
314                                 aux_info->sb, aux_info->sb->inodes_per_group);
315
316                 ext4_bg_set_used_dirs_count(&aux_info->bg_desc[i], aux_info->sb,
317                                             0);
318
319                 ext4_bg_set_flag(&aux_info->bg_desc[i],
320                                 EXT4_BLOCK_GROUP_BLOCK_UNINIT |
321                                 EXT4_BLOCK_GROUP_INODE_UNINIT);
322
323                 sb_free_blk += bg_free_blk;
324         }
325
326         ext4_sb_set_free_blocks_cnt(aux_info->sb, sb_free_blk);
327 }
328
329
330 static int write_bgroups(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
331                          struct ext4_mkfs_info *info)
332 {
333         int r;
334         uint32_t i;
335         struct ext4_block b;
336         for (i = 0; i < aux_info->groups; i++) {
337                 uint64_t bg_start_block = aux_info->first_data_block +
338                         aux_info->first_data_block + i * info->blocks_per_group;
339                 uint32_t blk_off = 0;
340
341                 blk_off += aux_info->bg_desc_blocks;
342                 if (has_superblock(info, i)) {
343                         bg_start_block++;
344                         blk_off += info->bg_desc_reserve_blocks;
345                 }
346
347                 uint32_t block_size = ext4_sb_get_block_size(aux_info->sb);
348                 uint32_t dsc_pos = 0;
349                 uint32_t dsc_id = 0;
350                 uint32_t dsc_size = ext4_sb_get_desc_size(aux_info->sb);
351                 uint32_t dsc_blk_cnt = aux_info->bg_desc_blocks;
352                 uint64_t dsc_blk = bg_start_block;
353
354                 while (dsc_blk_cnt--) {
355                         r = ext4_block_get(bd, &b, dsc_blk++);
356                         if (r != EOK)
357                                 return r;
358
359                         while (dsc_pos + dsc_size < block_size) {
360                                 memcpy(b.data + dsc_pos,
361                                        &aux_info->bg_desc[dsc_id],
362                                        dsc_size);
363
364                                 dsc_pos += dsc_size;
365                                 dsc_id++;
366                         }
367
368                         b.dirty = true;
369                         r = ext4_block_set(bd, &b);
370                         if (r != EOK)
371                                 return r;
372                 }
373
374                 r = ext4_block_get_noread(bd, &b, bg_start_block + blk_off + 1);
375                 if (r != EOK)
376                         return r;
377                 memset(b.data, 0, block_size);
378                 b.dirty = true;
379                 r = ext4_block_set(bd, &b);
380                 if (r != EOK)
381                         return r;
382                 r = ext4_block_get_noread(bd, &b, bg_start_block + blk_off + 2);
383                 if (r != EOK)
384                         return r;
385                 memset(b.data, 0, block_size);
386                 b.dirty = true;
387                 r = ext4_block_set(bd, &b);
388                 if (r != EOK)
389                         return r;
390         }
391
392
393         return r;
394 }
395
396 static int write_sblocks(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
397                           struct ext4_mkfs_info *info)
398 {
399         uint64_t offset;
400         uint32_t i;
401         int r;
402
403         /* write out the backup superblocks */
404         for (i = 1; i < aux_info->groups; i++) {
405                 if (has_superblock(info, i)) {
406                         offset = info->block_size * (aux_info->first_data_block
407                                 + i * info->blocks_per_group);
408
409                         aux_info->sb->block_group_index = i;
410                         r = ext4_block_writebytes(bd, offset, aux_info->sb,
411                                                   EXT4_SUPERBLOCK_SIZE);
412                         if (r != EOK)
413                                 return r;
414                 }
415         }
416
417         /* write out the primary superblock */
418         aux_info->sb->block_group_index = 0;
419         return ext4_block_writebytes(bd, 1024, aux_info->sb,
420                         EXT4_SUPERBLOCK_SIZE);
421 }
422
423
424 int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
425 {
426         int r;
427         struct ext4_sblock *sb = NULL;
428         r = ext4_block_init(bd);
429         if (r != EOK)
430                 return r;
431
432         sb = malloc(EXT4_SUPERBLOCK_SIZE);
433         if (!sb)
434                 goto Finish;
435
436
437         r = ext4_sb_read(bd, sb);
438         if (r != EOK)
439                 goto Finish;
440
441         r = sb2info(sb, info);
442
443 Finish:
444         if (sb)
445                 free(sb);
446         ext4_block_fini(bd);
447         return r;
448 }
449
450 static int mkfs_initial(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
451 {
452         int r;
453         struct fs_aux_info aux_info;
454         memset(&aux_info, 0, sizeof(struct fs_aux_info));
455
456         r = create_fs_aux_info(&aux_info, info);
457         if (r != EOK)
458                 goto Finish;
459
460         fill_in_sb(&aux_info, info);
461         fill_bgroups(&aux_info, info);
462
463
464         r = write_bgroups(bd, &aux_info, info);
465         if (r != EOK)
466                 goto Finish;
467
468         r = write_sblocks(bd, &aux_info, info);
469         if (r != EOK)
470                 goto Finish;
471
472         Finish:
473         release_fs_aux_info(&aux_info);
474         return r;
475 }
476
477 static int init_bgs(struct ext4_fs *fs)
478 {
479         int r = EOK;
480         struct ext4_block_group_ref ref;
481         uint32_t i;
482         uint32_t bg_count = ext4_block_group_cnt(&fs->sb);
483         for (i = 0; i < bg_count; ++i) {
484                 r = ext4_fs_get_block_group_ref(fs, i, &ref);
485                 if (r != EOK)
486                         break;
487
488                 r = ext4_fs_put_block_group_ref(&ref);
489                 if (r != EOK)
490                         break;
491         }
492         return r;
493 }
494
495 static int alloc_inodes(struct ext4_fs *fs)
496 {
497         int r = EOK;
498         int i;
499         struct ext4_inode_ref inode_ref;
500         for (i = 1; i < 12; ++i) {
501                 int filetype = EXT4_DIRENTRY_REG_FILE;
502
503                 switch (i) {
504                 case EXT4_ROOT_INO:
505                 case EXT4_GOOD_OLD_FIRST_INO:
506                         filetype = EXT4_DIRENTRY_DIR;
507                         break;
508                 }
509
510                 r = ext4_fs_alloc_inode(fs, &inode_ref, filetype);
511                 if (r != EOK)
512                         return r;
513
514                 ext4_inode_set_mode(&fs->sb, inode_ref.inode, 0);
515                 ext4_fs_put_inode_ref(&inode_ref);
516         }
517
518         return r;
519 }
520
521 static int create_dirs(struct ext4_fs *fs)
522 {
523         int r = EOK;
524         struct ext4_inode_ref root;
525         struct ext4_inode_ref child;
526
527         r = ext4_fs_get_inode_ref(fs, EXT4_ROOT_INO, &root);
528         if (r != EOK)
529                 return r;
530
531         r = ext4_fs_get_inode_ref(fs, EXT4_GOOD_OLD_FIRST_INO, &child);
532         if (r != EOK)
533                 return r;
534
535         ext4_inode_set_mode(&fs->sb, child.inode,
536                         EXT4_INODE_MODE_DIRECTORY | 0777);
537
538         ext4_inode_set_mode(&fs->sb, root.inode,
539                         EXT4_INODE_MODE_DIRECTORY | 0777);
540
541 #if CONFIG_DIR_INDEX_ENABLE
542         /* Initialize directory index if supported */
543         if (ext4_sb_feature_com(&fs->sb, EXT4_FCOM_DIR_INDEX)) {
544                 r = ext4_dir_dx_init(&root, &root);
545                 if (r != EOK)
546                         return r;
547
548                 r = ext4_dir_dx_init(&child, &root);
549                 if (r != EOK)
550                         return r;
551
552                 ext4_inode_set_flag(root.inode, EXT4_INODE_FLAG_INDEX);
553                 ext4_inode_set_flag(child.inode, EXT4_INODE_FLAG_INDEX);
554         } else
555 #endif
556         {
557                 r = ext4_dir_add_entry(&root, ".", strlen("."), &root);
558                 if (r != EOK)
559                         return r;
560
561                 r = ext4_dir_add_entry(&root, "..", strlen(".."), &root);
562                 if (r != EOK)
563                         return r;
564
565                 r = ext4_dir_add_entry(&child, ".", strlen("."), &child);
566                 if (r != EOK)
567                         return r;
568
569                 r = ext4_dir_add_entry(&child, "..", strlen(".."), &root);
570                 if (r != EOK)
571                         return r;
572         }
573
574         r = ext4_dir_add_entry(&root, "lost+found", strlen("lost+found"), &child);
575         if (r != EOK)
576                 return r;
577
578         ext4_inode_set_links_count(root.inode, 3);
579         ext4_inode_set_links_count(child.inode, 2);
580
581         child.dirty = true;
582         root.dirty = true;
583         ext4_fs_put_inode_ref(&child);
584         ext4_fs_put_inode_ref(&root);
585         return r;
586 }
587
588 int ext4_mkfs(struct ext4_fs *fs, struct ext4_blockdev *bd,
589               struct ext4_mkfs_info *info)
590 {
591         int r;
592
593         r = ext4_block_init(bd);
594         if (r != EOK)
595                 return r;
596
597         if (info->len == 0)
598                 info->len = bd->ph_bcnt * bd->ph_bsize;
599
600         if (info->block_size == 0)
601                 info->block_size = 4096; /*Set block size to default value*/
602
603         /* Round down the filesystem length to be a multiple of the block size */
604         info->len &= ~((uint64_t)info->block_size - 1);
605
606         if (info->journal_blocks == 0)
607                 info->journal_blocks = compute_journal_blocks(info);
608
609         if (info->blocks_per_group == 0)
610                 info->blocks_per_group = compute_blocks_per_group(info);
611
612         if (info->inodes == 0)
613                 info->inodes = compute_inodes(info);
614
615         if (info->inode_size == 0)
616                 info->inode_size = 256;
617
618         if (info->label == NULL)
619                 info->label = "";
620
621         info->inodes_per_group = compute_inodes_per_group(info);
622
623         info->feat_compat = EXT4_SUPPORTED_FCOM;
624         info->feat_ro_compat = EXT4_SUPPORTED_FRO_COM;
625         info->feat_incompat = EXT4_SUPPORTED_FINCOM;
626
627         /*TODO: handle this features*/
628         info->feat_incompat &= ~EXT4_FINCOM_META_BG;
629         info->feat_incompat &= ~EXT4_FINCOM_FLEX_BG;
630         info->feat_ro_compat &= ~EXT4_FRO_COM_METADATA_CSUM;
631
632         if (info->no_journal == 0)
633                 info->feat_compat |= 0;
634
635         if (info->dsc_size == 0) {
636
637                 if (info->feat_incompat & EXT4_FINCOM_64BIT)
638                         info->dsc_size = EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE;
639                 else
640                         info->dsc_size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE;
641         }
642
643         info->bg_desc_reserve_blocks = 0;
644
645         ext4_dbg(DEBUG_MKFS, DBG_INFO "Creating filesystem with parameters:\n");
646         ext4_dbg(DEBUG_MKFS, DBG_NONE "Size: %"PRIu64"\n", info->len);
647         ext4_dbg(DEBUG_MKFS, DBG_NONE "Block size: %"PRIu32"\n",
648                         info->block_size);
649         ext4_dbg(DEBUG_MKFS, DBG_NONE "Blocks per group: %"PRIu32"\n",
650                         info->blocks_per_group);
651         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes per group: %"PRIu32"\n",
652                         info->inodes_per_group);
653         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inode size: %"PRIu32"\n",
654                         info->inode_size);
655         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes: %"PRIu32"\n", info->inodes);
656         ext4_dbg(DEBUG_MKFS, DBG_NONE "Journal blocks: %"PRIu32"\n",
657                         info->journal_blocks);
658         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features ro_compat: 0x%x\n",
659                         info->feat_ro_compat);
660         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features compat: 0x%x\n",
661                         info->feat_compat);
662         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features incompat: 0x%x\n",
663                         info->feat_incompat);
664         ext4_dbg(DEBUG_MKFS, DBG_NONE "BG desc reserve: %"PRIu32"\n",
665                         info->bg_desc_reserve_blocks);
666         ext4_dbg(DEBUG_MKFS, DBG_NONE "journal: %s\n",
667                         !info->no_journal ? "yes" : "no");
668         ext4_dbg(DEBUG_MKFS, DBG_NONE "Label: %s\n", info->label);
669
670         struct ext4_bcache bc;
671         memset(&bc, 0, sizeof(struct ext4_bcache));
672         ext4_block_set_lb_size(bd, info->block_size);
673         r = ext4_bcache_init_dynamic(&bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
674                                       info->block_size);
675         if (r != EOK)
676                 goto block_fini;
677
678         /*Bind block cache to block device*/
679         r = ext4_block_bind_bcache(bd, &bc);
680         if (r != EOK)
681                 goto cache_fini;
682
683         r = ext4_block_cache_write_back(bd, 0);
684         if (r != EOK)
685                 goto cache_fini;
686
687         r = mkfs_initial(bd, info);
688         if (r != EOK)
689                 goto cache_fini;
690
691         r = ext4_fs_init(fs, bd);
692         if (r != EOK)
693                 goto cache_fini;
694
695         r = init_bgs(fs);
696         if (r != EOK)
697                 goto fs_fini;
698
699         r = alloc_inodes(fs);
700         if (r != EOK)
701                 goto fs_fini;
702
703         r = create_dirs(fs);
704         if (r != EOK)
705                 goto fs_fini;
706
707         fs_fini:
708         ext4_fs_fini(fs);
709
710         cache_fini:
711         ext4_bcache_fini_dynamic(&bc);
712
713         block_fini:
714         ext4_block_fini(bd);
715
716         return r;
717 }
718
719 /**
720  * @}
721  */