xref: /titanic_52/usr/src/test/zfs-tests/tests/functional/cache/cache.kshlib (revision 59596c01ca1b980a016d25670874f53e64c27ec0)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2013 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32
33function cleanup
34{
35	if datasetexists $TESTPOOL ; then
36		log_must $ZPOOL destroy -f $TESTPOOL
37	fi
38	if datasetexists $TESTPOOL2 ; then
39		log_must $ZPOOL destroy -f $TESTPOOL2
40	fi
41}
42
43#
44# Try zpool status/iostat for given pool
45#
46# $1 pool
47#
48function display_status
49{
50	typeset pool=$1
51
52	typeset -i ret=0
53	$ZPOOL status -xv $pool > /dev/null 2>&1
54	ret=$?
55
56	$ZPOOL iostat > /dev/null 2>&1
57	((ret |= $?))
58
59	typeset mntpnt=$(get_prop mountpoint $pool)
60	$DD if=/dev/random of=$mntpnt/testfile.$$ &
61	typeset pid=$!
62
63	$ZPOOL iostat -v 1 3 > /dev/null
64	((ret |= $?))
65
66	kill -9 $pid
67
68	return $ret
69}
70
71#
72# Verify the given cache device have correct type and status
73#
74# $1 pool name
75# $2 device name
76# $3 device status
77# $4 device type
78#
79function verify_cache_device
80{
81	typeset pool=$1
82	typeset device=$2
83	typeset status=$3
84	typeset type=$4
85
86	if [[ -z $pool || -z $device || -z $status ]]; then
87		log_fail "Usage: verify_cache_device <pool> <device> " \
88			"<status> [type]"
89	fi
90
91	if [[ $WRAPPER == *"smi"* ]]; then
92		$ECHO $device | $EGREP "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
93		if (( $? == 0 )); then
94			device=${device}s2
95		fi
96	fi
97
98	#
99	# Get all the cache devices and status table like below
100	#
101	# mirror:/disks/d ONLINE mirror:/disks/e ONLINE stripe:/disks/f ONLINE
102	#
103	set -A dev_stat_tab $($ZPOOL status -v $pool | $NAWK 'BEGIN {start=0} \
104				/\tcache/ {start=1}
105				/\tmirror/ || /\tspares/ || /^$/ {start=0}
106				(start==1) && /\t  (\/|[a-zA-Z])/ \
107					{print "stripe:" $1 " " $2}
108				(start==1) && /\t    (\/|[a-zA-Z])/ \
109					{print "mirror:" $1 " " $2}
110				# When hotspare is replacing
111				(start==1) && /\t      (\/|[a-zA-Z])/ \
112					{print "mirror:" $1 " " $2}'
113			     )
114
115	typeset -i i=0
116	typeset find=0
117	while (( i < ${#dev_stat_tab[@]} )); do
118		typeset dev=${dev_stat_tab[$i]}
119		typeset stat=${dev_stat_tab[((i+1))]}
120
121		case $dev in
122			stripe:$device)
123				if [[ "$type" == 'mirror' ]]; then
124					log_note "Unexpected type: mirror"
125					return 1
126				else
127					if [[ $stat != $status ]]; then
128						log_note "Status($stat) " \
129							"!= Expected stat($status)"
130						return 1
131					fi
132					return 0
133				fi
134				;;
135			mirror:$device)
136				if [[ -z "$type" || $type == 'stripe' ]]; then
137					log_note "Unexpected type: stripe"
138					return 1
139				else
140					if [[ $stat != $status ]]; then
141						log_note "Status($stat) " \
142							"!= Expected stat($status)"
143						return 1
144					fi
145					return 0
146				fi
147				;;
148		esac
149		((i += 2))
150	done
151
152	log_note "Can not find device: $device"
153	return 1
154}
155
156function verify_cache_support
157{
158	$ZPOOL upgrade -v | $GREP "Cache devices" > /dev/null 2>&1
159	return $?
160}
161