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