xfs: Check for extent overflow when trivally adding a new extent
[xfstests-dev.git] / tests / btrfs / 081
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2014 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FSQA Test No. 081
6 #
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).
13 #
14 # This issue was fixed by the following linux kernel patch:
15 #
16 #     Btrfs: ensure readers see new data after a clone operation
17 #     (commit c125b8bff1d9f6c8c91ce4eb8bd5616058c7d510)
18 #
19 seq=`basename $0`
20 seqres=$RESULT_DIR/$seq
21 echo "QA output created by $seq"
22 tmp=/tmp/$$
23 status=1        # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
25
26 _cleanup()
27 {
28         rm -f $tmp.*
29 }
30
31 # get standard environment, filters and checks
32 . ./common/rc
33 . ./common/filter
34
35 # real QA test starts here
36 _supported_fs btrfs
37 _require_scratch
38 _require_cloner
39
40 rm -f $seqres.full
41
42 num_extents=100
43 extent_size=8192
44
45 create_source_file()
46 {
47         local name=$1
48
49         # Create a file with $num_extents extents, each with a size of
50         # $extent_size bytes.
51         touch $SCRATCH_MNT/$name
52         for ((i = 0; i < $num_extents; i++)); do
53                 off=$((i * $extent_size))
54                 $XFS_IO_PROG \
55                         -c "pwrite -S $i -b $extent_size $off $extent_size" \
56                         -c "fsync" $SCRATCH_MNT/$name | _filter_xfs_io
57         done
58 }
59
60 create_target_file()
61 {
62         local name=$1
63         local file_size=$(($num_extents * $extent_size))
64
65         $XFS_IO_PROG -f -c "pwrite -S 0xff 0 $file_size" \
66                 -c "fsync" $SCRATCH_MNT/$name | _filter_xfs_io
67 }
68
69 reader_loop()
70 {
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
76
77         local name=$1
78
79         while true; do
80                 cat $SCRATCH_MNT/$name > /dev/null
81         done
82 }
83
84 _scratch_mkfs >>$seqres.full 2>&1
85 _scratch_mount
86
87 echo "Creating source file..."
88 create_source_file "foo"
89 echo "Creating target file..."
90 create_target_file "bar"
91
92 reader_loop "bar" &
93 reader_pid=$!
94
95 $CLONER_PROG -s 0 -d 0 -l $(($num_extents * $extent_size)) \
96         $SCRATCH_MNT/foo $SCRATCH_MNT/bar
97
98 kill $reader_pid > /dev/null 2>&1
99
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
110 # valid data.
111 echo "Verifying file digests after cloning"
112 md5sum $SCRATCH_MNT/foo | _filter_scratch
113 md5sum $SCRATCH_MNT/bar | _filter_scratch
114
115 # Validate the content of bar still matches foo's content even after
116 # clearing all of bar's data from the page cache.
117 _scratch_cycle_mount
118 echo "Verifying target file digest after umount + mount"
119 md5sum $SCRATCH_MNT/bar | _filter_scratch
120
121 status=0
122 exit