thebeanogamer / rpms / qemu-kvm

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