1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test what happens when a stream job completes in a blk_drain().
5#
6# Copyright (C) 2022 Red Hat, Inc.
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
22import os
23import iotests
24from iotests import imgfmt, qemu_img_create, qemu_io, QMPTestCase
25
26
27image_size = 1 * 1024 * 1024
28data_size = 64 * 1024
29base = os.path.join(iotests.test_dir, 'base.img')
30top = os.path.join(iotests.test_dir, 'top.img')
31
32
33# We want to test completing a stream job in a blk_drain().
34#
35# The blk_drain() we are going to use is a virtio-scsi device resetting,
36# which we can trigger by resetting the system.
37#
38# In order to have the block job complete on drain, we (1) throttle its
39# base image so we can start the drain after it has begun, but before it
40# completes, and (2) make it encounter an I/O error on the ensuing write.
41# (If it completes regularly, the completion happens after the drain for
42# some reason.)
43
44class TestStreamErrorOnReset(QMPTestCase):
45    def setUp(self) -> None:
46        """
47        Create two images:
48        - base image {base} with {data_size} bytes allocated
49        - top image {top} without any data allocated
50
51        And the following VM configuration:
52        - base image throttled to {data_size}
53        - top image with a blkdebug configuration so the first write access
54          to it will result in an error
55        - top image is attached to a virtio-scsi device
56        """
57        qemu_img_create('-f', imgfmt, base, str(image_size))
58        qemu_io('-c', f'write 0 {data_size}', base)
59        qemu_img_create('-f', imgfmt, top, str(image_size))
60
61        self.vm = iotests.VM()
62        self.vm.add_args('-accel', 'tcg') # Make throttling work properly
63        self.vm.add_object(self.vm.qmp_to_opts({
64            'qom-type': 'throttle-group',
65            'id': 'thrgr',
66            'x-bps-total': str(data_size)
67        }))
68        self.vm.add_blockdev(self.vm.qmp_to_opts({
69            'driver': imgfmt,
70            'node-name': 'base',
71            'file': {
72                'driver': 'throttle',
73                'throttle-group': 'thrgr',
74                'file': {
75                    'driver': 'file',
76                    'filename': base
77                }
78            }
79        }))
80        self.vm.add_blockdev(self.vm.qmp_to_opts({
81            'driver': imgfmt,
82            'node-name': 'top',
83            'file': {
84                'driver': 'blkdebug',
85                'node-name': 'top-blkdebug',
86                'inject-error': [{
87                    'event': 'pwritev',
88                    'immediately': 'true',
89                    'once': 'true'
90                }],
91                'image': {
92                    'driver': 'file',
93                    'filename': top
94                }
95            },
96            'backing': 'base'
97        }))
98        self.vm.add_device(self.vm.qmp_to_opts({
99            'driver': 'virtio-scsi',
100            'id': 'vscsi'
101        }))
102        self.vm.add_device(self.vm.qmp_to_opts({
103            'driver': 'scsi-hd',
104            'bus': 'vscsi.0',
105            'drive': 'top'
106        }))
107        self.vm.launch()
108
109    def tearDown(self) -> None:
110        self.vm.shutdown()
111        os.remove(top)
112        os.remove(base)
113
114    def test_stream_error_on_reset(self) -> None:
115        # Launch a stream job, which will take at least a second to
116        # complete, because the base image is throttled (so we can
117        # get in between it having started and it having completed)
118        self.vm.cmd('block-stream', job_id='stream', device='top')
119
120        while True:
121            ev = self.vm.event_wait('JOB_STATUS_CHANGE')
122            if ev['data']['status'] == 'running':
123                # Once the stream job is running, reset the system, which
124                # forces the virtio-scsi device to be reset, thus draining
125                # the stream job, and making it complete.  Completing
126                # inside of that drain should not result in a segfault.
127                self.vm.cmd('system_reset')
128            elif ev['data']['status'] == 'null':
129                # The test is done once the job is gone
130                break
131
132
133if __name__ == '__main__':
134    # Passes with any format with backing file support, but qed and
135    # qcow1 do not seem to exercise the used-to-be problematic code
136    # path, so there is no point in having them in this list
137    iotests.main(supported_fmts=['qcow2', 'vmdk'],
138                 supported_protocols=['file'])
139