thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
432cb7
From 0ba4c0836f702bb3abbd173c7ee486a8247331ae Mon Sep 17 00:00:00 2001
432cb7
From: Hanna Reitz <hreitz@redhat.com>
432cb7
Date: Tue, 18 Jan 2022 18:00:00 +0100
432cb7
Subject: [PATCH 7/7] iotests/block-status-cache: New test
432cb7
432cb7
RH-Author: Hanna Reitz <hreitz@redhat.com>
432cb7
RH-MergeRequest: 112: block/io: Update BSC only if want_zero is true
432cb7
RH-Commit: [2/2] ba86b4db32c33e17a85f476d445ef0523cf8f60e
432cb7
RH-Bugzilla: 2041480
432cb7
RH-Acked-by: Eric Blake <eblake@redhat.com>
432cb7
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
432cb7
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
432cb7
432cb7
Add a new test to verify that want_zero=false block-status calls do not
432cb7
pollute the block-status cache for want_zero=true calls.
432cb7
432cb7
We check want_zero=true calls and their results using `qemu-img map`
432cb7
(over NBD), and want_zero=false calls also using `qemu-img map` over
432cb7
NBD, but using the qemu:allocation-depth context.
432cb7
432cb7
(This test case cannot be integrated into nbd-qemu-allocation, because
432cb7
that is a qcow2 test, and this is a raw test.)
432cb7
432cb7
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
432cb7
Message-Id: <20220118170000.49423-3-hreitz@redhat.com>
432cb7
Reviewed-by: Nir Soffer <nsoffer@redhat.com>
432cb7
Reviewed-by: Eric Blake <eblake@redhat.com>
432cb7
Tested-by: Eric Blake <eblake@redhat.com>
432cb7
Signed-off-by: Eric Blake <eblake@redhat.com>
432cb7
(cherry picked from commit 6384dd534d742123d26c008d9794b20bc41359d5)
432cb7
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
432cb7
---
432cb7
 tests/qemu-iotests/tests/block-status-cache   | 139 ++++++++++++++++++
432cb7
 .../qemu-iotests/tests/block-status-cache.out |   5 +
432cb7
 2 files changed, 144 insertions(+)
432cb7
 create mode 100755 tests/qemu-iotests/tests/block-status-cache
432cb7
 create mode 100644 tests/qemu-iotests/tests/block-status-cache.out
