xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/procfs/pool_state.ksh (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1#!/bin/ksh -p
2# SPDX-License-Identifier: CDDL-1.0
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 https://opensource.org/licenses/CDDL-1.0.
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# Copyright (c) 2018 by Lawrence Livermore National Security, LLC.
25#
26
27#
28# DESCRIPTION:
29# Test /proc/spl/kstat/zfs/<pool>/state kstat
30#
31# STRATEGY:
32# 1. Create a mirrored pool
33# 2. Check that pool is ONLINE
34# 3. Fault one disk
35# 4. Check that pool is DEGRADED
36# 5. Create a new pool with a single scsi_debug disk
37# 6. Remove the disk
38# 7. Check that pool is SUSPENDED
39# 8. Add the disk back in
40# 9. Clear errors and destroy the pools
41
42. $STF_SUITE/include/libtest.shlib
43
44verify_runnable "both"
45
46function cleanup
47{
48	# Destroy the scsi_debug pool
49	if [ -n "$TESTPOOL2" ] ; then
50		if  [ -n "$host" ] ; then
51			# Re-enable the disk
52			scan_scsi_hosts $host
53
54			# Device may have changed names after being inserted
55			SDISK=$(get_debug_device)
56			log_must ln $DEV_RDSKDIR/$SDISK $REALDISK
57		fi
58
59		# Restore our working pool image
60		if [ -n "$BACKUP" ] ; then
61			gunzip -c $BACKUP > $REALDISK
62			log_must rm -f $BACKUP
63		fi
64
65		if poolexists $TESTPOOL2 ; then
66			# Our disk is back.  Now we can clear errors and destroy the
67			# pool cleanly.
68			log_must zpool clear $TESTPOOL2
69
70			# Now that the disk is back and errors cleared, wait for our
71			# hung 'zpool scrub' to finish.
72			wait
73
74			destroy_pool $TESTPOOL2
75		fi
76		log_must rm -f $REALDISK
77		unload_scsi_debug
78	fi
79}
80
81# Check that our pool state values match what's expected
82#
83# $1: pool name
84# $2: expected state ("ONLINE", "DEGRADED", "SUSPENDED", etc)
85function check_all
86{
87	pool=$1
88	expected=$2
89
90	state1=$(zpool status $pool | awk '/state: /{print $2}');
91	state2=$(zpool list -H -o health $pool)
92	state3=$(</proc/spl/kstat/zfs/$pool/state)
93	log_note "Checking $expected = $state1 = $state2 = $state3"
94	if [[ "$expected" == "$state1" &&  "$expected" == "$state2" && \
95	    "$expected" == "$state3" ]] ; then
96		true
97	else
98		false
99	fi
100}
101
102log_onexit cleanup
103
104log_assert "Testing /proc/spl/kstat/zfs/<pool>/state kstat"
105
106# Test that the initial pool is healthy
107check_all $TESTPOOL "ONLINE"
108
109# Fault one of the disks, and check that pool is degraded
110read -r DISK1 _ <<<"$DISKS"
111log_must zpool offline -tf $TESTPOOL $DISK1
112check_all $TESTPOOL "DEGRADED"
113log_must zpool online $TESTPOOL $DISK1
114log_must zpool clear $TESTPOOL
115
116# Create a new pool out of a scsi_debug disk
117TESTPOOL2=testpool2
118MINVDEVSIZE_MB=$((MINVDEVSIZE / 1048576))
119load_scsi_debug $MINVDEVSIZE_MB 1 1 1 '512b'
120
121SDISK=$(get_debug_device)
122host=$(get_scsi_host $SDISK)
123
124# Use $REALDISK instead of $SDISK in our pool because $SDISK can change names
125# as we remove/add the disk (i.e. /dev/sdf -> /dev/sdg).
126REALDISK=/dev/kstat-state-realdisk
127log_must [ ! -e $REALDISK ]
128ln $DEV_RDSKDIR/$SDISK $REALDISK
129
130log_must zpool create $TESTPOOL2 $REALDISK
131
132# Backup the contents of the disk image
133BACKUP=$TEST_BASE_DIR/kstat-state-realdisk.gz
134log_must [ ! -e $BACKUP ]
135gzip -c $REALDISK > $BACKUP
136
137# Yank out the disk from under the pool
138log_must rm $REALDISK
139remove_disk $SDISK
140
141# Run a 'zpool scrub' in the background to suspend the pool.  We run it in the
142# background since the command will hang when the pool gets suspended.  The
143# command will resume and exit after we restore the missing disk later on.
144zpool scrub $TESTPOOL2 &
145# Once we trigger the zpool scrub, all zpool/zfs command gets stuck for 180 seconds.
146# Post 180 seconds zpool/zfs commands gets start executing however few more seconds(10s)
147# it take to update the status.
148# hence sleeping for 200 seconds so that we get the correct status.
149sleep 200		# Give the scrub some time to run before we check if it fails
150
151log_must check_all $TESTPOOL2 "SUSPENDED"
152
153log_pass "/proc/spl/kstat/zfs/<pool>/state test successful"
154