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