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