All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] ext4: fix underflow in ext4_max_bitmap_size()
@ 2022-03-01 11:17 Zhang Yi
  2022-03-01 11:36 ` Jan Kara
  2022-03-03 15:14 ` Theodore Ts'o
  0 siblings, 2 replies; 3+ messages in thread
From: Zhang Yi @ 2022-03-01 11:17 UTC (permalink / raw)
  To: linux-ext4; +Cc: tytso, adilger.kernel, jack, yi.zhang, yukuai3

when ext4 filesystem is created with 64k block size, ^extent and
^huge_file features. the upper_limit would underflow during the
computations in ext4_max_bitmap_size(). The problem is the size of block
index tree for such large block size is more than i_blocks can carry.
So fix the computation to count with this possibility. After this fix,
the 'res' cannot overflow loff_t on the extreme case of filesystem with
huge_files and 64K block size, so this patch also revert commit
75ca6ad408f4 ("ext4: fix loff_t overflow in ext4_max_bitmap_size()").

Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
---
v3->v2: rewrite change log and use ppb to compute 'res' blocks.
v2->v1: use DIV_ROUND_UP_ULL instead of DIV_ROUND_UP.

 fs/ext4/super.c | 46 +++++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index c5021ca0a28a..bfba62206a14 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -3468,8 +3468,9 @@ static loff_t ext4_max_size(int blkbits, int has_huge_files)
  */
 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
 {
-	unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
+	loff_t upper_limit, res = EXT4_NDIR_BLOCKS;
 	int meta_blocks;
+	unsigned int ppb = 1 << (bits - 2);
 
 	/*
 	 * This is calculated to be the largest file size for a dense, block
@@ -3501,27 +3502,42 @@ static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
 
 	}
 
+	/* Compute how many blocks we can address by block tree */
+	res += ppb;
+	res += ppb * ppb;
+	res += ((loff_t)ppb) * ppb * ppb;
+	/* Compute how many metadata blocks are needed */
+	meta_blocks = 1;
+	meta_blocks += 1 + ppb;
+	meta_blocks += 1 + ppb + ppb * ppb;
+	/* Does block tree limit file size? */
+	if (res + meta_blocks <= upper_limit)
+		goto check_lfs;
+
+	res = upper_limit;
+	/* How many metadata blocks are needed for addressing upper_limit? */
+	upper_limit -= EXT4_NDIR_BLOCKS;
 	/* indirect blocks */
 	meta_blocks = 1;
+	upper_limit -= ppb;
 	/* double indirect blocks */
-	meta_blocks += 1 + (1LL << (bits-2));
-	/* tripple indirect blocks */
-	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
-
-	upper_limit -= meta_blocks;
-	upper_limit <<= bits;
-
-	res += 1LL << (bits-2);
-	res += 1LL << (2*(bits-2));
-	res += 1LL << (3*(bits-2));
+	if (upper_limit < ppb * ppb) {
+		meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb);
+		res -= meta_blocks;
+		goto check_lfs;
+	}
+	meta_blocks += 1 + ppb;
+	upper_limit -= ppb * ppb;
+	/* tripple indirect blocks for the rest */
+	meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb) +
+		DIV_ROUND_UP_ULL(upper_limit, ppb*ppb);
+	res -= meta_blocks;
+check_lfs:
 	res <<= bits;
-	if (res > upper_limit)
-		res = upper_limit;
-
 	if (res > MAX_LFS_FILESIZE)
 		res = MAX_LFS_FILESIZE;
 
-	return (loff_t)res;
+	return res;
 }
 
 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] ext4: fix underflow in ext4_max_bitmap_size()
  2022-03-01 11:17 [PATCH v3] ext4: fix underflow in ext4_max_bitmap_size() Zhang Yi
@ 2022-03-01 11:36 ` Jan Kara
  2022-03-03 15:14 ` Theodore Ts'o
  1 sibling, 0 replies; 3+ messages in thread
From: Jan Kara @ 2022-03-01 11:36 UTC (permalink / raw)
  To: Zhang Yi; +Cc: linux-ext4, tytso, adilger.kernel, jack, yukuai3

On Tue 01-03-22 19:17:04, Zhang Yi wrote:
> when ext4 filesystem is created with 64k block size, ^extent and
> ^huge_file features. the upper_limit would underflow during the
> computations in ext4_max_bitmap_size(). The problem is the size of block
> index tree for such large block size is more than i_blocks can carry.
> So fix the computation to count with this possibility. After this fix,
> the 'res' cannot overflow loff_t on the extreme case of filesystem with
> huge_files and 64K block size, so this patch also revert commit
> 75ca6ad408f4 ("ext4: fix loff_t overflow in ext4_max_bitmap_size()").
> 
> Signed-off-by: Zhang Yi <yi.zhang@huawei.com>

