#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2017 Liu Bo.  All Rights Reserved.
#
# FS QA Test 140
#
# Regression test for btrfs DIO read's repair during read.
#
# Commit 2dabb3248453 ("Btrfs: Direct I/O read: Work on sectorsized blocks")
# introduced the regression.
# The upstream fix is
#	commit 2e949b0a5592 ("Btrfs: fix invalid dereference in btrfs_retry_endio")
#
. ./common/preamble
_begin_fstest auto quick

# Import common functions.
. ./common/filter

# real QA test starts here

# Modify as appropriate.
_supported_fs btrfs
_require_scratch_dev_pool 2

_require_btrfs_command inspect-internal dump-tree
_require_command "$FILEFRAG_PROG" filefrag
_require_odirect
# Overwriting data is forbidden on a zoned block device
_require_non_zoned_device "${SCRATCH_DEV}"

get_physical()
{
	local logical=$1
	local stripe=$2
	$BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
		grep $logical -A 6 | \
		$AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /$stripe/) { print \$6 }"
}

get_devid()
{
	local logical=$1
	local stripe=$2
	$BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
		grep $logical -A 6 | \
		$AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /$stripe/) { print \$4 }"
}

get_device_path()
{
	local devid=$1
	echo "$SCRATCH_DEV_POOL" | $AWK_PROG "{print \$$devid}"
}

_scratch_dev_pool_get 2
# step 1, create a raid1 btrfs which contains one 128k file.
echo "step 1......mkfs.btrfs" >>$seqres.full

mkfs_opts="-d raid1 -b 1G"
_scratch_pool_mkfs $mkfs_opts >>$seqres.full 2>&1

# -o nospace_cache makes sure data is written to the start position of the data
# chunk
_scratch_mount -o nospace_cache

$XFS_IO_PROG -f -d -c "pwrite -S 0xaa -b 128K 0 128K" "$SCRATCH_MNT/foobar" |\
	_filter_xfs_io_offset

# step 2, corrupt the first 64k of one copy (on SCRATCH_DEV which is the first
# one in $SCRATCH_DEV_POOL
echo "step 2......corrupt file extent" >>$seqres.full

${FILEFRAG_PROG} -v $SCRATCH_MNT/foobar >> $seqres.full
logical_in_btrfs=`${FILEFRAG_PROG} -v $SCRATCH_MNT/foobar | _filter_filefrag | cut -d '#' -f 1`
physical=$(get_physical ${logical_in_btrfs} 1)
devid=$(get_devid ${logical_in_btrfs} 1)
devpath=$(get_device_path ${devid})

_scratch_unmount

echo " corrupt stripe #1, devid $devid devpath $devpath physical $physical" \
	>> $seqres.full
$XFS_IO_PROG -d -c "pwrite -S 0xbb -b 64K $physical 64K" $devpath > /dev/null

_scratch_mount

# step 3, 128k dio read (this read can repair bad copy)
echo "step 3......repair the bad copy" >>$seqres.full

# since raid1 consists of two copies, and the bad copy was put on stripe #1
# while the good copy lies on stripe #0, the bad copy only gets access when the
# reader's pid % 2 == 1 is true
while true; do
	$XFS_IO_PROG -d -c "pread -b 128K 0 128K" "$SCRATCH_MNT/foobar" > /dev/null &
	pid=$!
	wait
	[ $((pid % 2)) == 1 ] && break
done

_scratch_unmount

# check if the repair works
$XFS_IO_PROG -d -c "pread -v -b 512 $physical 512" $devpath |\
	_filter_xfs_io_offset

_scratch_dev_pool_put
# success, all done
status=0
exit
