btrfs: make nospace_cache related test cases to work with latest v2 cache
[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 . ./common/preamble
19 _begin_fstest auto quick trim
20
21 # Override the default cleanup function.
22 _cleanup()
23 {
24         cd /
25         umount $loop_mnt &> /dev/null
26         _destroy_loop_device $loop_dev &> /dev/null
27         rm -rf $tmp.*
28 }
29
30 # Import common functions.
31 . ./common/filter
32
33 # real QA test starts here
34
35 # Modify as appropriate.
36 _supported_fs btrfs
37 _require_loop
38 _require_xfs_io_command "fiemap"
39
40 # We need less than 2G data write, consider it 2G and double it just in case
41 _require_scratch_size   $((4 * 1024 * 1024))
42
43 loop_file="$SCRATCH_MNT/image"
44
45 # The margin when checking trimmed size.
46 #
47 # The margin is for tree blocks, calculated by
48 # 3 * max_tree_block_size
49 # |   |- 64K
50 # |- 3 trees get modified (root tree, extent tree, fs tree)
51 #
52 # In reality, there should be no margin at all due to the mount options.
53 # But who knows what will happen in later kernels.
54 margin_kb=$(( 3 * 64 ))
55 trimmed_kb=$(( 768 * 1024 )) # 768M
56
57 _scratch_mkfs >> $seqres.full
58 _scratch_mount
59
60 # Create a sparse file as the loopback target.
61 # 10G size makes sure we have 1G chunk size.
62 truncate -s 10G "$loop_file"
63
64 _mkfs_dev -d SINGLE "$loop_file"
65
66 loop_dev=$(_create_loop_device "$loop_file")
67 loop_mnt=$tmp/loop_mnt
68
69 mkdir -p $loop_mnt
70 # - $(_btrfs_no_v1_cache_opt)
71 #   Since v1 cache using DATA space, it can break data extent bytenr
72 #   continuousness.
73 # - nodatasum
74 #   As there will be 1.5G data write, generating 1.5M csum.
75 #   Disabling datasum could reduce the margin caused by metadata to minimal
76 # - discard
77 #   What we're testing
78 _mount $(_btrfs_no_v1_cache_opt) -o nodatasum,discard $loop_dev $loop_mnt
79
80 # Craft the following extent layout:
81 #         |  BG1 |      BG2        |       BG3            |
82 # Bytenr: X-8M   X      X+512M     X+1G  X+1G+128M 
83 #         |//////|//////|                |//|
84 #            V      V           V          V
85 #            |      |           |          |- file 'tail_padding'
86 #            |      |           |- file 'cross_boundary'
87 #            |      |- file 'lead_padding2'
88 #            |- file 'lead_padding1'
89 # So that all extents of file 'cross_boundary' are all adjacent and crosses the
90 # boundary of BG1 and BG2
91 # File 'lead_padding1' and 'lead_padding2' are all paddings to fill the
92 # leading gap.
93 # File 'tail_padding' is to ensure after deleting file 'cross_boundary' we still
94 # have used extent in BG3, to prevent trimming the whole BG3.
95 # And since BG1 needs exactly 8M to fill, we need to sync write to ensure
96 # the write sequence.
97 _pwrite_byte 0xcd 0 8M $loop_mnt/lead_padding1 > /dev/null
98 sync
99
100 _pwrite_byte 0xcd 0 512M $loop_mnt/lead_padding2 > /dev/null
101 sync
102 _pwrite_byte 0xcd 0 $(($trimmed_kb * 1024)) $loop_mnt/cross_boundary \
103         > /dev/null
104 sync
105
106 _pwrite_byte 0xcd 0 1M $loop_mnt/tail_padding > /dev/null
107 sync
108
109 $XFS_IO_PROG -c "fiemap" $loop_mnt/cross_boundary >> $seqres.full
110 # Ensure all extent are continuous
111 # Btrfs fiemap will merge continuous results, so the output should be only
112 # 2 lines, 1 line for filename, 1 line for a large merged fiemap result.
113 if [ $($XFS_IO_PROG -c "fiemap" $loop_mnt/cross_boundary | wc -l) -ne 2 ]; then
114         _notrun "Non-continuous extent bytenr detected"
115 fi
116
117 size1_kb=$(du $loop_file| cut -f1)
118
119 # Delete the file 'cross_boundary'.
120 # This will delete $trimmed_kb data extents across the chunk boundary.
121 rm -f $loop_mnt/cross_boundary
122
123 # sync so btrfs will commit transaction and trim the freed extents
124 sync
125
126 size2_kb=$(du $loop_file | cut -f1)
127
128 echo "loopback file size before discard: $size1_kb KiB" >> $seqres.full
129 echo "loopback file size after discard:  $size2_kb KiB" >> $seqres.full
130 echo "Expect trimmed size:               $trimmed_kb KiB" >> $seqres.full
131 echo "Have trimmed size:                 $(($size1_kb - $size2_kb)) KiB" >> $seqres.full
132
133 if [ $(($size2_kb+ $trimmed_kb)) -gt $(($size1_kb + $margin_kb)) -o \
134      $(($size2_kb+ $trimmed_kb)) -lt $(($size1_kb - $margin_kb)) ]; then
135         echo "Btrfs doesn't trim the range correctly"
136 fi
137
138 echo "Silence is golden"
139
140 # success, all done
141 status=0
142 exit