From: Filipe Manana Date: Tue, 3 Feb 2026 15:22:54 +0000 (+0000) Subject: btrfs: move min sys chunk array size check to validate_sys_chunk_array() X-Git-Tag: v7.1-rc1~231^2~112 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6141abb7f18f381d879d98f6c200494f14e502a0;p=thirdparty%2Fkernel%2Flinux.git btrfs: move min sys chunk array size check to validate_sys_chunk_array() We check the minimum size of the sys chunk array in btrfs_validate_super() but we have a better place for that, the helper validate_sys_chunk_array() which we use for every other sys chunk array check. So move it there, also converting the return error from -EINVAL to -EUCLEAN, which is a better fit and also consistent with the other checks. Reviewed-by: Qu Wenruo Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba --- diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 1b5fa06813311..6c4a791838de2 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -2299,6 +2299,15 @@ static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info, return -EUCLEAN; } + /* It must hold at least one key and one chunk. */ + if (unlikely(sys_array_size < sizeof(struct btrfs_disk_key) + + sizeof(struct btrfs_chunk))) { + btrfs_err(fs_info, "system chunk array too small %u < %zu", + sys_array_size, + sizeof(struct btrfs_disk_key) + sizeof(struct btrfs_chunk)); + return -EUCLEAN; + } + while (cur < sys_array_size) { struct btrfs_disk_key *disk_key; struct btrfs_chunk *chunk; @@ -2541,19 +2550,6 @@ int btrfs_validate_super(const struct btrfs_fs_info *fs_info, ret = validate_sys_chunk_array(fs_info, sb); - /* - * Obvious sys_chunk_array corruptions, it must hold at least one key - * and one chunk - */ - if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key) - + sizeof(struct btrfs_chunk)) { - btrfs_err(fs_info, "system chunk array too small %u < %zu", - btrfs_super_sys_array_size(sb), - sizeof(struct btrfs_disk_key) - + sizeof(struct btrfs_chunk)); - ret = -EINVAL; - } - /* * The generation is a global counter, we'll trust it more than the others * but it's still possible that it's the one that's wrong.