thebeanogamer / rpms / qemu-kvm

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