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