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