[PATCH 3/3] ext4: Fix estimate extent index blocks in ext4_ext_index_trans_blocks()

Jan Kara <[email protected]> Wed, 5 Aug 2026 17:35:49 +0200
Newsgroups org.kernel.vger.linux-ext4
Message-ID <[email protected]>
The estimate of the number of impacted extent tree index blocks could be
one-too-low. If we modify say 2 extents, already two leaf index blocks
could be impacted, not just one the current estimate counts with. Fix
the estimate.

Signed-off-by: Jan Kara <[email protected]>
---
 fs/ext4/extents.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 44ab246a3176..713b2c098af5 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -2427,9 +2427,17 @@ int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
 	 */
 	if (extents <= 1)
 		index = (EXT4_MAX_EXTENT_DEPTH * 2) + extents;
-	else
-		index = (EXT4_MAX_EXTENT_DEPTH * 3) +
-			DIV_ROUND_UP(extents, ext4_ext_space_block(inode, 0));
+	else {
+		int ext_max = ext4_ext_space_block(inode, 0);
+
+		index = EXT4_MAX_EXTENT_DEPTH * 3;
+		/*
+		 * Modified extents need not start at the beginning of the
+		 * leaf. Already two extents may need two leaf block
+		 * modifications...
+		 */
+		index += DIV_ROUND_UP(extents + ext_max - 1, ext_max);
+	}
 
 	return index;
 }
-- 
2.51.0