Skip to contentSkip to conversation

Corrupt BTRFS can't be get into a consistent state

DISCUSSION Gerhard Wiesinger 2 participants 7 messages Sep 1, 2024 – Sep 2, 2024 Btrfs file system

lore
Share & tools
lore
#0 lore raw

Hello,

I'm having some Fedora Linux VMs (actual versions, latest updates) in a virtual test infrastructure on Virtualbox. There I run different VMs with different filesystems (ext4, xfs, zfs, bcachefs and btrfs).

I had a hardware problem on the underlying hardware where around 1000 4k blocks could not be read anymore. I migrated with ddrescure the whole disk which worked well.

Of course I was expecting some data loss in the VMs but wanted to get them in a consistent state.

The following file systems got very easy in a consistent state with the corresponding repair/scrub tools of the filesystems:
- ext4
- xfs
- zfs

Unfortunately 2 filesystem can't get into a state, where the filesystem repair tools report "everything fine" (of course with some loss data, but that's fine):
- btrfs
- bcachefs

btrfs --version
btrfs-progs v6.10.1
-EXPERIMENTAL -INJECT -STATIC +LZO +ZSTD +UDEV +FSVERITY +ZONED CRYPTO=libgcrypt

commands run with btrfs:
btrfs check device
btrfs check --init-csum-tree device
btrfs check --init-extent-tree device

But btrfs never got into a consistent state, also with newer versions (have copies of original virtual disk, around 6 month ago). Also btrfs check runs for hours for around 4GB of data.

To reproduce the problem I created a new filesystem and copied some files there:
# Copy around 4GB
time cp -Rap /usr /mnt

Afterwards I created a (quick&dirty) script "corrupt_device.sh" to corrupt the device in the same manner as the original failure (1000 4k blocks will be randomly overwritten).
Script: see below

Result: It can be reproduced, that btrfs can't be brought into a consistent state even after several runs of the repair. If the filesystem is modified in between (e.g. some further files are written) it gets even worser.

You can also try to reproduce it and create a testcase out of it.

Any ideas how to repair and what can be done to get it into a consistent state?

Thnx.

Ciao,
Gerhard

Script corrupt_device.sh:
#!/usr/bin/env bash

RANDOM_DEVICE=/dev/urandom
OUTPUT_DEVICE=/dev/sdb
COUNT=1000
BLOCK_SIZE=4096

MAX_BLOCK_SIZE=$(blockdev --getsize64 ${OUTPUT_DEVICE})

echo "# Configured maximum size=${MAX_BLOCK_SIZE}" MAX_BLOCK_NUMBER=$((MAX_BLOCK_SIZE/BLOCK_SIZE)) echo "# Maximum block number=${MAX_BLOCK_NUMBER}"

for ((BLOCK_NUMBER=1; BLOCK_NUMBER<=${COUNT}; BLOCK_NUMBER++ )) do
  BLOCK=shuf --input-range=0-${MAX_BLOCK_NUMBER} --head-count=1
  dd if=${RANDOM_DEVICE} of=${OUTPUT_DEVICE} bs=${BLOCK_SIZE} seek=${BLOCK} count=1 > /dev/null 2>&1 done

#1 lore raw
Quoted 25 lines · Gerhard Wiesinger

Hello,

I'm having some Fedora Linux VMs (actual versions, latest updates) in a virtual test infrastructure on Virtualbox. There I run different VMs with different filesystems (ext4, xfs, zfs, bcachefs and btrfs).

I had a hardware problem on the underlying hardware where around 1000 4k blocks could not be read anymore. I migrated with ddrescure the whole disk which worked well.

Of course I was expecting some data loss in the VMs but wanted to get them in a consistent state.

The following file systems got very easy in a consistent state with the corresponding repair/scrub tools of the filesystems:
- ext4
- xfs
- zfs

Unfortunately 2 filesystem can't get into a state, where the filesystem repair tools report "everything fine" (of course with some loss data, but that's fine):
- btrfs
- bcachefs

Quoted 7 lines

btrfs --version
btrfs-progs v6.10.1
-EXPERIMENTAL -INJECT -STATIC +LZO +ZSTD +UDEV +FSVERITY +ZONED CRYPTO=libgcrypt

commands run with btrfs:
btrfs check device

This is readonly operation, just to tell you what's going wrong.

Quoted 2 lines

btrfs check --init-csum-tree device
btrfs check --init-extent-tree device

These two are dangerous operations.

And why you didn't try "btrfs scrub"?

Quoted 15 lines

But btrfs never got into a consistent state, also with newer versions (have copies of original virtual disk, around 6 month ago). Also btrfs check runs for hours for around 4GB of data.

To reproduce the problem I created a new filesystem and copied some files there:
# Copy around 4GB
time cp -Rap /usr /mnt

Afterwards I created a (quick&dirty) script "corrupt_device.sh" to corrupt the device in the same manner as the original failure (1000 4k blocks will be randomly overwritten).
Script: see below

Quoted 6 lines

Result: It can be reproduced, that btrfs can't be brought into a consistent state even after several runs of the repair.
If the filesystem is modified in between (e.g. some further files are written) it gets even worser.

You can also try to reproduce it and create a testcase out of it.

I created a 10GiB fs, filled with around 3.7G data using fsstress, run your script.

And the result is the opposite as yours:

Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: c2be653b-6b00-4ed9-925f-258cc7ca5391
[1/7] checking root items
checksum verify failed on 37896192 wanted 0x1e3b3b76 found 0xf6ba3f0c [2/7] checking extents
checksum verify failed on 276135936 wanted 0x142c0f06 found 0xdcc4a8df checksum verify failed on 66502656 wanted 0x6fff8502 found 0x333397eb checksum verify failed on 107397120 wanted 0x52894737 found 0x56e3558b checksum verify failed on 235864064 wanted 0x3a0b6ded found 0xe11cbb02 checksum verify failed on 72384512 wanted 0x9306ee79 found 0x62a123d8 checksum verify failed on 264683520 wanted 0x2cebc20f found 0x293c9426 checksum verify failed on 196001792 wanted 0xe3ec9b3f found 0x29d32fce [3/7] checking free space tree
[4/7] checking fs roots
[5/7] checking only csums items (without verifying data) [6/7] checking root refs
[7/7] checking quota groups skipped (not enabled on this FS) found 3776757760 bytes used, no error found <<<
total csum bytes: 1692008
total tree bytes: 82444288
total fs tree bytes: 71237632
total extent tree bytes: 6455296
btree space waste bytes: 43820357
file data blocks allocated: 29158084608
referenced 4905811968

