1#!/bin/ksh -p
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 (c) 2022, Klara Inc.
24#
25# This software was developed by Rob Wing <rob.wing@klarasystems.com>
26# under sponsorship from Seagate Technology LLC and Klara Inc.
27
28# DESCRIPTION:
29#	Verify that checksum errors are accurately reported to ZED
30#
31# STRATEGY:
32#	1. Create a mirrored/raidz pool
33#	2. Inject checksum error
34#	3. Verify checksum error count reported to ZED is not zero
35#
36
37. $STF_SUITE/include/libtest.shlib
38. $STF_SUITE/tests/functional/events/events_common.kshlib
39
40verify_runnable "both"
41
42MOUNTDIR="$TEST_BASE_DIR/checksum_mount"
43FILEPATH="$MOUNTDIR/checksum_file"
44VDEV="$TEST_BASE_DIR/vdevfile.$$"
45VDEV1="$TEST_BASE_DIR/vdevfile1.$$"
46POOL="checksum_pool"
47FILESIZE="10M"
48
49function cleanup
50{
51	log_must zed_stop
52
53	log_must zinject -c all
54	if poolexists $POOL ; then
55		destroy_pool $POOL
56	fi
57	log_must rm -fd $VDEV $MOUNTDIR
58}
59log_onexit cleanup
60
61log_assert "Test reported checksum errors to ZED"
62
63function setup_pool
64{
65	type="$1"
66
67	log_must zpool create -f -m $MOUNTDIR $POOL $type $VDEV $VDEV1
68	log_must zpool events -c
69	log_must truncate -s 0 $ZED_DEBUG_LOG
70	log_must zfs set compression=off $POOL
71	log_must zfs set primarycache=none $POOL
72}
73
74function do_clean
75{
76	log_must zinject -c all
77	log_must zpool destroy $POOL
78}
79
80function do_checksum_error
81{
82	log_must mkfile $FILESIZE $FILEPATH
83	log_must zinject -a -t data -e checksum -T read -f 100 $FILEPATH
84
85	dd if=$FILEPATH of=/dev/null bs=1 count=1 2>/dev/null
86
87	log_must file_wait_event $ZED_DEBUG_LOG "ereport.fs.zfs.checksum" 10
88
89	# checksum error as reported from the vdev.
90	zpool_cksum=`zpool get -H -o value checksum_errors $POOL $VDEV`
91
92	# first checksum error reported to ZED.
93	zed_cksum=$(awk '/ZEVENT_CLASS=ereport.fs.zfs.checksum/, \
94	    /ZEVENT_VDEV_CKSUM_ERRORS=/ { \
95	    if ($1 ~ "ZEVENT_VDEV_CKSUM_ERRORS") \
96	    { print $0; exit } }' $ZED_DEBUG_LOG)
97
98	log_must [ $zpool_cksum -gt 0 ]
99
100	log_mustnot [ "$zed_cksum" = "ZEVENT_VDEV_CKSUM_ERRORS=0" ]
101
102	log_must [ "$zed_cksum" = "ZEVENT_VDEV_CKSUM_ERRORS=1" ]
103}
104
105# Set checksum_n=1
106# fire 1 event, should degrade.
107function checksum_error
108{
109	type=$1
110
111	setup_pool $type
112	do_checksum_error
113	do_clean
114}
115
116log_must truncate -s $MINVDEVSIZE $VDEV
117log_must truncate -s $MINVDEVSIZE $VDEV1
118log_must mkdir -p $MOUNTDIR
119
120log_must zed_start
121checksum_error mirror
122checksum_error raidz
123
124log_pass "Test reported checksum errors to ZED"
125