1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22# Copyright (c) 2019, Delphix. All rights reserved.
23# Copyright (c) 2023, George Amanakis. All rights reserved.
24#
25
26. $STF_SUITE/include/libtest.shlib
27. $STF_SUITE/tests/functional/cli_root/zpool_scrub/zpool_scrub.cfg
28
29#
30# DESCRIPTION:
31#	Verify error scrub clears the errorlog, if errors no longer exist.
32#
33# STRATEGY:
34#	1. Create a pool and create file in it.
35#	2. Zinject errors and read using dd to log errors to disk.
36#	3. Make sure file name is mentioned in the list of error files.
37#	4. Start error scrub and wait for it finish.
38#	5. Check scrub ran and errors are still reported.
39#	6. Clear corruption and error scrub again.
40#	7. Check scrub ran and errors are cleared.
41#
42
43verify_runnable "global"
44
45function cleanup
46{
47	zinject -c all
48	rm -f /$TESTPOOL2/$TESTFILE0
49	destroy_pool $TESTPOOL2
50}
51
52log_onexit cleanup
53
54log_assert "Verify error scrub clears the errorlog, if errors no longer exist."
55
56truncate -s $MINVDEVSIZE $TESTDIR/vdev_a
57log_must zpool create -f -O primarycache=none $TESTPOOL2 $TESTDIR/vdev_a
58log_must zfs create $TESTPOOL2/$TESTFS1
59typeset file=/$TESTPOOL2/$TESTFS1/$TESTFILE0
60log_must dd if=/dev/urandom of=$file bs=2M count=10
61
62lastfs="$(zfs list -r $TESTPOOL2 | tail -1 | awk '{print $1}')"
63for i in {1..3}; do
64	log_must zfs snap $lastfs@snap$i
65	log_must zfs clone $lastfs@snap$i $TESTPOOL2/clone$i
66	lastfs="$(zfs list -r $TESTPOOL2/clone$i | tail -1 | awk '{print $1}')"
67done
68
69log_must zinject -t data -e checksum -f 100 -a $file
70dd if=$file of=/dev/null bs=2M count=10
71
72# Important: sync error log to disk
73log_must sync_pool $TESTPOOL2
74
75# Check reported errors
76log_must zpool status -v $TESTPOOL2
77log_must eval "zpool status -v $TESTPOOL2 | \
78    grep \"Permanent errors have been detected\""
79log_must eval "zpool status -v | grep '$TESTPOOL2/$TESTFS1/$TESTFILE0'"
80log_must eval "zpool status -v | grep '$TESTPOOL2/$TESTFS1@snap1:/$TESTFILE0'"
81log_must eval "zpool status -v | grep '$TESTPOOL2/clone1/$TESTFILE0'"
82log_must eval "zpool status -v | grep '$TESTPOOL2/clone1@snap2:/$TESTFILE0'"
83log_must eval "zpool status -v | grep '$TESTPOOL2/clone2/$TESTFILE0'"
84log_must eval "zpool status -v | grep '$TESTPOOL2/clone2@snap3:/$TESTFILE0'"
85log_must eval "zpool status -v | grep '$TESTPOOL2/clone3/$TESTFILE0'"
86
87# Check errors are reported if corruption persists
88log_must zpool scrub -e -w $TESTPOOL2
89log_must eval "zpool status -v | grep 'error blocks'"
90log_must zpool status -v $TESTPOOL2
91log_must eval "zpool status -v $TESTPOOL2 | \
92    grep \"Permanent errors have been detected\""
93log_must eval "zpool status -v | grep '$TESTPOOL2/$TESTFS1/$TESTFILE0'"
94log_must eval "zpool status -v | grep '$TESTPOOL2/$TESTFS1@snap1:/$TESTFILE0'"
95log_must eval "zpool status -v | grep '$TESTPOOL2/clone1/$TESTFILE0'"
96log_must eval "zpool status -v | grep '$TESTPOOL2/clone1@snap2:/$TESTFILE0'"
97log_must eval "zpool status -v | grep '$TESTPOOL2/clone2/$TESTFILE0'"
98log_must eval "zpool status -v | grep '$TESTPOOL2/clone2@snap3:/$TESTFILE0'"
99log_must eval "zpool status -v | grep '$TESTPOOL2/clone3/$TESTFILE0'"
100
101# Check errors are cleared
102log_must zinject -c all
103log_must zpool scrub -e -w $TESTPOOL2
104log_must zpool status -v $TESTPOOL2
105log_must eval "zpool status -v | grep 'error blocks'"
106log_mustnot eval "zpool status -v | grep '$TESTFILE0'"
107
108
109log_pass "Verify error scrub clears the errorlog, if errors no longer exist."
110