Btrfs properly detects the corruption in metadata, but since the default profile is DUP, it's totally fine and can go with the other mirror.

And sure since your data is still SINGLE thus you will lose some data, but your metadata is totally fine.

With proper btrfs scrub run:

scrub done for c2be653b-6b00-4ed9-925f-258cc7ca5391

Scrub started:    Mon Sep  2 06:08:23 2024
Status:           finished
Duration:         0:00:03
Total to scrub:   3.61GiB
Rate:             1.20GiB/s
Error summary:    verify=36 csum=169
  Corrected:      60
  Uncorrectable:  145
  Unverified:     0

So it means 60 metadata csum mismatch is fixed, only 145 bad data sectors.

And after the above scrub, btrfs check reports no more error (at least for metadata):

Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: c2be653b-6b00-4ed9-925f-258cc7ca5391
[1/7] checking root items
[2/7] checking extents
[3/7] checking free space tree
[4/7] checking fs roots
[5/7] checking only csums items (without verifying data) [6/7] checking root refs
[7/7] checking quota groups skipped (not enabled on this FS) found 3776757760 bytes used, no error found
total csum bytes: 1692008
total tree bytes: 82444288
total fs tree bytes: 71237632
total extent tree bytes: 6455296
btree space waste bytes: 43819992
file data blocks allocated: 29158084608
referenced 4905811968

Quoted 3 lines

Any ideas how to repair and what can be done to get it into a consistent state?

Please give the original "btrfs check" output.

I think your original fs is either using SINGLE metadata profile (then very hard to do repair), AND you're using incorrect way to repair (scrub first, then btrfs check to verify, never --init-extent-tree nor --init-csum-tree unless you know what you're really doing).

And your random corruption script is the best case scenario for btrfs, it's pretty easy for btrfs just to use the good mirror to repair metadata, without any need to extra repair inside metadata.

Thanks,
Qu

Quoted 27 lines

Thnx.

Ciao,
Gerhard

Script corrupt_device.sh:
#!/usr/bin/env bash

RANDOM_DEVICE=/dev/urandom
OUTPUT_DEVICE=/dev/sdb
COUNT=1000
BLOCK_SIZE=4096

MAX_BLOCK_SIZE=$(blockdev --getsize64 ${OUTPUT_DEVICE})

echo "# Configured maximum size=${MAX_BLOCK_SIZE}" MAX_BLOCK_NUMBER=$((MAX_BLOCK_SIZE/BLOCK_SIZE)) echo "# Maximum block number=${MAX_BLOCK_NUMBER}"

for ((BLOCK_NUMBER=1; BLOCK_NUMBER<=${COUNT}; BLOCK_NUMBER++ )) do
  BLOCK=shuf --input-range=0-${MAX_BLOCK_NUMBER} --head-count=1 dd if=${RANDOM_DEVICE} of=${OUTPUT_DEVICE} bs=${BLOCK_SIZE}
seek=${BLOCK} count=1 > /dev/null 2>&1
done

#2 lore raw
Quoted 36 lines · Qu Wenruo
Quoted 25 lines · Gerhard Wiesinger

Hello,

I'm having some Fedora Linux VMs (actual versions, latest updates) in a virtual test infrastructure on Virtualbox. There I run different VMs with different filesystems (ext4, xfs, zfs, bcachefs and btrfs).

I had a hardware problem on the underlying hardware where around 1000 4k blocks could not be read anymore. I migrated with ddrescure the whole disk which worked well.

Of course I was expecting some data loss in the VMs but wanted to get them in a consistent state.

The following file systems got very easy in a consistent state with the corresponding repair/scrub tools of the filesystems:
- ext4
- xfs
- zfs

Unfortunately 2 filesystem can't get into a state, where the filesystem repair tools report "everything fine" (of course with some loss data, but that's fine):
- btrfs
- bcachefs

Quoted 7 lines

btrfs --version
btrfs-progs v6.10.1
-EXPERIMENTAL -INJECT -STATIC +LZO +ZSTD +UDEV +FSVERITY +ZONED CRYPTO=libgcrypt

commands run with btrfs:
btrfs check device

Quoted 8 lines

This is readonly operation, just to tell you what's going wrong.

Quoted 2 lines

btrfs check --init-csum-tree device
btrfs check --init-extent-tree device

These two are dangerous operations.

And why you didn't try "btrfs scrub"?

Forgot to mention, started with this one.

Quoted 74 lines
Quoted 16 lines

But btrfs never got into a consistent state, also with newer versions (have copies of original virtual disk, around 6 month ago). Also btrfs check runs for hours for around 4GB of data.

To reproduce the problem I created a new filesystem and copied some files there:
# Copy around 4GB
time cp -Rap /usr /mnt

Afterwards I created a (quick&dirty) script "corrupt_device.sh" to corrupt the device in the same manner as the original failure (1000 4k blocks will be randomly overwritten).
Script: see below

Quoted 6 lines

Result: It can be reproduced, that btrfs can't be brought into a consistent state even after several runs of the repair.
If the filesystem is modified in between (e.g. some further files are written) it gets even worser.

You can also try to reproduce it and create a testcase out of it.

I created a 10GiB fs, filled with around 3.7G data using fsstress, run your script.

