4 # Test that if we fsync a file that got one extent partially cloned into a
5 # lower file offset, after a power failure our file has the same content it
6 # had before the power failure and after the extent cloning operation.
8 #-----------------------------------------------------------------------
10 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
11 # Author: Filipe Manana <fdmanana@suse.com>
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License as
15 # published by the Free Software Foundation.
17 # This program is distributed in the hope that it would be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write the Free Software Foundation,
24 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #-----------------------------------------------------------------------
29 seqres=$RESULT_DIR/$seq
30 echo "QA output created by $seq"
32 status=1 # failure is the default!
33 trap "_cleanup; exit \$status" 0 1 2 3 15
41 # get standard environment, filters and checks
46 # real QA test starts here
51 _require_dm_target flakey
53 _require_metadata_journaling $SCRATCH_DEV
57 _scratch_mkfs >>$seqres.full 2>&1
61 # Create our test file with a single 100K extent starting at file offset 800K.
62 # We fsync the file here to make the fsync log tree gets a single csum item that
63 # covers the whole 100K extent, which causes the second fsync, done after the
64 # cloning operation below, to not leave in the log tree two csum items covering
65 # two sub-ranges ([0, 20K[ and [20K, 100K[)) of our extent.
66 $XFS_IO_PROG -f -c "pwrite -S 0xaa 800K 100K" \
68 $SCRATCH_MNT/foo | _filter_xfs_io
70 # Now clone part of our extent into file offset 400K. This adds a file extent
71 # item to our inode's metadata that points to the 100K extent we created before,
72 # using a data offset of 20K and a data length of 20K, so that it refers to
73 # the sub-range [20K, 40K[ of our original extent.
74 $CLONER_PROG -s $((800 * 1024 + 20 * 1024)) -d $((400 * 1024)) \
75 -l $((20 * 1024)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
77 # Now fsync our file to make sure the extent cloning is durably persisted. This
78 # fsync will not add a second csum item to the log tree containing the checksums
79 # for the blocks in the sub-range [20K, 40K[ of our extent, because there was
80 # already a csum item in the log tree covering the whole extent, added by the
81 # first fsync we did before.
82 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
84 echo "File digest before power failure:"
85 md5sum $SCRATCH_MNT/foo | _filter_scratch
87 # Silently drop all writes and ummount to simulate a crash/power failure.
88 _load_flakey_table $FLAKEY_DROP_WRITES
91 # Allow writes again, mount to trigger log replay and validate file contents.
92 # The fsync log replay first processes the file extent item corresponding to the
93 # file offset 400K (the one which refers to the [20K, 40K[ sub-range of our 100K
94 # extent) and then processes the file extent item for file offset 800K. It used
95 # to happen that when processing the later, it erroneously left in the csum tree
96 # 2 csum items that overlapped each other, 1 for the sub-range [20K, 40K[ and 1
97 # for the whole range of our extent. This introduced a problem where subsequent
98 # lookups for the checksums of blocks within the range [40K, 100K[ of our extent
99 # would not find anything because lookups in the csum tree ended up looking only
100 # at the smaller csum item, the one covering the subrange [20K, 40K[. This made
101 # read requests assume an expected checksum with a value of 0 for those blocks,
102 # which caused checksum verification failure when the read operations finished.
103 # However those checksum failure did not result in read requests returning an
104 # error to user space (like -EIO for e.g.) because the expected checksum value
105 # had the special value 0, and in that case btrfs set all bytes of the
106 # corresponding pages with the value 0x01 and produce the following warning in
109 # "BTRFS warning (device dm-0): csum failed ino 257 off 917504 csum 1322675045\
112 _load_flakey_table $FLAKEY_ALLOW_WRITES
115 echo "File digest after log replay:"
116 # Must match the same digest he had after cloning the extent and before the
117 # power failure happened.
118 md5sum $SCRATCH_MNT/foo | _filter_scratch