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