And the result is the opposite as yours:

Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: c2be653b-6b00-4ed9-925f-258cc7ca5391
[1/7] checking root items
checksum verify failed on 37896192 wanted 0x1e3b3b76 found 0xf6ba3f0c [2/7] checking extents
checksum verify failed on 276135936 wanted 0x142c0f06 found 0xdcc4a8df checksum verify failed on 66502656 wanted 0x6fff8502 found 0x333397eb checksum verify failed on 107397120 wanted 0x52894737 found 0x56e3558b checksum verify failed on 235864064 wanted 0x3a0b6ded found 0xe11cbb02 checksum verify failed on 72384512 wanted 0x9306ee79 found 0x62a123d8 checksum verify failed on 264683520 wanted 0x2cebc20f found 0x293c9426 checksum verify failed on 196001792 wanted 0xe3ec9b3f found 0x29d32fce [3/7] checking free space tree
[4/7] checking fs roots
[5/7] checking only csums items (without verifying data) [6/7] checking root refs
[7/7] checking quota groups skipped (not enabled on this FS) found 3776757760 bytes used, no error found <<<
total csum bytes: 1692008
total tree bytes: 82444288
total fs tree bytes: 71237632
total extent tree bytes: 6455296
btree space waste bytes: 43820357
file data blocks allocated: 29158084608
 referenced 4905811968

Btrfs properly detects the corruption in metadata, but since the default profile is DUP, it's totally fine and can go with the other mirror.

And sure since your data is still SINGLE thus you will lose some data, but your metadata is totally fine.

With proper btrfs scrub run:

scrub done for c2be653b-6b00-4ed9-925f-258cc7ca5391 Scrub started:    Mon Sep  2 06:08:23 2024 Status:           finished
Duration:         0:00:03
Total to scrub:   3.61GiB
Rate:             1.20GiB/s
Error summary:    verify=36 csum=169 Corrected:      60
  Uncorrectable:  145
  Unverified:     0

Quoted 1 line

So it means 60 metadata csum mismatch is fixed, only 145 bad data sectors.

How to get rid of the 145 uncorrectables?

Quoted 27 lines

And after the above scrub, btrfs check reports no more error (at least for metadata):

Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: c2be653b-6b00-4ed9-925f-258cc7ca5391
[1/7] checking root items
[2/7] checking extents
[3/7] checking free space tree
[4/7] checking fs roots
[5/7] checking only csums items (without verifying data) [6/7] checking root refs
[7/7] checking quota groups skipped (not enabled on this FS) found 3776757760 bytes used, no error found
total csum bytes: 1692008
total tree bytes: 82444288
total fs tree bytes: 71237632
total extent tree bytes: 6455296
btree space waste bytes: 43819992
file data blocks allocated: 29158084608
 referenced 4905811968

Quoted 3 lines

Any ideas how to repair and what can be done to get it into a consistent state?

Quoted 6 lines

Please give the original "btrfs check" output.

I think your original fs is either using SINGLE metadata profile (then very hard to do repair), AND you're using incorrect way to repair (scrub first, then btrfs check to verify, never --init-extent-tree nor --init-csum-tree unless you know what you're really doing).

I originally did scrub first (forget to mention, but there were still uncorrectable errors as with my test script)

Original ones: DUP Metadata, so repair should work fine regarding metadata (I have also an original copy of the virtual disk image so I can play around).

btrfs inspect-internal list-chunks /mnt
Devid PNumber      Type/profile   PStart    Length     PEnd LNumber   LStart Usage% ----- ------- ----------------- -------- --------- -------- ------- -------- ------ 1       1       Data/single 12.00MiB   8.00MiB 20.00MiB 1 12.00MiB  99.76 1       2   Metadata/DUP    36.00MiB   1.00GiB  1.04GiB 2 28.00MiB  14.74 1       3   Metadata/DUP     1.04GiB   1.00GiB  2.04GiB 3 28.00MiB  14.74 1       4       Data/single  2.04GiB   1.00GiB  3.04GiB 4  1.03GiB  93.51 1       5       Data/single  3.04GiB   1.00GiB  4.04GiB 5  2.03GiB  49.37 1       6     System/DUP     4.10GiB  32.00MiB  4.13GiB 15 15.18GiB   0.05 1       7     System/DUP     4.13GiB  32.00MiB  4.16GiB 16 15.18GiB   0.05 1       8       Data/single  4.60GiB   1.00GiB  5.60GiB 6  6.56GiB  37.75 1       9   Metadata/DUP     5.60GiB 128.00MiB  5.72GiB 7  7.71GiB   9.55 1      10   Metadata/DUP     5.72GiB 128.00MiB  5.85GiB 8  7.71GiB   9.55 1      11       Data/single  5.85GiB   1.00GiB  6.85GiB 9  8.12GiB  11.79 1      12       Data/single  6.85GiB   1.00GiB  7.85GiB 10  9.12GiB  16.43 1      13       Data/single  7.85GiB   1.00GiB  8.85GiB 11 10.12GiB  69.76 1      14       Data/single  8.85GiB   1.00GiB  9.85GiB 12 11.15GiB  80.00 1      15       Data/single  9.85GiB   1.00GiB 10.85GiB 13 12.15GiB  45.58 1      16       Data/single 11.85GiB   1.00GiB 12.85GiB 14 14.18GiB  67.44 1      17       Data/single 12.85GiB   1.00GiB 13.85GiB 17 15.21GiB  35.54

(BTW: Any other way to find this out?)

Quoted 4 lines

And your random corruption script is the best case scenario for btrfs, it's pretty easy for btrfs just to use the good mirror to repair metadata, without any need to extra repair inside metadata.

# Syntethic test case:

# Destroyed with script after btrfs scrub start /mnt [root@localhost ~]# btrfs scrub status /mnt UUID:             717cee50-d376-4c5a-941d-3dac986256fd Scrub started:    Mon Sep  2 07:11:30 2024 Status:           finished
Duration:         0:00:25
Total to scrub:   3.73GiB
Rate:             152.74MiB/s
Error summary:    verify=8 csum=182 Corrected:      12
  Uncorrectable:  178
  Unverified:     0

