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