overlay/029: fix test failure with nfs_export feature enabled
[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 _supported_os Linux
45 _require_scratch_nocheck
46 _require_test_program "af_unix"
47 _require_test_program "t_dir_type"
48 _require_command "$FLOCK_PROG" "flock"
49 # We need to require all features together, because nfs_export cannot
50 # be enabled when index is disabled
51 _require_scratch_overlay_features index nfs_export redirect_dir
52 _require_loop
53
54 lower=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
55 upper=$OVL_BASE_SCRATCH_MNT/$OVL_UPPER
56 work=$OVL_BASE_SCRATCH_MNT/$OVL_WORK
57 # Lower dir of nested overlay is the scratch overlay mount at SCRATCH_MNT
58 upper2=$OVL_BASE_SCRATCH_MNT/$OVL_UPPER.2
59 work2=$OVL_BASE_SCRATCH_MNT/$OVL_WORK.2
60 mnt2=$OVL_BASE_SCRATCH_MNT/$OVL_MNT.2
61
62 lowerdir=$lower/lowertestdir
63 upperdir=$upper/uppertestdir
64 lowertestdir=$mnt2/lowertestdir
65 uppertestdir=$mnt2/uppertestdir
66
67 create_dirs()
68 {
69         # Create the underlying overlay dirs
70         _scratch_mkfs
71
72         # Create the nested overlay upper dirs
73         mkdir -p $upper2 $work2 $mnt2
74
75         # Create a loop device for blkdev tests
76         $XFS_IO_PROG -f -c "truncate 128k" $lower/img >> $seqres.full 2>&1
77         loopdev=`_create_loop_device $lower/img`
78 }
79
80 # Mount a nested overlay with $SCRATCH_MNT as lower layer
81 mount_dirs()
82 {
83         # Mount the underlying overlay with file handle support
84         _scratch_mount -o "index=on,nfs_export=on"
85
86         # Mount the nested overlay
87         # Enable redirect_dir for renaming a merge directory.
88         # Enabling xino in this test requires that base filesystem inode numbers will
89         # not use bit 63 in inode number of the test files, because bit 63 is used by
90         # overlayfs to indicate the layer. Let's just assume that this is true for all
91         # tested filesystems and if we are wrong, the test may fail.
92         _overlay_mount_dirs $SCRATCH_MNT $upper2 $work2 overlay2 $mnt2 \
93                 -o "redirect_dir=on,index=on,xino=on" || \
94                 _notrun "cannot mount nested overlay with xino=on option"
95         _fs_options overlay2 | grep -q "xino=on" || \
96                 _notrun "cannot enable xino feature on nested overlay"
97 }
98
99 # Unmount the nested overlay mount and check underlying overlay layers
100 unmount_dirs()
101 {
102         # unmount & check nested overlay
103         $UMOUNT_PROG $mnt2
104         _overlay_check_dirs $SCRATCH_MNT $upper2 $work2 \
105                 -o "redirect_dir=on,index=on,xino=on"
106
107         # unmount & check underlying overlay
108         $UMOUNT_PROG $SCRATCH_MNT
109         _overlay_check_scratch_dirs $lower $upper $work \
110                 -o "index=on,nfs_export=on"
111 }
112
113 FILES="dir file symlink link chrdev blkdev fifo socket"
114
115 create_test_files()
116 {
117         local dir=$1
118
119         # Create our test files.
120         mkdir -p $dir/dir
121         touch $dir/file
122         ln -s $dir/file $dir/symlink
123         touch $dir/link
124         ln $dir/link $dir/link2
125         cp -a /dev/zero $dir/chrdev
126         cp -a $loopdev $dir/blkdev
127         mknod $dir/fifo p
128         $here/src/af_unix $dir/socket
129 }
130
131 # Record inode numbers in format <ino> <basename>
132 record_inode_numbers()
133 {
134         local dir=$1
135         local outfile=$2
136
137         echo "record_inode_numbers $outfile" >> $seqres.full
138
139         for n in $FILES; do
140                 ls -id $dir/$n
141         done | \
142         while read ino file; do
143                 f=`basename $file`
144                 echo $ino $f | tee -a $seqres.full >> $outfile
145                 # /proc/locks exposes i_ino - compare it to st_ino. flock -n
146                 # doesn't follow symlink, blocks on fifo and fails on socket
147                 [[ $f =~ fifo|socket|symlink ]] || \
148                 $FLOCK_PROG -n $file cat /proc/locks | tee -a $seqres.full | grep -q ":$ino " || \
149                         echo "lock for $f not found by ino $ino ($outfile) - see $seqres.full"
150         done
151 }
152
153 # Check inode numbers match recorder inode numbers
154 check_inode_numbers()
155 {
156         local dir=$1
157         local before=$2
158         local after=$3
159
160         record_inode_numbers $dir $after
161
162         # Test constant stat(2) st_ino -
163         # Compare before..after - expect silence
164         # We use diff -u so out.bad will tell us which stage failed
165         diff -u $before $after
166
167         # Test constant readdir(3)/getdents(2) d_ino -
168         # Expect to find file by inode number
169         cat $before | while read ino f; do
170                 $here/src/t_dir_type $dir $ino | tee -a $seqres.full | grep -q $f || \
171                         echo "$f not found by ino $ino (from $before) - see $seqres.full"
172         done
173 }
174
175 rm -f $seqres.full
176
177 create_dirs
178
179 create_test_files $lowerdir
180 create_test_files $upperdir
181
182 mount_dirs
183
184 # Record inode numbers in the lower overlay
185 record_inode_numbers $SCRATCH_MNT/lowertestdir $tmp.lower.lo
186 record_inode_numbers $SCRATCH_MNT/uppertestdir $tmp.lower.up
187
188 # Record inode numbers before copy up
189 record_inode_numbers $lowertestdir $tmp.before.lo
190 record_inode_numbers $uppertestdir $tmp.before.up
191
192 # Copy up all files
193 for f in $FILES; do
194         # chown -h modifies all those file types
195         chown -h 100 $lowertestdir/$f
196         chown -h 100 $uppertestdir/$f
197 done
198
199 # Compare inode numbers before/after copy up
200 check_inode_numbers $lowertestdir $tmp.before.lo $tmp.after_copyup.lo
201 check_inode_numbers $uppertestdir $tmp.before.up $tmp.after_copyup.up
202
203 # Move all files to another dir
204 mkdir $lowertestdir.2 $uppertestdir.2
205
206 for f in $FILES; do
207         mv $lowertestdir/$f $lowertestdir.2/
208         mv $uppertestdir/$f $uppertestdir.2/
209 done
210
211 echo 3 > /proc/sys/vm/drop_caches
212
213 # Compare inode numbers before/after rename and drop caches
214 check_inode_numbers $lowertestdir.2 $tmp.after_copyup.lo $tmp.after_move.lo
215 check_inode_numbers $uppertestdir.2 $tmp.after_copyup.up $tmp.after_move.up
216
217 # Verify that the inode numbers survive a mount cycle
218 unmount_dirs
219 mount_dirs
220
221 # Compare inode numbers before/after mount cycle
222 check_inode_numbers $lowertestdir.2 $tmp.after_move.lo $tmp.after_cycle.lo
223 check_inode_numbers $uppertestdir.2 $tmp.after_move.up $tmp.after_cycle.up
224
225 unmount_dirs
226
227 echo "Silence is golden"
228 status=0
229 exit