# After finishing
btrfs scrub status /mnt
UUID:             717cee50-d376-4c5a-941d-3dac986256fd Scrub started:    Mon Sep  2 07:12:39 2024 Status:           finished
Duration:         0:00:26
Total to scrub:   3.73GiB
Rate:             146.87MiB/s
Error summary:    csum=178
  Corrected:      0
  Uncorrectable:  178
  Unverified:     0

=> so there are still uncorrectables, why can't they be fixed with scrub? (Expectation: "Clean filesystem afterwards, maybe some data lost due to overwritten parts")

# Original disk (after running a lot of scrub/repair commands):

btrfs check /dev/mapper/fedora--defect-root--defect

[1/7] checking root items
[2/7] checking extents
[3/7] checking free space cache
[4/7] checking fs roots
Missing extent item in extent tree for disk_bytenr 1402974208, num_bytes 1847296 Missing extent item in extent tree for disk_bytenr 1423011840, num_bytes 598016 Missing extent item in extent tree for disk_bytenr 1457127424, num_bytes 6164480 Missing extent item in extent tree for disk_bytenr 1484931072, num_bytes 7127040 Missing extent item in extent tree for disk_bytenr 2022019072, num_bytes 1773568 Missing extent item in extent tree for disk_bytenr 2635603968, num_bytes 5255168 Missing extent item in extent tree for disk_bytenr 2817658880, num_bytes 8073216 Missing extent item in extent tree for disk_bytenr 2935803904, num_bytes 3244032 Missing extent item in extent tree for disk_bytenr 2605039616, num_bytes 8355840 Missing extent item in extent tree for disk_bytenr 2745331712, num_bytes 3706880 Missing extent item in extent tree for disk_bytenr 3018563584, num_bytes 3121152 Missing extent item in extent tree for disk_bytenr 2947518464, num_bytes 8302592 Missing extent item in extent tree for disk_bytenr 3068272640, num_bytes 2781184 Missing extent item in extent tree for disk_bytenr 2791301120, num_bytes 3547136 Missing extent item in extent tree for disk_bytenr 7574302720, num_bytes 3350528 Missing extent item in extent tree for disk_bytenr 3031347200, num_bytes 1216512 root 5 inode 72933 errors 840, bad file extent, odd csum item
root 5 inode 90368 errors 840, bad file extent, odd csum item
root 5 inode 166783 errors 840, bad file extent, odd csum item
root 5 inode 219687 errors 840, bad file extent, odd csum item
root 5 inode 248131 errors 840, bad file extent, odd csum item
root 5 inode 346844 errors 840, bad file extent, odd csum item
root 5 inode 379257 errors 840, bad file extent, odd csum item
root 5 inode 464039 errors 840, bad file extent, odd csum item
root 5 inode 464044 errors 840, bad file extent, odd csum item
root 5 inode 471393 errors 840, bad file extent, odd csum item
root 5 inode 681858 errors 840, bad file extent, odd csum item
root 5 inode 833692 errors 840, bad file extent, odd csum item
root 5 inode 909657 errors 840, bad file extent, odd csum item
root 5 inode 910009 errors 840, bad file extent, odd csum item
root 5 inode 1032866 errors 840, bad file extent, odd csum item

root 5 inode 1153395 errors 2001, no inode item, link count wrong
        unresolved ref dir 53460 index 0 namelen 75 name 7ae2e1b7acb59ea0b30163c03de1cf3884abdc3f-kernel-core-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1153401 errors 2001, no inode item, link count wrong
        unresolved ref dir 53460 index 0 namelen 70 name 2ed0519ee6bab67f5e5d5d6ce4650e541fbb359f-kernel-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1153403 errors 2001, no inode item, link count wrong
        unresolved ref dir 53460 index 0 namelen 76 name f2caebbe5620078065d21c37e4a88e8f5957215b-kernel-devel-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1153405 errors 2001, no inode item, link count wrong
        unresolved ref dir 53460 index 0 namelen 78 name 2ad09f77ebb3882396b40dfa44f2840cccf4cf9d-kernel-modules-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1159931 errors 2001, no inode item, link count wrong
        unresolved ref dir 271 index 0 namelen 15 name fc29-update.log filetype 1 errors 6, no dir index, no inode ref
root 5 inode 1159933 errors 2001, no inode item, link count wrong
        unresolved ref dir 53026 index 0 namelen 31 name fedora-modular-ce4dd907f26812da filetype 2 errors 6, no dir index, no inode ref

<skipped a lot here>

ERROR: errors found in fs roots
Opening filesystem to check...
Checking filesystem on /dev/mapper/fedora--defect-root--defect
UUID: 7bb1e7be-c83c-4cbb-a396-14c38393977c
found 5625135104 bytes used, error(s) found
total csum bytes: 4979288
total tree bytes: 171147264
total fs tree bytes: 154255360
total extent tree bytes: 10895360
btree space waste bytes: 36824455
file data blocks allocated: 11575373824
 referenced 5772767232

Can test on the original disk in the evening.

Thnx.

Ciao,

Gerhard

#3 lore raw
Quoted 1 line · Gerhard Wiesinger

On 02.09.2024 00:13, Qu Wenruo wrote:

[...]

Quoted 4 lines
Quoted 2 lines

So it means 60 metadata csum mismatch is fixed, only 145 bad data sectors.

How to get rid of the 145 uncorrectables?

Those are all data, at least for my case.

It will not be repairable, just as you mentioned, data loss is expected.

Quoted 30 lines
Quoted 30 lines

And after the above scrub, btrfs check reports no more error (at least for metadata):

Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: c2be653b-6b00-4ed9-925f-258cc7ca5391
[1/7] checking root items
[2/7] checking extents
[3/7] checking free space tree
[4/7] checking fs roots
[5/7] checking only csums items (without verifying data) [6/7] checking root refs
[7/7] checking quota groups skipped (not enabled on this FS) found 3776757760 bytes used, no error found
total csum bytes: 1692008
total tree bytes: 82444288
total fs tree bytes: 71237632
total extent tree bytes: 6455296
btree space waste bytes: 43819992
file data blocks allocated: 29158084608
 referenced 4905811968

Quoted 3 lines

