]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-client.git/commitdiff
btrfs: tree-checker: introduce checks for FREE_SPACE_BITMAP
authorZhengYuan Huang <gality369@gmail.com>
Tue, 10 Mar 2026 21:56:10 +0000 (08:26 +1030)
committerDavid Sterba <dsterba@suse.com>
Tue, 7 Apr 2026 16:56:02 +0000 (18:56 +0200)
Introduce checks for FREE_SPACE_BITMAP item, which include:

- Key alignment check
  Same as FREE_SPACE_EXTENT, the objectid is the logical bytenr of the
  free space, and offset is the length of the free space, so both
  should be aligned to the fs block size.

- Non-zero range check
  A zero key->offset would describe an empty bitmap, which is invalid.

- Item size check
  The item must hold exactly DIV_ROUND_UP(key->offset >> sectorsize_bits,
  BITS_PER_BYTE) bytes.  A mismatch indicates a truncated or otherwise
  corrupt bitmap item; without this check, the bitmap loading path would
  walk past the end of the leaf and trigger a NULL dereference in
  assert_eb_folio_uptodate().

Signed-off-by: ZhengYuan Huang <gality369@gmail.com>
Reviewed-by: Qu Wenruo <wqu@suse.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

index f4a8f1c643ed53f438c59e6abae2977d0d9b8dfe..495d1dd61e66c2828b4bcf5dcd101594f8181b1b 100644 (file)
@@ -2018,6 +2018,46 @@ static int check_free_space_extent(struct extent_buffer *leaf, struct btrfs_key
        return 0;
 }
 
+static int check_free_space_bitmap(struct extent_buffer *leaf,
+                                  struct btrfs_key *key, int slot)
+{
+       struct btrfs_fs_info *fs_info = leaf->fs_info;
+       const u32 blocksize = fs_info->sectorsize;
+       u32 expected_item_size;
+
+       if (unlikely(!IS_ALIGNED(key->objectid, blocksize))) {
+               generic_err(leaf, slot,
+               "free space bitmap 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 bitmap key offset is not aligned to %u, has " BTRFS_KEY_FMT,
+                           blocksize, BTRFS_KEY_FMT_VALUE(key));
+               return -EUCLEAN;
+       }
+       if (unlikely(key->offset == 0)) {
+               generic_err(leaf, slot, "free space bitmap length is 0");
+               return -EUCLEAN;
+       }
+       /*
+        * The item must hold exactly the right number of bitmap bytes for the
+        * range described by key->offset.  A mismatch means the item was
+        * truncated or the key is corrupt; either way the bitmap data is not
+        * safe to access.
+        */
+       expected_item_size = DIV_ROUND_UP(key->offset >> fs_info->sectorsize_bits,
+                                         BITS_PER_BYTE);
+       if (unlikely(btrfs_item_size(leaf, slot) != expected_item_size)) {
+               generic_err(leaf, slot,
+                           "invalid item size for free space bitmap, has %u expect %u",
+                           btrfs_item_size(leaf, slot), expected_item_size);
+               return -EUCLEAN;
+       }
+       return 0;
+}
+
 /*
  * Common point to switch the item-specific validation.
  */
@@ -2087,6 +2127,9 @@ static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf,
        case BTRFS_FREE_SPACE_EXTENT_KEY:
                ret = check_free_space_extent(leaf, key, slot);
                break;
+       case BTRFS_FREE_SPACE_BITMAP_KEY:
+               ret = check_free_space_bitmap(leaf, key, slot);
+               break;
        }
 
        if (unlikely(ret))