#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2026 Christoph Hellwig
#
# FS QA Test No. 804
#
# Test that T10 DIF detects bitflips and misplaced writes.
#
. ./common/preamble
_begin_fstest auto rw pi

_cleanup()
{
	cd /
	rm -r -f $tmp.*
	[ -d "$SCSI_DEBUG_MNT" ] && _unmount $SCSI_DEBUG_MNT 2>/dev/null
	_put_scsi_debug_dev
}

. ./common/scsi_debug
. ./common/filter

_require_debugfs
_require_scsi_debug
# If TEST_DEV is block device, make sure current fs is a localfs which can be
# written on scsi_debug device
_require_test
_require_block_device $TEST_DEV
_require_xfs_io_command fiemap
_require_odirect

# FIEMAP on Btrfs returns logical addresses within the filesystem's address
# space, not physical device offsets. Writing to these offsets on $SCRATCH_DEV
# would corrupt the filesystem in multi-device setups.
_exclude_fs btrfs

size=$(_small_fs_size_mb 256)

test_dif()
{
	local pi_type=$1
	local last=$2
	local bit_errors=$3
	local reftag_adjust=$4

	echo -n "Testing DIF type $pi_type "
	if [ "$bit_errors" -gt "0" ]; then
		echo -n "bit errors ($bit_errors) in "
	fi
	if [ "$reftag_adjust" -ne "0" ]; then
		echo -n "reftag adjustment by $reftag_adjust in "
	fi
	if [ "$last" -eq 1 ]; then
		echo "last sector"
	else
		echo "first sector"
	fi

	scsi_debug_pi_opts="write_same_length=0 dif=${pi_type} dix=1"
	SCSI_DEBUG_DEV=`_get_scsi_debug_dev 512 512 0 $size $scsi_debug_pi_opts`

	local integrity_path="$(_sysfs_block_integrity_path $SCSI_DEBUG_DEV)"
	if [ ! -d "$integrity_path" ]; then
		_notrun "DIF not supported by running kernel"
	fi

	SCSI_DEBUG_MNT="$TEST_DIR/scsi_debug_$seq"
	rm -rf $SCSI_DEBUG_MNT
	mkdir $SCSI_DEBUG_MNT

	_mkfs_dev $SCSI_DEBUG_DEV || \
		_fail "Can't make $FSTYP on DIF-enabled scsi_debug device"
	run_check _mount $SCSI_DEBUG_DEV $SCSI_DEBUG_MNT

	local blksz=$(_get_block_size $SCSI_DEBUG_MNT)
	local testfile="$SCSI_DEBUG_MNT/test"

	$XFS_IO_PROG -d -f \
		-c "pwrite -S 0x66 0 $((128 * blksz))" \
		-c fsync \
		$testfile >> $seqres.full 2>&1

	local phys_sector=$(_get_file_extent_sector $testfile 0)
	if [ -z "$phys_sector" ]; then
		_fail "$test_name: failed to get physical block offset via fiemap"
	fi

	local dev=$(_short_dev $SCSI_DEBUG_DEV)
	local debugfs_dir=$(ls /sys/block/$dev/device/scsi_device/)
	local debugfs_file="/sys/kernel/debug/scsi_debug/$debugfs_dir/corrupt"

	if [ ! -f "$debugfs_file" ]; then
		_notrun "scsi_debug corruption injection not supported"
	fi

	if [ "$last" -eq 1 ]; then
		echo "adding to $phys_sector" >> $seqres.full
		phys_sector=$((phys_sector + (128 * blksz / 512) - 1))
	fi

	echo "lba=$phys_sector,num=1,bit_errors=$bit_errors,reftag_adjust=$reftag_adjust" \
		 > $debugfs_file

	$XFS_IO_PROG -d -f \
		-c "pread 0 $((128 * blksz))" \
		$testfile

	[ -d "$SCSI_DEBUG_MNT" ] && _unmount $SCSI_DEBUG_MNT 2>/dev/null
	_put_scsi_debug_dev
}

# test data corruption in first block
test_dif 1 0 2 0
test_dif 2 0 2 0
test_dif 3 0 2 0

# test data corruption in last block
test_dif 1 1 2 0
test_dif 2 1 2 0
test_dif 3 1 2 0


# Note: Type 3 doesn't have a reftag, so don't test it

# reftag mismatch in first block
test_dif 1 0 0 8
test_dif 2 0 0 8

# reftag mismatch in last block
test_dif 1 1 0 8
test_dif 2 1 0 8

_exit 0
