032d1ffe5819c601827c0a4a4cb7c9e1cdaa965e
[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 _need_to_be_root
52 _supported_fs btrfs
53 _supported_os Linux
54 _require_scratch
55 _require_dm_flakey
56 _require_cloner
57 _require_metadata_journaling $SCRATCH_DEV
58 _require_xfs_io_command "falloc"
59
60 rm -f $seqres.full
61
62 _scratch_mkfs >>$seqres.full 2>&1
63 _init_flakey
64 _mount_flakey
65
66 # Create prealloc extent covering range [160K, 620K[
67 $XFS_IO_PROG -f -c "falloc 160K 460K" $SCRATCH_MNT/foo
68
69 # Now write to the last 80K of the prealloc extent plus 40K to the unallocated
70 # space that immediately follows it. This creates a new extent of 40K that spans
71 # the range [620K, 660K[.
72 $XFS_IO_PROG -c "pwrite -S 0xaa 540K 120K" $SCRATCH_MNT/foo | _filter_xfs_io
73
74 # At this point, there are now 2 back references to the prealloc extent in our
75 # extent tree. Both are for our file offset 160K and one relates to a file
76 # extent item with a data offset of 0 and a length of 380K, while the other
77 # relates to a file extent item with a data offset of 380K and a length of 80K.
78
79 # Make sure everything done so far is durably persisted (all back references are
80 # in the extent tree, etc).
81 sync
82
83 # Now clone all extents of our file that cover the offset 160K up to its eof
84 # (660K at this point) into itself at offset 2M. This leaves a hole in the file
85 # covering the range [660K, 2M[. The prealloc extent will now be referenced by
86 # the file twice, once for offset 160K and once for offset 2M. The 40K extent
87 # that follows the prealloc extent will also be referenced twice by our file,
88 # once for offset 620K and once for offset 2M + 460K.
89 $CLONER_PROG -s $((160 * 1024)) -d $((2 * 1024 * 1024)) -l 0 $SCRATCH_MNT/foo \
90         $SCRATCH_MNT/foo
91
92 # Now create one new extent in our file with a size of 100Kb. It will span the
93 # range [3M, 3M + 100K[. It also will cause creation of a hole spanning the
94 # range [2M + 460K, 3M[. Our new file size is 3M + 100K.
95 $XFS_IO_PROG -c "pwrite -S 0xbb 3M 100K" $SCRATCH_MNT/foo | _filter_xfs_io
96
97 # At this point, there are now (in memory) 4 back references to the prealloc
98 # extent.
99 #
100 # Two of them are for file offset 160K, related to file extent items
101 # matching the file offsets 160K and 540K respectively, with data offsets of
102 # 0 and 380K respectively, and with lengths of 380K and 80K respectively.
103 #
104 # The other two references are for file offset 2M, related to file extent items
105 # matching the file offsets 2M and 2M + 380K respectively, with data offsets of
106 # 0 and 380K respectively, and with lengths of 380K and 80K respectively.
107 #
108 # The 40K extent has 2 back references, one for file offset 620K and the other
109 # for file offset 2M + 460K.
110 #
111 # The 100K extent has a single back reference and it relates to file offset 3M.
112
113 # Now clone our 100K extent into offset 600K. That offset covers the last 20K
114 # of the prealloc extent, the whole 40K extent and 40K of the hole starting at
115 # offset 660K.
116 $CLONER_PROG -s $((3 * 1024 * 1024)) -d $((600 * 1024)) -l $((100 * 1024)) \
117         $SCRATCH_MNT/foo $SCRATCH_MNT/foo
118
119 # At this point there's only one reference to the 40K extent, at file offset
120 # 2M + 460K, we have 4 references for the prealloc extent (2 for file offset
121 # 160K and 2 for file offset 2M) and 2 references for the 100K extent (1 for
122 # file offset 3M and a new one for file offset 600K).
123
124 # Now fsync our file to make all its new data and metadata updates are durably
125 # persisted and present if a power failure/crash happens after a successful
126 # fsync and before the next transaction commit.
127 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
128
129 echo "File digest before power failure:"
130 md5sum $SCRATCH_MNT/foo | _filter_scratch
131
132 # Silently drop all writes and ummount to simulate a crash/power failure.
133 _load_flakey_table $FLAKEY_DROP_WRITES
134 _unmount_flakey
135
136 # Allow writes again, mount to trigger log replay and validate file contents.
137 # During log replay, the btrfs delayed references implementation used to run the
138 # deletion of back references before the addition of new back references, which
139 # made the addition fail as it didn't find the key in the extent tree that it
140 # was looking for. The failure triggered by this test was related to the 40K
141 # extent, which got 1 reference dropped and 1 reference added during the fsync
142 # log replay - when running the delayed references at transaction commit time,
143 # btrfs was applying the deletion before the insertion, resulting in a failure
144 # of the insertion that ended up turning the fs into read-only mode.
145 _load_flakey_table $FLAKEY_ALLOW_WRITES
146 _mount_flakey
147
148 echo "File digest after log replay:"
149 md5sum $SCRATCH_MNT/foo | _filter_scratch
150
151 _unmount_flakey
152
153 status=0
154 exit