Any ideas how to repair and what can be done to get it into a consistent state?

Please give the original "btrfs check" output.

Quoted 7 lines
Quoted 4 lines

I think your original fs is either using SINGLE metadata profile (then very hard to do repair), AND you're using incorrect way to repair (scrub first, then btrfs check to verify, never --init-extent-tree nor --init-csum-tree unless you know what you're really doing).

I originally did scrub first (forget to mention, but there were still uncorrectable errors as with my test script)

Aren't you expect data corruption?

The uncorrectable errors just means, btrfs knows there is some data with checksum, but the on-disk data mismatch with the checksum.

Since it's SINGLE, there is not way to recover, exactly your "data loss" case.
The only difference from other fses is, btrfs can detect the corruption and report them as error.

Quoted 45 lines

Original ones: DUP Metadata, so repair should work fine regarding metadata (I have also an original copy of the virtual disk image so I can play around).

btrfs inspect-internal list-chunks /mnt
Devid PNumber      Type/profile   PStart    Length     PEnd LNumber LStart Usage%
----- ------- ----------------- -------- --------- -------- ------- -------- ------
    1       1       Data/single 12.00MiB   8.00MiB 20.00MiB 1 12.00MiB
99.76
    1       2   Metadata/DUP    36.00MiB   1.00GiB  1.04GiB 2 28.00MiB
14.74
    1       3   Metadata/DUP     1.04GiB   1.00GiB  2.04GiB 3 28.00MiB
14.74
    1       4       Data/single  2.04GiB   1.00GiB  3.04GiB 4  1.03GiB
93.51
    1       5       Data/single  3.04GiB   1.00GiB  4.04GiB 5  2.03GiB
49.37
    1       6     System/DUP     4.10GiB  32.00MiB  4.13GiB 15
15.18GiB   0.05
    1       7     System/DUP     4.13GiB  32.00MiB  4.16GiB 16
15.18GiB   0.05
    1       8       Data/single  4.60GiB   1.00GiB  5.60GiB 6  6.56GiB
37.75
    1       9   Metadata/DUP     5.60GiB 128.00MiB  5.72GiB 7 7.71GiB
9.55
    1      10   Metadata/DUP     5.72GiB 128.00MiB  5.85GiB 8 7.71GiB
9.55
    1      11       Data/single  5.85GiB   1.00GiB  6.85GiB 9  8.12GiB
11.79
    1      12       Data/single  6.85GiB   1.00GiB  7.85GiB 10 9.12GiB
16.43
    1      13       Data/single  7.85GiB   1.00GiB  8.85GiB 11
10.12GiB  69.76
    1      14       Data/single  8.85GiB   1.00GiB  9.85GiB 12
11.15GiB  80.00
    1      15       Data/single  9.85GiB   1.00GiB 10.85GiB 13
12.15GiB  45.58
    1      16       Data/single 11.85GiB   1.00GiB 12.85GiB 14
14.18GiB  67.44
    1      17       Data/single 12.85GiB   1.00GiB 13.85GiB 17
15.21GiB  35.54

Quoted 1 line

(BTW: Any other way to find this out?)

btrfs fi suage is more than enough to find out the profiles.

Quoted 35 lines
Quoted 6 lines

And your random corruption script is the best case scenario for btrfs, it's pretty easy for btrfs just to use the good mirror to repair metadata, without any need to extra repair inside metadata.

# Syntethic test case:

# Destroyed with script after btrfs scrub start /mnt [root@localhost ~]# btrfs scrub status /mnt UUID:             717cee50-d376-4c5a-941d-3dac986256fd Scrub started:    Mon Sep  2 07:11:30 2024 Status:           finished
Duration:         0:00:25
Total to scrub:   3.73GiB
Rate:             152.74MiB/s
Error summary:    verify=8 csum=182
  Corrected:      12
  Uncorrectable:  178
  Unverified:     0

# After finishing
btrfs scrub status /mnt
UUID:             717cee50-d376-4c5a-941d-3dac986256fd Scrub started:    Mon Sep  2 07:12:39 2024 Status:           finished
Duration:         0:00:26
Total to scrub:   3.73GiB
Rate:             146.87MiB/s
Error summary:    csum=178
  Corrected:      0
  Uncorrectable:  178
  Unverified:     0

Quoted 3 lines

=> so there are still uncorrectables, why can't they be fixed with scrub? (Expectation: "Clean filesystem afterwards, maybe some data lost due to overwritten parts")

That's your "data lost", btrfs just detects those data loss and report them. Unlike other fses with data checksum, they can only accept whatever data is on the disk, not knowing if it's good or bad.

Quoted 78 lines

# Original disk (after running a lot of scrub/repair commands):

btrfs check /dev/mapper/fedora--defect-root--defect

[1/7] checking root items
[2/7] checking extents
[3/7] checking free space cache
[4/7] checking fs roots
Missing extent item in extent tree for disk_bytenr 1402974208, num_bytes 1847296
Missing extent item in extent tree for disk_bytenr 1423011840, num_bytes 598016
Missing extent item in extent tree for disk_bytenr 1457127424, num_bytes 6164480
Missing extent item in extent tree for disk_bytenr 1484931072, num_bytes 7127040
Missing extent item in extent tree for disk_bytenr 2022019072, num_bytes 1773568
Missing extent item in extent tree for disk_bytenr 2635603968, num_bytes 5255168
Missing extent item in extent tree for disk_bytenr 2817658880, num_bytes 8073216
Missing extent item in extent tree for disk_bytenr 2935803904, num_bytes 3244032
Missing extent item in extent tree for disk_bytenr 2605039616, num_bytes 8355840
Missing extent item in extent tree for disk_bytenr 2745331712, num_bytes 3706880
Missing extent item in extent tree for disk_bytenr 3018563584, num_bytes 3121152
Missing extent item in extent tree for disk_bytenr 2947518464, num_bytes 8302592
Missing extent item in extent tree for disk_bytenr 3068272640, num_bytes 2781184
Missing extent item in extent tree for disk_bytenr 2791301120, num_bytes 3547136
Missing extent item in extent tree for disk_bytenr 7574302720, num_bytes 3350528
Missing extent item in extent tree for disk_bytenr 3031347200, num_bytes 1216512
root 5 inode 72933 errors 840, bad file extent, odd csum item root 5 inode 90368 errors 840, bad file extent, odd csum item root 5 inode 166783 errors 840, bad file extent, odd csum item root 5 inode 219687 errors 840, bad file extent, odd csum item root 5 inode 248131 errors 840, bad file extent, odd csum item root 5 inode 346844 errors 840, bad file extent, odd csum item root 5 inode 379257 errors 840, bad file extent, odd csum item root 5 inode 464039 errors 840, bad file extent, odd csum item root 5 inode 464044 errors 840, bad file extent, odd csum item root 5 inode 471393 errors 840, bad file extent, odd csum item root 5 inode 681858 errors 840, bad file extent, odd csum item root 5 inode 833692 errors 840, bad file extent, odd csum item root 5 inode 909657 errors 840, bad file extent, odd csum item root 5 inode 910009 errors 840, bad file extent, odd csum item root 5 inode 1032866 errors 840, bad file extent, odd csum item

