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