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