cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-block-file-posix-Unaligned-O_DIRECT-block-status.patch

b38b0f
From 29592218d57f1fe49c1254fffd9b0206cfe29ec7 Mon Sep 17 00:00:00 2001
b38b0f
From: Max Reitz <mreitz@redhat.com>
b38b0f
Date: Tue, 23 Jul 2019 14:45:40 +0100
b38b0f
Subject: [PATCH 02/14] block/file-posix: Unaligned O_DIRECT block-status
b38b0f
b38b0f
RH-Author: Max Reitz <mreitz@redhat.com>
b38b0f
Message-id: <20190723144546.23701-2-mreitz@redhat.com>
b38b0f
Patchwork-id: 89647
b38b0f
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 1/7] block/file-posix: Unaligned O_DIRECT block-status
b38b0f
Bugzilla: 1678979
b38b0f
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
b38b0f
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
b38b0f
RH-Acked-by: John Snow <jsnow@redhat.com>
b38b0f
b38b0f
Currently, qemu crashes whenever someone queries the block status of an
b38b0f
unaligned image tail of an O_DIRECT image:
b38b0f
$ echo > foo
b38b0f
$ qemu-img map --image-opts driver=file,filename=foo,cache.direct=on
b38b0f
Offset          Length          Mapped to       File
b38b0f
qemu-img: block/io.c:2093: bdrv_co_block_status: Assertion `*pnum &&
b38b0f
QEMU_IS_ALIGNED(*pnum, align) && align > offset - aligned_offset'
b38b0f
failed.
b38b0f
b38b0f
This is because bdrv_co_block_status() checks that the result returned
b38b0f
by the driver's implementation is aligned to the request_alignment, but
b38b0f
file-posix can fail to do so, which is actually mentioned in a comment
b38b0f
there: "[...] possibly including a partial sector at EOF".
b38b0f
b38b0f
Fix this by rounding up those partial sectors.
b38b0f
b38b0f
There are two possible alternative fixes:
b38b0f
(1) We could refuse to open unaligned image files with O_DIRECT
b38b0f
    altogether.  That sounds reasonable until you realize that qcow2
b38b0f
    does necessarily not fill up its metadata clusters, and that nobody
b38b0f
    runs qemu-img create with O_DIRECT.  Therefore, unpreallocated qcow2
b38b0f
    files usually have an unaligned image tail.
b38b0f
b38b0f
(2) bdrv_co_block_status() could ignore unaligned tails.  It actually
b38b0f
    throws away everything past the EOF already, so that sounds
b38b0f
    reasonable.
b38b0f
    Unfortunately, the block layer knows file lengths only with a
b38b0f
    granularity of BDRV_SECTOR_SIZE, so bdrv_co_block_status() usually
b38b0f
    would have to guess whether its file length information is inexact
b38b0f
    or whether the driver is broken.
b38b0f
b38b0f
Fixing what raw_co_block_status() returns is the safest thing to do.
b38b0f
b38b0f
There seems to be no other block driver that sets request_alignment and
b38b0f
does not make sure that it always returns aligned values.
b38b0f
b38b0f
Cc: qemu-stable@nongnu.org
b38b0f
Signed-off-by: Max Reitz <mreitz@redhat.com>
b38b0f
Reviewed-by: Eric Blake <eblake@redhat.com>
b38b0f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
b38b0f
(cherry picked from commit 9c3db310ff0b7473272ae8dce5e04e2f8a825390)
b38b0f
Signed-off-by: Max Reitz <mreitz@redhat.com>
b38b0f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
b38b0f
---
b38b0f
 block/file-posix.c | 16 ++++++++++++++++
b38b0f
 1 file changed, 16 insertions(+)
b38b0f
b38b0f
diff --git a/block/file-posix.c b/block/file-posix.c
b38b0f
index 5fb5a9a..4b404e4 100644
b38b0f
--- a/block/file-posix.c
b38b0f
+++ b/block/file-posix.c
b38b0f
@@ -2413,6 +2413,8 @@ static int coroutine_fn raw_co_block_status(BlockDriverState *bs,
b38b0f
     off_t data = 0, hole = 0;
b38b0f
     int ret;
b38b0f
 
b38b0f
+    assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
b38b0f
+
b38b0f
     ret = fd_open(bs);
b38b0f
     if (ret < 0) {
b38b0f
         return ret;
b38b0f
@@ -2438,6 +2440,20 @@ static int coroutine_fn raw_co_block_status(BlockDriverState *bs,
b38b0f
         /* On a data extent, compute bytes to the end of the extent,
b38b0f
          * possibly including a partial sector at EOF. */
b38b0f
         *pnum = MIN(bytes, hole - offset);
b38b0f
+
b38b0f
+        /*
b38b0f
+         * We are not allowed to return partial sectors, though, so
b38b0f
+         * round up if necessary.
b38b0f
+         */
b38b0f
+        if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) {
b38b0f
+            int64_t file_length = raw_getlength(bs);
b38b0f
+            if (file_length > 0) {
b38b0f
+                /* Ignore errors, this is just a safeguard */
b38b0f
+                assert(hole == file_length);
b38b0f
+            }
b38b0f
+            *pnum = ROUND_UP(*pnum, bs->bl.request_alignment);
b38b0f
+        }
b38b0f
+
b38b0f
         ret = BDRV_BLOCK_DATA;
b38b0f
     } else {
b38b0f
         /* On a hole, compute bytes to the beginning of the next extent.  */
b38b0f
-- 
b38b0f
1.8.3.1
b38b0f