34b7ce4c3bf24b2cc787aec0d5ee5e41019fd340
[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 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20         cd /
21         rm -f $tmp.*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27 . ./common/attr
28
29 # remove previous $seqres.full before test
30 rm -f $seqres.full
31
32 # real QA test starts here
33 _supported_fs overlay
34 _supported_os Linux
35 _require_scratch_nocheck
36 _require_attrs
37 _require_command "$FSCK_OVERLAY_PROG" fsck.overlay
38
39 OVL_XATTR_OPAQUE_VAL=y
40 OVL_XATTR_IMPURE_VAL=y
41
42 # remove all files from previous tests
43 _scratch_mkfs
44
45 # Check whiteout
46 check_whiteout()
47 {
48         for arg in $*; do
49                 local ttype=`stat -c "%F:%t,%T" $arg`
50
51                 [[ "$ttype" == "character special file:0,0" ]] || \
52                         echo "Valid whiteout removed incorrectly"
53         done
54 }
55
56 # Create a whiteout
57 make_whiteout()
58 {
59         for arg in $*; do
60                 mknod $arg c 0 0
61         done
62 }
63
64 # Create an opaque directory
65 make_opaque_dir()
66 {
67         local target=$1
68
69         mkdir -p $target
70         $SETFATTR_PROG -n $OVL_XATTR_OPAQUE -v $OVL_XATTR_OPAQUE_VAL $target
71 }
72
73 # Create impure directories
74 make_impure_dir()
75 {
76         for dir in $*; do
77                 mkdir -p $dir
78                 $SETFATTR_PROG -n $OVL_XATTR_IMPURE -v $OVL_XATTR_IMPURE_VAL $dir
79         done
80 }
81
82 # Create a redirect directory
83 make_redirect_dir()
84 {
85         local target=$1
86         local value=$2
87
88         mkdir -p $target
89         $SETFATTR_PROG -n $OVL_XATTR_REDIRECT -v $value $target
90 }
91
92 # Create test directories
93 lowerdir=$OVL_BASE_SCRATCH_MNT/lower
94 lowerdir2=$OVL_BASE_SCRATCH_MNT/lower2
95 upperdir=$OVL_BASE_SCRATCH_MNT/upper
96 workdir=$OVL_BASE_SCRATCH_MNT/workdir
97
98 make_test_dirs()
99 {
100         rm -rf $lowerdir $lowerdir2 $upperdir $workdir
101         mkdir -p $lowerdir $lowerdir2 $upperdir $workdir
102 }
103
104 # Test orphan whiteout in lower and upper layer, should remove
105 echo "+ Orphan whiteout"
106 make_test_dirs
107 make_whiteout $lowerdir/foo $upperdir/{foo,bar}
108
109 _overlay_fsck_expect $FSCK_NONDESTRUCT $lowerdir $upperdir $workdir -p
110 ls $lowerdir
111 ls $upperdir
112
113 # Test valid whiteout covering lower target, should not remove
114 echo "+ Valid whiteout"
115 make_test_dirs
116 touch $lowerdir2/{foo,bar}
117 make_whiteout $upperdir/foo $lowerdir/bar
118
119 _overlay_fsck_expect $FSCK_OK "$lowerdir:$lowerdir2" $upperdir $workdir -p
120 check_whiteout $upperdir/foo $lowerdir/bar
121
122 # Test orphan whiteout in opaque directory, should remove
123 echo "+ Orphan whiteout(2)"
124 make_test_dirs
125 mkdir $lowerdir/testdir
126 touch $lowerdir/testdir/foo
127 make_opaque_dir $upperdir/testdir
128 make_whiteout $upperdir/testdir/foo
129
130 _overlay_fsck_expect $FSCK_NONDESTRUCT $lowerdir $upperdir $workdir -p
131 ls $upperdir/testdir
132
133 # Test orphan whiteout whose parent path is not an merged directory,
134 # should remove
135 echo "+ Orphan whiteout(3)"
136 make_test_dirs
137 mkdir $lowerdir2/{testdir1,testdir2,testdir3}
138 touch $lowerdir2/{testdir1/foo,testdir2/foo,testdir3/foo}
139 mkdir $upperdir/{testdir1,testdir2,testdir3,testdir4}
140 touch $lowerdir/testdir1
141 make_whiteout $lowerdir/testdir2
142 make_opaque_dir $lowerdir/testdir3
143 make_whiteout $upperdir/{testdir1/foo,/testdir2/foo,testdir3/foo,testdir4/foo}
144
145 _overlay_fsck_expect $FSCK_NONDESTRUCT "$lowerdir:$lowerdir2" $upperdir $workdir -p
146 ls $upperdir/testdir1
147 ls $upperdir/testdir2
148 ls $upperdir/testdir3
149 ls $upperdir/testdir4
150
151 # Test orphan whiteout in redirect directory, should remove
152 echo "+ Orphan whiteout(4)"
153 make_test_dirs
154 mkdir $lowerdir/{testdir,origin}
155 touch $lowerdir/testdir/foo
156 make_redirect_dir $upperdir/testdir "origin"
157 make_whiteout $upperdir/testdir/foo
158
159 _overlay_fsck_expect $FSCK_NONDESTRUCT $lowerdir $upperdir $workdir -p
160 ls $upperdir/testdir
161
162 # Test valid whiteout in redirect directory cover file in lower
163 # redirect origin directory, should not remove
164 echo "+ Valid whiteout(2)"
165 make_test_dirs
166 mkdir $lowerdir/origin
167 touch $lowerdir/origin/foo
168 make_impure_dir $upperdir
169 make_redirect_dir $upperdir/testdir "origin"
170 make_whiteout $upperdir/origin $upperdir/testdir/foo
171
172 _overlay_fsck_expect $FSCK_OK $lowerdir $upperdir $workdir -p
173 check_whiteout $upperdir/testdir/foo
174
175 # Test valid whiteout covering lower target whose parent directory
176 # merge with a redirect directory in the middle layer, should not remove.
177 echo "+ Valid whiteout(3)"
178 make_test_dirs
179 mkdir -p $lowerdir2/origin/subdir
180 touch $lowerdir2/origin/subdir/foo
181 make_redirect_dir $lowerdir/testdir "origin"
182 mkdir -p $upperdir/testdir/subdir
183 make_whiteout $lowerdir/origin $upperdir/testdir/subdir/foo
184 make_impure_dir $upperdir/testdir $upperdir
185
186 _overlay_fsck_expect $FSCK_OK "$lowerdir:$lowerdir2" $upperdir $workdir -p
187 check_whiteout $upperdir/testdir/subdir/foo
188
189 # Test invalid whiteout in opaque subdirectory in a redirect directory,
190 # should remove
191 echo "+ Orphan whiteout(5)"
192 make_test_dirs
193 mkdir -p $lowerdir/origin/subdir
194 touch $lowerdir/origin/subdir/foo
195 make_redirect_dir $upperdir/testdir "origin"
196 make_opaque_dir $upperdir/testdir/subdir
197 make_whiteout $upperdir/testdir/subdir/foo
198
199 _overlay_fsck_expect $FSCK_NONDESTRUCT $lowerdir $upperdir $workdir -p
200 ls $upperdir/testdir/subdir
201
202 # Test valid whiteout in reidrect subdirectory in a opaque directory
203 # covering lower target, should not remove
204 echo "+ Valid whiteout(4)"
205 make_test_dirs
206 mkdir $lowerdir/origin
207 touch $lowerdir/origin/foo
208 make_opaque_dir $upperdir/testdir
209 make_redirect_dir $upperdir/testdir/subdir "/origin"
210 make_whiteout $upperdir/origin $upperdir/testdir/subdir/foo
211 make_impure_dir $upperdir/testdir
212
213 _overlay_fsck_expect $FSCK_OK $lowerdir $upperdir $workdir -p
214 check_whiteout $upperdir/testdir/subdir/foo
215
216 # success, all done
217 status=0
218 exit