]> git.hungrycats.org Git - linux/commitdiff
btrfs: make chunk size validation to be simpler
authorQu Wenruo <wqu@suse.com>
Sun, 18 Oct 2026 04:17:02 +0000 (14:47 +1030)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:17 +0000 (21:36 +0100)
Commit 6ded22c1bfe6 ("btrfs: reduce div64 calls by limiting the number
of stripes of a chunk to u32") introduced a chunk size limit check, but
it is using a wrong result in the comment:

  U32_MAX << BTRFS_STRIPE_LEN_SHIFT results 256T, not 256G.

Instead of calculating the size limit, using an immediate number
(SZ_256G).

This has the following benefit:

- Still large enough for future expansion
  Although the check is 1024 times more strict, I don't think we will
  allow 256G chunk anytime soon.

  And we have higher chance (10 more bits) to catch a bitflip in the
  high bits.

- Much harder to diverge from the comment
  We don't need to calculate the number anymore.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/tree-checker.c

index 9cd79d97b9b5e71544db0998b87d3359d816f74e..503b6be90ddadbc64f3d0be1118fc3cb03613943 100644 (file)
@@ -1051,17 +1051,19 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
                return -EUCLEAN;
        }
        /*
-        * We artificially limit the chunk size, so that the number of stripes
-        * inside a chunk can be fit into a U32.  The current limit (256G) is
-        * way too large for real world usage anyway, and it's also much larger
-        * than our existing limit (10G).
+        * We artificially limit the chunk size, the current limit is 10G.
         *
-        * Thus it should be a good way to catch obvious bitflips.
+        * But we don't want to follow that 10G limit too strict, as if we
+        * relax the requirement one day, we will need a dedicated flag to
+        * prevent rejecting such new chunks.
+        *
+        * Here use 256G as a sane threshold, which should be large enough
+        * and also a good way to catch obvious bitflips.
         */
-       if (unlikely(length >= btrfs_stripe_nr_to_offset(U32_MAX))) {
+       if (unlikely(length > SZ_256G)) {
                chunk_err(fs_info, leaf, chunk, logical,
                          "chunk length too large: have %llu limit %llu",
-                         length, btrfs_stripe_nr_to_offset(U32_MAX));
+                         length, SZ_256G);
                return -EUCLEAN;
        }
        if (unlikely(type & ~BTRFS_BLOCK_GROUP_VALID)) {