1#!/bin/bash 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 22owner=mreitz@redhat.com 23 24seq="$(basename $0)" 25echo "QA output created by $seq" 26 27here="$PWD" 28tmp=/tmp/$$ 29status=1 # failure is the default! 30 31_cleanup() 32{ 33 _cleanup_test_img 34} 35trap "_cleanup; exit \$status" 0 1 2 3 15 36 37# get standard environment, filters and checks 38. ./common.rc 39. ./common.filter 40 41# This tests qcow2-specific low-level functionality 42_supported_fmt qcow2 43_supported_proto file 44_supported_os Linux 45# This test will set refcount_bits on its own which would conflict with the 46# manual setting; compat will be overridden as well 47_unsupported_imgopts refcount_bits 'compat=0.10' 48 49function print_refcount_bits() 50{ 51 $QEMU_IMG info "$TEST_IMG" | sed -n '/refcount bits:/ s/^ *//p' 52} 53 54echo 55echo '=== refcount_bits limits ===' 56echo 57 58# Must be positive (non-zero) 59IMGOPTS="$IMGOPTS,refcount_bits=0" _make_test_img 64M 60# Must be positive (non-negative) 61IMGOPTS="$IMGOPTS,refcount_bits=-1" _make_test_img 64M 62# May not exceed 64 63IMGOPTS="$IMGOPTS,refcount_bits=128" _make_test_img 64M 64# Must be a power of two 65IMGOPTS="$IMGOPTS,refcount_bits=42" _make_test_img 64M 66 67# 1 is the minimum 68IMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M 69print_refcount_bits 70 71# 64 is the maximum 72IMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M 73print_refcount_bits 74 75# 16 is the default 76_make_test_img 64M 77print_refcount_bits 78 79echo 80echo '=== refcount_bits and compat=0.10 ===' 81echo 82 83# Should work 84IMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=16" _make_test_img 64M 85print_refcount_bits 86 87# Should not work 88IMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=1" _make_test_img 64M 89IMGOPTS="$IMGOPTS,compat=0.10,refcount_bits=64" _make_test_img 64M 90 91 92echo 93echo '=== Snapshot limit on refcount_bits=1 ===' 94echo 95 96IMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M 97print_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 108echo 109echo '=== Snapshot limit on refcount_bits=2 ===' 110echo 111 112IMGOPTS="$IMGOPTS,refcount_bits=2" _make_test_img 64M 113print_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 126echo 127echo '=== Compressed clusters with refcount_bits=1 ===' 128echo 129 130IMGOPTS="$IMGOPTS,refcount_bits=1" _make_test_img 64M 131print_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 142echo 143echo '=== MSb set in 64 bit refcount ===' 144echo 145 146IMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M 147print_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 152poke_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 155poke_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 160echo 161echo '=== Snapshot on maximum 64 bit refcount value ===' 162echo 163 164IMGOPTS="$IMGOPTS,refcount_bits=64" _make_test_img 64M 165print_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 170poke_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 173poke_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 183 184# success, all done 185echo '*** done' 186rm -f $seq.full 187status=0 188