lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230925060801.1397581-1-dongyangli@ddn.com>
Date:   Mon, 25 Sep 2023 16:08:00 +1000
From:   Li Dongyang <dongyangli@....com>
To:     linux-ext4@...r.kernel.org
Cc:     adilger@...ger.ca
Subject: [PATCH 1/2] mke2fs: set free blocks accurately for groups has GDT

This patch is part of the preparation required to allow
GDT blocks expand beyond a single group,
it introduces 2 new interfaces:
- ext2fs_count_used_blocks(), to return the blocks used
in the bitmap range.
- ext2fs_reserve_super_and_bgd2() to return blocks used by
superblock/GDT blocks for every group, by looking up blocks used.

Signed-off-by: Li Dongyang <dongyangli@....com>
---
 lib/ext2fs/alloc_sb.c     | 28 ++++++++++++++++++++++++++--
 lib/ext2fs/ext2fs.h       |  6 ++++++
 lib/ext2fs/gen_bitmap64.c | 17 +++++++++++++++--
 lib/ext2fs/initialize.c   | 30 ++++++++++++++++++++----------
 misc/mke2fs.c             |  2 +-
 5 files changed, 68 insertions(+), 15 deletions(-)

diff --git a/lib/ext2fs/alloc_sb.c b/lib/ext2fs/alloc_sb.c
index 8530b40f6..e92739ecc 100644
--- a/lib/ext2fs/alloc_sb.c
+++ b/lib/ext2fs/alloc_sb.c
@@ -46,8 +46,7 @@ int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
 				 ext2fs_block_bitmap bmap)
 {
 	blk64_t	super_blk, old_desc_blk, new_desc_blk;
-	blk_t	used_blks;
-	int	old_desc_blocks, num_blocks;
+	blk_t	used_blks, old_desc_blocks, num_blocks;
 
 	ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
 				  &old_desc_blk, &new_desc_blk, &used_blks);
@@ -79,3 +78,28 @@ int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
 
 	return num_blocks  ;
 }
