c367a01281dcc7d2ced326237824390c9e0558de
[xfstests-dev.git] / tests / btrfs / 081
1 #! /bin/bash
2 # FSQA Test No. 081
3 #
4 # Regression test for a btrfs clone ioctl issue where races between
5 # a clone operation and concurrent target file reads would result in
6 # leaving stale data in the page cache. After the clone operation
7 # finished, reading from the clone target file would return the old
8 # and no longer valid data. This affected only buffered reads (i.e.
9 # didn't affect direct IO reads).
10 #
11 # This issue was fixed by the following linux kernel patch:
12 #
13 #     Btrfs: ensure readers see new data after a clone operation
14 #     (commit c125b8bff1d9f6c8c91ce4eb8bd5616058c7d510)
15 #
16 #-----------------------------------------------------------------------
17 #
18 # Copyright (C) 2014 SUSE Linux Products GmbH. All Rights Reserved.
19 # Author: Filipe Manana <fdmanana@suse.com>
20 #
21 # This program is free software; you can redistribute it and/or
22 # modify it under the terms of the GNU General Public License as
23 # published by the Free Software Foundation.
24 #
25 # This program is distributed in the hope that it would be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 # GNU General Public License for more details.
29 #
30 # You should have received a copy of the GNU General Public License
31 # along with this program; if not, write the Free Software Foundation,
32 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
33 #-----------------------------------------------------------------------
34 #
35
36 seq=`basename $0`
37 seqres=$RESULT_DIR/$seq
38 echo "QA output created by $seq"
39 tmp=/tmp/$$
40 status=1        # failure is the default!
41 trap "_cleanup; exit \$status" 0 1 2 3 15
42
43 _cleanup()
44 {
45         rm -f $tmp.*
46 }
47
48 # get standard environment, filters and checks
49 . ./common/rc
50 . ./common/filter
51
52 # real QA test starts here
53 _supported_fs btrfs
54 _supported_os Linux
55 _require_scratch
56 _require_cloner
57
58 rm -f $seqres.full
59
60 num_extents=100
61 extent_size=8192
62
63 create_source_file()
64 {
65         name=$1
66
67         # Create a file with $num_extents extents, each with a size of
68         # $extent_size bytes.
69         touch $SCRATCH_MNT/$name
70         for ((i = 0; i < $num_extents; i++)); do
71                 off=$((i * $extent_size))
72                 $XFS_IO_PROG \
73                         -c "pwrite -S $i -b $extent_size $off $extent_size" \
74                         -c "fsync" $SCRATCH_MNT/$name | _filter_xfs_io
75         done
76 }
77
78 create_target_file()
79 {
80         name=$1
81         file_size=$(($num_extents * $extent_size))
82
83         $XFS_IO_PROG -f -c "pwrite -S 0xff 0 $file_size" \
84                 -c "fsync" $SCRATCH_MNT/$name | _filter_xfs_io
85 }
86
87 reader_loop()
88 {
89         name=$1
90
91         while true; do
92                 cat $SCRATCH_MNT/$name > /dev/null
93         done
94 }
95
96 _scratch_mkfs >>$seqres.full 2>&1
97 _scratch_mount
98
99 echo "Creating source file..."
100 create_source_file "foo"
101 echo "Creating target file..."
102 create_target_file "bar"
103
104 reader_loop "bar" &
105 reader_pid=$!
106
107 $CLONER_PROG -s 0 -d 0 -l $(($num_extents * $extent_size)) \
108         $SCRATCH_MNT/foo $SCRATCH_MNT/bar
109
110 kill $reader_pid > /dev/null 2>&1
111
112 # Now both foo and bar should have exactly the same content.
113 # This didn't use to be the case before the btrfs kernel fix mentioned
114 # above. The clone ioctl was racy, as it removed bar's pages from the
115 # page cache and only after it would update bar's metadata to point to
116 # the same extents that foo's metadata points to - and this was done in
117 # an unprotected way, so that a file read request done right after the
118 # clone ioctl removed the pages from the page cache and before it updated
119 # bar's metadata, would result in populating the page cache with stale
120 # data. Therefore a file read after the clone operation finished would
121 # not get the cloned data but it would get instead the old and no longer
122 # valid data.
123 echo "Verifying file digests after cloning"
124 md5sum $SCRATCH_MNT/foo | _filter_scratch
125 md5sum $SCRATCH_MNT/bar | _filter_scratch
126
127 # Validate the content of bar still matches foo's content even after
128 # clearing all of bar's data from the page cache.
129 _scratch_cycle_mount
130 echo "Verifying target file digest after umount + mount"
131 md5sum $SCRATCH_MNT/bar | _filter_scratch
132
133 status=0
134 exit