xref: /freebsd/tests/sys/cddl/zfs/tests/slog/slog.kshlib (revision 61e21613)
1# vim: filetype=sh
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. $STF_SUITE/include/libtest.kshlib
28
29function cleanup
30{
31	poolexists $TESTPOOL && log_must $ZPOOL status $TESTPOOL
32	poolexists $TESTPOOL2 && log_must $ZPOOL status $TESTPOOL2
33	destroy_pool $TESTPOOL
34	destroy_pool $TESTPOOL2
35}
36
37#
38# Try zpool status/iostat for given pool
39#
40# $1 pool
41#
42function display_status
43{
44	typeset pool=$1
45
46	typeset -i ret=0
47	$ZPOOL status -xv $pool > /dev/null 2>&1
48	ret=$?
49
50	$ZPOOL iostat > /dev/null 2>&1
51	((ret |= $?))
52
53	typeset mntpnt=$(get_prop mountpoint $pool)
54	$DD if=/dev/random of=$mntpnt/testfile.${TESTCASE_ID} &
55	typeset pid=$!
56
57	$ZPOOL iostat -v 1 3 > /dev/null
58	((ret |= $?))
59
60	kill -9 $pid
61
62	return $ret
63}
64
65function slog_devstat_table
66{
67	typeset pool=$1
68
69	$ZPOOL status -v $pool | $NAWK '
70		BEGIN { start = 0; }
71		/\tlogs/ { start = 1; }
72		(start == 0) { next; }
73		/\t  (\/|[a-zA-Z])/ { print "stripe:" $1 " " $2; }
74		/\t    (\/|[a-zA-Z])/ { print "mirror:" $1 " " $2; }
75		/\t  (\/|[0-9])/ {print "stripe:" $NF " " $2}
76		/\t    (\/|[0-9])/ {print "mirror:" $NF " " $2}
77		# When a hotspare is replacing
78		/\t      (\/|[a-zA-Z])/ {print "mirror:" $1 " " $2}
79	'
80}
81
82#
83# Verify the given slog device have correct type and status
84#
85# $1 pool name
86# $2 device name
87# $3 device status
88# $4 device type
89#
90function verify_slog_device
91{
92	typeset pool=$1
93	typeset device=$2
94	typeset status=$3
95	typeset type=$4
96
97	if [[ -z $pool || -z $device || -z $status ]]; then
98		log_fail "Usage: verify_slog_device <pool> <device> " \
99			"<status> [type]"
100	fi
101
102	if [[ $WRAPPER == *"smi"* ]]; then
103		$ECHO $device | $EGREP "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
104		if (( $? == 0 )); then
105			device=${device}s2
106		fi
107	fi
108
109	#
110	# Get all the slog devices and status table like below
111	#
112	# mirror:/disks/d ONLINE mirror:/disks/e ONLINE stripe:/disks/f ONLINE
113	#
114	set -A dev_stat_tab $(slog_devstat_table $pool)
115
116	typeset find=0
117	for (( i = 0; i < ${#dev_stat_tab[@]}; i += 2 )); do
118		typeset dev=${dev_stat_tab[$i]}
119		typeset stat=${dev_stat_tab[((i+1))]}
120
121		typeset statmsg="$dev: Status($stat) != Expected stat($status)"
122		case $dev in
123			stripe:$device)
124				if [[ "$type" == 'mirror' ]]; then
125					log_note "Unexpected type: mirror"
126					return 1
127				fi
128				if [[ $stat != $status ]]; then
129					log_note statmsg
130					return 1
131				fi
132				return 0
133				;;
134			mirror:$device)
135				if [[ -z "$type" || $type == 'stripe' ]]; then
136					log_note "Unexpected type: stripe"
137					return 1
138				fi
139				if [[ $stat != $status ]]; then
140					log_note statmsg
141					return 1
142				fi
143				return 0
144				;;
145		esac
146	done
147	return 1
148}
149
150# Calls <callback> [args...] <pooltype> <sparetype>.
151function slog_foreach_nologtype # <callback>
152{
153	typeset callback="$1"
154
155	for pooltype in "" "mirror" "raidz" "raidz2"; do
156		for sparetype in "" "spare"; do
157			$callback "$pooltype" "$sparetype"
158		done
159	done
160}
161
162# Calls <callback> [args...] <pooltype> <sparetype> <logtype>.
163# Unfortunately, this has to be duplicated because some arguments are empty,
164# so if they aren't explicitly forwarded they aren't arguments to $callback.
165function slog_foreach_all # <callback>
166{
167	typeset callback="$1"
168
169	for pooltype in "" "mirror" "raidz" "raidz2"; do
170		for sparetype in "" "spare"; do
171			for logtype in "" "mirror"; do
172				$callback "$pooltype" "$sparetype" "$logtype"
173			done
174		done
175	done
176}
177