#! /bin/bash
# FS QA Test No. btrfs/101
#
# Test device delete when the source device has EIO
#
# Copyright (c) 2015 Oracle.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

seq=`basename $0`
seqres=$RESULT_DIR/$seq
echo "QA output created by $seq"

here=`pwd`
tmp=/tmp/$$

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15


_cleanup()
{
	_dmerror_cleanup
	rm -f $tmp.*
}

# get standard environment, filters and checks
. ./common/rc
. ./common/filter
. ./common/filter.btrfs
. ./common/dmerror

_supported_fs btrfs
_supported_os Linux
_require_scratch_dev_pool 3
_require_btrfs_dev_del_by_devid
_require_dm_target error

rm -f $seqres.full

dev1="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $2}'`"
dev2="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $3}'`"

_dmerror_init
_mkfs_dev -f -d raid1 -m raid1 $dev1 $dev2 $DMERROR_DEV
_dmerror_mount

_run_btrfs_util_prog filesystem show -m $SCRATCH_MNT
$BTRFS_UTIL_PROG filesystem show -m $SCRATCH_MNT | _filter_btrfs_filesystem_show

error_devid=`$BTRFS_UTIL_PROG filesystem show -m $SCRATCH_MNT |\
			egrep $DMERROR_DEV | $AWK_PROG '{print $2}'`

snapshot_cmd="$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT"
snapshot_cmd="$snapshot_cmd $SCRATCH_MNT/snap_\`date +'%H_%M_%S_%N'\`"
run_check $FSSTRESS_PROG -d $SCRATCH_MNT -n 200 -p 8 $FSSTRESS_AVOID -x \
							"$snapshot_cmd" -X 50

# now load the error into the DMERROR_DEV
_dmerror_load_error_table

_run_btrfs_util_prog device delete $error_devid $SCRATCH_MNT

_run_btrfs_util_prog filesystem show -m $SCRATCH_MNT
$BTRFS_UTIL_PROG filesystem show -m $SCRATCH_MNT | _filter_btrfs_filesystem_show

echo "=== device delete completed"

status=0; exit