432cb7
432cb7
diff --git a/tests/qemu-iotests/tests/block-status-cache b/tests/qemu-iotests/tests/block-status-cache
432cb7
new file mode 100755
432cb7
index 0000000000..6fa10bb8f8
432cb7
--- /dev/null
432cb7
+++ b/tests/qemu-iotests/tests/block-status-cache
432cb7
@@ -0,0 +1,139 @@
432cb7
+#!/usr/bin/env python3
432cb7
+# group: rw quick
432cb7
+#
432cb7
+# Test cases for the block-status cache.
432cb7
+#
432cb7
+# Copyright (C) 2022 Red Hat, Inc.
432cb7
+#
432cb7
+# This program is free software; you can redistribute it and/or modify
432cb7
+# it under the terms of the GNU General Public License as published by
432cb7
+# the Free Software Foundation; either version 2 of the License, or
432cb7
+# (at your option) any later version.
432cb7
+#
432cb7
+# This program is distributed in the hope that it will be useful,
432cb7
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
432cb7
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
432cb7
+# GNU General Public License for more details.
432cb7
+#
432cb7
+# You should have received a copy of the GNU General Public License
432cb7
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
432cb7
+#
432cb7
+
432cb7
+import os
432cb7
+import signal
432cb7
+import iotests
432cb7
+from iotests import qemu_img_create, qemu_img_pipe, qemu_nbd
432cb7
+
432cb7
+
432cb7
+image_size = 1 * 1024 * 1024
432cb7
+test_img = os.path.join(iotests.test_dir, 'test.img')
432cb7
+
432cb7
+nbd_pidfile = os.path.join(iotests.test_dir, 'nbd.pid')
432cb7
+nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
432cb7
+
432cb7
+
432cb7
+class TestBscWithNbd(iotests.QMPTestCase):
432cb7
+    def setUp(self) -> None:
432cb7
+        """Just create an empty image with a read-only NBD server on it"""
432cb7
+        assert qemu_img_create('-f', iotests.imgfmt, test_img,
432cb7
+                               str(image_size)) == 0
432cb7
+
432cb7
+        # Pass --allocation-depth to enable the qemu:allocation-depth context,
432cb7
+        # which we are going to query to provoke a block-status inquiry with
432cb7
+        # want_zero=false.
432cb7
+        assert qemu_nbd(f'--socket={nbd_sock}',
432cb7
+                        f'--format={iotests.imgfmt}',
432cb7
+                        '--persistent',
432cb7
+                        '--allocation-depth',
432cb7
+                        '--read-only',
432cb7
+                        f'--pid-file={nbd_pidfile}',
432cb7
+                        test_img) \
432cb7
+            == 0
432cb7
+
432cb7
+    def tearDown(self) -> None:
432cb7
+        with open(nbd_pidfile, encoding='utf-8') as f:
432cb7
+            pid = int(f.read())
432cb7
+        os.kill(pid, signal.SIGTERM)
432cb7
+        os.remove(nbd_pidfile)
432cb7
+        os.remove(test_img)
432cb7
+
432cb7
+    def test_with_zero_bug(self) -> None:
432cb7
+        """
432cb7
+        Verify that the block-status cache is not corrupted by a
432cb7
+        want_zero=false call.
432cb7
+        We can provoke a want_zero=false call with `qemu-img map` over NBD with
432cb7
+        x-dirty-bitmap=qemu:allocation-depth, so we first run a normal `map`
432cb7
+        (which results in want_zero=true), then using said
432cb7
+        qemu:allocation-depth context, and finally another normal `map` to
432cb7
+        verify that the cache has not been corrupted.
432cb7
+        """
432cb7
+
432cb7
+        nbd_img_opts = f'driver=nbd,server.type=unix,server.path={nbd_sock}'
432cb7
+        nbd_img_opts_alloc_depth = nbd_img_opts + \
432cb7
+            ',x-dirty-bitmap=qemu:allocation-depth'
432cb7
+
432cb7
+        # Normal map, results in want_zero=true.
432cb7
+        # This will probably detect an allocated data sector first (qemu likes
432cb7
+        # to allocate the first sector to facilitate alignment probing), and
432cb7
+        # then the rest to be zero.  The BSC will thus contain (if anything)
432cb7
+        # one range covering the first sector.
432cb7
+        map_pre = qemu_img_pipe('map', '--output=json', '--image-opts',
432cb7
+                                nbd_img_opts)
432cb7
+
432cb7
+        # qemu:allocation-depth maps for want_zero=false.
432cb7
+        # want_zero=false should (with the file driver, which the server is
432cb7
+        # using) report everything as data.  While this is sufficient for
432cb7
+        # want_zero=false, this is nothing that should end up in the
432cb7
+        # block-status cache.
432cb7
+        # Due to a bug, this information did end up in the cache, though, and
432cb7
+        # this would lead to wrong information being returned on subsequent
432cb7
+        # want_zero=true calls.
432cb7
+        #
432cb7
+        # We need to run this map twice: On the first call, we probably still
432cb7
+        # have the first sector in the cache, and so this will be served from
432cb7
+        # the cache; and only the subsequent range will be queried from the
432cb7
+        # block driver.  This subsequent range will then be entered into the
432cb7
+        # cache.
432cb7
+        # If we did a want_zero=true call at this point, we would thus get
432cb7
+        # correct information: The first sector is not covered by the cache, so
432cb7
+        # we would get fresh block-status information from the driver, which
432cb7
+        # would return a data range, and this would then go into the cache,
432cb7
+        # evicting the wrong range from the want_zero=false call before.
432cb7
+        #
432cb7
+        # Therefore, we need a second want_zero=false map to reproduce:
432cb7
+        # Since the first sector is not in the cache, the query for its status
432cb7
+        # will go to the driver, which will return a result that reports the
432cb7
+        # whole image to be a single data area.  This result will then go into
432cb7
+        # the cache, and so the cache will then report the whole image to
432cb7
+        # contain data.
432cb7
+        #
432cb7
+        # Note that once the cache reports the whole image to contain data, any
432cb7
+        # subsequent map operation will be served from the cache, and so we can
432cb7
+        # never loop too many times here.
432cb7
+        for _ in range(2):
432cb7
+            # (Ignore the result, this is just to contaminate the cache)
432cb7
+            qemu_img_pipe('map', '--output=json', '--image-opts',
432cb7
+                          nbd_img_opts_alloc_depth)
432cb7
+
432cb7
+        # Now let's see whether the cache reports everything as data, or
432cb7
+        # whether we get correct information (i.e. the same as we got on our
432cb7
+        # first attempt).
432cb7
+        map_post = qemu_img_pipe('map', '--output=json', '--image-opts',
432cb7
+                                 nbd_img_opts)
432cb7
+
432cb7
+        if map_pre != map_post:
432cb7
+            print('ERROR: Map information differs before and after querying ' +
432cb7
+                  'qemu:allocation-depth')
432cb7
+            print('Before:')
432cb7
+            print(map_pre)
432cb7
+            print('After:')
432cb7
+            print(map_post)
432cb7
+
432cb7
+            self.fail("Map information differs")
432cb7
+
432cb7
+
432cb7
+if __name__ == '__main__':
432cb7
+    # The block-status cache only works on the protocol layer, so to test it,
432cb7
+    # we can only use the raw format
432cb7
+    iotests.main(supported_fmts=['raw'],
432cb7
+                 supported_protocols=['file'])
432cb7
diff --git a/tests/qemu-iotests/tests/block-status-cache.out b/tests/qemu-iotests/tests/block-status-cache.out
432cb7
new file mode 100644
432cb7
index 0000000000..ae1213e6f8
432cb7
--- /dev/null
432cb7
+++ b/tests/qemu-iotests/tests/block-status-cache.out
432cb7
@@ -0,0 +1,5 @@
432cb7
+.
432cb7
+----------------------------------------------------------------------
432cb7
+Ran 1 tests
432cb7
+
432cb7
+OK
432cb7
-- 
432cb7
2.27.0
432cb7