Skip to contentSkip to conversation

ext4: fix wrong calculation of minlen in ext4_trim_fs()

PATCH In review Chao Yu 2 participants 3 messages Apr 6, 2023 – Jun 5, 2023 ext4 file system

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH 1/1 ext4: fix wrong calculation of minlen in ext4_trim_fs()

As Ted pointed out as below:

"
However the minlen variable in ext4_trim_fs is in units of *clusters*. And so it gets rounded up two places. The first time is when it is converted into units of a cluster:

	minlen = EXT4_NUM_B2C(EXT4_SB(sb),
			      range->minlen >> sb->s_blocksize_bits);

Oh, and by the way, that first conversion is not correct as currently
written in ext4_fs_trim(). It should be

	minlen = (range->minlen + EXT4_CLUSTER_SIZE(sb) - 1) >>
		(sb->s_blocksize_bits + EXT4_CLUSTER_BITS(sb));

"

The reason is if range->minlen is smaller than block size of ext4, original calculation "range->minlen >> sb->s_blocksize_bits" may return zero, but since EXT4_NUM_B2C() expects a non-zero in-parameter, so it needs to round up range->minlen to cluster size directly as above.

---
 fs/ext4/mballoc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +2 -2
index 5b2ae37a8b80..d8b9d6a83d1e 100644
@@ -6478,8 +6478,8 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 
 	start = range->start >> sb->s_blocksize_bits;
 	end = start + (range->len >> sb->s_blocksize_bits) - 1;
-	minlen = EXT4_NUM_B2C(EXT4_SB(sb),
-			      range->minlen >> sb->s_blocksize_bits);
+	minlen = (range->minlen + EXT4_CLUSTER_SIZE(sb) - 1) >>
+			(sb->s_blocksize_bits + EXT4_CLUSTER_BITS(sb));
 
 	if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
 	    start >= max_blks ||
Signature · 1 line
2.25.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Chao Yu

As Ted pointed out as below:

"
However the minlen variable in ext4_trim_fs is in units of *clusters*. And so it gets rounded up two places. The first time is when it is converted into units of a cluster:

	minlen = EXT4_NUM_B2C(EXT4_SB(sb),
			      range->minlen >> sb->s_blocksize_bits);

Oh, and by the way, that first conversion is not correct as currently
written in ext4_fs_trim(). It should be

	minlen = (range->minlen + EXT4_CLUSTER_SIZE(sb) - 1) >>
		(sb->s_blocksize_bits + EXT4_CLUSTER_BITS(sb));

"

Quoted 8 lines

The reason is if range->minlen is smaller than block size of ext4, original calculation "range->minlen >> sb->s_blocksize_bits" may return zero, but since EXT4_NUM_B2C() expects a non-zero in-parameter, so it needs to round up range->minlen to cluster size directly as above.

Looks good to me. Feel free to add:

Honza

Quoted 22 lines
---
 fs/ext4/mballoc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +2 -2
index 5b2ae37a8b80..d8b9d6a83d1e 100644
@@ -6478,8 +6478,8 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 
 	start = range->start >> sb->s_blocksize_bits;
 	end = start + (range->len >> sb->s_blocksize_bits) - 1;
-	minlen = EXT4_NUM_B2C(EXT4_SB(sb),
-			      range->minlen >> sb->s_blocksize_bits);
+	minlen = (range->minlen + EXT4_CLUSTER_SIZE(sb) - 1) >>
+			(sb->s_blocksize_bits + EXT4_CLUSTER_BITS(sb));
 
 	if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
 	    start >= max_blks ||
Signature · 1 line
2.25.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#2 lore raw

Ping,

Quoted 55 lines · Jan Kara
Quoted 17 lines · Chao Yu

As Ted pointed out as below:

"
However the minlen variable in ext4_trim_fs is in units of *clusters*. And so it gets rounded up two places. The first time is when it is converted into units of a cluster:

	minlen = EXT4_NUM_B2C(EXT4_SB(sb),
			      range->minlen >> sb->s_blocksize_bits);

Oh, and by the way, that first conversion is not correct as currently
written in ext4_fs_trim(). It should be

	minlen = (range->minlen + EXT4_CLUSTER_SIZE(sb) - 1) >>
		(sb->s_blocksize_bits + EXT4_CLUSTER_BITS(sb));

"

Quoted 8 lines

The reason is if range->minlen is smaller than block size of ext4, original calculation "range->minlen >> sb->s_blocksize_bits" may return zero, but since EXT4_NUM_B2C() expects a non-zero in-parameter, so it needs to round up range->minlen to cluster size directly as above.

Looks good to me. Feel free to add:

Honza

Quoted 22 lines
---
  fs/ext4/mballoc.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +2 -2
index 5b2ae37a8b80..d8b9d6a83d1e 100644
@@ -6478,8 +6478,8 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 
  	start = range->start >> sb->s_blocksize_bits;
  	end = start + (range->len >> sb->s_blocksize_bits) - 1;
-	minlen = EXT4_NUM_B2C(EXT4_SB(sb),
-			      range->minlen >> sb->s_blocksize_bits);
+	minlen = (range->minlen + EXT4_CLUSTER_SIZE(sb) - 1) >>
+			(sb->s_blocksize_bits + EXT4_CLUSTER_BITS(sb));
 
  	if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
  	    start >= max_blks ||
Signature · 1 line
2.25.1