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 https://opensource.org/licenses/CDDL-1.0.
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
23#
24# Copyright (c) 2023 George Amanakis. All rights reserved.
25#
26
27#
28# DESCRIPTION:
29# Verify reporting errors when deleting corrupted files after scrub
30#
31# STRATEGY:
32# 1. Create a pool, and a file
33# 2. Corrupt the file
34# 3. Create snapshots and clones like:
35# 	fs->snap1->clone1->snap2->clone2->...
36# 4. Read the original file and immediately delete it
37# 5. Delete the file in clone2
38# 6. Snapshot clone2->snapxx and clone into snapxx->clonexx
39# 7. Verify we report errors in the pool in 'zpool status -v'
40# 8. Promote clone1
41# 9. Verify we report errors in the pool in 'zpool status -v'
42# 10. Delete the corrupted file and origin snapshots.
43# 11. Verify we do not report data errors anymore, without requiring
44# 	a scrub.
45
46. $STF_SUITE/include/libtest.shlib
47
48verify_runnable "both"
49
50function cleanup
51{
52	destroy_pool $TESTPOOL2
53	rm -f $TESTDIR/vdev_a
54}
55
56log_assert "Verify reporting errors when deleting corrupted files after scrub"
57log_onexit cleanup
58
59typeset file="/$TESTPOOL2/$TESTFS1/$TESTFILE0"
60
61truncate -s $MINVDEVSIZE $TESTDIR/vdev_a
62log_must zpool create -f $TESTPOOL2 $TESTDIR/vdev_a
63log_must zfs create -o primarycache=none $TESTPOOL2/$TESTFS1
64log_must dd if=/dev/urandom of=$file bs=1024 count=1024 oflag=sync
65corrupt_blocks_at_level $file 0
66
67lastfs="$(zfs list -r $TESTPOOL2 | tail -1 | awk '{print $1}')"
68for i in {1..3}; do
69	log_must zfs snap $lastfs@snap$i
70	log_must zfs clone $lastfs@snap$i $TESTPOOL2/clone$i
71	lastfs="$(zfs list -r $TESTPOOL2/clone$i | tail -1 | awk '{print $1}')"
72done
73
74log_must zpool scrub -w $TESTPOOL2
75log_must rm $file /$TESTPOOL2/clone2/$TESTFILE0
76log_must zfs snap $TESTPOOL2/clone2@snapxx
77log_must zfs clone $TESTPOOL2/clone2@snapxx $TESTPOOL2/clonexx
78log_must zpool status -v $TESTPOOL2
79
80log_must eval "zpool status -v $TESTPOOL2 | \
81    grep \"Permanent errors have been detected\""
82log_must eval "zpool status -v | grep '$TESTPOOL2/$TESTFS1@snap1:/$TESTFILE0'"
83log_must eval "zpool status -v | grep '$TESTPOOL2/clone1/$TESTFILE0'"
84log_must eval "zpool status -v | grep '$TESTPOOL2/clone1@snap2:/$TESTFILE0'"
85log_mustnot eval "zpool status -v | grep '$TESTPOOL2/clone2/$TESTFILE0'"
86log_mustnot eval "zpool status -v | grep '$TESTPOOL2/clonexx/$TESTFILE0'"
87log_must eval "zpool status -v | grep '$TESTPOOL2/clone2@snap3:/$TESTFILE0'"
88log_must eval "zpool status -v | grep '$TESTPOOL2/clone3/$TESTFILE0'"
89
90log_must zfs promote $TESTPOOL2/clone1
91log_must eval "zpool status -v $TESTPOOL2 | \
92    grep \"Permanent errors have been detected\""
93log_must eval "zpool status -v | grep '$TESTPOOL2/clone1@snap1:/$TESTFILE0'"
94log_must eval "zpool status -v | grep '$TESTPOOL2/clone1/$TESTFILE0'"
95log_must eval "zpool status -v | grep '$TESTPOOL2/clone1@snap2:/$TESTFILE0'"
96log_mustnot eval "zpool status -v | grep '$TESTPOOL2/clone2/$TESTFILE0'"
97log_mustnot eval "zpool status -v | grep '$TESTPOOL2/clonexx/$TESTFILE0'"
98log_must eval "zpool status -v | grep '$TESTPOOL2/clone2@snap3:/$TESTFILE0'"
99log_must eval "zpool status -v | grep '$TESTPOOL2/clone3/$TESTFILE0'"
100
101log_must rm /$TESTPOOL2/clone1/$TESTFILE0
102log_must zfs destroy -R $TESTPOOL2/clone1@snap1
103log_must zfs destroy -R $TESTPOOL2/clone1@snap2
104log_must zfs list -r $TESTPOOL2
105log_must zpool status -v $TESTPOOL2
106log_must zpool sync
107log_must zpool status -v $TESTPOOL2
108log_must eval "zpool status -v $TESTPOOL2 | \
109    grep \"No known data errors\""
110
111log_pass "Verify reporting errors when deleting corrupted files after scrub"
112