btrfs: add remap-tree checks to check_block_group_item()
PATCH 1/1 btrfs: add remap-tree checks to check_block_group_item()
Add some write-time checks for block group items relating to the remap tree.
Here we're checking:
* That the REMAPPED or METADATA_REMAP flags aren't set unless the REMAP_TREE incompat flag is also set
* That remap_bytes isn't more than the size of the block group
* That identity_remap_count isn't more than the number of sectors in the block group
--- fs/btrfs/tree-checker.c | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
fs/btrfs/tree-checker.c +42 -0
@@ -777,6 +777,48 @@ static int check_block_group_item(struct extent_buffer *leaf, BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);return -EUCLEAN ; } + +if (unlikely (!btrfs_fs_incompat (fs_info,REMAP_TREE ) && + type ==BTRFS_BLOCK_GROUP_METADATA_REMAP )) { +block_group_err (leaf, slot, +"invalid type, METADATA_REMAP set but REMAP_TREE incompat flag not set" ); +return -EUCLEAN ; + } + +if (unlikely (!btrfs_fs_incompat (fs_info,REMAP_TREE ) && + flags &BTRFS_BLOCK_GROUP_REMAPPED )) { +block_group_err (leaf, slot, +"invalid flags, REMAPPED set but REMAP_TREE incompat flag not set" ); +return -EUCLEAN ; + } + +if (item_size ==sizeof (struct btrfs_block_group_item_v2 )) { +struct btrfs_block_group_item_v2 *bgi2; +u64 remap_bytes; +u32 identity_remap_count; + + bgi2 = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item_v2); + + remap_bytes =btrfs_block_group_v2_remap_bytes (leaf, bgi2); + +if (unlikely (remap_bytes > key->offset)) { +block_group_err (leaf, slot, +"invalid remap_bytes, have %llu expect [0, %llu]" , + remap_bytes, key->offset); +return -EUCLEAN ; + } + + identity_remap_count =btrfs_block_group_v2_identity_remap_count (leaf, bgi2); +if (unlikely ((u64 )identity_remap_count > + key->offset / fs_info->sectorsize)) { +block_group_err (leaf, slot, +"invalid identity_remap_count, have %u expect [0, %llu]" , + identity_remap_count, + key->offset / fs_info->sectorsize); +return -EUCLEAN ; + } + } +return 0 ; }
Signature · 1 line
2.52.0
Quoted 47 lines · Mark Harmstone
Add some write-time checks for block group items relating to the remap tree.
Here we're checking:
* That the REMAPPED orMETADATA_REMAPflags aren't set unless theREMAP_TREEincompat flag is also set
* Thatremap_bytesisn't more than the size of the block group
* Thatidentity_remap_countisn't more than the number of sectors in the block groupSigned-off-by Mark Harmstone <[email protected]>--- fs/btrfs/tree-checker.c | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)fs/btrfs/tree-checker.c +24
-0@@ -777,6 +777,48 @@ static int check_block_group_item(struct extent_buffer *leaf, BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);return -EUCLEAN ; } + +if (unlikely (!btrfs_fs_incompat (fs_info,REMAP_TREE ) && + type ==BTRFS_BLOCK_GROUP_METADATA_REMAP )) { +block_group_err (leaf, slot, +"invalid type, METADATA_REMAP set but REMAP_TREE incompat flag not set" ); +return -EUCLEAN ; + } + +if (unlikely (!btrfs_fs_incompat (fs_info,REMAP_TREE ) && + flags &BTRFS_BLOCK_GROUP_REMAPPED )) { +block_group_err (leaf, slot, +"invalid flags, REMAPPED set but REMAP_TREE incompat flag not set" ); +return -EUCLEAN ; + } + +if (item_size ==sizeof (struct btrfs_block_group_item_v2 )) { +struct btrfs_block_group_item_v2 *bgi2; +u64 remap_bytes; +u32 identity_remap_count; + + bgi2 = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item_v2); + + remap_bytes =btrfs_block_group_v2_remap_bytes (leaf, bgi2); +
fs/btrfs/tree-checker.c static int check_block_group_item(struct extent_buffer *leaf, · line 813
Quoted 10 lines
fs/btrfs/tree-checker.c +10
-0+if (unlikely (remap_bytes > key->offset)) { +block_group_err (leaf, slot, +"invalid remap_bytes, have %llu expect [0, %llu]" , + remap_bytes, key->offset); +return -EUCLEAN ; + } + + identity_remap_count =btrfs_block_group_v2_identity_remap_count (leaf, bgi2); +if (unlikely ((u64 )identity_remap_count > + key->offset / fs_info->sectorsize)) {
64bit / 32bit may not work on all arches, we have the sectorize_bits to avoid it.
Quoted 14 lines
fs/btrfs/tree-checker.c +8
-0+block_group_err (leaf, slot, +"invalid identity_remap_count, have %u expect [0, %llu]" , + identity_remap_count, + key->offset / fs_info->sectorsize); +return -EUCLEAN ; + } + } +return 0 ; }Signature · 1 line
2.52.0
Quoted 53 lines · David Sterba
Quoted 52 lines · Mark Harmstone
Add some write-time checks for block group items relating to the remap tree.
Here we're checking:
* That the REMAPPED orMETADATA_REMAPflags aren't set unless theREMAP_TREEincompat flag is also set
* Thatremap_bytesisn't more than the size of the block group
* Thatidentity_remap_countisn't more than the number of sectors in the block groupSigned-off-by Mark Harmstone <[email protected]>--- fs/btrfs/tree-checker.c | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)fs/btrfs/tree-checker.c +29
-0@@ -777,6 +777,48 @@ static int check_block_group_item(struct extent_buffer *leaf, BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);return -EUCLEAN ; } + +if (unlikely (!btrfs_fs_incompat (fs_info,REMAP_TREE ) && + type ==BTRFS_BLOCK_GROUP_METADATA_REMAP )) { +block_group_err (leaf, slot, +"invalid type, METADATA_REMAP set but REMAP_TREE incompat flag not set" ); +return -EUCLEAN ; + } + +if (unlikely (!btrfs_fs_incompat (fs_info,REMAP_TREE ) && + flags &BTRFS_BLOCK_GROUP_REMAPPED )) { +block_group_err (leaf, slot, +"invalid flags, REMAPPED set but REMAP_TREE incompat flag not set" ); +return -EUCLEAN ; + } + +if (item_size ==sizeof (struct btrfs_block_group_item_v2 )) { +struct btrfs_block_group_item_v2 *bgi2; +u64 remap_bytes; +u32 identity_remap_count; + + bgi2 = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item_v2); + + remap_bytes =btrfs_block_group_v2_remap_bytes (leaf, bgi2); + +if (unlikely (remap_bytes > key->offset)) { +block_group_err (leaf, slot, +"invalid remap_bytes, have %llu expect [0, %llu]" , + remap_bytes, key->offset); +return -EUCLEAN ;
Quoted 8 lines
fs/btrfs/tree-checker.c static int check_block_group_item(struct extent_buffer *leaf, · line 813
Quoted 5 lines
fs/btrfs/tree-checker.c +5
-0+ } + + identity_remap_count =btrfs_block_group_v2_identity_remap_count (leaf, bgi2); +if (unlikely ((u64 )identity_remap_count > + key->offset / fs_info->sectorsize)) {64bit / 32bit may not work on all arches, we have the
sectorize_bitsto avoid it.
Annoying. Thank you, I've sent another patch.
Quoted 14 lines
Quoted 14 lines
fs/btrfs/tree-checker.c +8
-0+block_group_err (leaf, slot, +"invalid identity_remap_count, have %u expect [0, %llu]" , + identity_remap_count, + key->offset / fs_info->sectorsize); +return -EUCLEAN ; + } + } +return 0 ; }Signature · 1 line
2.52.0