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