overlay: correct scratch dirs check
[xfstests-dev.git] / tests / overlay / 044
1 #! /bin/bash
2 # FSQA Test No. 044
3 #
4 # Test hardlink breakage on non-samefs setup
5 # This is a variant of overlay/018 to test.
6 #
7 # This simple test demonstrates a known issue with overlayfs:
8 # - file A and B are hardlinked in lower
9 # - modify A to trigger copy up
10 # - file A is no longer a hardlink of file B
11 #
12 #-----------------------------------------------------------------------
13 #
14 # Copyright (C) 2017 IBM Corporation. All Rights Reserved.
15 # Author: Chandan Rajendra <chandan@linux.vnet.ibm.com>
16 #
17 # This program is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU General Public License as
19 # published by the Free Software Foundation.
20 #
21 # This program is distributed in the hope that it would be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write the Free Software Foundation,
28 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29 #-----------------------------------------------------------------------
30 #
31
32 seq=`basename $0`
33 seqres=$RESULT_DIR/$seq
34 echo "QA output created by $seq"
35
36 tmp=/tmp/$$
37 status=1        # failure is the default!
38 trap "_cleanup; exit \$status" 0 1 2 3 15
39
40 _cleanup()
41 {
42         rm -f $tmp.*
43 }
44
45 # get standard environment, filters and checks
46 . ./common/rc
47 . ./common/filter
48
49 # real QA test starts here
50 _supported_fs overlay
51 _supported_os Linux
52 # Use non-default scratch underlying overlay dirs, we need to check
53 # them explicity after test.
54 _require_scratch_nocheck
55 _require_test
56 _require_scratch_feature index
57 _require_test_program "t_dir_type"
58
59 rm -f $seqres.full
60
61 # Record inode numbers in format <ino> <nlink>
62 function record_ino_nlink()
63 {
64         ls -li $FILES | awk '{ print $1, $3, $10}' > $1
65 }
66
67 # Check inode numbers match recorded inode numbers
68 function check_ino_nlink()
69 {
70         dir=$1
71         before=$2
72         after=$3
73
74         record_ino_nlink $after
75
76         # Test constant stat(2) st_ino/st_nlink -
77         #   Compare before..after - expect silence
78         # We use diff -u so out.bad will tell us which stage failed
79         diff -u $before $after
80
81         # Test constant readdir(3)/getdents(2) d_ino -
82         #   Expect to find file by inode number
83         cat $before | while read ino nlink f; do
84                 $here/src/t_dir_type $dir $ino | grep -q $(basename $f) || \
85                         echo "$(basename $f) not found by ino $ino (from $before)"
86         done
87 }
88
89 lowerdir=$OVL_BASE_TEST_DIR/$seq-ovl-lower
90 rm -rf $lowerdir
91 mkdir $lowerdir
92
93 # Create 2 hardlinked files in lower
94 echo "zero" >> $lowerdir/foo
95 ln $lowerdir/foo $lowerdir/bar
96
97 _scratch_mkfs >>$seqres.full 2>&1
98
99 upperdir=$OVL_BASE_SCRATCH_MNT/$OVL_UPPER
100 workdir=$OVL_BASE_SCRATCH_MNT/$OVL_WORK
101
102 # Enable overlay index feature to prevent breaking hardlinks on copy up
103 _overlay_scratch_mount_dirs $lowerdir $upperdir $workdir -o index=on
104
105 rm -f $tmp.*
106
107 foo=$SCRATCH_MNT/foo
108 bar=$SCRATCH_MNT/bar
109
110 FILES="$foo $bar"
111
112 echo "== Before copy up =="
113 cat $FILES
114 record_ino_nlink $tmp.before
115
116 # Modify content of one of the hardlinks
117 # Intentionally modify the last hardlink in $FILES, so after mount cycle
118 # when reading the first file in $FILES, last file won't be in inode/dcache
119 echo "one" >> $bar
120
121 echo "== After write one =="
122 cat $FILES
123 check_ino_nlink $SCRATCH_MNT $tmp.before $tmp.after_one
124
125 # Verify that the hardlinks survive a mount cycle
126 $UMOUNT_PROG $SCRATCH_MNT
127 _overlay_check_scratch_dirs $lowerdir $upperdir $workdir -o index=on
128 _overlay_scratch_mount_dirs $lowerdir $upperdir $workdir -o index=on
129
130 echo "== After mount cycle =="
131 cat $FILES
132 check_ino_nlink $SCRATCH_MNT $tmp.after_one $tmp.after_cycle
133
134 # Drop caches to get the copied up hardlink out of cache
135 echo 3 > /proc/sys/vm/drop_caches
136
137 # Modify content of the other hardlink
138 echo "two" >> $foo
139
140 echo "== After write two =="
141 cat $FILES
142 check_ino_nlink $SCRATCH_MNT $tmp.after_one $tmp.after_two
143
144 # check overlayfs
145 _overlay_check_scratch_dirs $lowerdir $upperdir $workdir -o index=on
146
147 status=0
148 exit