shithub: lwext4

Download patch

ref: 807f49f7a47c5051126230a6b452e04189513610
parent: c1737799ff0b5cbe14c34bec3c3f806437fdc20e
author: gkostka <kostka.grzegorz@gmail.com>
date: Mon Nov 16 15:29:36 EST 2015

Sblock fill by using to_le32/to_le_16 macros & drop gdt_reserved blocks

--- a/lwext4/ext4_mkfs.c
+++ b/lwext4/ext4_mkfs.c
@@ -38,7 +38,9 @@
 #include "ext4_super.h"
 #include "ext4_block_group.h"
 #include "ext4_fs.h"
+#include "ext4_inode.h"
 #include "ext4_debug.h"
+#include "ext4_ialloc.h"
 #include "ext4_mkfs.h"
 
 #include <inttypes.h>
@@ -118,22 +120,7 @@
 	return inodes;
 }
 
-static uint32_t compute_bg_desc_reserve_blocks(struct ext4_mkfs_info *info)
-{
-	uint32_t blocks = DIV_ROUND_UP(info->len, info->block_size);
-	uint32_t block_groups = DIV_ROUND_UP(blocks, info->blocks_per_group);
-	uint32_t bg_desc_blocks = DIV_ROUND_UP(block_groups *
-			sizeof(struct ext4_bgroup), info->block_size);
 
-	uint32_t bg_desc_reserve_blocks = DIV_ROUND_UP(block_groups * 1024 *
-		sizeof(struct ext4_bgroup), info->block_size) - bg_desc_blocks;
-
-	if (bg_desc_reserve_blocks > info->block_size / sizeof(uint32_t))
-		bg_desc_reserve_blocks = info->block_size / sizeof(uint32_t);
-
-	return bg_desc_reserve_blocks;
-}
-
 static uint32_t compute_journal_blocks(struct ext4_mkfs_info *info)
 {
 	uint32_t journal_blocks = DIV_ROUND_UP(info->len, info->block_size) / 64;
@@ -210,79 +197,71 @@
 {
 	struct ext4_sblock *sb = aux_info->sb;
 
-	sb->inodes_count = info->inodes_per_group * aux_info->groups;
-	sb->blocks_count_lo = aux_info->len_blocks;
-	sb->reserved_blocks_count_lo = 0;
-	sb->free_blocks_count_lo = 0;
-	sb->free_inodes_count = 0;
-	sb->first_data_block = aux_info->first_data_block;
-	sb->log_block_size = log_2(info->block_size / 1024);
-	sb->log_cluster_size = log_2(info->block_size / 1024);
-	sb->blocks_per_group = info->blocks_per_group;
-	sb->frags_per_group = info->blocks_per_group;
-	sb->inodes_per_group = info->inodes_per_group;
-	sb->mount_time = 0;
-	sb->write_time = 0;
-	sb->mount_count = 0;
-	sb->max_mount_count = 0xFFFF;
-	sb->magic = EXT4_SUPERBLOCK_MAGIC;
-	sb->state = EXT4_SUPERBLOCK_STATE_VALID_FS;
-	sb->errors = EXT4_SUPERBLOCK_ERRORS_RO;
-	sb->minor_rev_level = 0;
-	sb->last_check_time = 0;
-	sb->check_interval = 0;
-	sb->creator_os = EXT4_SUPERBLOCK_OS_LINUX;
-	sb->rev_level = 1;
-	sb->def_resuid = 0;
-	sb->def_resgid = 0;
+	sb->inodes_count = to_le32(info->inodes_per_group * aux_info->groups);
 
-	sb->first_inode = EXT4_GOOD_OLD_FIRST_INO;
-	sb->inode_size = info->inode_size;
-	sb->block_group_index = 0;
-	sb->features_compatible = info->feat_compat;
-	sb->features_incompatible = info->feat_incompat;
-	sb->features_read_only = info->feat_ro_compat;
+	ext4_sb_set_blocks_cnt(sb, aux_info->len_blocks);
+	ext4_sb_set_free_blocks_cnt(sb, aux_info->len_blocks);
+	sb->free_inodes_count = to_le32(info->inodes_per_group * aux_info->groups);
 
+	sb->reserved_blocks_count_lo = to_le32(0);
+	sb->first_data_block = to_le32(aux_info->first_data_block);
+	sb->log_block_size = to_le32(log_2(info->block_size / 1024));
+	sb->log_cluster_size = to_le32(log_2(info->block_size / 1024));
+	sb->blocks_per_group = to_le32(info->blocks_per_group);
+	sb->frags_per_group = to_le32(info->blocks_per_group);
+	sb->inodes_per_group = to_le32(info->inodes_per_group);
+	sb->mount_time = to_le32(0);
+	sb->write_time = to_le32(0);
+	sb->mount_count = to_le16(0);
+	sb->max_mount_count = to_le16(0xFFFF);
+	sb->magic = to_le16(EXT4_SUPERBLOCK_MAGIC);
+	sb->state = to_le16(EXT4_SUPERBLOCK_STATE_VALID_FS);
+	sb->errors = to_le16(EXT4_SUPERBLOCK_ERRORS_RO);
+	sb->minor_rev_level = to_le16(0);
+	sb->last_check_time = to_le32(0);
+	sb->check_interval = to_le32(0);
+	sb->creator_os = to_le32(EXT4_SUPERBLOCK_OS_LINUX);
+	sb->rev_level = to_le32(1);
+	sb->def_resuid = to_le16(0);
+	sb->def_resgid = to_le16(0);
+
+	sb->first_inode = to_le32(EXT4_GOOD_OLD_FIRST_INO);
+	sb->inode_size = to_le16(info->inode_size);
+	sb->block_group_index = to_le16(0);
+
+	sb->features_compatible = to_le32(info->feat_compat);
+	sb->features_incompatible = to_le32(info->feat_incompat);
+	sb->features_read_only = to_le32(info->feat_ro_compat);
+
 	memset(sb->uuid, 0, sizeof(sb->uuid));
 
 	memset(sb->volume_name, 0, sizeof(sb->volume_name));
 	strncpy(sb->volume_name, info->label, sizeof(sb->volume_name));
 	memset(sb->last_mounted, 0, sizeof(sb->last_mounted));
-	sb->algorithm_usage_bitmap = 0;
 
-	sb->s_reserved_gdt_blocks = info->bg_desc_reserve_blocks;
+	sb->algorithm_usage_bitmap = to_le32(0);
 	sb->s_prealloc_blocks = 0;
 	sb->s_prealloc_dir_blocks = 0;
+	sb->s_reserved_gdt_blocks = to_le16(info->bg_desc_reserve_blocks);
 
-	//memcpy(sb->journal_uuid, sb->uuid, sizeof(sb->journal_uuid));
 	if (info->feat_compat & EXT4_FCOM_HAS_JOURNAL)
-		sb->journal_inode_number = EXT4_JOURNAL_INO;
-	sb->journal_dev = 0;
-	sb->last_orphan = 0;
-	sb->hash_seed[0] = 0; /* FIXME */
+		sb->journal_inode_number = to_le32(EXT4_JOURNAL_INO);
+	sb->journal_dev = to_le32(0);
+	sb->last_orphan = to_le32(0);
+	memset(sb->hash_seed, 0, sizeof(sb->hash_seed));
 	sb->default_hash_version = EXT2_HTREE_HALF_MD4;
 	sb->checksum_type = 1;
-	sb->desc_size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE;
-	sb->default_mount_opts = 0; /* FIXME */
-	sb->first_meta_bg = 0;
-	sb->mkfs_time = 0;
-	//sb->jnl_blocks[17]; /* FIXME */
+	sb->desc_size = to_le16(EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE);
+	sb->default_mount_opts = to_le32(0);
+	sb->first_meta_bg = to_le32(0);
+	sb->mkfs_time = to_le32(0);
 
-	sb->blocks_count_hi = aux_info->len_blocks >> 32;
-	sb->reserved_blocks_count_hi = 0;
-	sb->free_blocks_count_hi = 0;
-	sb->min_extra_isize = sizeof(struct ext4_inode) -
-		EXT4_GOOD_OLD_INODE_SIZE;
-	sb->want_extra_isize = sizeof(struct ext4_inode) -
-		EXT4_GOOD_OLD_INODE_SIZE;
-	sb->flags = 2;
-	sb->raid_stride = 0;
-	sb->mmp_interval = 0;
-	sb->mmp_block = 0;
-	sb->raid_stripe_width = 0;
-	sb->log_groups_per_flex = 0;
-	sb->kbytes_written = 0;
-	sb->block_group_index = 0;
+	sb->reserved_blocks_count_hi = to_le32(0);
+	sb->min_extra_isize = to_le32(sizeof(struct ext4_inode) -
+		EXT4_GOOD_OLD_INODE_SIZE);
+	sb->want_extra_isize = to_le32(sizeof(struct ext4_inode) -
+		EXT4_GOOD_OLD_INODE_SIZE);
+	sb->flags = to_le32(2);
 }
 
 static void fill_bgroups(struct fs_aux_info *aux_info,
@@ -290,6 +269,9 @@
 {
 	uint32_t i;
 
+	uint64_t bg_free_blk = 0;
+	uint64_t sb_free_blk = 0;
+
 	for (i = 0; i < aux_info->groups; i++) {
 
 		uint64_t bg_start_block = aux_info->first_data_block +
@@ -296,10 +278,15 @@
 			aux_info->first_data_block + i * info->blocks_per_group;
 		uint32_t blk_off = 0;
 
+		bg_free_blk = info->blocks_per_group -
+			(aux_info->inode_table_blocks + aux_info->bg_desc_blocks);
+
+		blk_off += aux_info->bg_desc_blocks;
+
 		if (has_superblock(info, i)) {
 			bg_start_block++;
-			blk_off = info->bg_desc_reserve_blocks +
-				  aux_info->bg_desc_blocks ;
+			blk_off += info->bg_desc_reserve_blocks;
+			bg_free_blk -= info->bg_desc_reserve_blocks + 1;
 		}
 
 		ext4_bg_set_block_bitmap(&aux_info->bg_desc[i], aux_info->sb,
@@ -313,7 +300,7 @@
 				bg_start_block + blk_off + 3);
 
 		ext4_bg_set_free_blocks_count(&aux_info->bg_desc[i],
-				aux_info->sb, aux_info->sb->blocks_per_group);
+				aux_info->sb, bg_free_blk);
 
 		ext4_bg_set_free_inodes_count(&aux_info->bg_desc[i],
 				aux_info->sb, aux_info->sb->inodes_per_group);
@@ -320,8 +307,11 @@
 
 		ext4_bg_set_used_dirs_count(&aux_info->bg_desc[i], aux_info->sb,
 					    0);
+
+		sb_free_blk += bg_free_blk;
 	}
 
+	ext4_sb_set_free_blocks_cnt(aux_info->sb, sb_free_blk);
 }
 
 
@@ -414,7 +404,7 @@
 	/* write out the primary superblock */
 	aux_info->sb->block_group_index = 0;
 	return ext4_block_writebytes(bd, 1024, aux_info->sb,
-				     EXT4_SUPERBLOCK_SIZE);
+			EXT4_SUPERBLOCK_SIZE);
 }
 
 
