Skip to contentSkip to conversation

ext2: correct max file size computing

PATCH In review Zhang Yi 2 participants 2 messages Feb 12, 2022 – Feb 25, 2022 ext2 file system

lore
Share & tools
lore

Zhang Yi

author
#0 lore raw

PATCH 1/1 ext2: correct max file size computing

We need to calculate the max file size accurately if the total blocks that can address by block tree exceed the upper_limit. But this check is not correct now, it only compute the total data blocks but missing metadata blocks are needed. So in the case of "data blocks < upper_limit && total blocks > upper_limit", we will get wrong result. Fortunately, this case could not happen in reality, but it's confused and better to correct the computing.

bits data blocks metadatablocks upper_limit
10 16843020 66051 2147483647
11 134480396 263171 1073741823

  12      1074791436          1050627     536870911 (*)
  13      8594130956          4198403     268435455 (*)
  14     68736258060         16785411     134217727 (*)

15 549822930956 67125251 67108863 (*)
16 4398314962956 268468227 33554431 (*)

[*] Need to calculate in depth.

Fixes 1c2d14212b15 ("ext2: Fix underflow in ext2_max_size()")
---
 fs/ext2/super.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/ext2/super.c +5 -1
index 94f1fbd7d3ac..6d4f5ef74766 100644
@@ -753,8 +753,12 @@ static loff_t ext2_max_size(int bits)
 	res += 1LL << (bits-2);
 	res += 1LL << (2*(bits-2));
 	res += 1LL << (3*(bits-2));
+	/* 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 < upper_limit)
+	if (res + meta_blocks <= upper_limit)
 		goto check_lfs;
 
 	res = upper_limit;
Signature · 1 line
2.31.1
#1 lore raw
Quoted 14 lines · Zhang Yi

We need to calculate the max file size accurately if the total blocks that can address by block tree exceed the upper_limit. But this check is not correct now, it only compute the total data blocks but missing metadata blocks are needed. So in the case of "data blocks < upper_limit && total blocks > upper_limit", we will get wrong result. Fortunately, this case could not happen in reality, but it's confused and better to correct the computing.

bits data blocks metadatablocks upper_limit
10 16843020 66051 2147483647
11 134480396 263171 1073741823

  12      1074791436          1050627     536870911 (*)
  13      8594130956          4198403     268435455 (*)
  14     68736258060         16785411     134217727 (*)
Quoted 7 lines

15 549822930956 67125251 67108863 (*)
16 4398314962956 268468227 33554431 (*)

[*] Need to calculate in depth.

Fixes 1c2d14212b15 ("ext2: Fix underflow in ext2_max_size()")

Thanks for the cleanup! I've merged the patch to my tree.

Honza

Quoted 25 lines
---
 fs/ext2/super.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/ext2/super.c +5 -1
index 94f1fbd7d3ac..6d4f5ef74766 100644
@@ -753,8 +753,12 @@ static loff_t ext2_max_size(int bits)
 	res += 1LL << (bits-2);
 	res += 1LL << (2*(bits-2));
 	res += 1LL << (3*(bits-2));
+	/* 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 < upper_limit)
+	if (res + meta_blocks <= upper_limit)
 		goto check_lfs;
 
 	res = upper_limit;
Signature · 1 line
2.31.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR