Philippe Mathieu-Daudé | 11a82d1 | 2019-03-07 15:58:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Vladimir Sementsov-Ogievskiy | 9dd003a | 2021-01-16 16:44:19 +0300 | [diff] [blame] | 2 | # group: rw |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 3 | # |
| 4 | # Test cases for different refcount_bits values |
| 5 | # |
| 6 | # Copyright (C) 2015 Red Hat, Inc. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | # |
| 21 | |
| 22 | # creator |
John Snow | 42a5009 | 2022-03-22 13:42:12 -0400 | [diff] [blame] | 23 | owner=hreitz@redhat.com |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 24 | |
| 25 | seq="$(basename $0)" |
| 26 | echo "QA output created by $seq" |
| 27 | |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 28 | status=1 # failure is the default! |
| 29 | |
| 30 | _cleanup() |
| 31 | { |
| 32 | _cleanup_test_img |
| 33 | } |
| 34 | trap "_cleanup; exit \$status" 0 1 2 3 15 |
| 35 | |
| 36 | # get standard environment, filters and checks |
| 37 | . ./common.rc |
| 38 | . ./common.filter |
| 39 | |
| 40 | # This tests qcow2-specific low-level functionality |
| 41 | _supported_fmt qcow2 |
Max Reitz | 57284d2 | 2020-10-27 20:05:59 +0100 | [diff] [blame] | 42 | _supported_proto file fuse |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 43 | # This test will set refcount_bits on its own which would conflict with the |
Max Reitz | 3be2024 | 2019-11-07 17:37:07 +0100 | [diff] [blame] | 44 | # manual setting; compat will be overridden as well; |
| 45 | # and external data files do not work well with our refcount testing |
Vladimir Sementsov-Ogievskiy | e287a35 | 2021-12-23 17:01:44 +0100 | [diff] [blame] | 46 | # also, compression type is not supported with compat=0.10 used in test |
| 47 | _unsupported_imgopts refcount_bits 'compat=0.10' data_file compression_type |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 48 | |
Eric Blake | 8cedcff | 2018-11-16 15:50:02 -0600 | [diff] [blame] | 49 | print_refcount_bits() |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 50 | { |
| 51 | $QEMU_IMG info "$TEST_IMG" | sed -n '/refcount bits:/ s/^ *//p' |
| 52 | } |
| 53 | |
| 54 | echo |
| 55 | echo '=== refcount_bits limits ===' |
| 56 | echo |
| 57 | |
| 58 | # Must be positive (non-zero) |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 59 | _make_test_img -o "refcount_bits=0" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 60 | # Must be positive (non-negative) |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 61 | _make_test_img -o "refcount_bits=-1" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 62 | # May not exceed 64 |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 63 | _make_test_img -o "refcount_bits=128" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 64 | # Must be a power of two |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 65 | _make_test_img -o "refcount_bits=42" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 66 | |
| 67 | # 1 is the minimum |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 68 | _make_test_img -o "refcount_bits=1" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 69 | print_refcount_bits |
| 70 | |
| 71 | # 64 is the maximum |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 72 | _make_test_img -o "refcount_bits=64" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 73 | print_refcount_bits |
| 74 | |
| 75 | # 16 is the default |
| 76 | _make_test_img 64M |
| 77 | print_refcount_bits |
| 78 | |
| 79 | echo |
| 80 | echo '=== refcount_bits and compat=0.10 ===' |
| 81 | echo |
| 82 | |
| 83 | # Should work |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 84 | _make_test_img -o "compat=0.10,refcount_bits=16" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 85 | print_refcount_bits |
| 86 | |
| 87 | # Should not work |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 88 | _make_test_img -o "compat=0.10,refcount_bits=1" 64M |
| 89 | _make_test_img -o "compat=0.10,refcount_bits=64" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 90 | |
| 91 | |
| 92 | echo |
| 93 | echo '=== Snapshot limit on refcount_bits=1 ===' |
| 94 | echo |
| 95 | |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 96 | _make_test_img -o "refcount_bits=1" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 97 | print_refcount_bits |
| 98 | |
| 99 | $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io |
| 100 | |
| 101 | # Should fail for now; in the future, this might be supported by automatically |
| 102 | # copying all clusters with overflowing refcount |
| 103 | $QEMU_IMG snapshot -c foo "$TEST_IMG" |
| 104 | |
| 105 | # The new L1 table could/should be leaked |
| 106 | _check_test_img |
| 107 | |
| 108 | echo |
| 109 | echo '=== Snapshot limit on refcount_bits=2 ===' |
| 110 | echo |
| 111 | |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 112 | _make_test_img -o "refcount_bits=2" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 113 | print_refcount_bits |
| 114 | |
| 115 | $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io |
| 116 | |
| 117 | # Should succeed |
| 118 | $QEMU_IMG snapshot -c foo "$TEST_IMG" |
| 119 | $QEMU_IMG snapshot -c bar "$TEST_IMG" |
| 120 | # Should fail (4th reference) |
| 121 | $QEMU_IMG snapshot -c baz "$TEST_IMG" |
| 122 | |
| 123 | # The new L1 table could/should be leaked |
| 124 | _check_test_img |
| 125 | |
| 126 | echo |
| 127 | echo '=== Compressed clusters with refcount_bits=1 ===' |
| 128 | echo |
| 129 | |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 130 | _make_test_img -o "refcount_bits=1" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 131 | print_refcount_bits |
| 132 | |
| 133 | # Both should fit into a single host cluster; instead of failing to increase the |
| 134 | # refcount of that cluster, qemu should just allocate a new cluster and make |
| 135 | # this operation succeed |
| 136 | $QEMU_IO -c 'write -P 0 -c 0 64k' \ |
| 137 | -c 'write -P 1 -c 64k 64k' \ |
| 138 | "$TEST_IMG" | _filter_qemu_io |
| 139 | |
| 140 | _check_test_img |
| 141 | |
| 142 | echo |
| 143 | echo '=== MSb set in 64 bit refcount ===' |
| 144 | echo |
| 145 | |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 146 | _make_test_img -o "refcount_bits=64" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 147 | print_refcount_bits |
| 148 | |
| 149 | $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io |
| 150 | |
| 151 | # Set the MSb in the refblock entry of the data cluster |
| 152 | poke_file "$TEST_IMG" $((0x20028)) "\x80\x00\x00\x00\x00\x00\x00\x00" |
| 153 | |
| 154 | # Clear OFLAG_COPIED in the L2 entry of the data cluster |
| 155 | poke_file "$TEST_IMG" $((0x40000)) "\x00\x00\x00\x00\x00\x05\x00\x00" |
| 156 | |
| 157 | # Try to write to that cluster (should work, even though the MSb is set) |
| 158 | $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io |
| 159 | |
| 160 | echo |
| 161 | echo '=== Snapshot on maximum 64 bit refcount value ===' |
| 162 | echo |
| 163 | |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 164 | _make_test_img -o "refcount_bits=64" 64M |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 165 | print_refcount_bits |
| 166 | |
| 167 | $QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io |
| 168 | |
| 169 | # Set the refblock entry to the maximum value possible |
| 170 | poke_file "$TEST_IMG" $((0x20028)) "\xff\xff\xff\xff\xff\xff\xff\xff" |
| 171 | |
| 172 | # Clear OFLAG_COPIED in the L2 entry of the data cluster |
| 173 | poke_file "$TEST_IMG" $((0x40000)) "\x00\x00\x00\x00\x00\x05\x00\x00" |
| 174 | |
| 175 | # Try a snapshot (should correctly identify the overflow; may work in the future |
| 176 | # by falling back to COW) |
| 177 | $QEMU_IMG snapshot -c foo "$TEST_IMG" |
| 178 | |
| 179 | # The new L1 table could/should be leaked; and obviously the data cluster is |
| 180 | # leaked (refcount=UINT64_MAX reference=1) |
| 181 | _check_test_img |
| 182 | |
Max Reitz | e9dbdc5 | 2015-07-27 17:51:40 +0200 | [diff] [blame] | 183 | echo |
| 184 | echo '=== Amend from refcount_bits=16 to refcount_bits=1 ===' |
| 185 | echo |
| 186 | |
| 187 | _make_test_img 64M |
| 188 | print_refcount_bits |
| 189 | |
| 190 | $QEMU_IO -c 'write 16M 32M' "$TEST_IMG" | _filter_qemu_io |
| 191 | $QEMU_IMG amend -o refcount_bits=1 "$TEST_IMG" |
| 192 | _check_test_img |
| 193 | print_refcount_bits |
| 194 | |
| 195 | echo |
| 196 | echo '=== Amend from refcount_bits=1 to refcount_bits=64 ===' |
| 197 | echo |
| 198 | |
| 199 | $QEMU_IMG amend -o refcount_bits=64 "$TEST_IMG" |
| 200 | _check_test_img |
| 201 | print_refcount_bits |
| 202 | |
| 203 | echo |
| 204 | echo '=== Amend to compat=0.10 ===' |
| 205 | echo |
| 206 | |
| 207 | # Should not work because refcount_bits needs to be 16 for compat=0.10 |
| 208 | $QEMU_IMG amend -o compat=0.10 "$TEST_IMG" |
| 209 | print_refcount_bits |
| 210 | # Should work |
| 211 | $QEMU_IMG amend -o compat=0.10,refcount_bits=16 "$TEST_IMG" |
| 212 | _check_test_img |
| 213 | print_refcount_bits |
| 214 | |
| 215 | # Get back to compat=1.1 and refcount_bits=16 |
| 216 | $QEMU_IMG amend -o compat=1.1 "$TEST_IMG" |
| 217 | print_refcount_bits |
| 218 | # Should not work |
| 219 | $QEMU_IMG amend -o refcount_bits=32,compat=0.10 "$TEST_IMG" |
| 220 | print_refcount_bits |
| 221 | |
| 222 | echo |
| 223 | echo '=== Amend with snapshot ===' |
| 224 | echo |
| 225 | |
| 226 | $QEMU_IMG snapshot -c foo "$TEST_IMG" |
| 227 | # Just to have different refcounts across the image |
| 228 | $QEMU_IO -c 'write 0 16M' "$TEST_IMG" | _filter_qemu_io |
| 229 | |
| 230 | # Should not work (may work in the future by first decreasing all refcounts so |
| 231 | # they fit into the target range by copying them) |
| 232 | $QEMU_IMG amend -o refcount_bits=1 "$TEST_IMG" |
| 233 | _check_test_img |
| 234 | print_refcount_bits |
| 235 | |
| 236 | # Should work |
| 237 | $QEMU_IMG amend -o refcount_bits=2 "$TEST_IMG" |
| 238 | _check_test_img |
| 239 | print_refcount_bits |
| 240 | |
| 241 | echo |
| 242 | echo '=== Testing too many references for check ===' |
| 243 | echo |
| 244 | |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 245 | _make_test_img -o "refcount_bits=1" 64M |
Max Reitz | e9dbdc5 | 2015-07-27 17:51:40 +0200 | [diff] [blame] | 246 | print_refcount_bits |
| 247 | |
| 248 | # This cluster should be created at 0x50000 |
| 249 | $QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io |
| 250 | # Now make the second L2 entry (the L2 table should be at 0x40000) point to that |
| 251 | # cluster, so we have two references |
| 252 | poke_file "$TEST_IMG" $((0x40008)) "\x80\x00\x00\x00\x00\x05\x00\x00" |
| 253 | |
| 254 | # This should say "please use amend" |
| 255 | _check_test_img -r all |
| 256 | |
| 257 | # So we do that |
| 258 | $QEMU_IMG amend -o refcount_bits=2 "$TEST_IMG" |
| 259 | print_refcount_bits |
| 260 | |
| 261 | # And try again |
| 262 | _check_test_img -r all |
| 263 | |
| 264 | echo |
| 265 | echo '=== Multiple walks necessary during amend ===' |
| 266 | echo |
| 267 | |
Max Reitz | 407fb56 | 2019-11-07 17:36:57 +0100 | [diff] [blame] | 268 | _make_test_img -o "refcount_bits=1,cluster_size=512" 64k |
Max Reitz | e9dbdc5 | 2015-07-27 17:51:40 +0200 | [diff] [blame] | 269 | |
| 270 | # Cluster 0 is the image header, clusters 1 to 4 are used by the L1 table, a |
| 271 | # single L2 table, the reftable and a single refblock. This creates 58 data |
| 272 | # clusters (actually, the L2 table is created here, too), so in total there are |
| 273 | # then 63 used clusters in the image. With a refcount width of 64, one refblock |
| 274 | # describes 64 clusters (512 bytes / 64 bits/entry = 64 entries), so this will |
| 275 | # make the first refblock in the amended image have exactly one free entry. |
| 276 | $QEMU_IO -c "write 0 $((58 * 512))" "$TEST_IMG" | _filter_qemu_io |
| 277 | |
| 278 | # Now change the refcount width; since the first new refblock will have exactly |
| 279 | # one free entry, that entry will be used to store its own reference. No other |
| 280 | # refblocks are needed, so then the new reftable will be allocated; since the |
| 281 | # first new refblock is completely filled up, this will require a new refblock |
| 282 | # which is why the refcount width changing function will need to run through |
| 283 | # everything one more time until the allocations are stable. |
| 284 | # Having more walks than usual should be visible as regressing progress (from |
| 285 | # 66.67 % (2/3 walks) to 50.00 % (2/4 walks)). |
| 286 | $QEMU_IMG amend -o refcount_bits=64 -p "$TEST_IMG" | tr '\r' '\n' \ |
| 287 | | grep -A 1 '66.67' |
| 288 | print_refcount_bits |
| 289 | |
| 290 | _check_test_img |
| 291 | |
Max Reitz | d2eed8c | 2015-02-18 17:40:50 -0500 | [diff] [blame] | 292 | |
| 293 | # success, all done |
| 294 | echo '*** done' |
| 295 | rm -f $seq.full |
| 296 | status=0 |