xref: /qemu/tests/qemu-iotests/272 (revision fef80ea073c4862bc9eaddb6ddb0ed970b8ad7c4)
1b7cd2c11SMax Reitz#!/usr/bin/env bash
2*9dd003a9SVladimir Sementsov-Ogievskiy# group: rw
3b7cd2c11SMax Reitz#
4b7cd2c11SMax Reitz# Test compressed write to a qcow2 image at an offset above 4 GB
5b7cd2c11SMax Reitz#
6b7cd2c11SMax Reitz# Copyright (C) 2019 Red Hat, Inc.
7b7cd2c11SMax Reitz#
8b7cd2c11SMax Reitz# This program is free software; you can redistribute it and/or modify
9b7cd2c11SMax Reitz# it under the terms of the GNU General Public License as published by
10b7cd2c11SMax Reitz# the Free Software Foundation; either version 2 of the License, or
11b7cd2c11SMax Reitz# (at your option) any later version.
12b7cd2c11SMax Reitz#
13b7cd2c11SMax Reitz# This program is distributed in the hope that it will be useful,
14b7cd2c11SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
15b7cd2c11SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16b7cd2c11SMax Reitz# GNU General Public License for more details.
17b7cd2c11SMax Reitz#
18b7cd2c11SMax Reitz# You should have received a copy of the GNU General Public License
19b7cd2c11SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20b7cd2c11SMax Reitz#
21b7cd2c11SMax Reitz
22b7cd2c11SMax Reitzseq=$(basename "$0")
23b7cd2c11SMax Reitzecho "QA output created by $seq"
24b7cd2c11SMax Reitz
25b7cd2c11SMax Reitzstatus=1	# failure is the default!
26b7cd2c11SMax Reitz
27b7cd2c11SMax Reitz_cleanup()
28b7cd2c11SMax Reitz{
29b7cd2c11SMax Reitz    _cleanup_test_img
30b7cd2c11SMax Reitz}
31b7cd2c11SMax Reitztrap "_cleanup; exit \$status" 0 1 2 3 15
32b7cd2c11SMax Reitz
33b7cd2c11SMax Reitz# get standard environment, filters and checks
34b7cd2c11SMax Reitz. ./common.rc
35b7cd2c11SMax Reitz. ./common.filter
36b7cd2c11SMax Reitz
37b7cd2c11SMax Reitz# This is a qcow2 regression test
38b7cd2c11SMax Reitz_supported_fmt qcow2
3957284d2aSMax Reitz_supported_proto file fuse
40b7cd2c11SMax Reitz
41b7cd2c11SMax Reitz# External data files do not support compression;
42b7cd2c11SMax Reitz# We need an exact cluster size (2M) and refcount width (2) so we can
43b7cd2c11SMax Reitz# get this test quickly over with; and this in turn require
44b7cd2c11SMax Reitz# compat=1.1
45b7cd2c11SMax Reitz_unsupported_imgopts data_file cluster_size refcount_bits 'compat=0.10'
46b7cd2c11SMax Reitz
47b7cd2c11SMax Reitz# The idea is: Create an empty file, mark the first 4 GB as used, then
48b7cd2c11SMax Reitz# do a compressed write that thus must be put beyond 4 GB.
49b7cd2c11SMax Reitz# (This used to fail because the compressed sector mask was just a
50b7cd2c11SMax Reitz# 32 bit mask, so qemu-img check will count a cluster before 4 GB as
51b7cd2c11SMax Reitz# referenced twice.)
52b7cd2c11SMax Reitz
53b7cd2c11SMax Reitz# We would like to use refcount_bits=1 here, but then qemu-img check
54b7cd2c11SMax Reitz# will throw an error when trying to count a cluster as referenced
55b7cd2c11SMax Reitz# twice.
56b7cd2c11SMax Reitz_make_test_img -o cluster_size=2M,refcount_bits=2 64M
57b7cd2c11SMax Reitz
58b7cd2c11SMax Reitzreft_offs=$(peek_file_be "$TEST_IMG" 48 8)
59b7cd2c11SMax Reitzrefb_offs=$(peek_file_be "$TEST_IMG" $reft_offs 8)
60b7cd2c11SMax Reitz
61b7cd2c11SMax Reitz# We want to cover 4 GB, those are 2048 clusters, equivalent to
62b7cd2c11SMax Reitz# 4096 bit = 512 B.
63b7cd2c11SMax Reitztruncate -s 4G "$TEST_IMG"
64b7cd2c11SMax Reitzfor ((in_refb_offs = 0; in_refb_offs < 512; in_refb_offs += 8)); do
65b7cd2c11SMax Reitz    poke_file "$TEST_IMG" $((refb_offs + in_refb_offs)) \
66b7cd2c11SMax Reitz        '\x55\x55\x55\x55\x55\x55\x55\x55'
67b7cd2c11SMax Reitzdone
68b7cd2c11SMax Reitz
69b7cd2c11SMax Reitz$QEMU_IO -c 'write -c -P 42 0 2M' "$TEST_IMG" | _filter_qemu_io
70b7cd2c11SMax Reitz
71b7cd2c11SMax Reitzecho
72b7cd2c11SMax Reitzecho '--- Check ---'
73b7cd2c11SMax Reitz
74b7cd2c11SMax Reitz# This should only print the leaked clusters in the first 4 GB
75b7cd2c11SMax Reitz_check_test_img | grep -v '^Leaked cluster '
76b7cd2c11SMax Reitz
77b7cd2c11SMax Reitz# success, all done
78b7cd2c11SMax Reitzecho "*** done"
79b7cd2c11SMax Reitzrm -f $seq.full
80b7cd2c11SMax Reitzstatus=0
81