yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-block-Produce-zeros-when-protocols-reading-beyond-en.patch

218e99
From 12e88a4a74931578a5582b5caeca135fe85fb01b Mon Sep 17 00:00:00 2001
218e99
From: Asias He <asias@redhat.com>
218e99
Date: Thu, 12 Sep 2013 07:39:34 +0200
218e99
Subject: [PATCH 14/29] block: Produce zeros when protocols reading beyond end of file
218e99
218e99
RH-Author: Asias He <asias@redhat.com>
218e99
Message-id: <1378971575-22416-4-git-send-email-asias@redhat.com>
218e99
Patchwork-id: 54325
218e99
O-Subject: [RHEL7.0 qemu-kvm PATCH 3/4] block: Produce zeros when protocols reading beyond end of file
218e99
Bugzilla: 1007226
218e99
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
218e99
From: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
218e99
218e99
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1005052
218e99
Brew: https://brewweb.devel.redhat.com/taskinfo?taskID=6275752
218e99
218e99
While Asias is debugging an issue creating qcow2 images on top of
218e99
non-file protocols.  It boils down to this example using NBD:
218e99
218e99
$ qemu-io -c 'open -g nbd+unix:///?socket=/tmp/nbd.sock' -c 'read -v 0 512'
218e99
218e99
Notice the open -g option to set bs->growable.  This means you can
218e99
read/write beyond end of file.  Reading beyond end of file is supposed
218e99
to produce zeroes.
218e99
218e99
We rely on this behavior in qcow2_create2() during qcow2 image
218e99
creation.  We create a new file and then write the qcow2 header
218e99
structure using bdrv_pwrite().  Since QCowHeader is not a multiple of
218e99
sector size, block.c first uses bdrv_read() on the empty file to fetch
218e99
the first sector (should be all zeroes).
218e99
218e99
Here is the output from the qemu-io NBD example above:
218e99
218e99
$ qemu-io -c 'open -g nbd+unix:///?socket=/tmp/nbd.sock' -c 'read -v 0 512'
218e99
00000000:  ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab  ................
218e99
00000010:  ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab  ................
218e99
00000020:  ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab  ................
218e99
...
218e99
218e99
We are not zeroing the buffer!  As a result qcow2 image creation on top
218e99
of protocols is not guaranteed to work even when file creation is
218e99
supported by the protocol.
218e99
218e99
[Adapted this patch to use bs->zero_beyond_eof.
218e99
-- Stefan]
218e99
218e99
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
218e99
Signed-off-by: Asias He <asias@redhat.com>
218e99
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
(cherry picked from commit 893a8f6220368a9ebff9a74bd48359928545cf6a)
218e99
---
218e99
 block.c | 30 +++++++++++++++++++++++++++++-
218e99
 1 file changed, 29 insertions(+), 1 deletion(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block.c |   30 +++++++++++++++++++++++++++++-
218e99
 1 files changed, 29 insertions(+), 1 deletions(-)
218e99
218e99
diff --git a/block.c b/block.c
218e99
index dbcad0e..a3bc800 100644
218e99
--- a/block.c
218e99
+++ b/block.c
218e99
@@ -2555,7 +2555,35 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
218e99
         }
218e99
     }
218e99
 
218e99
-    ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
218e99
+    if (!(bs->zero_beyond_eof && bs->growable)) {
218e99
+        ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
218e99
+    } else {
218e99
+        /* Read zeros after EOF of growable BDSes */
218e99
+        int64_t len, total_sectors, max_nb_sectors;
218e99
+
218e99
+        len = bdrv_getlength(bs);
218e99
+        if (len < 0) {
218e99
+            ret = len;
218e99
+            goto out;
218e99
+        }
218e99
+
218e99
+        total_sectors = len >> BDRV_SECTOR_BITS;
218e99
+        max_nb_sectors = MAX(0, total_sectors - sector_num);
218e99
+        if (max_nb_sectors > 0) {
218e99
+            ret = drv->bdrv_co_readv(bs, sector_num,
218e99
+                                     MIN(nb_sectors, max_nb_sectors), qiov);
218e99
+        } else {
218e99
+            ret = 0;
218e99
+        }
218e99
+
218e99
+        /* Reading beyond end of file is supposed to produce zeroes */
218e99
+        if (ret == 0 && total_sectors < sector_num + nb_sectors) {
218e99
+            uint64_t offset = MAX(0, total_sectors - sector_num);
218e99
+            uint64_t bytes = (sector_num + nb_sectors - offset) *
218e99
+                              BDRV_SECTOR_SIZE;
218e99
+            qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes);
218e99
+        }
218e99
+    }
218e99
 
218e99
 out:
218e99
     tracked_request_end(&req;;
218e99
-- 
218e99
1.7.1
218e99