Thanks! The patch looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
> v3->v2: rewrite change log and use ppb to compute 'res' blocks.
> v2->v1: use DIV_ROUND_UP_ULL instead of DIV_ROUND_UP.
> 
>  fs/ext4/super.c | 46 +++++++++++++++++++++++++++++++---------------
>  1 file changed, 31 insertions(+), 15 deletions(-)
> 
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index c5021ca0a28a..bfba62206a14 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -3468,8 +3468,9 @@ static loff_t ext4_max_size(int blkbits, int has_huge_files)
>   */
>  static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
>  {
> -	unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
> +	loff_t upper_limit, res = EXT4_NDIR_BLOCKS;
>  	int meta_blocks;
> +	unsigned int ppb = 1 << (bits - 2);
>  
>  	/*
>  	 * This is calculated to be the largest file size for a dense, block
> @@ -3501,27 +3502,42 @@ static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
>  
>  	}
>  
> +	/* Compute how many blocks we can address by block tree */
> +	res += ppb;
> +	res += ppb * ppb;
> +	res += ((loff_t)ppb) * ppb * ppb;
> +	/* Compute how many metadata blocks are needed */
> +	meta_blocks = 1;
> +	meta_blocks += 1 + ppb;
> +	meta_blocks += 1 + ppb + ppb * ppb;
> +	/* Does block tree limit file size? */
> +	if (res + meta_blocks <= upper_limit)
> +		goto check_lfs;
> +
> +	res = upper_limit;
> +	/* How many metadata blocks are needed for addressing upper_limit? */
> +	upper_limit -= EXT4_NDIR_BLOCKS;
>  	/* indirect blocks */
>  	meta_blocks = 1;
> +	upper_limit -= ppb;
>  	/* double indirect blocks */
> -	meta_blocks += 1 + (1LL << (bits-2));
> -	/* tripple indirect blocks */
> -	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
> -
> -	upper_limit -= meta_blocks;
> -	upper_limit <<= bits;
> -
> -	res += 1LL << (bits-2);
> -	res += 1LL << (2*(bits-2));
> -	res += 1LL << (3*(bits-2));
> +	if (upper_limit < ppb * ppb) {
> +		meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb);
> +		res -= meta_blocks;
> +		goto check_lfs;
> +	}
> +	meta_blocks += 1 + ppb;
> +	upper_limit -= ppb * ppb;
> +	/* tripple indirect blocks for the rest */
> +	meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb) +
> +		DIV_ROUND_UP_ULL(upper_limit, ppb*ppb);
> +	res -= meta_blocks;
> +check_lfs:
>  	res <<= bits;
> -	if (res > upper_limit)
> -		res = upper_limit;
> -
>  	if (res > MAX_LFS_FILESIZE)
>  		res = MAX_LFS_FILESIZE;
>  
> -	return (loff_t)res;
> +	return res;
>  }
>  
>  static ext4_fsblk_t descriptor_loc(struct super_block *sb,
> -- 
> 2.31.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] ext4: fix underflow in ext4_max_bitmap_size()
  2022-03-01 11:17 [PATCH v3] ext4: fix underflow in ext4_max_bitmap_size() Zhang Yi
  2022-03-01 11:36 ` Jan Kara
@ 2022-03-03 15:14 ` Theodore Ts'o
  1 sibling, 0 replies; 3+ messages in thread
From: Theodore Ts'o @ 2022-03-03 15:14 UTC (permalink / raw)
  To: linux-ext4, Zhang Yi; +Cc: Theodore Ts'o, adilger.kernel, jack, yukuai3

On Tue, 1 Mar 2022 19:17:04 +0800, Zhang Yi wrote:
> when ext4 filesystem is created with 64k block size, ^extent and
> ^huge_file features. the upper_limit would underflow during the
> computations in ext4_max_bitmap_size(). The problem is the size of block
> index tree for such large block size is more than i_blocks can carry.
> So fix the computation to count with this possibility. After this fix,
> the 'res' cannot overflow loff_t on the extreme case of filesystem with
> huge_files and 64K block size, so this patch also revert commit
> 75ca6ad408f4 ("ext4: fix loff_t overflow in ext4_max_bitmap_size()").
> 
> [...]

Applied, thanks!

[1/1] ext4: fix underflow in ext4_max_bitmap_size()
      commit: 5c93e8ecd5bd3bfdee013b6da0850357eb6ca4d8

Best regards,
-- 
Theodore Ts'o <tytso@mit.edu>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-03-03 15:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-01 11:17 [PATCH v3] ext4: fix underflow in ext4_max_bitmap_size() Zhang Yi
2022-03-01 11:36 ` Jan Kara
2022-03-03 15:14 ` Theodore Ts'o

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.