Hi, Please, consider for inclusion the patch attempting to shrink extent tree after successful extent merging. Thanks, Alex >From 72a8dda0cb490fdb7fbcd0be2c19dbb3e2973b23 Mon Sep 17 00:00:00 2001 From: Alex Zhuravlev <bzzz@xxxxxxxxxxxxx> Date: Thu, 25 Jan 2024 21:24:08 +0300 Subject: [PATCH 2/2] Shrink extent's tree if the next level is presented by a single node and all entries can fit the root. after successful extent merging a 1st level index can collapse enough to fit the root. Signed-off-by: Alex Zhuravlev <bzzz@xxxxxxxxxxxxx> --- fs/ext4/extents.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index a2de6b863df1..d7ad0cd92dfe 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -2025,10 +2025,31 @@ static int ext4_ext_merge_blocks(handle_t *handle, } } - /* - * TODO: given we've got two paths, it should be possible to - * collapse those two blocks into the root one in some cases - */ + /* move the next level into the root if possible */ + k = le16_to_cpu(path[1].p_hdr->eh_entries); + if (depth > 2 && le16_to_cpu(path[0].p_hdr->eh_entries == 1) && + path[1].p_hdr == npath[1].p_hdr && + k <= le16_to_cpu(path[0].p_hdr->eh_max)) { + + next = ext4_idx_pblock(path[0].p_idx); + + err = ext4_ext_get_access(handle, inode, path + 0); + if (err) + return err; + memcpy(EXT_FIRST_INDEX(path[0].p_hdr), + EXT_FIRST_INDEX(path[1].p_hdr), + k * sizeof(struct ext4_extent_idx)); + path[0].p_hdr->eh_entries = cpu_to_le16(k); + le16_add_cpu(&path[0].p_hdr->eh_depth, -1); + err = ext4_ext_dirty(handle, inode, path + 0); + if (err) + return err; + + ext4_free_blocks(handle, inode, NULL, next, 1, + EXT4_FREE_BLOCKS_METADATA | + EXT4_FREE_BLOCKS_FORGET); + } + return 1; } -- 2.44.0