root 5 inode 1153395 errors 2001, no inode item, link count wrong
        unresolved ref dir 53460 index 0 namelen 75 name
7ae2e1b7acb59ea0b30163c03de1cf3884abdc3f-kernel-core-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1153401 errors 2001, no inode item, link count wrong
        unresolved ref dir 53460 index 0 namelen 70 name
2ed0519ee6bab67f5e5d5d6ce4650e541fbb359f-kernel-4.19.6-200.fc28-x86_64
filetype 2 errors 6, no dir index, no inode ref
root 5 inode 1153403 errors 2001, no inode item, link count wrong
        unresolved ref dir 53460 index 0 namelen 76 name
f2caebbe5620078065d21c37e4a88e8f5957215b-kernel-devel-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1153405 errors 2001, no inode item, link count wrong
        unresolved ref dir 53460 index 0 namelen 78 name
2ad09f77ebb3882396b40dfa44f2840cccf4cf9d-kernel-modules-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1159931 errors 2001, no inode item, link count wrong
        unresolved ref dir 271 index 0 namelen 15 name fc29-update.log
filetype 1 errors 6, no dir index, no inode ref
root 5 inode 1159933 errors 2001, no inode item, link count wrong
        unresolved ref dir 53026 index 0 namelen 31 name
fedora-modular-ce4dd907f26812da filetype 2 errors 6, no dir index, no
inode ref

Quoted 7 lines

<skipped a lot here>

ERROR: errors found in fs roots
Opening filesystem to check...
Checking filesystem on /dev/mapper/fedora--defect-root--defect
UUID: 7bb1e7be-c83c-4cbb-a396-14c38393977c
found 5625135104 bytes used, error(s) found

Please provide the full output with both stderr and stdout combined.

It looks like there is one or more tree block fully corrupted (for both mirrors), thus we got this many errors.

In that case, it's very hard to repair, but you can still try "btrfs check --repair" to try your luck, just remember do not try those fancy flags unless you know what you're doing.

Just remember even for DUP, the chance of a full recovery is still fully depending on that at least one copy is still good for your metadata.

If your corruption is not really random, it's not that hard to corrupt two mirrors in one go, and lead to the above very complex corruption thus much harder to repair.

Thanks,
Qu

Quoted 16 lines

total csum bytes: 4979288
total tree bytes: 171147264
total fs tree bytes: 154255360
total extent tree bytes: 10895360
btree space waste bytes: 36824455
file data blocks allocated: 11575373824
 referenced 5772767232

Can test on the original disk in the evening.

Thnx.

Ciao,

Gerhard

#4 lore raw
Quoted 5 lines · Qu Wenruo
Quoted 1 line · Gerhard Wiesinger

On 02.09.2024 00:13, Qu Wenruo wrote:

[...]

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

So it means 60 metadata csum mismatch is fixed, only 145 bad data sectors.

How to get rid of the 145 uncorrectables?

Those are all data, at least for my case.

It will not be repairable, just as you mentioned, data loss is expected.

Yes, but I'm expecting a repaired state and not still an inconsistent state with known errors (as command repair states). Other filesystems move broken files to e.g. lost+found (expecting at least with a switch).

Quoted 36 lines
Quoted 36 lines
Quoted 35 lines

And after the above scrub, btrfs check reports no more error (at least for metadata):

Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: c2be653b-6b00-4ed9-925f-258cc7ca5391
[1/7] checking root items
[2/7] checking extents
[3/7] checking free space tree
[4/7] checking fs roots
[5/7] checking only csums items (without verifying data) [6/7] checking root refs
[7/7] checking quota groups skipped (not enabled on this FS) found 3776757760 bytes used, no error found
total csum bytes: 1692008
total tree bytes: 82444288
total fs tree bytes: 71237632
total extent tree bytes: 6455296
btree space waste bytes: 43819992
file data blocks allocated: 29158084608
 referenced 4905811968

Quoted 3 lines

Any ideas how to repair and what can be done to get it into a consistent state?

Please give the original "btrfs check" output.

I think your original fs is either using SINGLE metadata profile (then very hard to do repair), AND you're using incorrect way to repair (scrub first, then btrfs check to verify, never --init-extent-tree nor --init-csum-tree unless you know what you're really doing).

Quoted 4 lines
Quoted 2 lines

I originally did scrub first (forget to mention, but there were still uncorrectable errors as with my test script)

Aren't you expect data corruption?

Yes, but I'm expecting a repaired state and not still an inconsistent state with known errors (as command repair states). Other filesystems move broken files to e.g. lost+found (expecting at least with a switch)

Quoted 54 lines

The uncorrectable errors just means, btrfs knows there is some data with checksum, but the on-disk data mismatch with the checksum.

Since it's SINGLE, there is not way to recover, exactly your "data loss" case.
The only difference from other fses is, btrfs can detect the corruption and report them as error.

