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).
11 # This issue was fixed by the following linux kernel patch:
13 # Btrfs: ensure readers see new data after a clone operation
14 # (commit c125b8bff1d9f6c8c91ce4eb8bd5616058c7d510)
16 #-----------------------------------------------------------------------
18 # Copyright (C) 2014 SUSE Linux Products GmbH. All Rights Reserved.
19 # Author: Filipe Manana <fdmanana@suse.com>
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.
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.
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 #-----------------------------------------------------------------------
37 seqres=$RESULT_DIR/$seq
38 echo "QA output created by $seq"
40 status=1 # failure is the default!
41 trap "_cleanup; exit \$status" 0 1 2 3 15
48 # get standard environment, filters and checks
52 # real QA test starts here
67 # Create a file with $num_extents extents, each with a size of
69 touch $SCRATCH_MNT/$name
70 for ((i = 0; i < $num_extents; i++)); do
71 off=$((i * $extent_size))
73 -c "pwrite -S $i -b $extent_size $off $extent_size" \
74 -c "fsync" $SCRATCH_MNT/$name | _filter_xfs_io
81 file_size=$(($num_extents * $extent_size))
83 $XFS_IO_PROG -f -c "pwrite -S 0xff 0 $file_size" \
84 -c "fsync" $SCRATCH_MNT/$name | _filter_xfs_io
92 cat $SCRATCH_MNT/$name > /dev/null
96 _scratch_mkfs >>$seqres.full 2>&1
99 echo "Creating source file..."
100 create_source_file "foo"
101 echo "Creating target file..."
102 create_target_file "bar"
107 $CLONER_PROG -s 0 -d 0 -l $(($num_extents * $extent_size)) \
108 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
110 kill $reader_pid > /dev/null 2>&1
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
123 echo "Verifying file digests after cloning"
124 md5sum $SCRATCH_MNT/foo | _filter_scratch
125 md5sum $SCRATCH_MNT/bar | _filter_scratch
127 # Validate the content of bar still matches foo's content even after
128 # clearing all of bar's data from the page cache.
130 echo "Verifying target file digest after umount + mount"
131 md5sum $SCRATCH_MNT/bar | _filter_scratch