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 41*57284d2aSMax Reitz_supported_proto file fuse 42d2eed8c6SMax Reitz# This test will set refcount_bits on its own which would conflict with the 433be2024aSMax Reitz# manual setting; compat will be overridden as well; 443be2024aSMax Reitz# and external data files do not work well with our refcount testing 453be2024aSMax Reitz_unsupported_imgopts refcount_bits 'compat=0.10' data_file 46d2eed8c6SMax Reitz 478cedcffdSEric Blakeprint_refcount_bits() 48d2eed8c6SMax Reitz{ 49d2eed8c6SMax Reitz $QEMU_IMG info "$TEST_IMG" | sed -n '/refcount bits:/ s/^ *//p' 50d2eed8c6SMax Reitz} 51d2eed8c6SMax Reitz 52d2eed8c6SMax Reitzecho 53d2eed8c6SMax Reitzecho '=== refcount_bits limits ===' 54d2eed8c6SMax Reitzecho 55d2eed8c6SMax Reitz 56d2eed8c6SMax Reitz# Must be positive (non-zero) 57407fb56aSMax Reitz_make_test_img -o "refcount_bits=0" 64M 58d2eed8c6SMax Reitz# Must be positive (non-negative) 59407fb56aSMax Reitz_make_test_img -o "refcount_bits=-1" 64M 60d2eed8c6SMax Reitz# May not exceed 64 61407fb56aSMax Reitz_make_test_img -o "refcount_bits=128" 64M 62d2eed8c6SMax Reitz# Must be a power of two 63407fb56aSMax Reitz_make_test_img -o "refcount_bits=42" 64M 64d2eed8c6SMax Reitz 65d2eed8c6SMax Reitz# 1 is the minimum 66407fb56aSMax Reitz_make_test_img -o "refcount_bits=1" 64M 67d2eed8c6SMax Reitzprint_refcount_bits 68d2eed8c6SMax Reitz 69d2eed8c6SMax Reitz# 64 is the maximum 70407fb56aSMax Reitz_make_test_img -o "refcount_bits=64" 64M 71d2eed8c6SMax Reitzprint_refcount_bits 72d2eed8c6SMax Reitz 73d2eed8c6SMax Reitz# 16 is the default 74d2eed8c6SMax Reitz_make_test_img 64M 75d2eed8c6SMax Reitzprint_refcount_bits 76d2eed8c6SMax Reitz 77d2eed8c6SMax Reitzecho 78d2eed8c6SMax Reitzecho '=== refcount_bits and compat=0.10 ===' 79d2eed8c6SMax Reitzecho 80d2eed8c6SMax Reitz 81d2eed8c6SMax Reitz# Should work 82407fb56aSMax Reitz_make_test_img -o "compat=0.10,refcount_bits=16" 64M 83d2eed8c6SMax Reitzprint_refcount_bits 84d2eed8c6SMax Reitz 85d2eed8c6SMax Reitz# Should not work 86407fb56aSMax Reitz_make_test_img -o "compat=0.10,refcount_bits=1" 64M 87407fb56aSMax Reitz_make_test_img -o "compat=0.10,refcount_bits=64" 64M 88d2eed8c6SMax Reitz 89d2eed8c6SMax Reitz 90d2eed8c6SMax Reitzecho 91d2eed8c6SMax Reitzecho '=== Snapshot limit on refcount_bits=1 ===' 92d2eed8c6SMax Reitzecho 93d2eed8c6SMax Reitz 94407fb56aSMax Reitz_make_test_img -o "refcount_bits=1" 64M 95d2eed8c6SMax Reitzprint_refcount_bits 96d2eed8c6SMax Reitz 97d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 98d2eed8c6SMax Reitz 99d2eed8c6SMax Reitz# Should fail for now; in the future, this might be supported by automatically 100d2eed8c6SMax Reitz# copying all clusters with overflowing refcount 101d2eed8c6SMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 102d2eed8c6SMax Reitz 103d2eed8c6SMax Reitz# The new L1 table could/should be leaked 104d2eed8c6SMax Reitz_check_test_img 105d2eed8c6SMax Reitz 106d2eed8c6SMax Reitzecho 107d2eed8c6SMax Reitzecho '=== Snapshot limit on refcount_bits=2 ===' 108d2eed8c6SMax Reitzecho 109d2eed8c6SMax Reitz 110407fb56aSMax Reitz_make_test_img -o "refcount_bits=2" 64M 111d2eed8c6SMax Reitzprint_refcount_bits 112d2eed8c6SMax Reitz 113d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 114d2eed8c6SMax Reitz 115d2eed8c6SMax Reitz# Should succeed 116d2eed8c6SMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 117d2eed8c6SMax Reitz$QEMU_IMG snapshot -c bar "$TEST_IMG" 118d2eed8c6SMax Reitz# Should fail (4th reference) 119d2eed8c6SMax Reitz$QEMU_IMG snapshot -c baz "$TEST_IMG" 120d2eed8c6SMax Reitz 121d2eed8c6SMax Reitz# The new L1 table could/should be leaked 122d2eed8c6SMax Reitz_check_test_img 123d2eed8c6SMax Reitz 124d2eed8c6SMax Reitzecho 125d2eed8c6SMax Reitzecho '=== Compressed clusters with refcount_bits=1 ===' 126d2eed8c6SMax Reitzecho 127d2eed8c6SMax Reitz 128407fb56aSMax Reitz_make_test_img -o "refcount_bits=1" 64M 129d2eed8c6SMax Reitzprint_refcount_bits 130d2eed8c6SMax Reitz 131d2eed8c6SMax Reitz# Both should fit into a single host cluster; instead of failing to increase the 132d2eed8c6SMax Reitz# refcount of that cluster, qemu should just allocate a new cluster and make 133d2eed8c6SMax Reitz# this operation succeed 134d2eed8c6SMax Reitz$QEMU_IO -c 'write -P 0 -c 0 64k' \ 135d2eed8c6SMax Reitz -c 'write -P 1 -c 64k 64k' \ 136d2eed8c6SMax Reitz "$TEST_IMG" | _filter_qemu_io 137d2eed8c6SMax Reitz 138d2eed8c6SMax Reitz_check_test_img 139d2eed8c6SMax Reitz 140d2eed8c6SMax Reitzecho 141d2eed8c6SMax Reitzecho '=== MSb set in 64 bit refcount ===' 142d2eed8c6SMax Reitzecho 143d2eed8c6SMax Reitz 144407fb56aSMax Reitz_make_test_img -o "refcount_bits=64" 64M 145d2eed8c6SMax Reitzprint_refcount_bits 146d2eed8c6SMax Reitz 147d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 148d2eed8c6SMax Reitz 149d2eed8c6SMax Reitz# Set the MSb in the refblock entry of the data cluster 150d2eed8c6SMax Reitzpoke_file "$TEST_IMG" $((0x20028)) "\x80\x00\x00\x00\x00\x00\x00\x00" 151d2eed8c6SMax Reitz 152d2eed8c6SMax Reitz# Clear OFLAG_COPIED in the L2 entry of the data cluster 153d2eed8c6SMax Reitzpoke_file "$TEST_IMG" $((0x40000)) "\x00\x00\x00\x00\x00\x05\x00\x00" 154d2eed8c6SMax Reitz 155d2eed8c6SMax Reitz# Try to write to that cluster (should work, even though the MSb is set) 156d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 157d2eed8c6SMax Reitz 158d2eed8c6SMax Reitzecho 159d2eed8c6SMax Reitzecho '=== Snapshot on maximum 64 bit refcount value ===' 160d2eed8c6SMax Reitzecho 161d2eed8c6SMax Reitz 162407fb56aSMax Reitz_make_test_img -o "refcount_bits=64" 64M 163d2eed8c6SMax Reitzprint_refcount_bits 164d2eed8c6SMax Reitz 165d2eed8c6SMax Reitz$QEMU_IO -c 'write 0 512' "$TEST_IMG" | _filter_qemu_io 166d2eed8c6SMax Reitz 167d2eed8c6SMax Reitz# Set the refblock entry to the maximum value possible 168d2eed8c6SMax Reitzpoke_file "$TEST_IMG" $((0x20028)) "\xff\xff\xff\xff\xff\xff\xff\xff" 169d2eed8c6SMax Reitz 170d2eed8c6SMax Reitz# Clear OFLAG_COPIED in the L2 entry of the data cluster 171d2eed8c6SMax Reitzpoke_file "$TEST_IMG" $((0x40000)) "\x00\x00\x00\x00\x00\x05\x00\x00" 172d2eed8c6SMax Reitz 173d2eed8c6SMax Reitz# Try a snapshot (should correctly identify the overflow; may work in the future 174d2eed8c6SMax Reitz# by falling back to COW) 175d2eed8c6SMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 176d2eed8c6SMax Reitz 177d2eed8c6SMax Reitz# The new L1 table could/should be leaked; and obviously the data cluster is 178d2eed8c6SMax Reitz# leaked (refcount=UINT64_MAX reference=1) 179d2eed8c6SMax Reitz_check_test_img 180d2eed8c6SMax Reitz 181e9dbdc5eSMax Reitzecho 182e9dbdc5eSMax Reitzecho '=== Amend from refcount_bits=16 to refcount_bits=1 ===' 183e9dbdc5eSMax Reitzecho 184e9dbdc5eSMax Reitz 185e9dbdc5eSMax Reitz_make_test_img 64M 186e9dbdc5eSMax Reitzprint_refcount_bits 187e9dbdc5eSMax Reitz 188e9dbdc5eSMax Reitz$QEMU_IO -c 'write 16M 32M' "$TEST_IMG" | _filter_qemu_io 189e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=1 "$TEST_IMG" 190e9dbdc5eSMax Reitz_check_test_img 191e9dbdc5eSMax Reitzprint_refcount_bits 192e9dbdc5eSMax Reitz 193e9dbdc5eSMax Reitzecho 194e9dbdc5eSMax Reitzecho '=== Amend from refcount_bits=1 to refcount_bits=64 ===' 195e9dbdc5eSMax Reitzecho 196e9dbdc5eSMax Reitz 197e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=64 "$TEST_IMG" 198e9dbdc5eSMax Reitz_check_test_img 199e9dbdc5eSMax Reitzprint_refcount_bits 200e9dbdc5eSMax Reitz 201e9dbdc5eSMax Reitzecho 202e9dbdc5eSMax Reitzecho '=== Amend to compat=0.10 ===' 203e9dbdc5eSMax Reitzecho 204e9dbdc5eSMax Reitz 205e9dbdc5eSMax Reitz# Should not work because refcount_bits needs to be 16 for compat=0.10 206e9dbdc5eSMax Reitz$QEMU_IMG amend -o compat=0.10 "$TEST_IMG" 207e9dbdc5eSMax Reitzprint_refcount_bits 208e9dbdc5eSMax Reitz# Should work 209e9dbdc5eSMax Reitz$QEMU_IMG amend -o compat=0.10,refcount_bits=16 "$TEST_IMG" 210e9dbdc5eSMax Reitz_check_test_img 211e9dbdc5eSMax Reitzprint_refcount_bits 212e9dbdc5eSMax Reitz 213e9dbdc5eSMax Reitz# Get back to compat=1.1 and refcount_bits=16 214e9dbdc5eSMax Reitz$QEMU_IMG amend -o compat=1.1 "$TEST_IMG" 215e9dbdc5eSMax Reitzprint_refcount_bits 216e9dbdc5eSMax Reitz# Should not work 217e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=32,compat=0.10 "$TEST_IMG" 218e9dbdc5eSMax Reitzprint_refcount_bits 219e9dbdc5eSMax Reitz 220e9dbdc5eSMax Reitzecho 221e9dbdc5eSMax Reitzecho '=== Amend with snapshot ===' 222e9dbdc5eSMax Reitzecho 223e9dbdc5eSMax Reitz 224e9dbdc5eSMax Reitz$QEMU_IMG snapshot -c foo "$TEST_IMG" 225e9dbdc5eSMax Reitz# Just to have different refcounts across the image 226e9dbdc5eSMax Reitz$QEMU_IO -c 'write 0 16M' "$TEST_IMG" | _filter_qemu_io 227e9dbdc5eSMax Reitz 228e9dbdc5eSMax Reitz# Should not work (may work in the future by first decreasing all refcounts so 229e9dbdc5eSMax Reitz# they fit into the target range by copying them) 230e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=1 "$TEST_IMG" 231e9dbdc5eSMax Reitz_check_test_img 232e9dbdc5eSMax Reitzprint_refcount_bits 233e9dbdc5eSMax Reitz 234e9dbdc5eSMax Reitz# Should work 235e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=2 "$TEST_IMG" 236e9dbdc5eSMax Reitz_check_test_img 237e9dbdc5eSMax Reitzprint_refcount_bits 238e9dbdc5eSMax Reitz 239e9dbdc5eSMax Reitzecho 240e9dbdc5eSMax Reitzecho '=== Testing too many references for check ===' 241e9dbdc5eSMax Reitzecho 242e9dbdc5eSMax Reitz 243407fb56aSMax Reitz_make_test_img -o "refcount_bits=1" 64M 244e9dbdc5eSMax Reitzprint_refcount_bits 245e9dbdc5eSMax Reitz 246e9dbdc5eSMax Reitz# This cluster should be created at 0x50000 247e9dbdc5eSMax Reitz$QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io 248e9dbdc5eSMax Reitz# Now make the second L2 entry (the L2 table should be at 0x40000) point to that 249e9dbdc5eSMax Reitz# cluster, so we have two references 250e9dbdc5eSMax Reitzpoke_file "$TEST_IMG" $((0x40008)) "\x80\x00\x00\x00\x00\x05\x00\x00" 251e9dbdc5eSMax Reitz 252e9dbdc5eSMax Reitz# This should say "please use amend" 253e9dbdc5eSMax Reitz_check_test_img -r all 254e9dbdc5eSMax Reitz 255e9dbdc5eSMax Reitz# So we do that 256e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=2 "$TEST_IMG" 257e9dbdc5eSMax Reitzprint_refcount_bits 258e9dbdc5eSMax Reitz 259e9dbdc5eSMax Reitz# And try again 260e9dbdc5eSMax Reitz_check_test_img -r all 261e9dbdc5eSMax Reitz 262e9dbdc5eSMax Reitzecho 263e9dbdc5eSMax Reitzecho '=== Multiple walks necessary during amend ===' 264e9dbdc5eSMax Reitzecho 265e9dbdc5eSMax Reitz 266407fb56aSMax Reitz_make_test_img -o "refcount_bits=1,cluster_size=512" 64k 267e9dbdc5eSMax Reitz 268e9dbdc5eSMax Reitz# Cluster 0 is the image header, clusters 1 to 4 are used by the L1 table, a 269e9dbdc5eSMax Reitz# single L2 table, the reftable and a single refblock. This creates 58 data 270e9dbdc5eSMax Reitz# clusters (actually, the L2 table is created here, too), so in total there are 271e9dbdc5eSMax Reitz# then 63 used clusters in the image. With a refcount width of 64, one refblock 272e9dbdc5eSMax Reitz# describes 64 clusters (512 bytes / 64 bits/entry = 64 entries), so this will 273e9dbdc5eSMax Reitz# make the first refblock in the amended image have exactly one free entry. 274e9dbdc5eSMax Reitz$QEMU_IO -c "write 0 $((58 * 512))" "$TEST_IMG" | _filter_qemu_io 275e9dbdc5eSMax Reitz 276e9dbdc5eSMax Reitz# Now change the refcount width; since the first new refblock will have exactly 277e9dbdc5eSMax Reitz# one free entry, that entry will be used to store its own reference. No other 278e9dbdc5eSMax Reitz# refblocks are needed, so then the new reftable will be allocated; since the 279e9dbdc5eSMax Reitz# first new refblock is completely filled up, this will require a new refblock 280e9dbdc5eSMax Reitz# which is why the refcount width changing function will need to run through 281e9dbdc5eSMax Reitz# everything one more time until the allocations are stable. 282e9dbdc5eSMax Reitz# Having more walks than usual should be visible as regressing progress (from 283e9dbdc5eSMax Reitz# 66.67 % (2/3 walks) to 50.00 % (2/4 walks)). 284e9dbdc5eSMax Reitz$QEMU_IMG amend -o refcount_bits=64 -p "$TEST_IMG" | tr '\r' '\n' \ 285e9dbdc5eSMax Reitz | grep -A 1 '66.67' 286e9dbdc5eSMax Reitzprint_refcount_bits 287e9dbdc5eSMax Reitz 288e9dbdc5eSMax Reitz_check_test_img 289e9dbdc5eSMax Reitz 290d2eed8c6SMax Reitz 291d2eed8c6SMax Reitz# success, all done 292d2eed8c6SMax Reitzecho '*** done' 293d2eed8c6SMax Reitzrm -f $seq.full 294d2eed8c6SMax Reitzstatus=0 295