2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2014 SUSE Linux Products GmbH. All Rights Reserved.
7 # Regression test for a btrfs clone ioctl issue where races between
8 # a clone operation and concurrent target file reads would result in
9 # leaving stale data in the page cache. After the clone operation
10 # finished, reading from the clone target file would return the old
11 # and no longer valid data. This affected only buffered reads (i.e.
12 # didn't affect direct IO reads).
14 # This issue was fixed by the following linux kernel patch:
16 # Btrfs: ensure readers see new data after a clone operation
17 # (commit c125b8bff1d9f6c8c91ce4eb8bd5616058c7d510)
20 seqres=$RESULT_DIR/$seq
21 echo "QA output created by $seq"
23 status=1 # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
31 # get standard environment, filters and checks
35 # real QA test starts here
49 # Create a file with $num_extents extents, each with a size of
51 touch $SCRATCH_MNT/$name
52 for ((i = 0; i < $num_extents; i++)); do
53 off=$((i * $extent_size))
55 -c "pwrite -S $i -b $extent_size $off $extent_size" \
56 -c "fsync" $SCRATCH_MNT/$name | _filter_xfs_io
63 local file_size=$(($num_extents * $extent_size))
65 $XFS_IO_PROG -f -c "pwrite -S 0xff 0 $file_size" \
66 -c "fsync" $SCRATCH_MNT/$name | _filter_xfs_io
71 # Wait for any running 'cat' subcommand before exitting so that after
72 # the test kills the reader loop subshell it does not fail with an
73 # error message from a 'cat' subcommand due to the test file being
74 # temporarily unavailable due to the scratch fs unmount operation.
75 trap "wait; exit" SIGTERM
80 cat $SCRATCH_MNT/$name > /dev/null
84 _scratch_mkfs >>$seqres.full 2>&1
87 echo "Creating source file..."
88 create_source_file "foo"
89 echo "Creating target file..."
90 create_target_file "bar"
95 $CLONER_PROG -s 0 -d 0 -l $(($num_extents * $extent_size)) \
96 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
98 kill $reader_pid > /dev/null 2>&1
100 # Now both foo and bar should have exactly the same content.
101 # This didn't use to be the case before the btrfs kernel fix mentioned
102 # above. The clone ioctl was racy, as it removed bar's pages from the
103 # page cache and only after it would update bar's metadata to point to
104 # the same extents that foo's metadata points to - and this was done in
105 # an unprotected way, so that a file read request done right after the
106 # clone ioctl removed the pages from the page cache and before it updated
107 # bar's metadata, would result in populating the page cache with stale
108 # data. Therefore a file read after the clone operation finished would
109 # not get the cloned data but it would get instead the old and no longer
111 echo "Verifying file digests after cloning"
112 md5sum $SCRATCH_MNT/foo | _filter_scratch
113 md5sum $SCRATCH_MNT/bar | _filter_scratch
115 # Validate the content of bar still matches foo's content even after
116 # clearing all of bar's data from the page cache.
118 echo "Verifying target file digest after umount + mount"
119 md5sum $SCRATCH_MNT/bar | _filter_scratch