1#!/usr/local/bin/ksh93 -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# Copyright 2012,2013 Spectra Logic Corporation.  All rights reserved.
28# Use is subject to license terms.
29#
30# Portions taken from:
31# ident	"@(#)replacement_001_pos.ksh	1.4	08/02/27 SMI"
32#
33# $FreeBSD$
34
35. $STF_SUITE/include/libtest.kshlib
36. $STF_SUITE/include/libgnop.kshlib
37. $STF_SUITE/tests/hotspare/hotspare.kshlib
38. $STF_SUITE/tests/zfsd/zfsd.kshlib
39
40function cleanup
41{
42	destroy_pool $TESTPOOL
43	[[ -e $TESTDIR ]] && log_must $RM -rf $TESTDIR/*
44	for md in $MD0 $MD1 $MD2 $MD3; do
45		gnop destroy -f $md
46		for ((i=0; i<5; i=i+1)); do
47			$MDCONFIG -d -u $md && break
48			$SLEEP 1
49		done
50	done
51}
52
53log_assert "ZFSD will correctly replace disks that disappear and reappear \
54	   with different devnames"
55
56# Outline
57# Use gnop on top of file-backed md devices
58# * file-backed md devices so we can destroy them and recreate them with
59#   different devnames
60# * gnop so we can destroy them while still in use
61# Create a double-parity pool
62# Remove two vdevs
63# Destroy the md devices and recreate in the opposite order
64# Check that the md's devnames have swapped
65# Verify that the pool regains its health
66
67log_onexit cleanup
68ensure_zfsd_running
69
70
71N_DEVARRAY_FILES=4
72set_devs
73typeset FILE0="${devarray[0]}"
74typeset FILE1="${devarray[1]}"
75typeset FILE2="${devarray[2]}"
76typeset FILE3="${devarray[3]}"
77typeset MD0=`$MDCONFIG -a -t vnode -f ${FILE0}`
78[ $? -eq 0 ] || atf_fail "Failed to create md device"
79typeset MD1=`$MDCONFIG -a -t vnode -f ${FILE1}`
80[ $? -eq 0 ] || atf_fail "Failed to create md device"
81typeset MD2=`$MDCONFIG -a -t vnode -f ${FILE2}`
82[ $? -eq 0 ] || atf_fail "Failed to create md device"
83typeset MD3=`$MDCONFIG -a -t vnode -f ${FILE3}`
84[ $? -eq 0 ] || atf_fail "Failed to create md device"
85log_must create_gnops $MD0 $MD1 $MD2 $MD3
86
87for type in "raidz2" "mirror"; do
88	# Create a pool on the supplied disks
89	create_pool $TESTPOOL $type ${MD0}.nop ${MD1}.nop ${MD2}.nop ${MD3}.nop
90
91	log_must destroy_gnop $MD0
92	for ((i=0; i<5; i=i+1)); do
93		$MDCONFIG -d -u $MD0 && break
94		$SLEEP 1
95	done
96	[ -c /dev/$MD0.nop ] && atf_fail "failed to destroy $MD0"
97	log_must destroy_gnop $MD1
98	for ((i=0; i<5; i=i+1)); do
99		$MDCONFIG -d -u $MD1 && break
100		$SLEEP 1
101	done
102	[ -c /dev/$MD1.nop ] && atf_fail "failed to destroy $MD0"
103
104	# Make sure that the pool is degraded
105	$ZPOOL status $TESTPOOL |grep "state:" |grep DEGRADED > /dev/null
106	if [ $? != 0 ]; then
107		log_fail "Pool $TESTPOOL not listed as DEGRADED"
108	fi
109
110	# Do some I/O to ensure that the old vdevs will be out of date
111	log_must $DD if=/dev/random of=/$TESTPOOL/randfile bs=1m count=1
112	log_must $SYNC
113
114	# Recreate the vdevs in the opposite order
115	typeset MD0=`$MDCONFIG -a -t vnode -f ${FILE1}`
116	[ $? -eq 0 ] || atf_fail "Failed to create md device"
117	typeset MD1=`$MDCONFIG -a -t vnode -f ${FILE0}`
118	[ $? -eq 0 ] || atf_fail "Failed to create md device"
119	log_must create_gnops $MD0 $MD1
120
121	wait_until_resilvered
122	destroy_pool $TESTPOOL
123done
124
125log_pass
126