xref: /freebsd/tests/sys/cddl/zfs/tests/zfsd/zfsd.kshlib (revision d411c1d6)
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# $FreeBSD$
25
26#
27# Copyright 2013 Spectra Logic.  All rights reserved.
28# Use is subject to license terms.
29#
30
31# Common routines used by multiple zfsd tests
32
33function wait_for_pool_dev_state_change
34{
35	typeset -i timeout=$1
36	typeset disk=$2
37	typeset state=$3
38	typeset pool=$4
39
40	if [ -z "$pool" ]; then
41		pool=$TESTPOOL
42	fi
43
44	log_note "Waiting up to $timeout seconds for $disk to become $state ..."
45	for ((; $timeout > 0; timeout=$timeout-1)); do
46		check_state $pool "$disk" "$state"
47		[ $? -eq 0 ] && return
48		$SLEEP 1
49	done
50	log_must $ZPOOL status $pool
51	log_fail "ERROR: Disk $disk not marked as $state in $pool"
52}
53
54function wait_for_pool_removal
55{
56	typeset -i timeout=$1
57	wait_for_pool_dev_state_change $timeout $REMOVAL_DISK "REMOVED|UNAVAIL"
58}
59
60function wait_until_scrubbed
61{
62	typeset pool=$1
63
64	while is_pool_scrubbing $pool; do
65		log_note "$pool still scrubbing..."
66		$SLEEP 1
67	done
68}
69
70function corrupt_pool_vdev
71{
72	typeset pool=$1
73	typeset vdev=$2
74	typeset file=$3
75
76	# do some IO on the pool
77	log_must $DD if=/dev/zero of=$file bs=1024k count=64
78	$FSYNC $file
79
80	# scribble on the underlying file to corrupt the vdev
81	log_must $DD if=/dev/urandom of=$vdev bs=1024k count=64 conv=notrunc
82
83	# Scrub the pool to detect the corruption
84	log_must $ZPOOL scrub $pool
85	wait_until_scrubbed $pool
86
87	# ZFSD can take up to 60 seconds to degrade an array in response to
88	# errors (though it's usually faster).
89	wait_for_pool_dev_state_change 60 $vdev DEGRADED
90}
91