b477b5714c2b52a717ff9e5f86e1ac004a50fc9f
[xfstests-dev.git] / tests / btrfs / 095
1 #! /bin/bash
2 # FSQA Test No. 095
3 #
4 # Regression test for adding and dropping an equal number of references for
5 # file extents. Verify that if we drop N references for a file extent and we
6 # add too N new references for that same file extent in the same transaction,
7 # running the delayed references (always happens at transaction commit time)
8 # does not fail.
9 #
10 # The regression was introduced in the 4.2-rc1 Linux kernel.
11 #
12 #-----------------------------------------------------------------------
13 #
14 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
15 # Author: Filipe Manana <fdmanana@suse.com>
16 #
17 # This program is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU General Public License as
19 # published by the Free Software Foundation.
20 #
21 # This program is distributed in the hope that it would be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write the Free Software Foundation,
28 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29 #-----------------------------------------------------------------------
30 #
31
32 seq=`basename $0`
33 seqres=$RESULT_DIR/$seq
34 echo "QA output created by $seq"
35 tmp=/tmp/$$
36 status=1        # failure is the default!
37 trap "_cleanup; exit \$status" 0 1 2 3 15
38
39 _cleanup()
40 {
41         _cleanup_flakey
42         rm -f $tmp.*
43 }
44
45 # get standard environment, filters and checks
46 . ./common/rc
47 . ./common/filter
48 . ./common/dmflakey
49
50 # real QA test starts here
51 _supported_fs btrfs
52 _supported_os Linux
53 _require_scratch
54 _require_dm_target flakey
55 _require_cloner
56 _require_metadata_journaling $SCRATCH_DEV
57 _require_xfs_io_command "falloc"
58
59 rm -f $seqres.full
60
61 _scratch_mkfs >>$seqres.full 2>&1
62 _init_flakey
63 _mount_flakey
64
65 BLOCK_SIZE=$(get_block_size $SCRATCH_MNT)
66
67 # Create prealloc extent covering file block range [40, 155[
68 $XFS_IO_PROG -f -c "falloc $((40 * $BLOCK_SIZE)) $((115 * $BLOCK_SIZE))" \
69              $SCRATCH_MNT/foo
70
71 # Now write to the last 20 blocks of the prealloc extent plus 10 blocks to the
72 # unallocated space that immediately follows it. This creates a new extent of 10
73 # blocks that spans the block range [155, 165[.
74 $XFS_IO_PROG -c "pwrite -S 0xaa $((135 * $BLOCK_SIZE)) $((30 * $BLOCK_SIZE))" \
75              $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
76
77 # At this point, there are now 2 back references to the prealloc extent in our
78 # extent tree. Both are for our file offset mapped by the 40th block of the file
79 # and one relates to a file extent item with a data offset of 0 and a length of
80 # 95 blocks, while the other relates to a file extent item with a data offset of
81 # 95 blocks and a length of 20 blocks.
82
83 # Make sure everything done so far is durably persisted (all back references are
84 # in the extent tree, etc).
85 sync
86
87 # Now clone all extents of our file that cover the file range spanned by 40th
88 # block up to its eof (165th block at this point) into itself at 512th
89 # block. This leaves a hole in the file covering the block range [165, 512[. The
90 # prealloc extent will now be referenced by the file twice, once for offset
91 # mapped by the 40th block and once for offset mapped by 512th block. The 10
92 # blocks extent that follows the prealloc extent will also be referenced twice
93 # by our file, once for offset mapped by the 155th block and once for offset
94 # (512 block + 115 blocks)
95 $CLONER_PROG -s $((40 * $BLOCK_SIZE)) -d $((512 * $BLOCK_SIZE)) -l 0 \
96              $SCRATCH_MNT/foo $SCRATCH_MNT/foo
97
98 # Now create one new extent in our file with a size of 25 blocks. It will span
99 # the block range [768, 768 + 25[. It also will cause creation of a hole
100 # spanning the block range [512 + 115, 768[. Our new file size is the file
101 # offset mapped by (768 + 25)th block.
102 $XFS_IO_PROG -c "pwrite -S 0xbb $((768 * $BLOCK_SIZE)) $((25 * $BLOCK_SIZE))" \
103              $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
104
105 # At this point, there are now (in memory) 4 back references to the prealloc
106 # extent.
107 #
108 # Two of them are for file offset mapped by the 40th block, related to file
109 # extent items matching the file offsets mapped by 40th and 135th block
110 # respectively, with data offsets of 0 and 95 blocks respectively, and with
111 # lengths of 95 and 20 blocks respectively.
112 #
113 # The other two references are for file offset mapped by 512th block, related to
114 # file extent items matching the file offsets mapped by 512th and (512 + 95)th
115 # block respectively, with data offsets mapped by 0th and 95th block
116 # respectively, and with lengths of 95 and 20 blocks respectively.
117 #
118 # The 10 block extent has 2 back references, one for file offset mapped by 155th
119 # block and the other for file offset mapped by (512 + 115)th block.
120 #
121 # The 25 blocks extent has a single back reference and it relates to file offset
122 # mapped by 768th block.
123
124
125 # Now clone our 25 block extent into offset mapped by 150th block. That offset
126 # covers the last 5 blocks of the prealloc extent, the whole 10 block extent and
127 # 10 blocks of the hole starting at offset mapped by 165th block.
128 $CLONER_PROG -s $((768 * $BLOCK_SIZE)) -d $((150 * $BLOCK_SIZE)) -l $((25 * $BLOCK_SIZE)) \
129         $SCRATCH_MNT/foo $SCRATCH_MNT/foo
130
131 # At this point there's only one reference to the 10 block extent, at file
132 # offset mapped by (512 + 115) block, we have 4 references for the prealloc
133 # extent (2 for file offset mapped by 40th block and 2 for file offset mapped by
134 # 512th block) and 2 references for the 25 block extent (1 for file offset
135 # mapped by 768th block and a new one for file offset mapped by 150th block).
136
137 # Now fsync our file to make all its new data and metadata updates are durably
138 # persisted and present if a power failure/crash happens after a successful
139 # fsync and before the next transaction commit.
140 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
141
142 echo "File contents before power failure:"
143 od -t x1 $SCRATCH_MNT/foo | _filter_od
144
145 # During log replay, the btrfs delayed references implementation used to run the
146 # deletion of back references before the addition of new back references, which
147 # made the addition fail as it didn't find the key in the extent tree that it
148 # was looking for. The failure triggered by this test was related to the 10
149 # block extent, which got 1 reference dropped and 1 reference added during the
150 # fsync log replay - when running the delayed references at transaction commit
151 # time, btrfs was applying the deletion before the insertion, resulting in a
152 # failure of the insertion that ended up turning the fs into read-only mode.
153 _flakey_drop_and_remount
154
155 echo "File contents after log replay:"
156 od -t x1 $SCRATCH_MNT/foo | _filter_od
157
158 _unmount_flakey
159
160 status=0
161 exit