Skip to contentSkip to conversation

btrfs: cleanups to the sys chunk array checks

PATCH In review [email protected] 2 participants 4 messages Feb 3 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: cleanups to the sys chunk array checks

From: Filipe Manana <[email protected]>

Details in the changelogs.

Filipe Manana (2):
btrfs: remove duplicate system chunk array max size overflow check
btrfs: move min sys chunk array size check to validate_sys_chunk_array()

 fs/btrfs/disk-io.c | 28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/2 btrfs: remove duplicate system chunk array max size overflow check

From: Filipe Manana <[email protected]>

We check it twice, once in validate_sys_chunk_array() and then again in its caller, btrfs_validate_super(), right after it calls validate_sys_chunk_array(). So remove the duplicated check from btrfs_validate_super().

---
 fs/btrfs/disk-io.c | 6 ------
 1 file changed, 6 deletions(-)
fs/btrfs/disk-io.c +0 -6
index 20c405a4789d..540ec255b7a4 100644
@@ -2534,12 +2534,6 @@ int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
 	 * Obvious sys_chunk_array corruptions, it must hold at least one key
 	 * and one chunk
 	 */
-	if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
-		btrfs_err(fs_info, "system chunk array too big %u > %u",
-			  btrfs_super_sys_array_size(sb),
-			  BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
-		ret = -EINVAL;
-	}
 	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",
Signature · 1 line
2.47.2
#2 lore raw

PATCH 2/2 btrfs: move min sys chunk array size check to validate_sys_chunk_array()

In reply to [email protected]

From: Filipe Manana <[email protected]>

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.

---
 fs/btrfs/disk-io.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
fs/btrfs/disk-io.c +9 -13
index 540ec255b7a4..069f8017d425 100644
@@ -2288,6 +2288,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;
@@ -2530,19 +2539,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.
Signature · 1 line
2.47.2
Reviewed-by on the v1 series
#3 lore raw
In reply to [email protected]
Quoted 3 lines · 在 2026/2/4 02:09, [email protected] 写道:

From: Filipe Manana <[email protected]>

Details in the changelogs.

Thanks,
Qu

Quoted 8 lines

Filipe Manana (2):
btrfs: remove duplicate system chunk array max size overflow check
btrfs: move min sys chunk array size check to validate_sys_chunk_array()

  fs/btrfs/disk-io.c | 28 +++++++++-------------------
  1 file changed, 9 insertions(+), 19 deletions(-)