btrfs: check qgroup doesn't crash when beyond limit
[xfstests-dev.git] / tests / btrfs / 095
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. 095
6 #
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)
11 # does not fail.
12 #
13 # The regression was introduced in the 4.2-rc1 Linux kernel.
14 #
15 seq=`basename $0`
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
18 tmp=/tmp/$$
19 status=1        # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         _cleanup_flakey
25         rm -f $tmp.*
26 }
27
28 # get standard environment, filters and checks
29 . ./common/rc
30 . ./common/filter
31 . ./common/dmflakey
32
33 # real QA test starts here
34 _supported_fs btrfs
35 _require_scratch
36 _require_dm_target flakey
37 _require_cloner
38 _require_xfs_io_command "falloc"
39
40 rm -f $seqres.full
41
42 _scratch_mkfs >>$seqres.full 2>&1
43 _require_metadata_journaling $SCRATCH_DEV
44 _init_flakey
45 _mount_flakey
46
47 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
48
49 # Create prealloc extent covering file block range [40, 155[
50 $XFS_IO_PROG -f -c "falloc $((40 * $BLOCK_SIZE)) $((115 * $BLOCK_SIZE))" \
51              $SCRATCH_MNT/foo
52
53 # Now write to the last 20 blocks of the prealloc extent plus 10 blocks to the
54 # unallocated space that immediately follows it. This creates a new extent of 10
55 # blocks that spans the block range [155, 165[.
56 $XFS_IO_PROG -c "pwrite -S 0xaa $((135 * $BLOCK_SIZE)) $((30 * $BLOCK_SIZE))" \
57              $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
58
59 # At this point, there are now 2 back references to the prealloc extent in our
60 # extent tree. Both are for our file offset mapped by the 40th block of the file
61 # and one relates to a file extent item with a data offset of 0 and a length of
62 # 95 blocks, while the other relates to a file extent item with a data offset of
63 # 95 blocks and a length of 20 blocks.
64
65 # Make sure everything done so far is durably persisted (all back references are
66 # in the extent tree, etc).
67 sync
68
69 # Now clone all extents of our file that cover the file range spanned by 40th
70 # block up to its eof (165th block at this point) into itself at 512th
71 # block. This leaves a hole in the file covering the block range [165, 512[. The
72 # prealloc extent will now be referenced by the file twice, once for offset
73 # mapped by the 40th block and once for offset mapped by 512th block. The 10
74 # blocks extent that follows the prealloc extent will also be referenced twice
75 # by our file, once for offset mapped by the 155th block and once for offset
76 # (512 block + 115 blocks)
77 $CLONER_PROG -s $((40 * $BLOCK_SIZE)) -d $((512 * $BLOCK_SIZE)) -l 0 \
78              $SCRATCH_MNT/foo $SCRATCH_MNT/foo
79
80 # Now create one new extent in our file with a size of 25 blocks. It will span
81 # the block range [768, 768 + 25[. It also will cause creation of a hole
82 # spanning the block range [512 + 115, 768[. Our new file size is the file
83 # offset mapped by (768 + 25)th block.
84 $XFS_IO_PROG -c "pwrite -S 0xbb $((768 * $BLOCK_SIZE)) $((25 * $BLOCK_SIZE))" \
85              $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
86
87 # At this point, there are now (in memory) 4 back references to the prealloc
88 # extent.
89 #
90 # Two of them are for file offset mapped by the 40th block, related to file
91 # extent items matching the file offsets mapped by 40th and 135th block
92 # respectively, with data offsets of 0 and 95 blocks respectively, and with
93 # lengths of 95 and 20 blocks respectively.
94 #
95 # The other two references are for file offset mapped by 512th block, related to
96 # file extent items matching the file offsets mapped by 512th and (512 + 95)th
97 # block respectively, with data offsets mapped by 0th and 95th block
98 # respectively, and with lengths of 95 and 20 blocks respectively.
99 #
100 # The 10 block extent has 2 back references, one for file offset mapped by 155th
101 # block and the other for file offset mapped by (512 + 115)th block.
102 #
103 # The 25 blocks extent has a single back reference and it relates to file offset
104 # mapped by 768th block.
105
106
107 # Now clone our 25 block extent into offset mapped by 150th block. That offset
108 # covers the last 5 blocks of the prealloc extent, the whole 10 block extent and
109 # 10 blocks of the hole starting at offset mapped by 165th block.
110 $CLONER_PROG -s $((768 * $BLOCK_SIZE)) -d $((150 * $BLOCK_SIZE)) -l $((25 * $BLOCK_SIZE)) \
111         $SCRATCH_MNT/foo $SCRATCH_MNT/foo
112
113 # At this point there's only one reference to the 10 block extent, at file
114 # offset mapped by (512 + 115) block, we have 4 references for the prealloc
115 # extent (2 for file offset mapped by 40th block and 2 for file offset mapped by
116 # 512th block) and 2 references for the 25 block extent (1 for file offset
117 # mapped by 768th block and a new one for file offset mapped by 150th block).
118
119 # Now fsync our file to make all its new data and metadata updates are durably
120 # persisted and present if a power failure/crash happens after a successful
121 # fsync and before the next transaction commit.
122 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
123
124 echo "File contents before power failure:"
125 od -t x1 $SCRATCH_MNT/foo | _filter_od
126
127 # During log replay, the btrfs delayed references implementation used to run the
128 # deletion of back references before the addition of new back references, which
129 # made the addition fail as it didn't find the key in the extent tree that it
130 # was looking for. The failure triggered by this test was related to the 10
131 # block extent, which got 1 reference dropped and 1 reference added during the
132 # fsync log replay - when running the delayed references at transaction commit
133 # time, btrfs was applying the deletion before the insertion, resulting in a
134 # failure of the insertion that ended up turning the fs into read-only mode.
135 _flakey_drop_and_remount
136
137 echo "File contents after log replay:"
138 od -t x1 $SCRATCH_MNT/foo | _filter_od
139
140 _unmount_flakey
141
142 status=0
143 exit