xref: /openbmc/qemu/tests/qemu-iotests/305 (revision 30b5707c)
1#!/usr/bin/env bash
2#
3# Test the handling of errors in write requests with multiple allocations
4#
5# Copyright (C) 2020 Igalia, S.L.
6# Author: Alberto Garcia <berto@igalia.com>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22# creator
23owner=berto@igalia.com
24
25seq=`basename $0`
26echo "QA output created by $seq"
27
28status=1    # failure is the default!
29
30_cleanup()
31{
32    _cleanup_test_img
33}
34trap "_cleanup; exit \$status" 0 1 2 3 15
35
36# get standard environment, filters and checks
37. ./common.rc
38. ./common.filter
39
40_supported_fmt qcow2
41_supported_proto file
42_supported_os Linux
43_unsupported_imgopts cluster_size refcount_bits extended_l2 compat=0.10 data_file
44
45echo '### Create the image'
46_make_test_img -o refcount_bits=64,cluster_size=1k 1M
47
48# The reference counts of the clusters for the first 123k of this
49# write request are stored in the first refcount block. The last
50# cluster (guest offset 123k) is referenced in the second refcount
51# block.
52echo '### Fill the first refcount block and one data cluster from the second'
53$QEMU_IO -c 'write 0 124k' "$TEST_IMG" | _filter_qemu_io
54
55echo '### Discard two of the last data clusters, leave one in the middle'
56$QEMU_IO -c 'discard 121k 1k' "$TEST_IMG" | _filter_qemu_io
57$QEMU_IO -c 'discard 123k 1k' "$TEST_IMG" | _filter_qemu_io
58
59echo '### Corrupt the offset of the second refcount block'
60refcount_table_offset=$(peek_file_be "$TEST_IMG" 48 8)
61poke_file "$TEST_IMG" $(($refcount_table_offset+14)) "\x06"
62
63# This tries to allocate the two clusters discarded earlier (guest
64# offsets 121k and 123k). Their reference counts are in the first and
65# second refcount blocks respectively, but only the first one can be
66# allocated correctly because the second entry of the refcount table
67# is corrupted.
68echo '### Try to allocate the discarded clusters again'
69$QEMU_IO -c 'write 121k 3k' "$TEST_IMG" | _filter_qemu_io
70
71# success, all done
72echo "*** done"
73rm -f $seq.full
74status=0
75