[PATCH v3 2/2] btrfs: Allow error injection on btrfs_data_csum_ok

From: Song Chen

Date: Mon Oct 12 2026 - 09:39:55 EST


btrfs_data_csum_ok validates the checksum of a data block
after a read I/O completes. A mismatch between the on-disk
checksum and the one computed from the read data indicates
silent data corruption, which can be caused by bit flips due
to DRAM errors, storage media degradation, or bus transmission
faults.

Testing the error handling path that responds to such
corruption, normally requires reproducing the hardware
fault, which is not always feasible.

Mark btrfs_data_csum_ok with ALLOW_ERROR_INJECTION so that
the fail_function infrastructure can override its return value
to false, simulating a checksum mismatch without requiring
actual data corruption. This enables validation of the full
error detection and reporting path in a controlled environment.

Usage:
cd /sys/kernel/debug/fail_function
echo btrfs_data_csum_ok > inject
echo 0 > btrfs_data_csum_ok/retval
echo 100 > probability
echo N > times

Acked-by: David Sterba <dsterba@xxxxxxxx>
Signed-off-by: Song Chen <chensong_2000@xxxxxxx>
---
fs/btrfs/inode.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 93ef3cec191e..73c801197934 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -3586,6 +3586,7 @@ bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
memzero_page(phys_to_page(paddrs[i]), offset_in_page(paddrs[i]), step);
return false;
}
+ALLOW_ERROR_INJECTION(btrfs_data_csum_ok, FALSE);

/*
* Perform a delayed iput on @inode.
--
2.43.0