xfs/144: Use _qsetup instead of qsetup
[xfstests-dev.git] / tests / overlay / 070
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2019 CTERA Networks. All Rights Reserved.
4 #
5 # FSQA Test No. 070
6 #
7 # This is a variant of overlay/017 to test constant st_ino numbers for
8 # nested overlay setup, where all layers of both overlays are on the
9 # same fs.
10 #
11 # This simple test demonstrates a known issue with overlayfs:
12 # - stat file A shows inode number X
13 # - modify A to trigger copy up
14 # - stat file A shows inode number Y != X
15 #
16 # Also test that d_ino of readdir entries and i_ino from /proc/locks are
17 # consistent with st_ino and that inode numbers persist after rename to
18 # new parent, drop caches and mount cycle.
19 #
20 seq=`basename $0`
21 seqres=$RESULT_DIR/$seq
22 echo "QA output created by $seq"
23
24 here=`pwd`
25 tmp=/tmp/$$
26 status=1        # failure is the default!
27 trap "_cleanup; exit \$status" 0 1 2 3 15
28
29 _cleanup()
30 {
31         cd /
32         rm -f $tmp.*
33         # Unmount the nested overlay mount
34         $UMOUNT_PROG $mnt2 2>/dev/null
35         [ -z "$loopdev" ] || _destroy_loop_device $loopdev
36 }
37
38 # get standard environment, filters and checks
39 . ./common/rc
40 . ./common/filter
41
42 # real QA test starts here
43 _supported_fs overlay
44 _require_scratch_nocheck
45 _require_test_program "af_unix"
46 _require_test_program "t_dir_type"
47 _require_command "$FLOCK_PROG" "flock"
48 # We need to require all features together, because nfs_export cannot
49 # be enabled when index is disabled
50 _require_scratch_overlay_features index nfs_export redirect_dir
51 _require_loop
52
53 lower=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
54 upper=$OVL_BASE_SCRATCH_MNT/$OVL_UPPER
55 work=$OVL_BASE_SCRATCH_MNT/$OVL_WORK
56 # Lower dir of nested overlay is the scratch overlay mount at SCRATCH_MNT
57 upper2=$OVL_BASE_SCRATCH_MNT/$OVL_UPPER.2
58 work2=$OVL_BASE_SCRATCH_MNT/$OVL_WORK.2
59 mnt2=$OVL_BASE_SCRATCH_MNT/$OVL_MNT.2
60
61 lowerdir=$lower/lowertestdir
62 upperdir=$upper/uppertestdir
63 lowertestdir=$mnt2/lowertestdir
64 uppertestdir=$mnt2/uppertestdir
65
66 create_dirs()
67 {
68         # Create the underlying overlay dirs
69         _scratch_mkfs
70
71         # Create the nested overlay upper dirs
72         mkdir -p $upper2 $work2 $mnt2
73
74         # Create a loop device for blkdev tests
75         $XFS_IO_PROG -f -c "truncate 128k" $lower/img >> $seqres.full 2>&1
76         loopdev=`_create_loop_device $lower/img`
77 }
78
79 # Mount a nested overlay with $SCRATCH_MNT as lower layer
80 mount_dirs()
81 {
82         # Mount the underlying overlay with file handle support
83         _scratch_mount -o "index=on,nfs_export=on"
84
85         # Mount the nested overlay
86         # Enable redirect_dir for renaming a merge directory.
87         # Enabling xino in this test requires that base filesystem inode numbers will
88         # not use bit 63 in inode number of the test files, because bit 63 is used by
89         # overlayfs to indicate the layer. Let's just assume that this is true for all
90         # tested filesystems and if we are wrong, the test may fail.
91         _overlay_mount_dirs $SCRATCH_MNT $upper2 $work2 overlay2 $mnt2 \
92                 -o "redirect_dir=on,index=on,xino=on" || \
93                 _notrun "cannot mount nested overlay with xino=on option"
94         _fs_options overlay2 | grep -q "xino=on" || \
95                 _notrun "cannot enable xino feature on nested overlay"
96 }
97
98 # Unmount the nested overlay mount and check underlying overlay layers
99 unmount_dirs()
100 {
101         # unmount & check nested overlay
102         $UMOUNT_PROG $mnt2
103         _overlay_check_dirs $SCRATCH_MNT $upper2 $work2 \
104                 -o "redirect_dir=on,index=on,xino=on"
105
106         # unmount & check underlying overlay
107         $UMOUNT_PROG $SCRATCH_MNT
108         _overlay_check_scratch_dirs $lower $upper $work \
109                 -o "index=on,nfs_export=on"
110 }
111
112 FILES="dir file symlink link chrdev blkdev fifo socket"
113
114 create_test_files()
115 {
116         local dir=$1
117
118         # Create our test files.
119         mkdir -p $dir/dir
120         touch $dir/file
121         ln -s $dir/file $dir/symlink
122         touch $dir/link
123         ln $dir/link $dir/link2
124         cp -a /dev/zero $dir/chrdev
125         cp -a $loopdev $dir/blkdev
126         mknod $dir/fifo p
127         $here/src/af_unix $dir/socket
128 }
129
130 # Record inode numbers in format <ino> <basename>
131 record_inode_numbers()
132 {
133         local dir=$1
134         local outfile=$2
135
136         echo "record_inode_numbers $outfile" >> $seqres.full
137
138         for n in $FILES; do
139                 ls -id $dir/$n
140         done | \
141         while read ino file; do
142                 f=`basename $file`
143                 echo $ino $f | tee -a $seqres.full >> $outfile
144                 # /proc/locks exposes i_ino - compare it to st_ino. flock -n
145                 # doesn't follow symlink, blocks on fifo and fails on socket
146                 [[ $f =~ fifo|socket|symlink ]] || \
147                 $FLOCK_PROG -n $file cat /proc/locks | tee -a $seqres.full | grep -q ":$ino " || \
148                         echo "lock for $f not found by ino $ino ($outfile) - see $seqres.full"
149         done
150 }
151
152 # Check inode numbers match recorder inode numbers
153 check_inode_numbers()
154 {
155         local dir=$1
156         local before=$2
157         local after=$3
158
159         record_inode_numbers $dir $after
160
161         # Test constant stat(2) st_ino -
162         # Compare before..after - expect silence
163         # We use diff -u so out.bad will tell us which stage failed
164         diff -u $before $after
165
166         # Test constant readdir(3)/getdents(2) d_ino -
167         # Expect to find file by inode number
168         cat $before | while read ino f; do
169                 $here/src/t_dir_type $dir $ino | tee -a $seqres.full | grep -q $f || \
170                         echo "$f not found by ino $ino (from $before) - see $seqres.full"
171         done
172 }
173
174 rm -f $seqres.full
175
176 create_dirs
177
178 create_test_files $lowerdir
179 create_test_files $upperdir
180
181 mount_dirs
182
183 # Record inode numbers in the lower overlay
184 record_inode_numbers $SCRATCH_MNT/lowertestdir $tmp.lower.lo
185 record_inode_numbers $SCRATCH_MNT/uppertestdir $tmp.lower.up
186
187 # Record inode numbers before copy up
188 record_inode_numbers $lowertestdir $tmp.before.lo
189 record_inode_numbers $uppertestdir $tmp.before.up
190
191 # Copy up all files
192 for f in $FILES; do
193         # chown -h modifies all those file types
194         chown -h 100 $lowertestdir/$f
195         chown -h 100 $uppertestdir/$f
196 done
197
198 # Compare inode numbers before/after copy up
199 check_inode_numbers $lowertestdir $tmp.before.lo $tmp.after_copyup.lo
200 check_inode_numbers $uppertestdir $tmp.before.up $tmp.after_copyup.up
201
202 # Move all files to another dir
203 mkdir $lowertestdir.2 $uppertestdir.2
204
205 for f in $FILES; do
206         mv $lowertestdir/$f $lowertestdir.2/
207         mv $uppertestdir/$f $uppertestdir.2/
208 done
209
210 echo 3 > /proc/sys/vm/drop_caches
211
212 # Compare inode numbers before/after rename and drop caches
213 check_inode_numbers $lowertestdir.2 $tmp.after_copyup.lo $tmp.after_move.lo
214 check_inode_numbers $uppertestdir.2 $tmp.after_copyup.up $tmp.after_move.up
215
216 # Verify that the inode numbers survive a mount cycle
217 unmount_dirs
218 mount_dirs
219
220 # Compare inode numbers before/after mount cycle
221 check_inode_numbers $lowertestdir.2 $tmp.after_move.lo $tmp.after_cycle.lo
222 check_inode_numbers $uppertestdir.2 $tmp.after_move.up $tmp.after_cycle.up
223
224 unmount_dirs
225
226 echo "Silence is golden"
227 status=0
228 exit