xref: /freebsd/tests/sys/cddl/zfs/tests/zfsd/zfsd.kshlib (revision d0b2dbfa)
1#!/usr/local/bin/ksh93 -p
2# vim: filetype=sh
3#
4# CDDL HEADER START
5#
6# The contents of this file are subject to the terms of the
7# Common Development and Distribution License (the "License").
8# You may not use this file except in compliance with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23
24#
25# Copyright 2013 Spectra Logic.  All rights reserved.
26# Use is subject to license terms.
27#
28
29# Common routines used by multiple zfsd tests
30
31function wait_for_pool_dev_state_change
32{
33	typeset -i timeout=$1
34	typeset disk=$2
35	typeset state=$3
36	typeset pool=$4
37
38	if [ -z "$pool" ]; then
39		pool=$TESTPOOL
40	fi
41
42	log_note "Waiting up to $timeout seconds for $disk to become $state ..."
43	for ((; $timeout > 0; timeout=$timeout-1)); do
44		check_state $pool "$disk" "$state"
45		[ $? -eq 0 ] && return
46		$SLEEP 1
47	done
48	log_must $ZPOOL status $pool
49	log_fail "ERROR: Disk $disk not marked as $state in $pool"
50}
51
52function wait_for_pool_removal
53{
54	typeset -i timeout=$1
55	wait_for_pool_dev_state_change $timeout $REMOVAL_DISK "REMOVED|UNAVAIL"
56}
57
58function wait_until_scrubbed
59{
60	typeset pool=$1
61
62	while is_pool_scrubbing $pool; do
63		log_note "$pool still scrubbing..."
64		$SLEEP 1
65	done
66}
67
68function corrupt_pool_vdev
69{
70	typeset pool=$1
71	typeset vdev=$2
72	typeset file=$3
73	typeset -li start=0
74	typeset -li now=0
75	typeset -li timeout=60
76
77	# do some IO on the pool
78	log_must $DD if=/dev/zero of=$file bs=1024k count=64
79	$FSYNC $file
80
81	# ZFS rate limits checksum errors to about 20 per second.  So in order
82	# to ensure that we reach zfsd's threshold, we must alternately
83	# scribble and scrub.
84	while (( "$now" - "$start" < "$timeout" )); do
85		# scribble on the underlying file to corrupt the vdev
86		log_must $DD if=/dev/urandom of=$vdev bs=1024k count=64 conv=notrunc
87
88		# Scrub the pool to detect and repair the corruption
89		log_must $ZPOOL scrub $pool
90		wait_until_scrubbed $pool
91		now=`date +%s`
92		if [ "$start" -eq 0 ]; then
93			start=`date +%s`
94		fi
95		check_state "$pool" "$vdev" DEGRADED && return
96		$SLEEP 1
97	done
98
99	log_must $ZPOOL status "$pool"
100	log_fail "ERROR: Disk $vdev not marked as DEGRADED in $pool"
101}
102