[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230421093502.n4x3c773udaapksl@quack3>
Date: Fri, 21 Apr 2023 11:35:02 +0200
From: Jan Kara <jack@...e.cz>
To: Chao Yu <chao@...nel.org>
Cc: Theodore Ts'o <tytso@....edu>,
Andreas Dilger <adilger.kernel@...ger.ca>,
linux-ext4@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] ext4: fix wrong calculation of minlen in ext4_trim_fs()
On Thu 06-04-23 23:06:21, Chao Yu wrote:
> 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.
>
> Link: https://lore.kernel.org/lkml/20230311031843.GF860405@mit.edu/
> Suggested-by: Theodore Ts'o <tytso@....edu>
> Signed-off-by: Chao Yu <chao@...nel.org>
Looks good to me. Feel free to add:
Reviewed-by: Jan Kara <jack@...e.cz>
Honza
> ---
> fs/ext4/mballoc.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> index 5b2ae37a8b80..d8b9d6a83d1e 100644
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
> @@ -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 ||
> --
> 2.25.1
>
--
Jan Kara <jack@...e.com>
SUSE Labs, CR
Powered by blists - more mailing lists