btrfs-progs: fix an old bug in lowmem mode
cover btrfs-progs: fix an old bug in lowmem mode
Inspired by Mark's recent enhanced super block dev item check against chunk tree device item, it turns out there are several existing images not passing the check.
One of the check is fsck/020/keyed_data_ref_with_reloc_leaf.img, which may be caused by some older kernels (the bug is already fixed a long time ago).
The first patch is to fix a lowmem mode check bug, that it doesn't correctly account the keyed backref with shared backref.
This is exposed by the image from the next patch.
Then the last patch is to update the image with a proper note on how to re-create it.
And the new image also acts as a regression test for the above lowmem bug.
Please note this is not the last fsck image which has such problem, fsck/057 is another one, and will be addressed in a dedicated patch.
Qu Wenruo (2):
btrfs-progs: check/lowmem: fix a false alert when counting the refs
btrfs-progs: fsck-tests: fix an image which has incorrect super dev
item
check/mode-lowmem.c | 145 +++++++++++++++++- .../keyed_data_ref_with_reloc_leaf.img | Bin 16384 -> 19456 bytes 2 files changed, 139 insertions(+), 6 deletions(-)
--
2.49.0
PATCH 2/2 btrfs-progs: fsck-tests: fix an image which has incorrect super dev item
[CI FAILURE]
With Mark's incoming fsck enhancement on super block device item, it exposed the following fs corruption on an existing image dump:
restoring image keyed_data_ref_with_reloc_leaf.img
====== RUN CHECK /home/runner/work/btrfs-progs/btrfs-progs/btrfs check ./keyed_data_ref_with_reloc_leaf.img.restored [1/8] checking log skipped (none written)
[2/8] checking root items
[3/8] checking extents
device 1's bytes_used was 55181312 in tree but 59375616 in superblock
ERROR: errors found in extent allocation tree or chunk allocation
[4/8] checking free space cache
[5/8] checking fs roots
[6/8] checking only csums items (without verifying data)
[7/8] checking root refs
[8/8] checking quota groups skipped (not enabled on this FS)
Opening filesystem to check...
Checking filesystem on ./keyed_data_ref_with_reloc_leaf.img.restored
UUID: ca3568a3-d9d8-4901-b4dd-b180a437a07f
found 1175552 bytes used, error(s) found
total csum bytes: 512
total tree bytes: 651264
total fs tree bytes: 606208
total extent tree bytes: 16384
btree space waste bytes: 291631
file data blocks allocated: 67633152
referenced 1048576
[CAUSE]
The image has the following device item in the super block:
dev_item.uuid 5a1c9f96-b581-4fd3-a73a-5cfd789c3c84
dev_item.fsid ca3568a3-d9d8-4901-b4dd-b180a437a07f [match]
dev_item.type 0
dev_item.total_bytes 67108864
dev_item.bytes_used 59375616
Meanwhile the tree dump shows the following device item in the chunk tree:
item 0 key (DEV_ITEMS DEV_ITEM 1) itemoff 3897 itemsize 98 devid 1 total_bytes 67108864 bytes_used 55181312 io_align 4096 io_width 4096 sector_size 4096 type 0
The bytes_used value does not match and triggered the fsck failure.
The root cause of such a mismatch is that, there was a bug in the kernel that resulted incorrect byte_used number.
The kernel bug is fixed by commit: ce7213c70c37 ("Btrfs: fix wrong device bytes_used in the super block").
However that fix doesn't have any fixes tag, thus not automatically backported to older stable kernels.
And the image seems to be created by an older stable kernel, thus missing the fix and caused the above mismatch.
[FIX]
Re-generate the image with the latest kernel so that the mismatch won't happen.
Furthermore for future updates, here is the needed steps to create the image.
- Modify the kernel btrfs module to commit transaction more frequently The idea is to replace the btrfs_end_transaction_throttle() call inside the while() loop of relocate_block_group().
So that every time a tree block is relocated, the fs will be committed.
- Use the following script to populate the fs with log-writes This requries CONFIG_DM_LOG_WRITES to be enabled.
```
dev_src="/dev/test/scratch1"
dev_log="/dev/test/log"
dev="/dev/mapper/log"
mnt="/mnt/btrfs/"
table="0 $(blockdev --getsz $dev_src) log-writes $dev_src $dev_log"
fail()
{
echo "!!! FAILED !!!"
exit 1
}
umount $dev &> /dev/null
umount $mnt &> /dev/null
dmsetup create log --table "$table" || fail
mkfs.btrfs -b 1G -n 4k -f -m single $dev
mount $dev $mnt
xfs_io -f -c "pwrite 0 512k" $mnt/src > /dev/null
sync
for ((i = 0; i < 128; i++)); do
xfs_io -f -c "reflink $mnt/src $(($i * 4096)) $(($i * 4096)) 4K" $mnt/file1 > /dev/null || fail
done
for ((i = 0; i < 128; i++)); do
xfs_io -f -c "pwrite 0 2k" $mnt/inline_$i > /dev/null || fail
done
sync
dmsetup message log 0 mark init
btrfs balance start -m $mnt
umount $mnt
dmsetup remove log
```- Replay the log and run "btrfs dump-tree -t extent" for each fua Use the log-writes tool (https://github.com/josefbacik/log-writes.git)
to replay the log.
And there will be one with the following contents for the data extent item, with mixed keyed and shared backref.
item 22 key (13631488 EXTENT_ITEM 524288) itemoff 3161 itemsize 108
refs 180 gen 9 flags DATA
(178 0xdfb591fbbf5f519) extent data backref root FS_TREE objectid 258 offset 0 count 77
(178 0xdfb591fa80d95ea) extent data backref root FS_TREE objectid 257 offset 0 count 1
(184 0x151e000) shared data backref parent 22142976 count 51
(184 0x512000) shared data backref parent 5316608 count 51- Take the image of that fs
This image has one extra benefit, that it exposed a bug in the older lowmem code where shared backref number is not correctly skipped, thus this image will also act as a regression test.
--- .../keyed_data_ref_with_reloc_leaf.img | Bin 16384 -> 19456 bytes 1 file changed, 0 insertions(+), 0 deletions(-)
tests/fsck-tests/020-extent-ref-cases/keyed_data_ref_with_reloc_leaf.img binary +0 -0
Signature · 1 line
2.49.0
PATCH 1/2 btrfs-progs: check/lowmem: fix a false alert when counting the refs
In reply to Qu Wenruo[BUG]
For a btrfs which is under metadata balance and interrupted (powerloss etc), we can have the following extent backref item for a data extent:
item 22 key (13631488 EXTENT_ITEM 524288) itemoff 3161 itemsize 108
refs 180 gen 9 flags DATA
(178 0xdfb591fbbf5f519) extent data backref root FS_TREE objectid 258 offset 0 count 77
(178 0xdfb591fa80d95ea) extent data backref root FS_TREE objectid 257 offset 0 count 1
(184 0x151e000) shared data backref parent 22142976 count 51
(184 0x512000) shared data backref parent 5316608 count 51Then lowmem mode will cause the following false alert:
[3/8] checking extents
ERROR: extent[13631488, 524288] referencer count mismatch (root: 5, owner: 258, offset: 0) wanted: 77, have: 128
ERROR: errors found in extent allocation tree or chunk allocation
[CAUSE]
When shared and keyed backref items are found, we must follow the following rules to avoid incorrect refs count:
- If the leaf belongs to the shared backref parent
Then any found EXTENT_DATA inside the leaf will be contributed to the shared backref values.
- Otherwise any found EXTENT_DATA can contributed to the keyed backref
In above case, if our leaf is 5316608 or 22142976, then we should not contribute the number of found EXTENT_DATA to the keyed backref.
Unfortunately the original fix d53d42fa2183 ("btrfs-progs: lowmem: fix false alerts of referencer count mismatch for blocks relocated") is not following the above strict rule, but relying on the flag of the leaf.
However that RELOC flag is not a correct indicator, e.g in above case the leaf at 5316608 is not yet being relocated, thus no RELOC flag.
[FIX]
Instead of check the unreliable RELOC flag, follow the correct rule when checking the leaf.
Before we start checking the content of a leaf for EXTENT_DATA items, make sure the leaf's bytenr is not in any shared backref item. If so skip to the next leaf.
--- check/mode-lowmem.c | 145 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 139 insertions(+), 6 deletions(-)
check/mode-lowmem.c +139 -6
@@ -3977,6 +3977,139 @@ out:return 0 ; } +/* +* A read-only version of lookup_inline_extent_backref(). +* We can not reuse that function as it always assume COW. +*/ +static int has_inline_shared_backref (u64 data_bytenr,u64 data_len,u64 parent) +{ +struct btrfs_root *extent_root =btrfs_extent_root (gfs_info, data_bytenr); +struct btrfs_extent_inline_ref *iref; +struct btrfs_extent_item *ei; +struct btrfs_path path = {0 }; +struct extent_buffer *leaf; +struct btrfs_key key; +unsigned long ptr; +unsigned long end; +bool found = false; +u32 item_size; +u64 flags; +int ret; + + key.objectid = data_bytenr; + key.type =BTRFS_EXTENT_ITEM_KEY ; + key.offset = data_len; + ret =btrfs_search_slot (NULL , extent_root, &key, &path,0 ,0 ); +if (ret >0 ) + ret = -ENOENT ; +if (ret <0 ) + goto out; + + leaf = path.nodes[0 ]; + item_size =btrfs_item_size (leaf, path.slots[0 ]); +if (item_size <sizeof (*ei)) { +error ("extent item size %u < %zu, leaf %llu slot %u" , + item_size,sizeof (*ei), leaf->start, path.slots[0 ]); + ret = -EUCLEAN ; + goto out; + } + ei = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_extent_item); + flags =btrfs_extent_flags (leaf, ei); + +if (!(flags &BTRFS_EXTENT_FLAG_DATA )) { +error ("backref item flag for bytenr %llu is not data" , + data_bytenr); + ret = -EUCLEAN ; + goto out; + } + + ptr = (unsigned long )(ei +1 ); + end = (unsigned long )ei + item_size; + +while (true) { +u64 ref_parent; +u8 type; + +if (ptr >= end) { +if (ptr > end) { +error ("inline extent item for %llu is not properly ended" , + data_bytenr); + ret = -EUCLEAN ; + goto out; + } +break ; + } + iref = (struct btrfs_extent_inline_ref *)ptr; + type =btrfs_extent_inline_ref_type (leaf, iref); +if (type !=BTRFS_SHARED_DATA_REF_KEY ) + goto next; + + ref_parent =btrfs_extent_inline_ref_offset (leaf, iref); +if (ref_parent == parent) { + found = true; + goto out; + } +next: + ptr +=btrfs_extent_inline_ref_size (type); + } + +out: +btrfs_release_path (&path); +if (ret <0 ) +return ret; +return found; +} + +static int has_keyed_shared_backref (u64 data_bytenr,u64 parent) +{ +struct btrfs_root *extent_root =btrfs_extent_root (gfs_info, data_bytenr); +struct btrfs_path path = {0 }; +struct btrfs_key key; +int ret; + + key.objectid = data_bytenr; + key.type =BTRFS_SHARED_DATA_REF_KEY ; + key.offset = parent; + ret =btrfs_search_slot (NULL , extent_root, &key, &path,0 ,0 ); +btrfs_release_path (&path); +if (ret <0 ) +return ret; +/* No keyed ref found, return 0. */ +if (ret >0 ) +return 0 ; +return 1 ; +} + +/* +* A helper to determine if the @leaf already belongs to a shared data backref item. +* (with parent bytenr) +* +* Return >0 if the @leaf belongs to a shared data backref. +* Return 0 if not. +* Return <0 for critical error. +*/ +static int is_leaf_shared (struct extent_buffer *leaf,u64 data_bytenr,u64 data_len) +{ +int ret; + + ret =has_inline_shared_backref (data_bytenr, data_len, leaf->start); +if (ret <0 ) { + errno = -ret; +error ("failed to search inlined shared backref for logical %llu len %llu, %m" , + data_bytenr, data_len); +return ret; + } +if (ret >0 ) +return ret; + ret =has_keyed_shared_backref (data_bytenr, leaf->start); +if (ret <0 ) { + errno = -ret; +error ("failed to search keyed shared backref for logical %llu len %llu, %m" , + data_bytenr, data_len); + } +return ret; +} +/* * Check referencer for normal (inlined) data ref * If len == 0, it will be resolved by searching in extent tree
@@ -4049,13 +4182,13 @@ static int check_extent_data_backref(u64 root_id, u64 objectid, u64 offset, btrfs_header_owner(leaf) != root_id) goto next;/* -* For tree blocks have been relocated, data backref are -* shared instead of keyed. Do not account it. +* If the node belongs to a shared backref item, we should not +* account the number. */ -if (btrfs_header_flag (leaf,BTRFS_HEADER_FLAG_RELOC )) { -/* -* skip the leaf to speed up. -*/ + ret =is_leaf_shared (leaf, bytenr, len); +if (ret <0 ) +break ; +if (ret >0 ) { slot =btrfs_header_nritems (leaf); goto next; }
Signature · 1 line
2.49.0
Quoted 18 lines · Qu Wenruo
Inspired by Mark's recent enhanced super block dev item check against chunk tree device item, it turns out there are several existing images not passing the check.
One of the check is fsck/020/keyed_data_ref_with_reloc_leaf.img, which may be caused by some older kernels (the bug is already fixed a long time ago).
The first patch is to fix a lowmem mode check bug, that it doesn't correctly account the keyed backref with shared backref.
This is exposed by the image from the next patch.
Then the last patch is to update the image with a proper note on how to re-create it.
And the new image also acts as a regression test for the above lowmem bug.
Quoted 7 lines
Please note this is not the last fsck image which has such problem, fsck/057 is another one, and will be addressed in a dedicated patch.
Qu Wenruo (2):
btrfs-progs: check/lowmem: fix a false alert when counting the refs
btrfs-progs: fsck-tests: fix an image which has incorrect super dev
item
Added to devel, thanks.