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 2008 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. $STF_SUITE/tests/functional/slog/slog.cfg
33
34function setup
35{
36	log_must rm -rf $VDIR $VDIR2
37	log_must mkdir -p $VDIR $VDIR2
38	log_must truncate -s $MINVDEVSIZE $VDEV $SDEV $LDEV $VDEV2 $SDEV2 $LDEV2
39
40	return 0
41}
42
43function cleanup
44{
45	poolexists $TESTPOOL && destroy_pool $TESTPOOL
46	poolexists $TESTPOOL2 && destroy_pool $TESTPOOL2
47	rm -rf $TESTDIR $VDIR $VDIR2
48}
49
50#
51# Try zpool status/iostat for given pool
52#
53# $1 pool
54#
55function display_status
56{
57	typeset pool=$1
58
59	typeset -i ret=0
60	zpool status -xv $pool > /dev/null 2>&1
61	ret=$?
62
63	zpool iostat > /dev/null 2>&1
64	((ret |= $?))
65
66	typeset mntpnt=$(get_prop mountpoint $pool)
67	dd if=/dev/urandom of=$mntpnt/testfile.$$ &
68	typeset pid=$!
69
70	zpool iostat -v 1 3 > /dev/null
71	((ret |= $?))
72
73	kill -9 $pid
74	wait
75
76	return $ret
77}
78
79#
80# Verify the give slog device have correct type and status
81#
82# $1 pool name
83# $2 device name
84# $3 device status
85# $4 device type
86#
87function verify_slog_device
88{
89	typeset pool=$1
90	typeset device=$2
91	typeset status=$3
92	typeset type=$4
93
94	if [[ -z $pool || -z $device || -z $status ]]; then
95		log_fail "Usage: verify_slog_device <pool> <device> " \
96			"<status> [type]"
97	fi
98
99	#
100	# Get all the slog devices and status table like below
101	#
102	# mirror:/disks/d ONLINE mirror:/disks/e ONLINE stripe:/disks/f ONLINE
103	#
104	set -A dev_stat_tab $(zpool status -v $pool | nawk 'BEGIN {start=0} \
105				/\tlogs/ {start=1}
106				/\tmirror/ || /\tspares/ || /^$/ {start=0}
107				(start==1) && /\t  (\/|[a-zA-Z])/ \
108					{print "stripe:" $1 " " $2}
109				(start==1) && /\t    (\/|[a-zA-Z])/ \
110					{print "mirror:" $1 " " $2}
111				# When hotspare is replacing
112				(start==1) && /\t      (\/|[a-zA-Z])/ \
113					{print "mirror:" $1 " " $2}'
114			     )
115
116	typeset -i i=0
117	typeset find=0
118	while (( i < ${#dev_stat_tab[@]} )); do
119		typeset dev=${dev_stat_tab[$i]}
120		typeset stat=${dev_stat_tab[((i+1))]}
121
122		case $dev in
123			stripe:$device)
124				if [[ "$type" == 'mirror' ]]; then
125					log_note "Unexpected type: mirror"
126					return 1
127				else
128					if [[ $stat != $status ]]; then
129						log_note "Status($stat) " \
130							"!= Expected stat($status)"
131						return 1
132					fi
133					return 0
134				fi
135				;;
136			mirror:$device)
137				if [[ -z "$type" || $type == 'stripe' ]]; then
138					log_note "Unexpected type: stripe"
139					return 1
140				else
141					if [[ $stat != $status ]]; then
142						log_note "Status($stat) " \
143							"!= Expected stat($status)"
144						return 1
145					fi
146					return 0
147				fi
148				;;
149		esac
150
151		((i += 2))
152	done
153
154	log_note "Can not find device: $device"
155
156	return 1
157}
158