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

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