2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup lwext4
37 #include "ext4_config.h"
38 #include "ext4_super.h"
39 #include "ext4_block_group.h"
42 #include "ext4_inode.h"
43 #include "ext4_debug.h"
44 #include "ext4_ialloc.h"
45 #include "ext4_mkfs.h"
51 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
52 #define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
55 struct ext4_sblock *sb;
56 struct ext4_bgroup *bg_desc;
57 struct xattr_list_element *xattrs;
58 uint32_t first_data_block;
60 uint32_t inode_table_blocks;
62 uint32_t bg_desc_blocks;
63 uint32_t default_i_flags;
64 uint32_t blocks_per_ind;
65 uint32_t blocks_per_dind;
66 uint32_t blocks_per_tind;
69 static inline int log_2(int j)
73 for (i = 0; j > 0; i++)
79 static int sb2info(struct ext4_sblock *sb, struct ext4_mkfs_info *info)
81 if (to_le16(sb->magic) != EXT4_SUPERBLOCK_MAGIC)
84 info->block_size = 1024 << to_le32(sb->log_block_size);
85 info->blocks_per_group = to_le32(sb->blocks_per_group);
86 info->inodes_per_group = to_le32(sb->inodes_per_group);
87 info->inode_size = to_le16(sb->inode_size);
88 info->inodes = to_le32(sb->inodes_count);
89 info->feat_ro_compat = to_le32(sb->features_read_only);
90 info->feat_compat = to_le32(sb->features_compatible);
91 info->feat_incompat = to_le32(sb->features_incompatible);
92 info->bg_desc_reserve_blocks = to_le16(sb->s_reserved_gdt_blocks);
93 info->label = sb->volume_name;
94 info->len = (uint64_t)info->block_size * ext4_sb_get_blocks_cnt(sb);
99 static uint32_t compute_blocks_per_group(struct ext4_mkfs_info *info)
101 return info->block_size * 8;
104 static uint32_t compute_inodes(struct ext4_mkfs_info *info)
106 return DIV_ROUND_UP(info->len, info->block_size) / 4;
109 static uint32_t compute_inodes_per_group(struct ext4_mkfs_info *info)
111 uint32_t blocks = DIV_ROUND_UP(info->len, info->block_size);
112 uint32_t block_groups = DIV_ROUND_UP(blocks, info->blocks_per_group);
113 uint32_t inodes = DIV_ROUND_UP(info->inodes, block_groups);
114 inodes = EXT4_ALIGN(inodes, (info->block_size / info->inode_size));
116 /* After properly rounding up the number of inodes/group,
117 * make sure to update the total inodes field in the info struct.
119 info->inodes = inodes * block_groups;
125 static uint32_t compute_journal_blocks(struct ext4_mkfs_info *info)
127 uint32_t journal_blocks = DIV_ROUND_UP(info->len, info->block_size) / 64;
128 if (journal_blocks < 1024)
129 journal_blocks = 1024;
130 if (journal_blocks > 32768)
131 journal_blocks = 32768;
132 return journal_blocks;
135 static bool has_superblock(struct ext4_mkfs_info *info, uint32_t bgid)
137 if (!(info->feat_ro_compat & EXT4_FRO_COM_SPARSE_SUPER))
140 return ext4_sb_sparse(bgid);
143 static int create_fs_aux_info(struct fs_aux_info *aux_info,
144 struct ext4_mkfs_info *info)
146 aux_info->first_data_block = (info->block_size > 1024) ? 0 : 1;
147 aux_info->len_blocks = info->len / info->block_size;
148 aux_info->inode_table_blocks = DIV_ROUND_UP(info->inodes_per_group *
149 info->inode_size, info->block_size);
150 aux_info->groups = DIV_ROUND_UP(aux_info->len_blocks -
151 aux_info->first_data_block, info->blocks_per_group);
152 aux_info->blocks_per_ind = info->block_size / sizeof(uint32_t);
153 aux_info->blocks_per_dind =
154 aux_info->blocks_per_ind * aux_info->blocks_per_ind;
155 aux_info->blocks_per_tind =
156 aux_info->blocks_per_dind * aux_info->blocks_per_dind;
158 aux_info->bg_desc_blocks =
159 DIV_ROUND_UP(aux_info->groups * sizeof(struct ext4_bgroup),
162 aux_info->default_i_flags = EXT4_INODE_FLAG_NOATIME;
164 uint32_t last_group_size = aux_info->len_blocks % info->blocks_per_group;
165 uint32_t last_header_size = 2 + aux_info->inode_table_blocks;
166 if (has_superblock(info, aux_info->groups - 1))
167 last_header_size += 1 + aux_info->bg_desc_blocks +
168 info->bg_desc_reserve_blocks;
170 if (last_group_size > 0 && last_group_size < last_header_size) {
172 aux_info->len_blocks -= last_group_size;
175 aux_info->sb = calloc(1, EXT4_SUPERBLOCK_SIZE);
179 aux_info->bg_desc = calloc(aux_info->groups, sizeof(struct ext4_bgroup));
180 if (!aux_info->bg_desc)
183 aux_info->xattrs = NULL;
187 static void release_fs_aux_info(struct fs_aux_info *aux_info)
191 if (aux_info->bg_desc)
192 free(aux_info->bg_desc);
196 /* Fill in the superblock memory buffer based on the filesystem parameters */
197 static void fill_in_sb(struct fs_aux_info *aux_info, struct ext4_mkfs_info *info)
199 struct ext4_sblock *sb = aux_info->sb;
201 sb->inodes_count = to_le32(info->inodes_per_group * aux_info->groups);
203 ext4_sb_set_blocks_cnt(sb, aux_info->len_blocks);
204 ext4_sb_set_free_blocks_cnt(sb, aux_info->len_blocks);
205 sb->free_inodes_count = to_le32(info->inodes_per_group * aux_info->groups);
207 sb->reserved_blocks_count_lo = to_le32(0);
208 sb->first_data_block = to_le32(aux_info->first_data_block);
209 sb->log_block_size = to_le32(log_2(info->block_size / 1024));
210 sb->log_cluster_size = to_le32(log_2(info->block_size / 1024));
211 sb->blocks_per_group = to_le32(info->blocks_per_group);
212 sb->frags_per_group = to_le32(info->blocks_per_group);
213 sb->inodes_per_group = to_le32(info->inodes_per_group);
214 sb->mount_time = to_le32(0);
215 sb->write_time = to_le32(0);
216 sb->mount_count = to_le16(0);
217 sb->max_mount_count = to_le16(0xFFFF);
218 sb->magic = to_le16(EXT4_SUPERBLOCK_MAGIC);
219 sb->state = to_le16(EXT4_SUPERBLOCK_STATE_VALID_FS);
220 sb->errors = to_le16(EXT4_SUPERBLOCK_ERRORS_RO);
221 sb->minor_rev_level = to_le16(0);
222 sb->last_check_time = to_le32(0);
223 sb->check_interval = to_le32(0);
224 sb->creator_os = to_le32(EXT4_SUPERBLOCK_OS_LINUX);
225 sb->rev_level = to_le32(1);
226 sb->def_resuid = to_le16(0);
227 sb->def_resgid = to_le16(0);
229 sb->first_inode = to_le32(EXT4_GOOD_OLD_FIRST_INO);
230 sb->inode_size = to_le16(info->inode_size);
231 sb->block_group_index = to_le16(0);
233 sb->features_compatible = to_le32(info->feat_compat);
234 sb->features_incompatible = to_le32(info->feat_incompat);
235 sb->features_read_only = to_le32(info->feat_ro_compat);
237 memset(sb->uuid, 0, sizeof(sb->uuid));
239 memset(sb->volume_name, 0, sizeof(sb->volume_name));
240 strncpy(sb->volume_name, info->label, sizeof(sb->volume_name));
241 memset(sb->last_mounted, 0, sizeof(sb->last_mounted));
243 sb->algorithm_usage_bitmap = to_le32(0);
244 sb->s_prealloc_blocks = 0;
245 sb->s_prealloc_dir_blocks = 0;
246 sb->s_reserved_gdt_blocks = to_le16(info->bg_desc_reserve_blocks);
248 if (info->feat_compat & EXT4_FCOM_HAS_JOURNAL)
249 sb->journal_inode_number = to_le32(EXT4_JOURNAL_INO);
250 sb->journal_dev = to_le32(0);
251 sb->last_orphan = to_le32(0);
252 memset(sb->hash_seed, 0, sizeof(sb->hash_seed));
253 sb->default_hash_version = EXT2_HTREE_HALF_MD4;
254 sb->checksum_type = 1;
255 sb->desc_size = to_le16(EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE);
256 sb->default_mount_opts = to_le32(0);
257 sb->first_meta_bg = to_le32(0);
258 sb->mkfs_time = to_le32(0);
260 sb->reserved_blocks_count_hi = to_le32(0);
261 sb->min_extra_isize = to_le32(sizeof(struct ext4_inode) -
262 EXT4_GOOD_OLD_INODE_SIZE);
263 sb->want_extra_isize = to_le32(sizeof(struct ext4_inode) -
264 EXT4_GOOD_OLD_INODE_SIZE);
265 sb->flags = to_le32(2);
268 static void fill_bgroups(struct fs_aux_info *aux_info,
269 struct ext4_mkfs_info *info)
273 uint64_t bg_free_blk = 0;
274 uint64_t sb_free_blk = 0;
276 for (i = 0; i < aux_info->groups; i++) {
278 uint64_t bg_start_block = aux_info->first_data_block +
279 aux_info->first_data_block + i * info->blocks_per_group;
280 uint32_t blk_off = 0;
282 bg_free_blk = info->blocks_per_group -
283 (aux_info->inode_table_blocks + aux_info->bg_desc_blocks);
286 blk_off += aux_info->bg_desc_blocks;
288 if (has_superblock(info, i)) {
290 blk_off += info->bg_desc_reserve_blocks;
291 bg_free_blk -= info->bg_desc_reserve_blocks + 1;
294 ext4_bg_set_block_bitmap(&aux_info->bg_desc[i], aux_info->sb,
295 bg_start_block + blk_off + 1);
297 ext4_bg_set_inode_bitmap(&aux_info->bg_desc[i], aux_info->sb,
298 bg_start_block + blk_off + 2);
300 ext4_bg_set_inode_table_first_block(&aux_info->bg_desc[i],
302 bg_start_block + blk_off + 3);
304 ext4_bg_set_free_blocks_count(&aux_info->bg_desc[i],
305 aux_info->sb, bg_free_blk);
307 ext4_bg_set_free_inodes_count(&aux_info->bg_desc[i],
308 aux_info->sb, aux_info->sb->inodes_per_group);
310 ext4_bg_set_used_dirs_count(&aux_info->bg_desc[i], aux_info->sb,
313 ext4_bg_set_flag(&aux_info->bg_desc[i],
314 EXT4_BLOCK_GROUP_BLOCK_UNINIT |
315 EXT4_BLOCK_GROUP_INODE_UNINIT);
317 sb_free_blk += bg_free_blk;
320 ext4_sb_set_free_blocks_cnt(aux_info->sb, sb_free_blk);
324 static int write_bgroups(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
325 struct ext4_mkfs_info *info)
330 for (i = 0; i < aux_info->groups; i++) {
331 uint64_t bg_start_block = aux_info->first_data_block +
332 aux_info->first_data_block + i * info->blocks_per_group;
333 uint32_t blk_off = 0;
335 blk_off += aux_info->bg_desc_blocks;
336 if (has_superblock(info, i)) {
338 blk_off += info->bg_desc_reserve_blocks;
341 uint32_t block_size = ext4_sb_get_block_size(aux_info->sb);
342 uint32_t dsc_pos = 0;
344 uint32_t dsc_size = ext4_sb_get_desc_size(aux_info->sb);
345 uint32_t dsc_blk_cnt = aux_info->bg_desc_blocks;
346 uint64_t dsc_blk = bg_start_block;
348 while (dsc_blk_cnt--) {
349 r = ext4_block_get(bd, &b, dsc_blk++);
353 while (dsc_pos + dsc_size < block_size) {
354 memcpy(b.data + dsc_pos,
355 &aux_info->bg_desc[dsc_id],
363 r = ext4_block_set(bd, &b);
368 r = ext4_block_get_noread(bd, &b, bg_start_block + blk_off + 1);
371 memset(b.data, 0, block_size);
373 r = ext4_block_set(bd, &b);
376 r = ext4_block_get_noread(bd, &b, bg_start_block + blk_off + 2);
379 memset(b.data, 0, block_size);
381 r = ext4_block_set(bd, &b);
390 static int write_sblocks(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
391 struct ext4_mkfs_info *info)
397 /* write out the backup superblocks */
398 for (i = 1; i < aux_info->groups; i++) {
399 if (has_superblock(info, i)) {
400 offset = info->block_size * (aux_info->first_data_block
401 + i * info->blocks_per_group);
403 aux_info->sb->block_group_index = i;
404 r = ext4_block_writebytes(bd, offset, aux_info->sb,
405 EXT4_SUPERBLOCK_SIZE);
411 /* write out the primary superblock */
412 aux_info->sb->block_group_index = 0;
413 return ext4_block_writebytes(bd, 1024, aux_info->sb,
414 EXT4_SUPERBLOCK_SIZE);
418 int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
421 struct ext4_sblock *sb = NULL;
422 r = ext4_block_init(bd);
426 sb = malloc(EXT4_SUPERBLOCK_SIZE);
431 r = ext4_sb_read(bd, sb);
435 r = sb2info(sb, info);
444 static int mkfs_initial(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
447 struct fs_aux_info aux_info;
448 memset(&aux_info, 0, sizeof(struct fs_aux_info));
450 r = create_fs_aux_info(&aux_info, info);
454 fill_in_sb(&aux_info, info);
455 fill_bgroups(&aux_info, info);
458 r = write_bgroups(bd, &aux_info, info);
462 r = write_sblocks(bd, &aux_info, info);
467 release_fs_aux_info(&aux_info);
471 static int init_bgs(struct ext4_fs *fs)
474 struct ext4_block_group_ref ref;
476 uint32_t bg_count = ext4_block_group_cnt(&fs->sb);
477 for (i = 0; i < bg_count; ++i) {
478 r = ext4_fs_get_block_group_ref(fs, i, &ref);
482 r = ext4_fs_put_block_group_ref(&ref);
489 static int alloc_inodes(struct ext4_fs *fs)
493 struct ext4_inode_ref inode_ref;
494 for (i = 1; i < 12; ++i) {
495 int filetype = EXT4_DIRENTRY_REG_FILE;
499 case EXT4_GOOD_OLD_FIRST_INO:
500 filetype = EXT4_DIRENTRY_DIR;
504 r = ext4_fs_alloc_inode(fs, &inode_ref, filetype);
508 ext4_inode_set_mode(&fs->sb, inode_ref.inode, 0);
509 ext4_fs_put_inode_ref(&inode_ref);
515 static int create_dirs(struct ext4_fs *fs)
518 struct ext4_inode_ref root;
519 struct ext4_inode_ref child;
521 r = ext4_fs_get_inode_ref(fs, EXT4_ROOT_INO, &root);
525 r = ext4_fs_get_inode_ref(fs, EXT4_GOOD_OLD_FIRST_INO, &child);
529 ext4_inode_set_mode(&fs->sb, child.inode,
530 EXT4_INODE_MODE_DIRECTORY | 0777);
532 ext4_inode_set_mode(&fs->sb, root.inode,
533 EXT4_INODE_MODE_DIRECTORY | 0777);
535 r = ext4_dir_add_entry(&root, ".", strlen("."), &root);
539 r = ext4_dir_add_entry(&root, "..", strlen(".."), &root);
543 r = ext4_dir_add_entry(&child, ".", strlen("."), &child);
547 r = ext4_dir_add_entry(&child, "..", strlen(".."), &root);
551 r = ext4_dir_add_entry(&root, "lost+found", strlen("lost+found"), &child);
555 ext4_inode_set_links_count(root.inode, 3);
556 ext4_inode_set_links_count(child.inode, 2);
558 ext4_fs_put_inode_ref(&child);
559 ext4_fs_put_inode_ref(&root);
563 int ext4_mkfs(struct ext4_fs *fs, struct ext4_blockdev *bd,
564 struct ext4_mkfs_info *info)
568 r = ext4_block_init(bd);
573 info->len = bd->ph_bcnt * bd->ph_bsize;
575 if (info->block_size == 0)
576 info->block_size = 4096; /*Set block size to default value*/
578 /* Round down the filesystem length to be a multiple of the block size */
579 info->len &= ~((uint64_t)info->block_size - 1);
581 if (info->journal_blocks == 0)
582 info->journal_blocks = compute_journal_blocks(info);
584 if (info->blocks_per_group == 0)
585 info->blocks_per_group = compute_blocks_per_group(info);
587 if (info->inodes == 0)
588 info->inodes = compute_inodes(info);
590 if (info->inode_size == 0)
591 info->inode_size = 256;
593 if (info->label == NULL)
596 info->inodes_per_group = compute_inodes_per_group(info);
598 info->feat_compat = EXT3_SUPPORTED_FCOM;
599 info->feat_ro_compat = EXT3_SUPPORTED_FRO_COM;
600 info->feat_incompat = EXT3_SUPPORTED_FINCOM;
602 info->feat_incompat &= ~EXT4_FINCOM_META_BG;
604 if (info->no_journal == 0)
605 info->feat_compat |= 0;
607 info->bg_desc_reserve_blocks = 0;
609 ext4_dbg(DEBUG_MKFS, DBG_INFO "Creating filesystem with parameters:\n");
610 ext4_dbg(DEBUG_MKFS, DBG_NONE "Size: %"PRIu64"\n", info->len);
611 ext4_dbg(DEBUG_MKFS, DBG_NONE "Block size: %"PRIu32"\n",
613 ext4_dbg(DEBUG_MKFS, DBG_NONE "Blocks per group: %"PRIu32"\n",
614 info->blocks_per_group);
615 ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes per group: %"PRIu32"\n",
616 info->inodes_per_group);
617 ext4_dbg(DEBUG_MKFS, DBG_NONE "Inode size: %"PRIu32"\n",
619 ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes: %"PRIu32"\n", info->inodes);
620 ext4_dbg(DEBUG_MKFS, DBG_NONE "Journal blocks: %"PRIu32"\n",
621 info->journal_blocks);
622 ext4_dbg(DEBUG_MKFS, DBG_NONE "Features ro_compat: 0x%x\n",
623 info->feat_ro_compat);
624 ext4_dbg(DEBUG_MKFS, DBG_NONE "Features compat: 0x%x\n",
626 ext4_dbg(DEBUG_MKFS, DBG_NONE "Features incompat: 0x%x\n",
627 info->feat_incompat);
628 ext4_dbg(DEBUG_MKFS, DBG_NONE "BG desc reserve: %"PRIu32"\n",
629 info->bg_desc_reserve_blocks);
630 ext4_dbg(DEBUG_MKFS, DBG_NONE "journal: %s\n",
631 !info->no_journal ? "yes" : "no");
632 ext4_dbg(DEBUG_MKFS, DBG_NONE "Label: %s\n", info->label);
634 struct ext4_bcache bc;
635 memset(&bc, 0, sizeof(struct ext4_bcache));
636 ext4_block_set_lb_size(bd, info->block_size);
637 r = ext4_bcache_init_dynamic(&bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
642 /*Bind block cache to block device*/
643 r = ext4_block_bind_bcache(bd, &bc);
647 r = ext4_block_cache_write_back(bd, 0);
651 r = mkfs_initial(bd, info);
655 r = ext4_fs_init(fs, bd);
663 r = alloc_inodes(fs);
675 ext4_bcache_fini_dynamic(&bc);