]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ext4: try all groups in ext4_mb_new_blocks_simple
authorKemeng Shi <shikemeng@huaweicloud.com>
Sat, 3 Jun 2023 15:03:15 +0000 (23:03 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 16 Jun 2024 11:39:32 +0000 (13:39 +0200)
[ Upstream commit 19a043bb1fd1b5cb2652ca33536c55e6c0a70df0 ]

ext4_mb_new_blocks_simple ignores the group before goal, so it will fail
if free blocks reside in group before goal. Try all groups to avoid
unexpected failure.
Search finishes either if any free block is found or if no available
blocks are found. Simpliy check "i >= max" to distinguish the above
cases.

Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Suggested-by: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Link: https://lore.kernel.org/r/20230603150327.3596033-8-shikemeng@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Stable-dep-of: 3f4830abd236 ("ext4: fix potential unnitialized variable")
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/ext4/mballoc.c

index 383703e20ea36b70c28ce1d60f656b9722aeac60..a346ab8f3e5f4a990b0f430283e9d67b84b2cea0 100644 (file)
@@ -5887,7 +5887,7 @@ static ext4_fsblk_t ext4_mb_new_blocks_simple(handle_t *handle,
        struct buffer_head *bitmap_bh;
        struct super_block *sb = ar->inode->i_sb;
        struct ext4_sb_info *sbi = EXT4_SB(sb);
-       ext4_group_t group;
+       ext4_group_t group, nr;
        ext4_grpblk_t blkoff;
        ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
        ext4_grpblk_t i = 0;
@@ -5901,7 +5901,7 @@ static ext4_fsblk_t ext4_mb_new_blocks_simple(handle_t *handle,
 
        ar->len = 0;
        ext4_get_group_no_and_offset(sb, goal, &group, &blkoff);
-       for (; group < ext4_get_groups_count(sb); group++) {
+       for (nr = ext4_get_groups_count(sb); nr > 0; nr--) {
                bitmap_bh = ext4_read_block_bitmap(sb, group);
                if (IS_ERR(bitmap_bh)) {
                        *errp = PTR_ERR(bitmap_bh);
@@ -5925,10 +5925,13 @@ static ext4_fsblk_t ext4_mb_new_blocks_simple(handle_t *handle,
                if (i < max)
                        break;
 
+               if (++group >= ext4_get_groups_count(sb))
+                       group = 0;
+
                blkoff = 0;
        }
 
-       if (group >= ext4_get_groups_count(sb) || i >= max) {
+       if (i >= max) {
                *errp = -ENOSPC;
                return 0;
        }