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 2013 Spectra Logic.  All rights reserved.
25# Use is subject to license terms.
26#
27#
28. $STF_SUITE/tests/hotspare/hotspare.kshlib
29
30################################################################################
31#
32# __stc_assertion_start
33#
34# ID: hotspare_scrub_002_pos
35#
36# DESCRIPTION:
37#   'zpool scrub will scan spares as well as original devices'
38#
39# STRATEGY:
40#	1. Create a storage pool
41#	2. Add hot spare devices to the pool
42#       4. Replace one of the original devices with a spare
43#       5. Simulate errors on the spare
44#       6. Scrub the pool
45#       7. Verify that scrub detected the simulated errors
46#
47# TESTABILITY: explicit
48#
49# TEST_AUTOMATION_LEVEL: automated
50#
51# CODING STATUS: COMPLETED (2013-01-14)
52#
53# __stc_assertion_end
54#
55###############################################################################
56
57verify_runnable "global"
58
59function cleanup
60{
61	poolexists $TESTPOOL && destroy_pool $TESTPOOL
62	partition_cleanup
63}
64
65# Returns the number of checksums errors detected on the given vdev
66function get_cksum #pool, vdev
67{
68	typeset pool=$1
69	typeset vdev=$2
70	$ZPOOL status $pool | awk -v vdev=$vdev '$1~vdev {print $5; exit}'
71}
72
73function verify_assertion # odev
74{
75	typeset odev=$1
76
77	log_must $ZPOOL replace $TESTPOOL $odev $sdev
78	log_must check_state $TESTPOOL "$sdev" "INUSE"
79
80	# corrupt out the $TESTPOOL to make sdev in use
81	# Skip the first input block so we don't overwrite the vdev label
82	log_must $DD if=/dev/zero bs=1024k count=63 oseek=1 conv=notrunc of=$sdev
83
84	$SYNC
85	# The pool may already have started scrubbing, so don't assert this.
86	# Expected postconditions are checked below anyway.
87	$ZPOOL scrub $TESTPOOL
88	while is_pool_scrubbing $TESTPOOL ; do
89		$SLEEP 2
90	done
91
92	# Verify that scrub detected the errors
93	# Some vdevs (ie raidz1) will display the errors on the spare-0 line
94	# instead of on the basic vdev line
95	[[ $(get_cksum $TESTPOOL $sdev) > 0 ]]
96	sdev_errors=$?
97	[[ $(get_cksum $TESTPOOL "spare-0") > 0 ]]
98	spare0_errors=$?
99	log_must [ $sdev_errors -o $spare0_errors ]
100
101	# Now clear the old errors, remove the original device and scrub again.
102	# No new errors should be found, because the scrub should've found and
103	# fixed all errors
104	log_must $ZPOOL clear $TESTPOOL
105	log_must $ZPOOL detach $TESTPOOL $odev
106	$ZPOOL scrub $TESTPOOL
107	while is_pool_scrubbing $TESTPOOL ; do
108		$SLEEP 2
109	done
110	if [ $(get_cksum $TESTPOOL $sdev) -ne 0 ]; then
111		log_fail "ERROR: Scrub missed cksum errors on a spare vdev"
112	fi
113}
114
115log_assert "'zpool scrub' scans spare vdevs"
116
117log_onexit cleanup
118
119set_devs
120typeset odev="${devarray[3]}"
121typeset sdev="${devarray[0]}"
122
123# Don't test striped pools because they can't have spares
124set -A keywords "mirror" "raidz" "raidz2"
125for keyword in "${keywords[@]}" ; do
126	setup_hotspares "$keyword"
127
128	iterate_over_hotspares verify_assertion $odev
129
130	destroy_pool "$TESTPOOL"
131done
132
133log_pass "'zpool scrub scans spare vdevs'"
134