1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test cases for the block-status cache.
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 signal
24import iotests
25from iotests import qemu_img_create, qemu_img_map, qemu_nbd
26
27
28image_size = 1 * 1024 * 1024
29test_img = os.path.join(iotests.test_dir, 'test.img')
30
31nbd_pidfile = os.path.join(iotests.test_dir, 'nbd.pid')
32nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
33
34
35class TestBscWithNbd(iotests.QMPTestCase):
36    def setUp(self) -> None:
37        """Just create an empty image with a read-only NBD server on it"""
38        qemu_img_create('-f', iotests.imgfmt, test_img, str(image_size))
39
40        # Pass --allocation-depth to enable the qemu:allocation-depth context,
41        # which we are going to query to provoke a block-status inquiry with
42        # want_zero=false.
43        assert qemu_nbd(f'--socket={nbd_sock}',
44                        f'--format={iotests.imgfmt}',
45                        '--persistent',
46                        '--allocation-depth',
47                        '--read-only',
48                        f'--pid-file={nbd_pidfile}',
49                        test_img) \
50            == 0
51
52    def tearDown(self) -> None:
53        with open(nbd_pidfile, encoding='utf-8') as f:
54            pid = int(f.read())
55        os.kill(pid, signal.SIGTERM)
56        os.remove(nbd_pidfile)
57        os.remove(test_img)
58
59    def test_with_zero_bug(self) -> None:
60        """
61        Verify that the block-status cache is not corrupted by a
62        want_zero=false call.
63        We can provoke a want_zero=false call with `qemu-img map` over NBD with
64        x-dirty-bitmap=qemu:allocation-depth, so we first run a normal `map`
65        (which results in want_zero=true), then using said
66        qemu:allocation-depth context, and finally another normal `map` to
67        verify that the cache has not been corrupted.
68        """
69
70        nbd_img_opts = f'driver=nbd,server.type=unix,server.path={nbd_sock}'
71        nbd_img_opts_alloc_depth = nbd_img_opts + \
72            ',x-dirty-bitmap=qemu:allocation-depth'
73
74        # Normal map, results in want_zero=true.
75        # This will probably detect an allocated data sector first (qemu likes
76        # to allocate the first sector to facilitate alignment probing), and
77        # then the rest to be zero.  The BSC will thus contain (if anything)
78        # one range covering the first sector.
79        map_pre = qemu_img_map('--image-opts', nbd_img_opts)
80
81        # qemu:allocation-depth maps for want_zero=false.
82        # want_zero=false should (with the file driver, which the server is
83        # using) report everything as data.  While this is sufficient for
84        # want_zero=false, this is nothing that should end up in the
85        # block-status cache.
86        # Due to a bug, this information did end up in the cache, though, and
87        # this would lead to wrong information being returned on subsequent
88        # want_zero=true calls.
89        #
90        # We need to run this map twice: On the first call, we probably still
91        # have the first sector in the cache, and so this will be served from
92        # the cache; and only the subsequent range will be queried from the
93        # block driver.  This subsequent range will then be entered into the
94        # cache.
95        # If we did a want_zero=true call at this point, we would thus get
96        # correct information: The first sector is not covered by the cache, so
97        # we would get fresh block-status information from the driver, which
98        # would return a data range, and this would then go into the cache,
99        # evicting the wrong range from the want_zero=false call before.
100        #
101        # Therefore, we need a second want_zero=false map to reproduce:
102        # Since the first sector is not in the cache, the query for its status
103        # will go to the driver, which will return a result that reports the
104        # whole image to be a single data area.  This result will then go into
105        # the cache, and so the cache will then report the whole image to
106        # contain data.
107        #
108        # Note that once the cache reports the whole image to contain data, any
109        # subsequent map operation will be served from the cache, and so we can
110        # never loop too many times here.
111        for _ in range(2):
112            # (Ignore the result, this is just to contaminate the cache)
113            qemu_img_map('--image-opts', nbd_img_opts_alloc_depth)
114
115        # Now let's see whether the cache reports everything as data, or
116        # whether we get correct information (i.e. the same as we got on our
117        # first attempt).
118        map_post = qemu_img_map('--image-opts', nbd_img_opts)
119
120        if map_pre != map_post:
121            print('ERROR: Map information differs before and after querying ' +
122                  'qemu:allocation-depth')
123            print('Before:')
124            print(map_pre)
125            print('After:')
126            print(map_post)
127
128            self.fail("Map information differs")
129
130
131if __name__ == '__main__':
132    # The block-status cache only works on the protocol layer, so to test it,
133    # we can only use the raw format
134    iotests.main(supported_fmts=['raw'],
135                 supported_protocols=['file'])
136