fsx/fsstress: round blocksize properly
[xfstests-dev.git] / tests / btrfs / 199
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2019 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test 199
6 #
7 # Test if btrfs discard mount option is trimming adjacent extents across
8 # block groups boundary.
9 # The test case uses loopback device and file used space to detect trimmed
10 # bytes.
11 #
12 # There is a long existing bug that btrfs doesn't discard all space for
13 # above mentioned case.
14 #
15 # The fix is: "btrfs: extent-tree: Ensure we trim ranges across block group
16 # boundary"
17 #
18 seq=`basename $0`
19 seqres=$RESULT_DIR/$seq
20 echo "QA output created by $seq"
21
22 here=`pwd`
23 tmp=/tmp/$$
24 status=1        # failure is the default!
25 trap "_cleanup; exit \$status" 0 1 2 3 15
26
27 _cleanup()
28 {
29         cd /
30         umount $loop_mnt &> /dev/null
31         _destroy_loop_device $loop_dev &> /dev/null
32         rm -rf $tmp.*
33 }
34
35 # get standard environment, filters and checks
36 . ./common/rc
37 . ./common/filter
38
39 # remove previous $seqres.full before test
40 rm -f $seqres.full
41
42 # real QA test starts here
43
44 # Modify as appropriate.
45 _supported_fs btrfs
46 _require_loop
47 _require_xfs_io_command "fiemap"
48
49 # We need less than 2G data write, consider it 2G and double it just in case
50 _require_scratch_size   $((4 * 1024 * 1024))
51
52 loop_file="$SCRATCH_MNT/image"
53
54 # The margin when checking trimmed size.
55 #
56 # The margin is for tree blocks, calculated by
57 # 3 * max_tree_block_size
58 # |   |- 64K
59 # |- 3 trees get modified (root tree, extent tree, fs tree)
60 #
61 # In reality, there should be no margin at all due to the mount options.
62 # But who knows what will happen in later kernels.
63 margin_kb=$(( 3 * 64 ))
64 trimmed_kb=$(( 768 * 1024 )) # 768M
65
66 _scratch_mkfs >> $seqres.full
67 _scratch_mount
68
69 # Create a sparse file as the loopback target.
70 # 10G size makes sure we have 1G chunk size.
71 truncate -s 10G "$loop_file"
72
73 _mkfs_dev -d SINGLE "$loop_file"
74
75 loop_dev=$(_create_loop_device "$loop_file")
76 loop_mnt=$tmp/loop_mnt
77
78 mkdir -p $loop_mnt
79 # - nospace_cache
80 #   Since v1 cache using DATA space, it can break data extent bytenr
81 #   continuousness.
82 # - nodatasum
83 #   As there will be 1.5G data write, generating 1.5M csum.
84 #   Disabling datasum could reduce the margin caused by metadata to minimal
85 # - discard
86 #   What we're testing
87 _mount -o nospace_cache,nodatasum,discard $loop_dev $loop_mnt
88
89 # Craft the following extent layout:
90 #         |  BG1 |      BG2        |       BG3            |
91 # Bytenr: X-8M   X      X+512M     X+1G  X+1G+128M 
92 #         |//////|//////|                |//|
93 #            V      V           V          V
94 #            |      |           |          |- file 'tail_padding'
95 #            |      |           |- file 'cross_boundary'
96 #            |      |- file 'lead_padding2'
97 #            |- file 'lead_padding1'
98 # So that all extents of file 'cross_boundary' are all adjacent and crosses the
99 # boundary of BG1 and BG2
100 # File 'lead_padding1' and 'lead_padding2' are all paddings to fill the
101 # leading gap.
102 # File 'tail_padding' is to ensure after deleting file 'cross_boundary' we still
103 # have used extent in BG3, to prevent trimming the whole BG3.
104 # And since BG1 needs exactly 8M to fill, we need to sync write to ensure
105 # the write sequence.
106 _pwrite_byte 0xcd 0 8M $loop_mnt/lead_padding1 > /dev/null
107 sync
108
109 _pwrite_byte 0xcd 0 512M $loop_mnt/lead_padding2 > /dev/null
110 sync
111 _pwrite_byte 0xcd 0 $(($trimmed_kb * 1024)) $loop_mnt/cross_boundary \
112         > /dev/null
113 sync
114
115 _pwrite_byte 0xcd 0 1M $loop_mnt/tail_padding > /dev/null
116 sync
117
118
119 $XFS_IO_PROG -c "fiemap" $loop_mnt/cross_boundary >> $seqres.full
120 # Ensure all extent are continuous
121 # Btrfs fiemap will merge continuous results, so the output should be only
122 # 2 lines, 1 line for filename, 1 line for a large merged fiemap result.
123 if [ $($XFS_IO_PROG -c "fiemap" $loop_mnt/cross_boundary | wc -l) -ne 2 ]; then
124         _notrun "Non-continuous extent bytenr detected"
125 fi
126
127 size1_kb=$(du $loop_file| cut -f1)
128
129 # Delete the file 'cross_boundary'.
130 # This will delete $trimmed_kb data extents across the chunk boundary.
131 rm -f $loop_mnt/cross_boundary
132
133 # sync so btrfs will commit transaction and trim the freed extents
134 sync
135
136 size2_kb=$(du $loop_file | cut -f1)
137
138 echo "loopback file size before discard: $size1_kb KiB" >> $seqres.full
139 echo "loopback file size after discard:  $size2_kb KiB" >> $seqres.full
140 echo "Expect trimmed size:               $trimmed_kb KiB" >> $seqres.full
141 echo "Have trimmed size:                 $(($size1_kb - $size2_kb)) KiB" >> $seqres.full
142
143 if [ $(($size2_kb+ $trimmed_kb)) -gt $(($size1_kb + $margin_kb)) -o \
144      $(($size2_kb+ $trimmed_kb)) -lt $(($size1_kb - $margin_kb)) ]; then
145         echo "Btrfs doesn't trim the range correctly"
146 fi
147
148 echo "Silence is golden"
149
150 # success, all done
151 status=0
152 exit