@@ -475,23 +465,22 @@
 {
 	int r = EOK;
 	int i;
-	struct ext4_inode_ref inode_ref;
 
-	int filetype = EXT4_DIRENTRY_REG_FILE;
 	for (i = 1; i < 12; ++i) {
+
+		bool dir = false;
+
 		switch (i) {
 		case EXT4_ROOT_INO:
-		case EXT4_UNDEL_DIR_INO:
 		case EXT4_GOOD_OLD_FIRST_INO:
-			 filetype = EXT4_DIRENTRY_DIR;
+			dir = true;
 			break;
 		}
 
-		r = ext4_fs_alloc_inode(fs, &inode_ref, filetype);
+		uint32_t index = 0;
+		r = ext4_ialloc_alloc_inode(fs, &index, dir);
 		if (r != EOK)
 			return r;
-
-		ext4_fs_put_inode_ref(&inode_ref);
 	}
 
 	return r;
@@ -532,7 +521,7 @@
 
 	info->inodes_per_group = compute_inodes_per_group(info);
 
-	info->feat_compat = EXT2_SUPPORTED_FCOM | EXT4_FCOM_RESIZE_INODE;
+	info->feat_compat = EXT2_SUPPORTED_FCOM;
 	info->feat_ro_compat = EXT2_SUPPORTED_FRO_COM;
 	info->feat_incompat = EXT2_SUPPORTED_FINCOM;
 
@@ -539,7 +528,7 @@
 	if (info->no_journal == 0)
 		info->feat_compat |= 0;
 
-	info->bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks(info);
+	info->bg_desc_reserve_blocks = 0;
 
 	ext4_dbg(DEBUG_MKFS, DBG_INFO "Creating filesystem with parameters:\n");
 	ext4_dbg(DEBUG_MKFS, DBG_NONE "Size: %"PRIu64"\n", info->len);