1#!/bin/ksh -p
2
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2016 by Delphix. All rights reserved.
16#
17
18. $STF_SUITE/tests/functional/cli_root/zpool_import/zpool_import.kshlib
19
20#
21# DESCRIPTION:
22#	A pool should be importable using an outdated cachefile that is unaware
23#	of a zpool replace operation at different stages in time.
24#
25# STRATEGY:
26#	1. Create a pool with some devices and an alternate cachefile.
27#	2. Backup the cachefile.
28#	3. Initiate device replacement, backup cachefile again and export pool.
29#	   Special care must be taken so that resilvering doesn't complete
30#	   before we exported the pool.
31#	4. Verify that we can import the pool using the first cachefile backup.
32#	   (Test 1. cachefile: pre-replace, pool: resilvering)
33#	5. Wait for the resilvering to finish and export the pool.
34#	6. Verify that we can import the pool using the first cachefile backup.
35#	   (Test 2. cachefile: pre-replace, pool: post-replace)
36#	7. Export the pool.
37#	8. Verify that we can import the pool using the second cachefile backup.
38#	   (Test 3. cachefile: resilvering, pool: post-replace)
39#
40# STRATEGY TO SLOW DOWN RESILVERING:
41#	1. Reduce zfs_txg_timeout, which controls how long can we resilver for
42#	   each sync.
43#	2. Add data to pool
44#	3. Re-import the pool so that data isn't cached
45#	4. Use zfs_scan_suspend_progress to ensure resilvers don't progress
46#	5. Trigger the resilvering
47#	6. Use spa freeze to stop writing to the pool.
48#	7. Re-enable scan progress
49#	8. Export the pool
50#
51
52verify_runnable "global"
53
54ZFS_TXG_TIMEOUT=""
55
56function custom_cleanup
57{
58	# Revert zfs_txg_timeout to defaults
59	[[ -n ZFS_TXG_TIMEOUT ]] &&
60	    log_must set_zfs_txg_timeout $ZFS_TXG_TIMEOUT
61
62	log_must set_tunable32 SCAN_SUSPEND_PROGRESS 0
63	cleanup
64}
65
66log_onexit custom_cleanup
67
68function test_replacing_vdevs
69{
70	typeset poolcreate="$1"
71	typeset replacevdev="$2"
72	typeset replaceby="$3"
73	typeset poolfinalstate="$4"
74	typeset zinjectdevices="$5"
75	typeset earlyremove="$6"
76	typeset writedata="$7"
77
78	log_note "$0: pool '$poolcreate', replace $replacevdev by $replaceby."
79
80	log_must zpool create -o cachefile=$CPATH $TESTPOOL1 $poolcreate
81
82	# Cachefile: pool in pre-replace state
83	log_must cp $CPATH $CPATHBKP
84
85	# Steps to insure resilvering happens very slowly.
86	log_must write_some_data $TESTPOOL1 $writedata
87	log_must zpool export $TESTPOOL1
88	log_must cp $CPATHBKP $CPATH
89	log_must zpool import -c $CPATH -o cachefile=$CPATH $TESTPOOL1
90	log_must set_tunable32 SCAN_SUSPEND_PROGRESS 1
91	log_must zpool replace $TESTPOOL1 $replacevdev $replaceby
92
93	# Cachefile: pool in resilvering state
94	log_must cp $CPATH $CPATHBKP2
95
96	# Confirm pool is still replacing
97	log_must pool_is_replacing $TESTPOOL1
98	log_must zpool export $TESTPOOL1
99	log_must set_tunable32 SCAN_SUSPEND_PROGRESS 0
100
101	( $earlyremove ) && log_must rm $replacevdev
102
103	############################################################
104	# Test 1. Cachefile: pre-replace, pool: resilvering
105	############################################################
106	log_must cp $CPATHBKP $CPATH
107	log_must zpool import -c $CPATH $TESTPOOL1
108
109	# Wait for resilvering to finish
110	log_must wait_for_pool_config $TESTPOOL1 "$poolfinalstate"
111	log_must zpool export $TESTPOOL1
112
113	( ! $earlyremove ) && log_must rm $replacevdev
114
115	############################################################
116	# Test 2. Cachefile: pre-replace, pool: post-replace
117	############################################################
118	log_must zpool import -c $CPATHBKP $TESTPOOL1
119	log_must check_pool_config $TESTPOOL1 "$poolfinalstate"
120	log_must zpool export $TESTPOOL1
121
122	############################################################
123	# Test 3. Cachefile: resilvering, pool: post-replace
124	############################################################
125	log_must zpool import -c $CPATHBKP2 $TESTPOOL1
126	log_must check_pool_config $TESTPOOL1 "$poolfinalstate"
127
128	# Cleanup
129	log_must zpool destroy $TESTPOOL1
130	log_must rm -f $CPATH $CPATHBKP $CPATHBKP2
131	log_must mkfile $FILE_SIZE $replacevdev
132
133	log_note ""
134}
135
136# We set zfs_txg_timeout to 1 to reduce resilvering time at each sync.
137ZFS_TXG_TIMEOUT=$(get_zfs_txg_timeout)
138set_zfs_txg_timeout 1
139
140test_replacing_vdevs "$VDEV0 $VDEV1" \
141    "$VDEV1" "$VDEV2" \
142    "$VDEV0 $VDEV2" \
143    "$VDEV0 $VDEV1" \
144    false 20
145
146test_replacing_vdevs "mirror $VDEV0 $VDEV1" \
147	"$VDEV1" "$VDEV2" \
148	"mirror $VDEV0 $VDEV2" \
149	"$VDEV0 $VDEV1" \
150	true 10
151
152test_replacing_vdevs "raidz $VDEV0 $VDEV1 $VDEV2" \
153	"$VDEV1" "$VDEV3" \
154	"raidz $VDEV0 $VDEV3 $VDEV2" \
155	"$VDEV0 $VDEV1 $VDEV2" \
156	true 20
157
158test_replacing_vdevs "draid:1s $VDEV0 $VDEV1 $VDEV2 $VDEV3 $VDEV4" \
159	"$VDEV1" "$VDEV5" \
160	"draid $VDEV0 $VDEV5 $VDEV2 $VDEV3 $VDEV4 spares draid1-0-0" \
161	"$VDEV0 $VDEV1 $VDEV2 $VDEV3 $VDEV4" \
162	true 30
163
164set_zfs_txg_timeout $ZFS_TXG_TIMEOUT
165
166log_pass "zpool import -c cachefile_unaware_of_replace passed."
167