2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
7 # Regression test for adding and dropping an equal number of references for
8 # file extents. Verify that if we drop N references for a file extent and we
9 # add too N new references for that same file extent in the same transaction,
10 # running the delayed references (always happens at transaction commit time)
13 # The regression was introduced in the 4.2-rc1 Linux kernel.
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
19 status=1 # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
28 # get standard environment, filters and checks
33 # real QA test starts here
37 _require_dm_target flakey
39 _require_xfs_io_command "falloc"
43 _scratch_mkfs >>$seqres.full 2>&1
44 _require_metadata_journaling $SCRATCH_DEV
48 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
50 # Create prealloc extent covering file block range [40, 155[
51 $XFS_IO_PROG -f -c "falloc $((40 * $BLOCK_SIZE)) $((115 * $BLOCK_SIZE))" \
54 # Now write to the last 20 blocks of the prealloc extent plus 10 blocks to the
55 # unallocated space that immediately follows it. This creates a new extent of 10
56 # blocks that spans the block range [155, 165[.
57 $XFS_IO_PROG -c "pwrite -S 0xaa $((135 * $BLOCK_SIZE)) $((30 * $BLOCK_SIZE))" \
58 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
60 # At this point, there are now 2 back references to the prealloc extent in our
61 # extent tree. Both are for our file offset mapped by the 40th block of the file
62 # and one relates to a file extent item with a data offset of 0 and a length of
63 # 95 blocks, while the other relates to a file extent item with a data offset of
64 # 95 blocks and a length of 20 blocks.
66 # Make sure everything done so far is durably persisted (all back references are
67 # in the extent tree, etc).
70 # Now clone all extents of our file that cover the file range spanned by 40th
71 # block up to its eof (165th block at this point) into itself at 512th
72 # block. This leaves a hole in the file covering the block range [165, 512[. The
73 # prealloc extent will now be referenced by the file twice, once for offset
74 # mapped by the 40th block and once for offset mapped by 512th block. The 10
75 # blocks extent that follows the prealloc extent will also be referenced twice
76 # by our file, once for offset mapped by the 155th block and once for offset
77 # (512 block + 115 blocks)
78 $CLONER_PROG -s $((40 * $BLOCK_SIZE)) -d $((512 * $BLOCK_SIZE)) -l 0 \
79 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
81 # Now create one new extent in our file with a size of 25 blocks. It will span
82 # the block range [768, 768 + 25[. It also will cause creation of a hole
83 # spanning the block range [512 + 115, 768[. Our new file size is the file
84 # offset mapped by (768 + 25)th block.
85 $XFS_IO_PROG -c "pwrite -S 0xbb $((768 * $BLOCK_SIZE)) $((25 * $BLOCK_SIZE))" \
86 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
88 # At this point, there are now (in memory) 4 back references to the prealloc
91 # Two of them are for file offset mapped by the 40th block, related to file
92 # extent items matching the file offsets mapped by 40th and 135th block
93 # respectively, with data offsets of 0 and 95 blocks respectively, and with
94 # lengths of 95 and 20 blocks respectively.
96 # The other two references are for file offset mapped by 512th block, related to
97 # file extent items matching the file offsets mapped by 512th and (512 + 95)th
98 # block respectively, with data offsets mapped by 0th and 95th block
99 # respectively, and with lengths of 95 and 20 blocks respectively.
101 # The 10 block extent has 2 back references, one for file offset mapped by 155th
102 # block and the other for file offset mapped by (512 + 115)th block.
104 # The 25 blocks extent has a single back reference and it relates to file offset
105 # mapped by 768th block.
108 # Now clone our 25 block extent into offset mapped by 150th block. That offset
109 # covers the last 5 blocks of the prealloc extent, the whole 10 block extent and
110 # 10 blocks of the hole starting at offset mapped by 165th block.
111 $CLONER_PROG -s $((768 * $BLOCK_SIZE)) -d $((150 * $BLOCK_SIZE)) -l $((25 * $BLOCK_SIZE)) \
112 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
114 # At this point there's only one reference to the 10 block extent, at file
115 # offset mapped by (512 + 115) block, we have 4 references for the prealloc
116 # extent (2 for file offset mapped by 40th block and 2 for file offset mapped by
117 # 512th block) and 2 references for the 25 block extent (1 for file offset
118 # mapped by 768th block and a new one for file offset mapped by 150th block).
120 # Now fsync our file to make all its new data and metadata updates are durably
121 # persisted and present if a power failure/crash happens after a successful
122 # fsync and before the next transaction commit.
123 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
125 echo "File contents before power failure:"
126 od -t x1 $SCRATCH_MNT/foo | _filter_od
128 # During log replay, the btrfs delayed references implementation used to run the
129 # deletion of back references before the addition of new back references, which
130 # made the addition fail as it didn't find the key in the extent tree that it
131 # was looking for. The failure triggered by this test was related to the 10
132 # block extent, which got 1 reference dropped and 1 reference added during the
133 # fsync log replay - when running the delayed references at transaction commit
134 # time, btrfs was applying the deletion before the insertion, resulting in a
135 # failure of the insertion that ended up turning the fs into read-only mode.
136 _flakey_drop_and_remount
138 echo "File contents after log replay:"
139 od -t x1 $SCRATCH_MNT/foo | _filter_od