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 https://opensource.org/licenses/CDDL-1.0.
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, 2016 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/urandom of=$mntpnt/testfile.$$ &
61	typeset pid=$!
62
63	zpool iostat -v 1 3 > /dev/null
64	((ret |= $?))
65
66	kill -9 $pid
67	wait
68
69	return $ret
70}
71
72#
73# Verify the given cache device have correct type and status
74#
75# $1 pool name
76# $2 device name
77# $3 device status
78# $4 device type
79#
80function verify_cache_device
81{
82	typeset pool=$1
83	typeset device=$2
84	typeset status=$3
85	typeset type=$4
86
87	if [[ -z $pool || -z $device || -z $status ]]; then
88		log_fail "Usage: verify_cache_device <pool> <device> " \
89			"<status> [type]"
90	fi
91
92	#
93	# Get all the cache devices and status table like below
94	#
95	# mirror:/disks/d ONLINE mirror:/disks/e ONLINE stripe:/disks/f ONLINE
96	#
97	set -A dev_stat_tab $(zpool status -v $pool | awk 'BEGIN {start=0} \
98				/\tcache/ {start=1}
99				/\tmirror/ || /\tspares/ || /^$/ {start=0}
100				(start==1) && /\t  (\/|[a-zA-Z])/ \
101					{print "stripe:" $1 " " $2}
102				(start==1) && /\t    (\/|[a-zA-Z])/ \
103					{print "mirror:" $1 " " $2}
104				# When hotspare is replacing
105				(start==1) && /\t      (\/|[a-zA-Z])/ \
106					{print "mirror:" $1 " " $2}'
107			     )
108
109	typeset -i i=0
110	typeset find=0
111	while (( i < ${#dev_stat_tab[@]} )); do
112		typeset dev=${dev_stat_tab[$i]}
113		typeset stat=${dev_stat_tab[((i+1))]}
114
115		case $dev in
116			stripe:$device)
117				if [[ "$type" == 'mirror' ]]; then
118					log_note "Unexpected type: mirror"
119					return 1
120				else
121					if [[ $stat != $status ]]; then
122						log_note "Status($stat) " \
123							"!= Expected stat($status)"
124						return 1
125					fi
126					return 0
127				fi
128				;;
129			mirror:$device)
130				if [[ -z "$type" || $type == 'stripe' ]]; then
131					log_note "Unexpected type: stripe"
132					return 1
133				else
134					if [[ $stat != $status ]]; then
135						log_note "Status($stat) " \
136							"!= Expected stat($status)"
137						return 1
138					fi
139					return 0
140				fi
141				;;
142		esac
143		((i += 2))
144	done
145
146	log_note "Can not find device: $device"
147	return 1
148}
149