Quoted 45 lines

Original ones: DUP Metadata, so repair should work fine regarding metadata (I have also an original copy of the virtual disk image so I can play around).

btrfs inspect-internal list-chunks /mnt
Devid PNumber      Type/profile   PStart    Length     PEnd LNumber LStart Usage%
----- ------- ----------------- -------- --------- -------- ------- -------- ------
     1       1       Data/single 12.00MiB   8.00MiB 20.00MiB 1 12.00MiB 99.76
     1       2   Metadata/DUP    36.00MiB   1.00GiB  1.04GiB 2 28.00MiB 14.74
     1       3   Metadata/DUP     1.04GiB   1.00GiB  2.04GiB 3 28.00MiB 14.74
     1       4       Data/single  2.04GiB   1.00GiB  3.04GiB 4 1.03GiB 93.51
     1       5       Data/single  3.04GiB   1.00GiB  4.04GiB 5 2.03GiB 49.37
     1       6     System/DUP     4.10GiB  32.00MiB  4.13GiB 15 15.18GiB   0.05
     1       7     System/DUP     4.13GiB  32.00MiB  4.16GiB 16 15.18GiB   0.05
     1       8       Data/single  4.60GiB   1.00GiB  5.60GiB 6 6.56GiB 37.75
     1       9   Metadata/DUP     5.60GiB 128.00MiB  5.72GiB 7 7.71GiB 9.55
     1      10   Metadata/DUP     5.72GiB 128.00MiB  5.85GiB 8 7.71GiB 9.55
     1      11       Data/single  5.85GiB   1.00GiB  6.85GiB 9 8.12GiB 11.79
     1      12       Data/single  6.85GiB   1.00GiB  7.85GiB 10 9.12GiB 16.43
     1      13       Data/single  7.85GiB   1.00GiB  8.85GiB 11 10.12GiB  69.76
     1      14       Data/single  8.85GiB   1.00GiB  9.85GiB 12 11.15GiB  80.00
     1      15       Data/single  9.85GiB   1.00GiB 10.85GiB 13 12.15GiB  45.58
     1      16       Data/single 11.85GiB   1.00GiB 12.85GiB 14 14.18GiB  67.44
     1      17       Data/single 12.85GiB   1.00GiB 13.85GiB 17 15.21GiB  35.54

Quoted 3 lines
Quoted 1 line

(BTW: Any other way to find this out?)

btrfs fi suage is more than enough to find out the profiles.

Will try it.

Quoted 36 lines
Quoted 36 lines
Quoted 7 lines

And your random corruption script is the best case scenario for btrfs, it's pretty easy for btrfs just to use the good mirror to repair metadata, without any need to extra repair inside metadata.

# Syntethic test case:

# Destroyed with script after btrfs scrub start /mnt [root@localhost ~]# btrfs scrub status /mnt UUID:             717cee50-d376-4c5a-941d-3dac986256fd Scrub started:    Mon Sep  2 07:11:30 2024 Status:           finished
Duration:         0:00:25
Total to scrub:   3.73GiB
Rate:             152.74MiB/s
Error summary:    verify=8 csum=182 Corrected:      12
   Uncorrectable:  178
   Unverified:     0

# After finishing
btrfs scrub status /mnt
UUID:             717cee50-d376-4c5a-941d-3dac986256fd Scrub started:    Mon Sep  2 07:12:39 2024 Status:           finished
Duration:         0:00:26
Total to scrub:   3.73GiB
Rate:             146.87MiB/s
Error summary:    csum=178
   Corrected:      0
   Uncorrectable:  178
   Unverified:     0

Quoted 7 lines
Quoted 3 lines

=> so there are still uncorrectables, why can't they be fixed with scrub? (Expectation: "Clean filesystem afterwards, maybe some data lost due to overwritten parts")

That's your "data lost", btrfs just detects those data loss and report them. Unlike other fses with data checksum, they can only accept whatever data is on the disk, not knowing if it's good or bad.

Yes, but I'm expecting a repaired state and not still an inconsistent state with known errors (as command repair states). Other filesystems move broken files to e.g. lost+found (expecting at least with a switch)

Quoted 81 lines
Quoted 81 lines

# Original disk (after running a lot of scrub/repair commands):

btrfs check /dev/mapper/fedora--defect-root--defect

[1/7] checking root items
[2/7] checking extents
[3/7] checking free space cache
[4/7] checking fs roots
Missing extent item in extent tree for disk_bytenr 1402974208, num_bytes 1847296
Missing extent item in extent tree for disk_bytenr 1423011840, num_bytes 598016
Missing extent item in extent tree for disk_bytenr 1457127424, num_bytes 6164480
Missing extent item in extent tree for disk_bytenr 1484931072, num_bytes 7127040
Missing extent item in extent tree for disk_bytenr 2022019072, num_bytes 1773568
Missing extent item in extent tree for disk_bytenr 2635603968, num_bytes 5255168
Missing extent item in extent tree for disk_bytenr 2817658880, num_bytes 8073216
Missing extent item in extent tree for disk_bytenr 2935803904, num_bytes 3244032
Missing extent item in extent tree for disk_bytenr 2605039616, num_bytes 8355840
Missing extent item in extent tree for disk_bytenr 2745331712, num_bytes 3706880
Missing extent item in extent tree for disk_bytenr 3018563584, num_bytes 3121152
Missing extent item in extent tree for disk_bytenr 2947518464, num_bytes 8302592
Missing extent item in extent tree for disk_bytenr 3068272640, num_bytes 2781184
Missing extent item in extent tree for disk_bytenr 2791301120, num_bytes 3547136
Missing extent item in extent tree for disk_bytenr 7574302720, num_bytes 3350528
Missing extent item in extent tree for disk_bytenr 3031347200, num_bytes 1216512
root 5 inode 72933 errors 840, bad file extent, odd csum item root 5 inode 90368 errors 840, bad file extent, odd csum item root 5 inode 166783 errors 840, bad file extent, odd csum item root 5 inode 219687 errors 840, bad file extent, odd csum item root 5 inode 248131 errors 840, bad file extent, odd csum item root 5 inode 346844 errors 840, bad file extent, odd csum item root 5 inode 379257 errors 840, bad file extent, odd csum item root 5 inode 464039 errors 840, bad file extent, odd csum item root 5 inode 464044 errors 840, bad file extent, odd csum item root 5 inode 471393 errors 840, bad file extent, odd csum item root 5 inode 681858 errors 840, bad file extent, odd csum item root 5 inode 833692 errors 840, bad file extent, odd csum item root 5 inode 909657 errors 840, bad file extent, odd csum item root 5 inode 910009 errors 840, bad file extent, odd csum item root 5 inode 1032866 errors 840, bad file extent, odd csum item

