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