]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
btrfs: tree-checker: introduce checks for FREE_SPACE_INFO
authorQu Wenruo <wqu@suse.com>
Mon, 9 Mar 2026 22:19:25 +0000 (08:49 +1030)
committerDavid Sterba <dsterba@suse.com>
Tue, 7 Apr 2026 16:56:01 +0000 (18:56 +0200)
Introduce checks for FREE_SPACE_INFO item, which include:

- Key alignment check
  The objectid is the logical bytenr of the chunk/bg, and offset is the
  length of the chunk/bg, thus they should all be aligned to the fs
  block size.

- Item size check
  The FREE_SPACE_INFO should a fix size.

- Flags check
  The flags member should have no other flags than
  BTRFS_FREE_SPACE_USING_BITMAPS.

  For future expansion, introduce a new macro
  BTRFS_FREE_SPACE_FLAGS_MASK for such checks.

  And since we're here, the BTRFS_FREE_SPACE_USING_BITMAPS should not
  use unsigned long long, as the flags is only 32 bits wide.
  So fix that to use unsigned long.

- Extent count check
  That member shows how many free space bitmap/extent items there are
  inside the chunk/bg.

  We know the chunk size (from key->offset), thus there should be at
  most (key->offset >> sectorsize_bits) blocks inside the chunk.
  Use that value as the upper limit and if that counter is larger than
  that, there is a high chance it's a bitflip in high bits.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/tree-checker.c
include/uapi/linux/btrfs_tree.h

index b4e114efff45629a17ae229a3c25b5f84bec8f7a..c4826b0484e6aa50d6b3e1852a683a43fe3f4e9e 100644 (file)
@@ -1945,6 +1945,53 @@ static int check_dev_extent_item(const struct extent_buffer *leaf,
        return 0;
 }
 
+static int check_free_space_info(struct extent_buffer *leaf, struct btrfs_key *key,
+                                int slot)
+{
+       struct btrfs_fs_info *fs_info = leaf->fs_info;
+       struct btrfs_free_space_info *fsi;
+       const u32 blocksize = fs_info->sectorsize;
+       u32 flags;
+
+       if (unlikely(!IS_ALIGNED(key->objectid, blocksize))) {
+               generic_err(leaf, slot,
+               "free space info key objectid is not aligned to %u, has " BTRFS_KEY_FMT,
+                           blocksize, BTRFS_KEY_FMT_VALUE(key));
+               return -EUCLEAN;
+       }
+       if (unlikely(!IS_ALIGNED(key->offset, blocksize))) {
+               generic_err(leaf, slot,
+               "free space info key offset is not aligned to %u, has " BTRFS_KEY_FMT,
+                           blocksize, BTRFS_KEY_FMT_VALUE(key));
+               return -EUCLEAN;
+       }
+       if (unlikely(btrfs_item_size(leaf, slot) !=
+                    sizeof(struct btrfs_free_space_info))) {
+               generic_err(leaf, slot,
+               "invalid item size for free space info, has %u expect %zu",
+                           btrfs_item_size(leaf, slot),
+                           sizeof(struct btrfs_free_space_info));
+               return -EUCLEAN;
+       }
+       fsi = btrfs_item_ptr(leaf, slot, struct btrfs_free_space_info);
+       flags = btrfs_free_space_flags(leaf, fsi);
+       if (unlikely(flags & ~BTRFS_FREE_SPACE_FLAGS_MASK)) {
+               generic_err(leaf, slot,
+               "unknown flags for free space info, has 0x%x valid mask 0x%lx",
+                           flags, BTRFS_FREE_SPACE_FLAGS_MASK);
+               return -EUCLEAN;
+       }
+       if (unlikely(btrfs_free_space_extent_count(leaf, fsi) >
+                    key->offset >> fs_info->sectorsize_bits)) {
+               generic_err(leaf, slot,
+                           "suspicious extent count, has %u max valid %llu",
+                           btrfs_free_space_extent_count(leaf, fsi),
+                           key->offset >> fs_info->sectorsize_bits);
+               return -EUCLEAN;
+       }
+       return 0;
+}
+
 /*
  * Common point to switch the item-specific validation.
  */
@@ -2008,6 +2055,9 @@ static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf,
        case BTRFS_RAID_STRIPE_KEY:
                ret = check_raid_stripe_extent(leaf, key, slot);
                break;
+       case BTRFS_FREE_SPACE_INFO_KEY:
+               ret = check_free_space_info(leaf, key, slot);
+               break;
        }
 
        if (unlikely(ret))
index f7843e6bb978dcc93972de57f5140e2965d6f719..cc3b9f7dccafa28eec9b29f2b90ada75464a0df6 100644 (file)
@@ -1245,7 +1245,8 @@ struct btrfs_free_space_info {
        __le32 flags;
 } __attribute__ ((__packed__));
 
-#define BTRFS_FREE_SPACE_USING_BITMAPS (1ULL << 0)
+#define BTRFS_FREE_SPACE_USING_BITMAPS (1UL << 0)
+#define BTRFS_FREE_SPACE_FLAGS_MASK    (BTRFS_FREE_SPACE_USING_BITMAPS)
 
 #define BTRFS_QGROUP_LEVEL_SHIFT               48
 static inline __u16 btrfs_qgroup_level(__u64 qgroupid)