1#! /bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2014, 2017 by Delphix. All rights reserved.
19# Copyright (c) 2018 by Lawrence Livermore National Security, LLC.
20#
21
22. $STF_SUITE/include/libtest.shlib
23. $STF_SUITE/tests/functional/removal/removal.kshlib
24
25#
26# DESCRIPTION:
27#
28# This test ensures the device removal is cancelled when hard IO
29# errors are encountered during the removal process.  This is done
30# to ensure that when removing a device all of the data is copied.
31#
32# STRATEGY:
33#
34# 1. We create a pool with enough redundancy such that IO errors
35#    will not result in the pool being suspended.
36# 2. We write some test data to the pool.
37# 3. We inject READ errors in to one half of the top-level mirror-0
38#    vdev which is being removed.  Then we start the removal process.
39# 4. Verify that the injected read errors cause the removal of
40#    mirror-0 to be cancelled and that mirror-0 has not been removed.
41# 5. Clear the read fault injection.
42# 6. Repeat steps 3-6 above except inject WRITE errors on one of
43#    child vdevs in the destination mirror-1.
44# 7. Lastly verify the pool data is still intact.
45#
46
47TMPDIR=${TMPDIR:-$TEST_BASE_DIR}
48DISK0=$TMPDIR/dsk0
49DISK1=$TMPDIR/dsk1
50DISK2=$TMPDIR/dsk2
51DISK3=$TMPDIR/dsk3
52
53log_must truncate -s $MINVDEVSIZE $DISK0 $DISK1
54log_must truncate -s $((MINVDEVSIZE * 4)) $DISK2 $DISK3
55
56function cleanup
57{
58	log_must zinject -c all
59	default_cleanup_noexit
60	log_must rm -f $DISK0 $DISK1 $DISK2 $DISK3
61}
62
63function wait_for_removing_cancel
64{
65	typeset pool=$1
66
67	log_must zpool wait -t remove $pool
68
69	#
70	# The pool state changes before the TXG finishes syncing; wait for
71	# the removal to be completed on disk.
72	#
73	sync_pool
74
75	log_mustnot is_pool_removed $pool
76	return 0
77}
78
79default_setup_noexit "mirror $DISK0 $DISK1 mirror $DISK2 $DISK3"
80log_onexit cleanup
81log_must zfs set compression=off $TESTPOOL
82
83FILE_CONTENTS="Leeloo Dallas mul-ti-pass."
84
85echo $FILE_CONTENTS  >$TESTDIR/$TESTFILE0
86log_must [ "x$(<$TESTDIR/$TESTFILE0)" = "x$FILE_CONTENTS" ]
87log_must file_write -o create -f $TESTDIR/$TESTFILE1 -b $((2**20)) -c $((2**8))
88
89# Flush the ARC to minimize cache effects.
90log_must zpool export $TESTPOOL
91log_must zpool import -d $TMPDIR $TESTPOOL
92
93# Verify that unexpected read errors automatically cancel the removal.
94log_must zinject -d $DISK0 -e io -T all -f 100 $TESTPOOL
95log_must zpool remove $TESTPOOL mirror-0
96log_must wait_for_removing_cancel $TESTPOOL
97log_must vdevs_in_pool $TESTPOOL mirror-0
98log_must zinject -c all
99
100# Flush the ARC to minimize cache effects.
101log_must zpool export $TESTPOOL
102log_must zpool import -d $TMPDIR $TESTPOOL
103
104# Verify that unexpected write errors automatically cancel the removal.
105log_must zinject -d $DISK3 -e io -T all -f 100 $TESTPOOL
106log_must zpool remove $TESTPOOL mirror-0
107log_must wait_for_removing_cancel $TESTPOOL
108log_must vdevs_in_pool $TESTPOOL mirror-0
109log_must zinject -c all
110
111log_must dd if=$TESTDIR/$TESTFILE0 of=/dev/null
112log_must [ "x$(<$TESTDIR/$TESTFILE0)" = "x$FILE_CONTENTS" ]
113log_must dd if=$TESTDIR/$TESTFILE1 of=/dev/null
114
115log_pass "Device not removed due to unexpected errors."
116