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

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