Skip to contentSkip to conversation

ext4: Verify s_clusters_per_group even without bigalloc

PATCH Superseded Jan Kara 2 participants 3 messages Feb 13, 2024 – Feb 19, 2024 ext4 file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

PATCH 1/1 ext4: Verify s_clusters_per_group even without bigalloc

Currently we ignore s_clusters_per_group field in the on-disk superblock if bigalloc feature is not enabled. However e2fsprogs don't even open the filesystem is s_clusters_per_group is invalid. This results in an odd state where kernel happily works with the filesystem while even e2fsck refuses to touch it. Verify that s_clusters_per_group is valid even if bigalloc feature is not enabled to make things consistent. Due to current e2fsprogs behavior it is unlikely there are filesystems out in the wild (except for intentionally fuzzed ones) with invalid s_clusters_per_group counts.

---
 fs/ext4/super.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/ext4/super.c +9 -1
index 0f931d0c227d..522683075067 100644
@@ -4451,7 +4451,15 @@ static int ext4_handle_clustersize(struct super_block *sb)
 				 sbi->s_blocks_per_group);
 			return -EINVAL;
 		}
-		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
+		sbi->s_clusters_per_group =
+			le32_to_cpu(es->s_clusters_per_group);
+		if (sbi->s_blocks_per_group != sbi->s_clusters_per_group) {
+			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
+				 "clusters per group (%lu) inconsistent",
+				 sbi->s_blocks_per_group,
+				 sbi->s_clusters_per_group);
+			return -EINVAL;
+		}
 		sbi->s_cluster_bits = 0;
 	}
 	sbi->s_cluster_ratio = clustersize / sb->s_blocksize;
Signature · 1 line
2.35.3
reviewer
#1 lore raw

Hello!

Quoted 3 lines · Jan Kara

Currently we ignore s_clusters_per_group field in the on-disk superblock if bigalloc feature is not enabled. However e2fsprogs don't even open the filesystem is s_clusters_per_group is invalid. This results in an

                 ^^
                 if
Quoted 20 lines

odd state where kernel happily works with the filesystem while even e2fsck refuses to touch it. Verify that s_clusters_per_group is valid even if bigalloc feature is not enabled to make things consistent. Due to current e2fsprogs behavior it is unlikely there are filesystems out in the wild (except for intentionally fuzzed ones) with invalid s_clusters_per_group counts.

---
 fs/ext4/super.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/ext4/super.c +0 -0
index 0f931d0c227d..522683075067 100644
@@ -4451,7 +4451,15 @@ static int ext4_handle_clustersize(struct super_block *sb)
 				 sbi->s_blocks_per_group);
 			return -EINVAL;
 		}

fs/ext4/super.c static int ext4_handle_clustersize(struct super_block *sb) · line 4462

Quoted 10 lines
fs/ext4/super.c +9 -1
-		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
+		sbi->s_clusters_per_group =
+			le32_to_cpu(es->s_clusters_per_group);
+		if (sbi->s_blocks_per_group != sbi->s_clusters_per_group) {
+			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
+				 "clusters per group (%lu) inconsistent",
+				 sbi->s_blocks_per_group,
+				 sbi->s_clusters_per_group);
+			return -EINVAL;
+		}

This is almost the same with the code snippet in bigalloc branch, would it be better to factor them out and reuse this hunk in both branch, just like the check e2fsprogs does?

Thanks,
Yi.

Quoted 4 lines
fs/ext4/super.c +0 -0
 		sbi->s_cluster_bits = 0;
 	}
 	sbi->s_cluster_ratio = clustersize / sb->s_blocksize;
author reviewer
#2 lore raw
Quoted 34 lines · Zhang Yi

Hello!

Quoted 3 lines · Jan Kara

Currently we ignore s_clusters_per_group field in the on-disk superblock if bigalloc feature is not enabled. However e2fsprogs don't even open the filesystem is s_clusters_per_group is invalid. This results in an

                 ^^
                 if
Quoted 26 lines

odd state where kernel happily works with the filesystem while even e2fsck refuses to touch it. Verify that s_clusters_per_group is valid even if bigalloc feature is not enabled to make things consistent. Due to current e2fsprogs behavior it is unlikely there are filesystems out in the wild (except for intentionally fuzzed ones) with invalid s_clusters_per_group counts.

---
 fs/ext4/super.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/ext4/super.c +5 -1
index 0f931d0c227d..522683075067 100644
@@ -4451,7 +4451,15 @@ static int ext4_handle_clustersize(struct super_block *sb)
 				 sbi->s_blocks_per_group);
 			return -EINVAL;
 		}
-		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
+		sbi->s_clusters_per_group =
+			le32_to_cpu(es->s_clusters_per_group);
+		if (sbi->s_blocks_per_group != sbi->s_clusters_per_group) {
+			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
+				 "clusters per group (%lu) inconsistent",
Quoted 8 lines

fs/ext4/super.c static int ext4_handle_clustersize(struct super_block *sb) · line 4462

Quoted 4 lines
fs/ext4/super.c +4 -0
+				 sbi->s_blocks_per_group,
+				 sbi->s_clusters_per_group);
+			return -EINVAL;
+		}

This is almost the same with the code snippet in bigalloc branch, would it be better to factor them out and reuse this hunk in both branch, just like the check e2fsprogs does?

Yeah, good point. Let me send v2.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR