ext4_mkfs: merge fill_bgroups and write_bgroups into one function
[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         struct ext4_bgroup *bg_desc;
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 = calloc(1, EXT4_SUPERBLOCK_SIZE);
180         if (!aux_info->sb)
181                 return ENOMEM;
182
183         aux_info->bg_desc = calloc(1, sizeof(struct ext4_bgroup));
184         if (!aux_info->bg_desc)
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                 free(aux_info->sb);
217         if (aux_info->bg_desc)
218                 free(aux_info->bg_desc);
219 }
220
221
222 /* Fill in the superblock memory buffer based on the filesystem parameters */
223 static void fill_in_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
299 static int write_bgroups(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
300                          struct ext4_mkfs_info *info)
301 {
302         int r = EOK;
303         uint32_t i, j;
304         uint32_t bg_free_blk = 0;
305         uint64_t sb_free_blk = 0;
306         struct ext4_block b;
307
308         uint32_t block_size = ext4_sb_get_block_size(aux_info->sb);
309
310         for (i = 0; i < aux_info->groups; i++) {
311                 uint64_t bg_start_block = aux_info->first_data_block +
312                         aux_info->first_data_block + i * info->blocks_per_group;
313                 uint32_t blk_off = 0;
314
315                 bg_free_blk = info->blocks_per_group -
316                                 aux_info->inode_table_blocks;
317
318                 bg_free_blk -= 2;
319                 blk_off += aux_info->bg_desc_blocks;
320
321                 if (i == (aux_info->groups - 1))
322                         bg_free_blk -= aux_info->first_data_block;
323
324                 if (has_superblock(info, i)) {
325                         bg_start_block++;
326                         blk_off += info->bg_desc_reserve_blocks;
327                         bg_free_blk -= info->bg_desc_reserve_blocks + 1;
328                         bg_free_blk -= aux_info->bg_desc_blocks;
329                 }
330
331                 ext4_bg_set_block_bitmap(aux_info->bg_desc, aux_info->sb,
332                                 bg_start_block + blk_off + 1);
333
334                 ext4_bg_set_inode_bitmap(aux_info->bg_desc, aux_info->sb,
335                                 bg_start_block + blk_off + 2);
336
337                 ext4_bg_set_inode_table_first_block(aux_info->bg_desc,
338                                 aux_info->sb,
339                                 bg_start_block + blk_off + 3);
340
341                 ext4_bg_set_free_blocks_count(aux_info->bg_desc,
342                                 aux_info->sb, bg_free_blk);
343
344                 ext4_bg_set_free_inodes_count(aux_info->bg_desc,
345                                 aux_info->sb, aux_info->sb->inodes_per_group);
346
347                 ext4_bg_set_used_dirs_count(aux_info->bg_desc, aux_info->sb,
348                                             0);
349
350                 ext4_bg_set_flag(aux_info->bg_desc,
351                                 EXT4_BLOCK_GROUP_BLOCK_UNINIT |
352                                 EXT4_BLOCK_GROUP_INODE_UNINIT);
353
354                 sb_free_blk += bg_free_blk;
355
356
357                 for (j = 0; j < aux_info->groups; j++) {
358                         uint64_t bg_start_block = aux_info->first_data_block +
359                                                   j * info->blocks_per_group;
360                         uint32_t blk_off = 0;
361
362                         blk_off += aux_info->bg_desc_blocks;
363                         if (has_superblock(info, j)) {
364                                 bg_start_block++;
365                                 blk_off += info->bg_desc_reserve_blocks;
366                         }
367
368
369                         uint32_t dsc_pos = 0;
370                         uint32_t dsc_size = ext4_sb_get_desc_size(aux_info->sb);
371                         uint64_t dsc_blk = bg_start_block +
372                                            i * dsc_size / block_size;
373
374                         r = ext4_block_get(bd, &b, dsc_blk);
375                         if (r != EOK)
376                                 return r;
377
378                         dsc_pos = (i * dsc_size) % block_size;
379                         memcpy(b.data + dsc_pos,
380                                         aux_info->bg_desc,
381                                         dsc_size);
382
383
384                         ext4_bcache_set_dirty(b.buf);
385                         r = ext4_block_set(bd, &b);
386                         if (r != EOK)
387                                 return r;
388                 }
389
390                 r = ext4_block_get_noread(bd, &b, bg_start_block + blk_off + 1);
391                 if (r != EOK)
392                         return r;
393                 memset(b.data, 0, block_size);
394                 ext4_bcache_set_dirty(b.buf);
395                 r = ext4_block_set(bd, &b);
396                 if (r != EOK)
397                         return r;
398                 r = ext4_block_get_noread(bd, &b, bg_start_block + blk_off + 2);
399                 if (r != EOK)
400                         return r;
401                 memset(b.data, 0, block_size);
402                 ext4_bcache_set_dirty(b.buf);
403                 r = ext4_block_set(bd, &b);
404                 if (r != EOK)
405                         return r;
406         }
407
408         ext4_sb_set_free_blocks_cnt(aux_info->sb, sb_free_blk);
409         return r;
410 }
411
412 static int write_sblocks(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
413                           struct ext4_mkfs_info *info)
414 {
415         uint64_t offset;
416         uint32_t i;
417         int r;
418
419         /* write out the backup superblocks */
420         for (i = 1; i < aux_info->groups; i++) {
421                 if (has_superblock(info, i)) {
422                         offset = info->block_size * (aux_info->first_data_block
423                                 + i * info->blocks_per_group);
424
425                         aux_info->sb->block_group_index = to_le16(i);
426                         r = ext4_block_writebytes(bd, offset, aux_info->sb,
427                                                   EXT4_SUPERBLOCK_SIZE);
428                         if (r != EOK)
429                                 return r;
430                 }
431         }
432
433         /* write out the primary superblock */
434         aux_info->sb->block_group_index = to_le16(0);
435         return ext4_block_writebytes(bd, 1024, aux_info->sb,
436                         EXT4_SUPERBLOCK_SIZE);
437 }
438
439
440 int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
441 {
442         int r;
443         struct ext4_sblock *sb = NULL;
444         r = ext4_block_init(bd);
445         if (r != EOK)
446                 return r;
447
448         sb = malloc(EXT4_SUPERBLOCK_SIZE);
449         if (!sb)
450                 goto Finish;
451
452
453         r = ext4_sb_read(bd, sb);
454         if (r != EOK)
455                 goto Finish;
456
457         r = sb2info(sb, info);
458
459 Finish:
460         if (sb)
461                 free(sb);
462         ext4_block_fini(bd);
463         return r;
464 }
465
466 static int mkfs_initial(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
467 {
468         int r;
469         struct fs_aux_info aux_info;
470         memset(&aux_info, 0, sizeof(struct fs_aux_info));
471
472         r = create_fs_aux_info(&aux_info, info);
473         if (r != EOK)
474                 goto Finish;
475
476         fill_in_sb(&aux_info, info);
477
478         r = write_bgroups(bd, &aux_info, info);
479         if (r != EOK)
480                 goto Finish;
481
482         r = write_sblocks(bd, &aux_info, info);
483         if (r != EOK)
484                 goto Finish;
485
486         Finish:
487         release_fs_aux_info(&aux_info);
488         return r;
489 }
490
491 static int init_bgs(struct ext4_fs *fs)
492 {
493         int r = EOK;
494         struct ext4_block_group_ref ref;
495         uint32_t i;
496         uint32_t bg_count = ext4_block_group_cnt(&fs->sb);
497         for (i = 0; i < bg_count; ++i) {
498                 r = ext4_fs_get_block_group_ref(fs, i, &ref);
499                 if (r != EOK)
500                         break;
501
502                 r = ext4_fs_put_block_group_ref(&ref);
503                 if (r != EOK)
504                         break;
505         }
506         return r;
507 }
508
509 static int alloc_inodes(struct ext4_fs *fs)
510 {
511         int r = EOK;
512         int i;
513         struct ext4_inode_ref inode_ref;
514         for (i = 1; i < 12; ++i) {
515                 int filetype = EXT4_DE_REG_FILE;
516
517                 switch (i) {
518                 case EXT4_ROOT_INO:
519                 case EXT4_GOOD_OLD_FIRST_INO:
520                         filetype = EXT4_DE_DIR;
521                         break;
522                 }
523
524                 r = ext4_fs_alloc_inode(fs, &inode_ref, filetype);
525                 if (r != EOK)
526                         return r;
527
528                 ext4_inode_set_mode(&fs->sb, inode_ref.inode, 0);
529                 ext4_fs_put_inode_ref(&inode_ref);
530         }
531
532         return r;
533 }
534
535 static int create_dirs(struct ext4_fs *fs)
536 {
537         int r = EOK;
538         struct ext4_inode_ref root;
539         struct ext4_inode_ref child;
540
541         r = ext4_fs_get_inode_ref(fs, EXT4_ROOT_INO, &root);
542         if (r != EOK)
543                 return r;
544
545         r = ext4_fs_get_inode_ref(fs, EXT4_GOOD_OLD_FIRST_INO, &child);
546         if (r != EOK)
547                 return r;
548
549         ext4_inode_set_mode(&fs->sb, child.inode,
550                         EXT4_INODE_MODE_DIRECTORY | 0777);
551
552         ext4_inode_set_mode(&fs->sb, root.inode,
553                         EXT4_INODE_MODE_DIRECTORY | 0777);
554
555 #if CONFIG_DIR_INDEX_ENABLE
556         /* Initialize directory index if supported */
557         if (ext4_sb_feature_com(&fs->sb, EXT4_FCOM_DIR_INDEX)) {
558                 r = ext4_dir_dx_init(&root, &root);
559                 if (r != EOK)
560                         return r;
561
562                 r = ext4_dir_dx_init(&child, &root);
563                 if (r != EOK)
564                         return r;
565
566                 ext4_inode_set_flag(root.inode, EXT4_INODE_FLAG_INDEX);
567                 ext4_inode_set_flag(child.inode, EXT4_INODE_FLAG_INDEX);
568         } else
569 #endif
570         {
571                 r = ext4_dir_add_entry(&root, ".", strlen("."), &root);
572                 if (r != EOK)
573                         return r;
574
575                 r = ext4_dir_add_entry(&root, "..", strlen(".."), &root);
576                 if (r != EOK)
577                         return r;
578
579                 r = ext4_dir_add_entry(&child, ".", strlen("."), &child);
580                 if (r != EOK)
581                         return r;
582
583                 r = ext4_dir_add_entry(&child, "..", strlen(".."), &root);
584                 if (r != EOK)
585                         return r;
586         }
587
588         r = ext4_dir_add_entry(&root, "lost+found", strlen("lost+found"), &child);
589         if (r != EOK)
590                 return r;
591
592         ext4_inode_set_links_cnt(root.inode, 3);
593         ext4_inode_set_links_cnt(child.inode, 2);
594
595         child.dirty = true;
596         root.dirty = true;
597         ext4_fs_put_inode_ref(&child);
598         ext4_fs_put_inode_ref(&root);
599         return r;
600 }
601
602 int ext4_mkfs(struct ext4_fs *fs, struct ext4_blockdev *bd,
603               struct ext4_mkfs_info *info, int fs_type)
604 {
605         int r;
606
607         r = ext4_block_init(bd);
608         if (r != EOK)
609                 return r;
610
611         bd->fs = fs;
612
613         if (info->len == 0)
614                 info->len = bd->bdif->ph_bcnt * bd->bdif->ph_bsize;
615
616         if (info->block_size == 0)
617                 info->block_size = 4096; /*Set block size to default value*/
618
619         /* Round down the filesystem length to be a multiple of the block size */
620         info->len &= ~((uint64_t)info->block_size - 1);
621
622         if (info->journal_blocks == 0)
623                 info->journal_blocks = compute_journal_blocks(info);
624
625         if (info->blocks_per_group == 0)
626                 info->blocks_per_group = compute_blocks_per_group(info);
627
628         if (info->inodes == 0)
629                 info->inodes = compute_inodes(info);
630
631         if (info->inode_size == 0)
632                 info->inode_size = 256;
633
634         if (info->label == NULL)
635                 info->label = "";
636
637         info->inodes_per_group = compute_inodes_per_group(info);
638
639         switch (fs_type) {
640         case F_SET_EXT2:
641                 info->feat_compat = EXT2_SUPPORTED_FCOM;
642                 info->feat_ro_compat = EXT2_SUPPORTED_FRO_COM;
643                 info->feat_incompat = EXT2_SUPPORTED_FINCOM;
644                 break;
645         case F_SET_EXT3:
646                 info->feat_compat = EXT3_SUPPORTED_FCOM;
647                 info->feat_ro_compat = EXT3_SUPPORTED_FRO_COM;
648                 info->feat_incompat = EXT3_SUPPORTED_FINCOM;
649                 break;
650         case F_SET_EXT4:
651                 info->feat_compat = EXT4_SUPPORTED_FCOM;
652                 info->feat_ro_compat = EXT4_SUPPORTED_FRO_COM;
653                 info->feat_incompat = EXT4_SUPPORTED_FINCOM;
654                 break;
655         }
656
657         /*TODO: handle this features*/
658         info->feat_incompat &= ~EXT4_FINCOM_META_BG;
659         info->feat_incompat &= ~EXT4_FINCOM_FLEX_BG;
660         info->feat_ro_compat &= ~EXT4_FRO_COM_METADATA_CSUM;
661
662         /*TODO: handle journal feature & inode*/
663         if (info->journal == 0)
664                 info->feat_compat |= 0;
665
666         if (info->dsc_size == 0) {
667
668                 if (info->feat_incompat & EXT4_FINCOM_64BIT)
669                         info->dsc_size = EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE;
670                 else
671                         info->dsc_size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE;
672         }
673
674         info->bg_desc_reserve_blocks = 0;
675
676         ext4_dbg(DEBUG_MKFS, DBG_INFO "Creating filesystem with parameters:\n");
677         ext4_dbg(DEBUG_MKFS, DBG_NONE "Size: %"PRIu64"\n", info->len);
678         ext4_dbg(DEBUG_MKFS, DBG_NONE "Block size: %"PRIu32"\n",
679                         info->block_size);
680         ext4_dbg(DEBUG_MKFS, DBG_NONE "Blocks per group: %"PRIu32"\n",
681                         info->blocks_per_group);
682         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes per group: %"PRIu32"\n",
683                         info->inodes_per_group);
684         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inode size: %"PRIu32"\n",
685                         info->inode_size);
686         ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes: %"PRIu32"\n", info->inodes);
687         ext4_dbg(DEBUG_MKFS, DBG_NONE "Journal blocks: %"PRIu32"\n",
688                         info->journal_blocks);
689         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features ro_compat: 0x%x\n",
690                         info->feat_ro_compat);
691         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features compat: 0x%x\n",
692                         info->feat_compat);
693         ext4_dbg(DEBUG_MKFS, DBG_NONE "Features incompat: 0x%x\n",
694                         info->feat_incompat);
695         ext4_dbg(DEBUG_MKFS, DBG_NONE "BG desc reserve: %"PRIu32"\n",
696                         info->bg_desc_reserve_blocks);
697         ext4_dbg(DEBUG_MKFS, DBG_NONE "Descriptor size: %"PRIu16"\n",
698                         info->dsc_size);
699         ext4_dbg(DEBUG_MKFS, DBG_NONE "journal: %s\n",
700                         info->journal ? "yes" : "no");
701         ext4_dbg(DEBUG_MKFS, DBG_NONE "Label: %s\n", info->label);
702
703         struct ext4_bcache bc;
704         memset(&bc, 0, sizeof(struct ext4_bcache));
705         ext4_block_set_lb_size(bd, info->block_size);
706         r = ext4_bcache_init_dynamic(&bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
707                                       info->block_size);
708         if (r != EOK)
709                 goto block_fini;
710
711         /*Bind block cache to block device*/
712         r = ext4_block_bind_bcache(bd, &bc);
713         if (r != EOK)
714                 goto cache_fini;
715
716         r = ext4_block_cache_write_back(bd, 1);
717         if (r != EOK)
718                 goto cache_fini;
719
720         r = mkfs_initial(bd, info);
721         if (r != EOK)
722                 goto cache_fini;
723
724         r = ext4_fs_init(fs, bd, false);
725         if (r != EOK)
726                 goto cache_fini;
727
728         r = init_bgs(fs);
729         if (r != EOK)
730                 goto fs_fini;
731
732         r = alloc_inodes(fs);
733         if (r != EOK)
734                 goto fs_fini;
735
736         r = create_dirs(fs);
737         if (r != EOK)
738                 goto fs_fini;
739
740         fs_fini:
741         ext4_fs_fini(fs);
742
743         cache_fini:
744         ext4_block_cache_write_back(bd, 0);
745         ext4_bcache_fini_dynamic(&bc);
746
747         block_fini:
748         ext4_block_fini(bd);
749
750         return r;
751 }
752
753 /**
754  * @}
755  */