ext4: improving calculation of 'fe_{len|start}' in mb_find_extent()
authorGou Hao <gouhao@uniontech.com>
Mon, 13 Nov 2023 08:26:17 +0000 (16:26 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 5 Jan 2024 04:35:55 +0000 (23:35 -0500)
After first execution of mb_find_order_for_block():

'fe_start' is the value of 'block' passed in mb_find_extent().

'fe_len' is the difference between the length of order-chunk and
remainder of the block divided by order-chunk.

And 'next' does not require initialization after above modifications.

Signed-off-by: Gou Hao <gouhao@uniontech.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20231113082617.11258-1-gouhao@uniontech.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/mballoc.c

index 847dc0fb15735a1500d2cec0fc370281cd0dc258..0c69f2f8cf8854164fea405897b9a25b6f25a0bf 100644 (file)
@@ -1957,8 +1957,7 @@ done:
 static int mb_find_extent(struct ext4_buddy *e4b, int block,
                                int needed, struct ext4_free_extent *ex)
 {
-       int next = block;
-       int max, order;
+       int max, order, next;
        void *buddy;
 
        assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
@@ -1976,16 +1975,12 @@ static int mb_find_extent(struct ext4_buddy *e4b, int block,
 
        /* find actual order */
        order = mb_find_order_for_block(e4b, block);
-       block = block >> order;
 
-       ex->fe_len = 1 << order;
-       ex->fe_start = block << order;
+       ex->fe_len = (1 << order) - (block & ((1 << order) - 1));
+       ex->fe_start = block;
        ex->fe_group = e4b->bd_group;
 
-       /* calc difference from given start */
-       next = next - ex->fe_start;
-       ex->fe_len -= next;
-       ex->fe_start += next;
+       block = block >> order;
 
        while (needed > ex->fe_len &&
               mb_find_buddy(e4b, order, &max)) {