xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/fault/suspend_resume_single.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#
25# Copyright (c) 2024, Klara Inc.
26#
27
28. $STF_SUITE/include/libtest.shlib
29
30DATAFILE=$(mktemp)
31
32function cleanup
33{
34	zpool clear $TESTPOOL
35	destroy_pool $TESTPOOL
36	unload_scsi_debug
37	rm -f $DATAFILE
38}
39
40log_onexit cleanup
41
42log_assert "ensure single-disk pool resumes properly after suspend and clear"
43
44# create a file, and take a checksum, so we can compare later
45log_must dd if=/dev/urandom of=$DATAFILE bs=128K count=1
46typeset sum1=$(xxh128digest $DATAFILE)
47
48# make a debug device that we can "unplug"
49load_scsi_debug 100 1 1 1 '512b'
50sd=$(get_debug_device)
51
52# create a single-device pool
53log_must zpool create $TESTPOOL $sd
54log_must zpool sync
55
56# "pull" the disk
57log_must eval "echo offline > /sys/block/$sd/device/state"
58
59# copy data onto the pool. it'll appear to succeed, but only be in memory
60log_must cp $DATAFILE /$TESTPOOL/file
61
62# wait until sync starts, and the pool suspends
63log_note "waiting for pool to suspend"
64typeset -i tries=10
65until [[ $(kstat_pool $TESTPOOL state) == "SUSPENDED" ]] ; do
66	if ((tries-- == 0)); then
67		log_fail "pool didn't suspend"
68	fi
69	sleep 1
70done
71
72# return the disk
73log_must eval "echo running > /sys/block/$sd/device/state"
74
75# clear the error states, which should reopen the vdev, get the pool back
76# online, and replay the failed IO
77log_must zpool clear $TESTPOOL
78
79# wait a while for everything to sync out. if something is going to go wrong,
80# this is where it will happen
81log_note "giving pool time to settle and complete txg"
82sleep 7
83
84# if the pool suspended, then everything is bad
85if [[ $(kstat_pool $TESTPOOL state) == "SUSPENDED" ]] ; then
86	log_fail "pool suspended"
87fi
88
89# export the pool, to make sure it exports clean, and also to clear the file
90# out of the cache
91log_must zpool export $TESTPOOL
92
93# import the pool
94log_must zpool import $TESTPOOL
95
96# sum the file we wrote earlier
97typeset sum2=$(xxh128digest /$TESTPOOL/file)
98
99# make sure the checksums match
100log_must test "$sum1" = "$sum2"
101
102log_pass "single-disk pool resumes properly after disk suspend and clear"
103