+
+/*
+ * This function reserves the superblock and block group descriptors
+ * for a given block group and returns the number of blocks used by the
+ * super block and group descriptors by looking up the block bitmap.
+ */
+errcode_t ext2fs_reserve_super_and_bgd2(ext2_filsys fs,
+				        dgrp_t group,
+				        ext2fs_block_bitmap bmap,
+				        blk_t *desc_blocks)
+{
+	blk64_t	num_blocks;
+	errcode_t retval = 0;
+
+	ext2fs_reserve_super_and_bgd(fs, group, bmap);
+
+	retval = ext2fs_count_used_blocks(fs,
+					ext2fs_group_first_block2(fs, group),
+					ext2fs_group_last_block2(fs, group),
+					&num_blocks);
+	if (!retval)
+		*desc_blocks = num_blocks;
+
+	return retval;
+}
diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index 72c60d2b5..ae79a3443 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -795,6 +795,10 @@ errcode_t ext2fs_alloc_range(ext2_filsys fs, int flags, blk64_t goal,
 extern int ext2fs_reserve_super_and_bgd(ext2_filsys fs,
 					dgrp_t group,
 					ext2fs_block_bitmap bmap);
+extern errcode_t ext2fs_reserve_super_and_bgd2(ext2_filsys fs,
+					       dgrp_t group,
+					       ext2fs_block_bitmap bmap,
+					       blk_t *desc_blocks);
 extern void ext2fs_set_block_alloc_stats_callback(ext2_filsys fs,
 						  void (*func)(ext2_filsys fs,
 							       blk64_t blk,
@@ -1483,6 +1487,8 @@ errcode_t ext2fs_convert_subcluster_bitmap(ext2_filsys fs,
 					   ext2fs_block_bitmap *bitmap);
 errcode_t ext2fs_count_used_clusters(ext2_filsys fs, blk64_t start,
 				     blk64_t end, blk64_t *out);
+errcode_t ext2fs_count_used_blocks(ext2_filsys fs, blk64_t start,
+				   blk64_t end, blk64_t *out);
 
 /* get_num_dirs.c */
 extern errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs);
diff --git a/lib/ext2fs/gen_bitmap64.c b/lib/ext2fs/gen_bitmap64.c
index 4289e8155..5936dcf53 100644
--- a/lib/ext2fs/gen_bitmap64.c
+++ b/lib/ext2fs/gen_bitmap64.c
@@ -945,8 +945,8 @@ errcode_t ext2fs_find_first_set_generic_bmap(ext2fs_generic_bitmap bitmap,
 	return ENOENT;
 }
 
-errcode_t ext2fs_count_used_clusters(ext2_filsys fs, blk64_t start,
-				     blk64_t end, blk64_t *out)
+errcode_t ext2fs_count_used_blocks(ext2_filsys fs, blk64_t start,
+				   blk64_t end, blk64_t *out)
 {
 	blk64_t		next;
 	blk64_t		tot_set = 0;
@@ -975,6 +975,19 @@ errcode_t ext2fs_count_used_clusters(ext2_filsys fs, blk64_t start,
 			break;
 	}
 
+	if (!retval)
+		*out = tot_set;
+	return retval;
+}
+
+errcode_t ext2fs_count_used_clusters(ext2_filsys fs, blk64_t start,
+				     blk64_t end, blk64_t *out)
+{
+	blk64_t		tot_set = 0;
+	errcode_t	retval = 0;
+
+	retval = ext2fs_count_used_blocks(fs, start, end, &tot_set);
+
 	if (!retval)
 		*out = EXT2FS_NUM_B2C(fs, tot_set);
 	return retval;
diff --git a/lib/ext2fs/initialize.c b/lib/ext2fs/initialize.c
index edd692bb9..90012f732 100644
--- a/lib/ext2fs/initialize.c
+++ b/lib/ext2fs/initialize.c
@@ -521,6 +521,15 @@ ipg_retry:
 	csum_flag = ext2fs_has_group_desc_csum(fs);
 	reserved_inos = super->s_first_ino;
 	for (i = 0; i < fs->group_desc_count; i++) {
+		blk_t grp_free_blocks;
+		ext2_ino_t inodes;
+
+		retval = ext2fs_reserve_super_and_bgd2(fs, i,
+						       fs->block_map,
+						       &numblocks);
+		if (retval)
+			goto cleanup;
+
 		/*
 		 * Don't set the BLOCK_UNINIT group for the last group
 		 * because the block bitmap needs to be padded.
@@ -530,24 +539,25 @@ ipg_retry:
 				ext2fs_bg_flags_set(fs, i,
 						    EXT2_BG_BLOCK_UNINIT);
 			ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
-			numblocks = super->s_inodes_per_group;
+			inodes = super->s_inodes_per_group;
 			if (reserved_inos) {
-				if (numblocks > reserved_inos) {
-					numblocks -= reserved_inos;
+				if (inodes > reserved_inos) {
+					inodes -= reserved_inos;
 					reserved_inos = 0;
 				} else {
-					reserved_inos -= numblocks;
-					numblocks = 0;
+					reserved_inos -= inodes;
+					inodes = 0;
 				}
 			}
-			ext2fs_bg_itable_unused_set(fs, i, numblocks);
+			ext2fs_bg_itable_unused_set(fs, i, inodes);
 		}
-		numblocks = ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
-		if (fs->super->s_log_groups_per_flex)
+
+		if (!fs->super->s_log_groups_per_flex)
 			numblocks += 2 + fs->inode_blocks_per_group;
 
-		free_blocks += numblocks;
-		ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
+		grp_free_blocks = ext2fs_group_blocks_count(fs, i) - numblocks;
+		free_blocks += grp_free_blocks;
+		ext2fs_bg_free_blocks_count_set(fs, i, grp_free_blocks);
 		ext2fs_bg_free_inodes_count_set(fs, i, fs->super->s_inodes_per_group);
 		ext2fs_bg_used_dirs_count_set(fs, i, 0);
 		ext2fs_group_desc_csum_set(fs, i);
diff --git a/misc/mke2fs.c b/misc/mke2fs.c
index 4a9c1b092..72c9da12e 100644
--- a/misc/mke2fs.c
+++ b/misc/mke2fs.c
@@ -3522,7 +3522,7 @@ no_journal:
 			       fs->super->s_mmp_update_interval);
 	}
 
-	overhead += fs->super->s_first_data_block;
+	overhead += EXT2FS_NUM_B2C(fs, fs->super->s_first_data_block);
 	if (!super_only)
 		fs->super->s_overhead_clusters = overhead;
 
-- 
2.41.0

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