diff mbox series

shrink extent tree

Message ID 20240125213117.60359293@x390.bzzz77.ru
State New
Headers show
Series shrink extent tree | expand

Commit Message

Alexey Zhuravlev Jan. 25, 2024, 6:31 p.m. UTC
Hi,

Please have a look at the patch attempting to shrink extents tree.

Thanks, Alex
---
 fs/ext5/extents.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 0fd9fbd65711..4cd2f0d57917 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;
 }