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