generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / overlay / 045
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Huawei.  All Rights Reserved.
4 #
5 # FS QA Test No. 045
6 #
7 # Test fsck.overlay how to deal with whiteouts in overlayfs.
8 #
9 . ./common/preamble
10 _begin_fstest auto quick fsck
11
12 # Import common functions.
13 . ./common/filter
14 . ./common/attr
15
16 # real QA test starts here
17 _supported_fs overlay
18 _require_scratch_nocheck
19 _require_attrs trusted
20 _require_command "$FSCK_OVERLAY_PROG" fsck.overlay
21
22 OVL_XATTR_OPAQUE_VAL=y
23 OVL_XATTR_IMPURE_VAL=y
24
25 # remove all files from previous tests
26 _scratch_mkfs
27
28 # Check whiteout
29 check_whiteout()
30 {
31         for arg in $*; do
32                 local ttype=`stat -c "%F:%t,%T" $arg`
33
34                 [[ "$ttype" == "character special file:0,0" ]] || \
35                         echo "Valid whiteout removed incorrectly"
36         done
37 }
38
39 # Create a whiteout
40 make_whiteout()
41 {
42         for arg in $*; do
43                 mknod $arg c 0 0
44         done
45 }
46
47 # Create an opaque directory
48 make_opaque_dir()
49 {
50         local target=$1
51
52         mkdir -p $target
53         $SETFATTR_PROG -n $OVL_XATTR_OPAQUE -v $OVL_XATTR_OPAQUE_VAL $target
54 }
55
56 # Create impure directories
57 make_impure_dir()
58 {
59         for dir in $*; do
60                 mkdir -p $dir
61                 $SETFATTR_PROG -n $OVL_XATTR_IMPURE -v $OVL_XATTR_IMPURE_VAL $dir
62         done
63 }
64
65 # Create a redirect directory
66 make_redirect_dir()
67 {
68         local target=$1
69         local value=$2
70
71         mkdir -p $target
72         $SETFATTR_PROG -n $OVL_XATTR_REDIRECT -v $value $target
73 }
74
75 # Create test directories
76 lowerdir=$OVL_BASE_SCRATCH_MNT/lower
77 lowerdir2=$OVL_BASE_SCRATCH_MNT/lower2
78 upperdir=$OVL_BASE_SCRATCH_MNT/upper
79 workdir=$OVL_BASE_SCRATCH_MNT/workdir
80
81 make_test_dirs()
82 {
83         rm -rf $lowerdir $lowerdir2 $upperdir $workdir
84         mkdir -p $lowerdir $lowerdir2 $upperdir $workdir
85 }
86
87 # Test orphan whiteout in lower and upper layer, should remove
88 echo "+ Orphan whiteout"
89 make_test_dirs
90 make_whiteout $lowerdir/foo $upperdir/{foo,bar}
91
92 _overlay_fsck_expect $FSCK_NONDESTRUCT $lowerdir $upperdir $workdir -p
93 ls $lowerdir
94 ls $upperdir
95
96 # Test valid whiteout covering lower target, should not remove
97 echo "+ Valid whiteout"
98 make_test_dirs
99 touch $lowerdir2/{foo,bar}
100 make_whiteout $upperdir/foo $lowerdir/bar
101
102 _overlay_fsck_expect $FSCK_OK "$lowerdir:$lowerdir2" $upperdir $workdir -p
103 check_whiteout $upperdir/foo $lowerdir/bar
104
105 # Test orphan whiteout in opaque directory, should remove
106 echo "+ Orphan whiteout(2)"
107 make_test_dirs
108 mkdir $lowerdir/testdir
109 touch $lowerdir/testdir/foo
110 make_opaque_dir $upperdir/testdir
111 make_whiteout $upperdir/testdir/foo
112
113 _overlay_fsck_expect $FSCK_NONDESTRUCT $lowerdir $upperdir $workdir -p
114 ls $upperdir/testdir
115
116 # Test orphan whiteout whose parent path is not an merged directory,
117 # should remove
118 echo "+ Orphan whiteout(3)"
119 make_test_dirs
120 mkdir $lowerdir2/{testdir1,testdir2,testdir3}
121 touch $lowerdir2/{testdir1/foo,testdir2/foo,testdir3/foo}
122 mkdir $upperdir/{testdir1,testdir2,testdir3,testdir4}
123 touch $lowerdir/testdir1
124 make_whiteout $lowerdir/testdir2
125 make_opaque_dir $lowerdir/testdir3
126 make_whiteout $upperdir/{testdir1/foo,/testdir2/foo,testdir3/foo,testdir4/foo}
127
128 _overlay_fsck_expect $FSCK_NONDESTRUCT "$lowerdir:$lowerdir2" $upperdir $workdir -p
129 ls $upperdir/testdir1
130 ls $upperdir/testdir2
131 ls $upperdir/testdir3
132 ls $upperdir/testdir4
133
134 # Test orphan whiteout in redirect directory, should remove
135 echo "+ Orphan whiteout(4)"
136 make_test_dirs
137 mkdir $lowerdir/{testdir,origin}
138 touch $lowerdir/testdir/foo
139 make_redirect_dir $upperdir/testdir "origin"
140 make_whiteout $upperdir/testdir/foo
141
142 _overlay_fsck_expect $FSCK_NONDESTRUCT $lowerdir $upperdir $workdir -p
143 ls $upperdir/testdir
144
145 # Test valid whiteout in redirect directory cover file in lower
146 # redirect origin directory, should not remove
147 echo "+ Valid whiteout(2)"
148 make_test_dirs
149 mkdir $lowerdir/origin
150 touch $lowerdir/origin/foo
151 make_impure_dir $upperdir
152 make_redirect_dir $upperdir/testdir "origin"
153 make_whiteout $upperdir/origin $upperdir/testdir/foo
154
155 _overlay_fsck_expect $FSCK_OK $lowerdir $upperdir $workdir -p
156 check_whiteout $upperdir/testdir/foo
157
158 # Test valid whiteout covering lower target whose parent directory
159 # merge with a redirect directory in the middle layer, should not remove.
160 echo "+ Valid whiteout(3)"
161 make_test_dirs
162 mkdir -p $lowerdir2/origin/subdir
163 touch $lowerdir2/origin/subdir/foo
164 make_redirect_dir $lowerdir/testdir "origin"
165 mkdir -p $upperdir/testdir/subdir
166 make_whiteout $lowerdir/origin $upperdir/testdir/subdir/foo
167 make_impure_dir $upperdir/testdir $upperdir
168
169 _overlay_fsck_expect $FSCK_OK "$lowerdir:$lowerdir2" $upperdir $workdir -p
170 check_whiteout $upperdir/testdir/subdir/foo
171
172 # Test invalid whiteout in opaque subdirectory in a redirect directory,
173 # should remove
174 echo "+ Orphan whiteout(5)"
175 make_test_dirs
176 mkdir -p $lowerdir/origin/subdir
177 touch $lowerdir/origin/subdir/foo
178 make_redirect_dir $upperdir/testdir "origin"
179 make_opaque_dir $upperdir/testdir/subdir
180 make_whiteout $upperdir/testdir/subdir/foo
181
182 _overlay_fsck_expect $FSCK_NONDESTRUCT $lowerdir $upperdir $workdir -p
183 ls $upperdir/testdir/subdir
184
185 # Test valid whiteout in reidrect subdirectory in a opaque directory
186 # covering lower target, should not remove
187 echo "+ Valid whiteout(4)"
188 make_test_dirs
189 mkdir $lowerdir/origin
190 touch $lowerdir/origin/foo
191 make_opaque_dir $upperdir/testdir
192 make_redirect_dir $upperdir/testdir/subdir "/origin"
193 make_whiteout $upperdir/origin $upperdir/testdir/subdir/foo
194 make_impure_dir $upperdir/testdir
195
196 _overlay_fsck_expect $FSCK_OK $lowerdir $upperdir $workdir -p
197 check_whiteout $upperdir/testdir/subdir/foo
198
199 # success, all done
200 status=0
201 exit