9ae3a8
From 39ee3b773c08395007137198542c2012e11295f5 Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Mon, 2 Dec 2013 16:09:46 +0100
9ae3a8
Subject: [PATCH 12/37] block: Introduce bdrv_co_do_preadv()
9ae3a8
9ae3a8
Message-id: <1392117622-28812-13-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: 57177
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 12/37] block: Introduce bdrv_co_do_preadv()
9ae3a8
Bugzilla: 748906
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
Similar to bdrv_pread(), which aligns byte-aligned request to 512 byte
9ae3a8
sectors, bdrv_co_do_preadv() takes a byte-aligned request and aligns it
9ae3a8
to the alignment specified in bs->request_alignment.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Reviewed-by: Benoit Canet <benoit@irqsave.net>
9ae3a8
(cherry picked from commit 1b0288ae7fc695a8e652973f75e92464bbc13416)
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
9ae3a8
 1 file changed, 58 insertions(+), 6 deletions(-)
9ae3a8
---
9ae3a8
 block.c |   64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
9ae3a8
 1 files changed, 58 insertions(+), 6 deletions(-)
9ae3a8
9ae3a8
diff --git a/block.c b/block.c
9ae3a8
index 1787ab0..ce3edb0 100644
9ae3a8
--- a/block.c
9ae3a8
+++ b/block.c
9ae3a8
@@ -2809,17 +2809,23 @@ out:
9ae3a8
 /*
9ae3a8
  * Handle a read request in coroutine context
9ae3a8
  */
9ae3a8
-static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
9ae3a8
-    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
9ae3a8
+static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
9ae3a8
+    int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
9ae3a8
     BdrvRequestFlags flags)
9ae3a8
 {
9ae3a8
     BlockDriver *drv = bs->drv;
9ae3a8
+    /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
9ae3a8
+    uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
9ae3a8
+    uint8_t *head_buf = NULL;
9ae3a8
+    uint8_t *tail_buf = NULL;
9ae3a8
+    QEMUIOVector local_qiov;
9ae3a8
+    bool use_local_qiov = false;
9ae3a8
     int ret;
9ae3a8
 
9ae3a8
     if (!drv) {
9ae3a8
         return -ENOMEDIUM;
9ae3a8
     }
9ae3a8
-    if (bdrv_check_request(bs, sector_num, nb_sectors)) {
9ae3a8
+    if (bdrv_check_byte_request(bs, offset, bytes)) {
9ae3a8
         return -EIO;
9ae3a8
     }
9ae3a8
 
9ae3a8
@@ -2829,14 +2835,60 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
9ae3a8
 
9ae3a8
     /* throttling disk I/O */
9ae3a8
     if (bs->io_limits_enabled) {
9ae3a8
-        bdrv_io_limits_intercept(bs, false, nb_sectors);
9ae3a8
+        /* TODO Switch to byte granularity */
9ae3a8
+        bdrv_io_limits_intercept(bs, false, bytes >> BDRV_SECTOR_BITS);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* Align read if necessary by padding qiov */
9ae3a8
+    if (offset & (align - 1)) {
9ae3a8
+        head_buf = qemu_blockalign(bs, align);
9ae3a8
+        qemu_iovec_init(&local_qiov, qiov->niov + 2);
9ae3a8
+        qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1));
9ae3a8
+        qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
9ae3a8
+        use_local_qiov = true;
9ae3a8
+
9ae3a8
+        bytes += offset & (align - 1);
9ae3a8
+        offset = offset & ~(align - 1);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if ((offset + bytes) & (align - 1)) {
9ae3a8
+        if (!use_local_qiov) {
9ae3a8
+            qemu_iovec_init(&local_qiov, qiov->niov + 1);
9ae3a8
+            qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size);
9ae3a8
+            use_local_qiov = true;
9ae3a8
+        }
9ae3a8
+        tail_buf = qemu_blockalign(bs, align);
9ae3a8
+        qemu_iovec_add(&local_qiov, tail_buf,
9ae3a8
+                       align - ((offset + bytes) & (align - 1)));
9ae3a8
+
9ae3a8
+        bytes = ROUND_UP(bytes, align);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    ret = bdrv_aligned_preadv(bs, offset, bytes,
9ae3a8
+                              use_local_qiov ? &local_qiov : qiov,
9ae3a8
+                              flags);
9ae3a8
+
9ae3a8
+    if (use_local_qiov) {
9ae3a8
+        qemu_iovec_destroy(&local_qiov);
9ae3a8
+        qemu_vfree(head_buf);
9ae3a8
+        qemu_vfree(tail_buf);
9ae3a8
     }
9ae3a8
 
9ae3a8
-    ret = bdrv_aligned_preadv(bs, sector_num << BDRV_SECTOR_BITS,
9ae3a8
-                             nb_sectors << BDRV_SECTOR_BITS, qiov, flags);
9ae3a8
     return ret;
9ae3a8
 }
9ae3a8
 
9ae3a8
+static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
9ae3a8
+    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
9ae3a8
+    BdrvRequestFlags flags)
9ae3a8
+{
9ae3a8
+    if (nb_sectors < 0 || nb_sectors > (UINT_MAX >> BDRV_SECTOR_BITS)) {
9ae3a8
+        return -EINVAL;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    return bdrv_co_do_preadv(bs, sector_num << BDRV_SECTOR_BITS,
9ae3a8
+                             nb_sectors << BDRV_SECTOR_BITS, qiov, flags);
9ae3a8
+}
9ae3a8
+
9ae3a8
 int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
9ae3a8
     int nb_sectors, QEMUIOVector *qiov)
9ae3a8
 {
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8