misc: move exit status into trap handler
[xfstests-dev.git] / tests / btrfs / 098
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FSQA Test No. 098
6 #
7 # Test that if we fsync a file that got one extent partially cloned into a
8 # lower file offset, after a power failure our file has the same content it
9 # had before the power failure and after the extent cloning operation.
10 #
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20         _cleanup_flakey
21         rm -f $tmp.*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27 . ./common/dmflakey
28
29 # real QA test starts here
30 _supported_fs btrfs
31 _require_scratch
32 _require_dm_target flakey
33 _require_cloner
34
35 rm -f $seqres.full
36
37 _scratch_mkfs >>$seqres.full 2>&1
38 _require_metadata_journaling $SCRATCH_DEV
39 _init_flakey
40 _mount_flakey
41
42 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
43
44 # Create our test file with a single 25 block extent starting at file offset
45 # mapped by 200th block We fsync the file here to make the fsync log tree get a
46 # single csum item that covers the whole 25 block extent, which causes the
47 # second fsync, done after the cloning operation below, to not leave in the log
48 # tree two csum items covering two block sub-ranges ([0, 5[ and [5, 25[)) of our
49 # extent.
50 $XFS_IO_PROG -f -c "pwrite -S 0xaa $((200 * $BLOCK_SIZE)) $((25 * $BLOCK_SIZE))" \
51                 -c "fsync"                     \
52                 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
53
54
55 # Now clone part of our extent into file offset mapped by 100th block. This adds
56 # a file extent item to our inode's metadata that points to the 25 block extent
57 # we created before, using a data offset of 5 blocks and a data length of 5
58 # blocks, so that it refers to the block sub-range [5, 10[ of our original
59 # extent.
60 $CLONER_PROG -s $(((200 * $BLOCK_SIZE) + (5 * $BLOCK_SIZE))) \
61              -d $((100 * $BLOCK_SIZE)) -l $((5 * $BLOCK_SIZE)) \
62              $SCRATCH_MNT/foo $SCRATCH_MNT/foo
63
64 # Now fsync our file to make sure the extent cloning is durably persisted. This
65 # fsync will not add a second csum item to the log tree containing the checksums
66 # for the blocks in the block sub-range [5, 10[ of our extent, because there was
67 # already a csum item in the log tree covering the whole extent, added by the
68 # first fsync we did before.
69 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
70
71 echo "File contents before power failure:"
72 od -t x1 $SCRATCH_MNT/foo | _filter_od
73
74 # The fsync log replay first processes the file extent item corresponding to the
75 # file offset mapped by 100th block (the one which refers to the [5, 10[ block
76 # sub-range of our 25 block extent) and then processes the file extent item for
77 # file offset mapped by 200th block. It used to happen that when processing the
78 # later, it erroneously left in the csum tree 2 csum items that overlapped each
79 # other, 1 for the block sub-range [5, 10[ and 1 for the whole range of our
80 # extent. This introduced a problem where subsequent lookups for the checksums
81 # of blocks within the block range [10, 25[ of our extent would not find
82 # anything because lookups in the csum tree ended up looking only at the smaller
83 # csum item, the one covering the block subrange [5, 10[. This made read
84 # requests assume an expected checksum with a value of 0 for those blocks, which
85 # caused checksum verification failure when the read operations finished.
86 # However those checksum failure did not result in read requests returning an
87 # error to user space (like -EIO for e.g.) because the expected checksum value
88 # had the special value 0, and in that case btrfs set all bytes of the
89 # corresponding pages with the value 0x01 and produce the following warning in
90 # dmesg/syslog:
91 #
92 #  "BTRFS warning (device dm-0): csum failed ino 257 off 917504 csum 1322675045\
93 #    expected csum 0"
94 #
95 _flakey_drop_and_remount
96
97 echo "File contents after log replay:"
98 # Must match the file contents we had after cloning the extent and before
99 # the power failure happened.
100 od -t x1 $SCRATCH_MNT/foo | _filter_od
101
102 _unmount_flakey
103
104 status=0
105 exit