root 5 inode 1153395 errors 2001, no inode item, link count wrong
         unresolved ref dir 53460 index 0 namelen 75 name 7ae2e1b7acb59ea0b30163c03de1cf3884abdc3f-kernel-core-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1153401 errors 2001, no inode item, link count wrong
         unresolved ref dir 53460 index 0 namelen 70 name 2ed0519ee6bab67f5e5d5d6ce4650e541fbb359f-kernel-4.19.6-200.fc28-x86_64
filetype 2 errors 6, no dir index, no inode ref
root 5 inode 1153403 errors 2001, no inode item, link count wrong
         unresolved ref dir 53460 index 0 namelen 76 name f2caebbe5620078065d21c37e4a88e8f5957215b-kernel-devel-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1153405 errors 2001, no inode item, link count wrong
         unresolved ref dir 53460 index 0 namelen 78 name 2ad09f77ebb3882396b40dfa44f2840cccf4cf9d-kernel-modules-4.19.6-200.fc28-x86_64 filetype 2 errors 6, no dir index, no inode ref root 5 inode 1159931 errors 2001, no inode item, link count wrong
         unresolved ref dir 271 index 0 namelen 15 name fc29-update.log
filetype 1 errors 6, no dir index, no inode ref
root 5 inode 1159933 errors 2001, no inode item, link count wrong
         unresolved ref dir 53026 index 0 namelen 31 name
fedora-modular-ce4dd907f26812da filetype 2 errors 6, no dir index, no
inode ref

<skipped a lot here>

Quoted 7 lines
Quoted 5 lines

ERROR: errors found in fs roots
Opening filesystem to check...
Checking filesystem on /dev/mapper/fedora--defect-root--defect
UUID: 7bb1e7be-c83c-4cbb-a396-14c38393977c
found 5625135104 bytes used, error(s) found

Please provide the full output with both stderr and stdout combined.

Was stripped log file with command btrfs check /dev/mapper/fedora--defect-root--defect > btrfs-root-defect-after-repairs.log 2>&1

Send you full log file in a private E-mail.

Yes, but I'm expecting a repaired state and not still an inconsistent state with known errors (as command repair states). Other filesystems move broken files to e.g. lost+found (expecting at least with a switch)

Ciao,

Gerhard

#5 lore raw
Quoted 11 lines · Gerhard Wiesinger
Quoted 10 lines · Qu Wenruo
Quoted 1 line · Gerhard Wiesinger

On 02.09.2024 00:13, Qu Wenruo wrote:

[...]

Quoted 4 lines
Quoted 2 lines

So it means 60 metadata csum mismatch is fixed, only 145 bad data sectors.

How to get rid of the 145 uncorrectables?

Quoted 7 lines
Quoted 3 lines

Those are all data, at least for my case.

It will not be repairable, just as you mentioned, data loss is expected.

Yes, but I'm expecting a repaired state and not still an inconsistent state with known errors (as command repair states). Other filesystems move broken files to e.g. lost+found (expecting at least with a switch).

Even btrfs moves those corrupted files, as long as those data sectors are still referred by any file, btrfs scrub will always report them as corrupted.

The only way to get rid of the report is to delete the files, which is not something that should be done by default at least.

Considering moving the files makes no difference, btrfs check/scrub won't touch those files, but only inform the end users (with the filename), and let them to determine what to do next.

Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines

And after the above scrub, btrfs check reports no more error (at least for metadata):

Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: c2be653b-6b00-4ed9-925f-258cc7ca5391
[1/7] checking root items
[2/7] checking extents
[3/7] checking free space tree
[4/7] checking fs roots
[5/7] checking only csums items (without verifying data) [6/7] checking root refs
[7/7] checking quota groups skipped (not enabled on this FS) found 3776757760 bytes used, no error found
total csum bytes: 1692008
total tree bytes: 82444288
total fs tree bytes: 71237632
total extent tree bytes: 6455296
btree space waste bytes: 43819992
file data blocks allocated: 29158084608
 referenced 4905811968

Quoted 4 lines

Any ideas how to repair and what can be done to get it into a consistent
state?

Please give the original "btrfs check" output.

I think your original fs is either using SINGLE metadata profile (then very hard to do repair), AND you're using incorrect way to repair (scrub
first, then btrfs check to verify, never --init-extent-tree nor --init-csum-tree unless you know what you're really doing).

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I originally did scrub first (forget to mention, but there were still uncorrectable errors as with my test script)

Aren't you expect data corruption?

Yes, but I'm expecting a repaired state and not still an inconsistent state with known errors (as command repair states). Other filesystems move broken files to e.g. lost+found (expecting at least with a switch)

As explained, that's impossible by just moving them to lost+found.

Btrfs is really in its own realm, on how to properly fix data corruption.

Our choice so far is to let end users to do the work. Either deleting the file, or find a backup to restore the file.

And repeating your sentence won't really make it any more persuading.

Thanks,
Qu

Quoted 1 line
#6 lore raw
In reply to Gerhard Wiesinger
Quoted 1 line · Gerhard Wiesinger

On 02.09.2024 07:43, Qu Wenruo wrote:

[...]

Quoted 3 lines

Send you full log file in a private E-mail.

The filenames is "after-repair". Any check before any repair attempt?

Still looks like one or more bad tree blocks.

Thanks,
Qu