Skip to contentSkip to conversation

block: Introduce common read/write function

PATCH Applied Kevin Wolf 2 participants 11 messages Mar 8, 2016 – Mar 17, 2016 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover block: Introduce common read/write function

BlockBackends support a few different interfaces for reads and writes. Until now they used to forward the requests directly to the BDS layer, which implemented wrappers around one central common read/write function that contained the actual implementation of features provided by the block layer.

This only works as long as none of the features are actually on the BlockBackend level. As it happens, we have features (writethrough cache, I/O throttling) that are currently implemented in the BDS, but must move to the BB.

As a preparation, this series introduces the mapping of the existing APIs to a single coroutine based preadv/pwritev function (as we already have on the BDS level) to the BB layer. The BDS version of the emulation can't go away just yet because there are internal users of them, but the goal is to remove them in the long term.

Depends on Max's "blockdev: Further BlockBackend work".

Kevin Wolf (8):
block: Use BdrvChild in BlockBackend
block: Use blk_co_preadv() for blk_read()
block: Use blk_co_pwritev() for blk_write()
block: Pull up blk_read_unthrottled() implementation
block: Use blk_co_pwritev() in blk_write_zeroes()
block: Use blk_prw() in blk_pread()/blk_pwrite()
block: Use blk_aio_prwv() for aio_read/write/write_zeroes
block: Use blk_co_pwritev() in blk_co_write_zeroes()

 block.c                   |  47 +++--
 block/block-backend.c     | 485 +++++++++++++++++++++++++++++++++-------------
 block/io.c                |  24 +--
 include/block/block.h     |   2 -
 include/block/block_int.h |  12 ++
 5 files changed, 401 insertions(+), 169 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/8 block: Use BdrvChild in BlockBackend

---
 block.c                   |  47 ++++++---
 block/block-backend.c     | 254 ++++++++++++++++++++++++++++------------------
 include/block/block_int.h |   5 +
 3 files changed, 192 insertions(+), 114 deletions(-)
block.c +34 -13
index 11ba73c..57056b5 100644
@@ -1195,10 +1195,9 @@ static int bdrv_fill_options(QDict **options, const char *filename,
     return 0;
 }
 
-static BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
-                                    BlockDriverState *child_bs,
-                                    const char *child_name,
-                                    const BdrvChildRole *child_role)
+BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
+                                  const char *child_name,
+                                  const BdrvChildRole *child_role)
 {
     BdrvChild *child = g_new(BdrvChild, 1);
     *child = (BdrvChild) {
@@ -1207,24 +1206,43 @@ static BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
         .role   = child_role,
     };
 
-    QLIST_INSERT_HEAD(&parent_bs->children, child, next);
     QLIST_INSERT_HEAD(&child_bs->parents, child, next_parent);
 
     return child;
 }
 
+static BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
+                                    BlockDriverState *child_bs,
+                                    const char *child_name,
+                                    const BdrvChildRole *child_role)
+{
+    BdrvChild *child = bdrv_root_attach_child(child_bs, child_name, child_role);
+    QLIST_INSERT_HEAD(&parent_bs->children, child, next);
+    return child;
+}
+
 static void bdrv_detach_child(BdrvChild *child)
 {
-    QLIST_REMOVE(child, next);
+    if (child->next.le_prev) {
+        QLIST_REMOVE(child, next);
+        child->next.le_prev = NULL;
+    }
     QLIST_REMOVE(child, next_parent);
     g_free(child->name);
     g_free(child);
 }
 
-void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
+void bdrv_root_unref_child(BdrvChild *child)
 {
     BlockDriverState *child_bs;
 
+    child_bs = child->bs;
+    bdrv_detach_child(child);
+    bdrv_unref(child_bs);
+}
+
+void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
+{
     if (child == NULL) {
         return;
     }
@@ -1233,9 +1251,7 @@ void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
         child->bs->inherits_from = NULL;
     }
 
-    child_bs = child->bs;
-    bdrv_detach_child(child);
-    bdrv_unref(child_bs);
+    bdrv_root_unref_child(child);
 }
 
 /*
@@ -2275,6 +2291,14 @@ static void change_parent_backing_link(BlockDriverState *from,
 {
     BdrvChild *c, *next;
 
+    if (from->blk) {
+        /* FIXME We bypass blk_set_bs(), so we need to make these updates
+         * manually. The root problem is not in this change function, but the
+         * existence of BlockDriverState.blk. */
+        to->blk = from->blk;
+        from->blk = NULL;
+    }
+
     QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {
         assert(c->role != &child_backing);
         c->bs = to;
@@ -2283,9 +2307,6 @@ static void change_parent_backing_link(BlockDriverState *from,
         bdrv_ref(to);
         bdrv_unref(from);
     }
-    if (from->blk) {
-        blk_set_bs(from->blk, to);
-    }
 }
 
 static void swap_feature_fields(BlockDriverState *bs_top,
block/block-backend.c +153 -101

659 lines not on this page · raw message

include/block/block_int.h +5 -0
index fdcacab..9d3a2cc 100644
@@ -690,6 +690,11 @@ void backup_start(BlockDriverState *bs, BlockDriverState *target,
                   BlockCompletionFunc *cb, void *opaque,
                   BlockJobTxn *txn, Error **errp);
 
+BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
+                                  const char *child_name,
+                                  const BdrvChildRole *child_role);
+void bdrv_root_unref_child(BdrvChild *child);
+
 void blk_set_bs(BlockBackend *blk, BlockDriverState *bs);
 
 void blk_dev_change_media_cb(BlockBackend *blk, bool load);
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 2/8 block: Use blk_co_preadv() for blk_read()

In reply to Kevin Wolf

This patch introduces blk_co_preadv() as a central function on the BlockBackend level that is supposed to handle all read requests from the BB to its root BDS eventually.

---
 block/block-backend.c     | 64 ++++++++++++++++++++++++++++++++++++++++++++---
 block/io.c                |  5 +---
 include/block/block_int.h |  4 +++
 3 files changed, 65 insertions(+), 8 deletions(-)
block/block-backend.c +60 -4
index 056d79e..d97d1ac 100644
@@ -22,6 +22,8 @@
 /* Number of coroutines to reserve per attached device model */
 #define COROUTINE_POOL_RESERVATION 64
 
+#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
+
 static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
 
 struct BlockBackend {
@@ -697,15 +699,69 @@ static int blk_check_request(BlockBackend *blk, int64_t sector_num,
                                   nb_sectors * BDRV_SECTOR_SIZE);
 }
 
-int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
-             int nb_sectors)
+static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
+                                      unsigned int bytes, QEMUIOVector *qiov,
+                                      BdrvRequestFlags flags)
 {
-    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    int ret = blk_check_byte_request(blk, offset, bytes);
     if (ret < 0) {
         return ret;
     }
 
-    return bdrv_read(blk_bs(blk), sector_num, buf, nb_sectors);
+    return bdrv_co_do_preadv(blk_bs(blk), offset, bytes, qiov, flags);
+}
+
+typedef struct BlkRwCo {
+    BlockBackend *blk;
+    int64_t offset;
+    QEMUIOVector *qiov;
+    int ret;
+    BdrvRequestFlags flags;
+} BlkRwCo;
+
+static void blk_read_entry(void *opaque)
+{
+    BlkRwCo *rwco = opaque;
+
+    rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
+                              rwco->qiov, rwco->flags);
+}
+
+int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
+             int nb_sectors)
+{
+    AioContext *aio_context;
+    QEMUIOVector qiov;
+    struct iovec iov;
+    Coroutine *co;
+    BlkRwCo rwco;
+
+    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
+        return -EINVAL;
+    }
+
+    iov = (struct iovec) {
+        .iov_base = buf,
+        .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
+    };
+    qemu_iovec_init_external(&qiov, &iov, 1);
+
+    rwco = (BlkRwCo) {
+        .blk    = blk,
+        .offset = sector_num << BDRV_SECTOR_BITS,
+        .qiov   = &qiov,
+        .ret    = NOT_DONE,
+    };
+
+    co = qemu_coroutine_create(blk_read_entry);
+    qemu_coroutine_enter(co, &rwco);
+
+    aio_context = blk_get_aio_context(blk);
+    while (rwco.ret == NOT_DONE) {
+        aio_poll(aio_context, true);
+    }
+
+    return rwco.ret;
 }
 
 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
block/io.c +1 -4
index 5f9b6d6..c7084e4 100644
@@ -44,9 +44,6 @@ static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
                                          int64_t sector_num, int nb_sectors,
                                          QEMUIOVector *iov);
-static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
-    int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
-    BdrvRequestFlags flags);
 static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
     int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
     BdrvRequestFlags flags);
@@ -939,7 +936,7 @@ out:
 /*
  * Handle a read request in coroutine context
  */
-static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
+int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
     int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
     BdrvRequestFlags flags)
 {
include/block/block_int.h +4 -0
index 9d3a2cc..db830f6 100644
@@ -508,6 +508,10 @@ extern BlockDriver bdrv_qcow2;
  */
 void bdrv_setup_io_funcs(BlockDriver *bdrv);
 
+int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
+    int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
+    BdrvRequestFlags flags);
+
 int get_tmp_filename(char *filename, int size);
 BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
                             const char *filename);
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 3/8 block: Use blk_co_pwritev() for blk_write()

In reply to Kevin Wolf
---
 block/block-backend.c     | 39 ++++++++++++++++++++++++++++++---------
 block/io.c                |  5 +----
 include/block/block_int.h |  3 +++
 3 files changed, 34 insertions(+), 13 deletions(-)
block/block-backend.c +30 -9
index d97d1ac..e159647 100644
@@ -711,6 +711,18 @@ static int coroutine_fn blk_co_preadv(BlockBackend *blk, int64_t offset,
     return bdrv_co_do_preadv(blk_bs(blk), offset, bytes, qiov, flags);
 }
 
+static int coroutine_fn blk_co_pwritev(BlockBackend *blk, int64_t offset,
+                                      unsigned int bytes, QEMUIOVector *qiov,
+                                      BdrvRequestFlags flags)
+{
+    int ret = blk_check_byte_request(blk, offset, bytes);
+    if (ret < 0) {
+        return ret;
+    }
+
+    return bdrv_co_do_pwritev(blk_bs(blk), offset, bytes, qiov, flags);
+}
+
 typedef struct BlkRwCo {
     BlockBackend *blk;
     int64_t offset;
@@ -727,8 +739,16 @@ static void blk_read_entry(void *opaque)
                               rwco->qiov, rwco->flags);
 }
 
-int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
-             int nb_sectors)
+static void blk_write_entry(void *opaque)
+{
+    BlkRwCo *rwco = opaque;
+
+    rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
+                               rwco->qiov, rwco->flags);
+}
+
+static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
+                  int nb_sectors, CoroutineEntry co_entry)
 {
     AioContext *aio_context;
     QEMUIOVector qiov;
@@ -753,7 +773,7 @@ int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
         .ret    = NOT_DONE,
     };
 
-    co = qemu_coroutine_create(blk_read_entry);
+    co = qemu_coroutine_create(co_entry);
     qemu_coroutine_enter(co, &rwco);
 
     aio_context = blk_get_aio_context(blk);
@@ -764,6 +784,12 @@ int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
     return rwco.ret;
 }
 
+int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
+             int nb_sectors)
+{
+    return blk_rw(blk, sector_num, buf, nb_sectors, blk_read_entry);
+}
+
 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
                          int nb_sectors)
 {
@@ -778,12 +804,7 @@ int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
               int nb_sectors)
 {
-    int ret = blk_check_request(blk, sector_num, nb_sectors);
-    if (ret < 0) {
-        return ret;
-    }
-
-    return bdrv_write(blk_bs(blk), sector_num, buf, nb_sectors);
+    return blk_rw(blk, sector_num, (uint8_t*) buf, nb_sectors, blk_write_entry);
 }
 
 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
block/io.c +1 -4
index c7084e4..aa8537c 100644
@@ -44,9 +44,6 @@ static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
                                          int64_t sector_num, int nb_sectors,
                                          QEMUIOVector *iov);
-static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
-    int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
-    BdrvRequestFlags flags);
 static BlockAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
                                          int64_t sector_num,
                                          QEMUIOVector *qiov,
@@ -1281,7 +1278,7 @@ fail:
 /*
  * Handle a write request in coroutine context
  */
-static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
+int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
     int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
     BdrvRequestFlags flags)
 {
include/block/block_int.h +3 -0
index db830f6..35ba42f 100644
@@ -511,6 +511,9 @@ void bdrv_setup_io_funcs(BlockDriver *bdrv);
 int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
     int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
     BdrvRequestFlags flags);
+int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
+    int64_t offset, unsigned int bytes, QEMUIOVector *qiov,
+    BdrvRequestFlags flags);
 
 int get_tmp_filename(char *filename, int size);
 BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
Signature · 1 line
1.8.3.1
author
#4 lore raw

PATCH 4/8 block: Pull up blk_read_unthrottled() implementation

In reply to Kevin Wolf

Use blk_read(), so that it goes through blk_co_preadv() like all read requests from the BB to the BDS.

---
 block/block-backend.c | 12 ++++++++++--
 block/io.c            | 14 --------------
 include/block/block.h |  2 --
 3 files changed, 10 insertions(+), 18 deletions(-)
block/block-backend.c +10 -2
index e159647..4fd0545 100644
@@ -793,12 +793,20 @@ int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
                          int nb_sectors)
 {
-    int ret = blk_check_request(blk, sector_num, nb_sectors);
+    BlockDriverState *bs = blk_bs(blk);
+    bool enabled;
+    int ret;
+
+    ret = blk_check_request(blk, sector_num, nb_sectors);
     if (ret < 0) {
         return ret;
     }
 
-    return bdrv_read_unthrottled(blk_bs(blk), sector_num, buf, nb_sectors);
+    enabled = bs->io_limits_enabled;
+    bs->io_limits_enabled = false;
+    ret = blk_read(blk, sector_num, buf, nb_sectors);
+    bs->io_limits_enabled = enabled;
+    return ret;
 }
 
 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
block/io.c +0 -14
index aa8537c..41d954ca 100644
@@ -615,20 +615,6 @@ int bdrv_read(BlockDriverState *bs, int64_t sector_num,
     return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0);
 }
 
-/* Just like bdrv_read(), but with I/O throttling temporarily disabled */
-int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
-                          uint8_t *buf, int nb_sectors)
-{
-    bool enabled;
-    int ret;
-
-    enabled = bs->io_limits_enabled;
-    bs->io_limits_enabled = false;
-    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
-    bs->io_limits_enabled = enabled;
-    return ret;
-}
-
 /* Return < 0 if error. Important errors are:
   -EIO         generic I/O error (may happen for all errors)
   -ENOMEDIUM   No media inserted.
include/block/block.h +0 -2
index d2e1a51..3a78414 100644
@@ -227,8 +227,6 @@ void bdrv_reopen_commit(BDRVReopenState *reopen_state);
 void bdrv_reopen_abort(BDRVReopenState *reopen_state);
 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
               uint8_t *buf, int nb_sectors);
-int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
-                          uint8_t *buf, int nb_sectors);
 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
                const uint8_t *buf, int nb_sectors);
 int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.8.3.1
author
#5 lore raw

PATCH 5/8 block: Use blk_co_pwritev() in blk_write_zeroes()

In reply to Kevin Wolf
---
 block/block-backend.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
block/block-backend.c +8 -9
index 4fd0545..5b9d387 100644
@@ -748,7 +748,8 @@ static void blk_write_entry(void *opaque)
 }
 
 static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
-                  int nb_sectors, CoroutineEntry co_entry)
+                  int nb_sectors, CoroutineEntry co_entry,
+                  BdrvRequestFlags flags)
 {
     AioContext *aio_context;
     QEMUIOVector qiov;
@@ -770,6 +771,7 @@ static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
         .blk    = blk,
         .offset = sector_num << BDRV_SECTOR_BITS,
         .qiov   = &qiov,
+        .flags  = flags,
         .ret    = NOT_DONE,
     };
 
@@ -787,7 +789,7 @@ static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
              int nb_sectors)
 {
-    return blk_rw(blk, sector_num, buf, nb_sectors, blk_read_entry);
+    return blk_rw(blk, sector_num, buf, nb_sectors, blk_read_entry, 0);
 }
 
 int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
@@ -812,18 +814,15 @@ int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
 int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
               int nb_sectors)
 {
-    return blk_rw(blk, sector_num, (uint8_t*) buf, nb_sectors, blk_write_entry);
+    return blk_rw(blk, sector_num, (uint8_t*) buf, nb_sectors,
+                  blk_write_entry, 0);
 }
 
 int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
                      int nb_sectors, BdrvRequestFlags flags)
 {
-    int ret = blk_check_request(blk, sector_num, nb_sectors);
-    if (ret < 0) {
-        return ret;
-    }
-
-    return bdrv_write_zeroes(blk_bs(blk), sector_num, nb_sectors, flags);
+    return blk_rw(blk, sector_num, NULL, nb_sectors, blk_write_entry,
+                  BDRV_REQ_ZERO_WRITE);
 }
 
 static void error_callback_bh(void *opaque)
Signature · 1 line
1.8.3.1
author
#6 lore raw

PATCH 6/8 block: Use blk_prw() in blk_pread()/blk_pwrite()

In reply to Kevin Wolf
---
 block/block-backend.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
block/block-backend.c +21 -15
index 5b9d387..579cc09 100644
@@ -747,9 +747,9 @@ static void blk_write_entry(void *opaque)
                                rwco->qiov, rwco->flags);
 }
 
-static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
-                  int nb_sectors, CoroutineEntry co_entry,
-                  BdrvRequestFlags flags)
+static int blk_prw(BlockBackend *blk, int64_t offset, uint8_t *buf,
+                   int64_t bytes, CoroutineEntry co_entry,
+                   BdrvRequestFlags flags)
 {
     AioContext *aio_context;
     QEMUIOVector qiov;
@@ -757,19 +757,15 @@ static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
     Coroutine *co;
     BlkRwCo rwco;
 
-    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
-        return -EINVAL;
-    }
-
     iov = (struct iovec) {
         .iov_base = buf,
-        .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
+        .iov_len = bytes,
     };
     qemu_iovec_init_external(&qiov, &iov, 1);
 
     rwco = (BlkRwCo) {
         .blk    = blk,
-        .offset = sector_num << BDRV_SECTOR_BITS,
+        .offset = offset,
         .qiov   = &qiov,
         .flags  = flags,
         .ret    = NOT_DONE,
@@ -786,6 +782,18 @@ static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
     return rwco.ret;
 }
 
+static int blk_rw(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
+                  int nb_sectors, CoroutineEntry co_entry,
+                  BdrvRequestFlags flags)
+{
+    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
+        return -EINVAL;
+    }
+
+    return blk_prw(blk, sector_num << BDRV_SECTOR_BITS, buf,
+                   nb_sectors << BDRV_SECTOR_BITS, co_entry, flags);
+}
+
 int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
              int nb_sectors)
 {
@@ -866,22 +874,20 @@ BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
 
 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
 {
-    int ret = blk_check_byte_request(blk, offset, count);
+    int ret = blk_prw(blk, offset, buf, count, blk_read_entry, 0);
     if (ret < 0) {
         return ret;
     }
-
-    return bdrv_pread(blk_bs(blk), offset, buf, count);
+    return count;
 }
 
 int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
 {
-    int ret = blk_check_byte_request(blk, offset, count);
+    int ret = blk_prw(blk, offset, (void*) buf, count, blk_write_entry, 0);
     if (ret < 0) {
         return ret;
     }
-
-    return bdrv_pwrite(blk_bs(blk), offset, buf, count);
+    return count;
 }
 
 int64_t blk_getlength(BlockBackend *blk)
Signature · 1 line
1.8.3.1
author
#7 lore raw

PATCH 7/8 block: Use blk_aio_prwv() for aio_read/write/write_zeroes

In reply to Kevin Wolf
---
 block/block-backend.c | 104 +++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 91 insertions(+), 13 deletions(-)
block/block-backend.c +91 -13
index 579cc09..2947e8c 100644
@@ -859,17 +859,95 @@ BlockAIOCB *blk_abort_aio_request(BlockBackend *blk,
     return &acb->common;
 }
 
+typedef struct BlkAioEmAIOCB {
+    BlockAIOCB common;
+    BlkRwCo rwco;
+    bool has_returned;
+    QEMUBH* bh;
+} BlkAioEmAIOCB;
+
+static const AIOCBInfo blk_aio_em_aiocb_info = {
+    .aiocb_size         = sizeof(BlkAioEmAIOCB),
+};
+
+static void blk_aio_complete(BlkAioEmAIOCB *acb)
+{
+    if (acb->bh) {
+        assert(acb->has_returned);
+        qemu_bh_delete(acb->bh);
+    }
+    if (acb->has_returned) {
+        acb->common.cb(acb->common.opaque, acb->rwco.ret);
+        qemu_aio_unref(acb);
+    }
+}
+
+static void blk_aio_complete_bh(void *opaque)
+{
+    blk_aio_complete(opaque);
+}
+
+static BlockAIOCB *blk_aio_prwv(BlockBackend *blk, int64_t offset,
+                                QEMUIOVector *qiov, CoroutineEntry co_entry,
+                                BdrvRequestFlags flags,
+                                BlockCompletionFunc *cb, void *opaque)
+{
+    BlkAioEmAIOCB *acb;
+    Coroutine *co;
+
+    acb = blk_aio_get(&blk_aio_em_aiocb_info, blk, cb, opaque);
+    acb->rwco = (BlkRwCo) {
+        .blk    = blk,
+        .offset = offset,
+        .qiov   = qiov,
+        .flags  = flags,
+        .ret    = NOT_DONE,
+    };
+    acb->bh = NULL;
+    acb->has_returned = false;
+
+    co = qemu_coroutine_create(co_entry);
+    qemu_coroutine_enter(co, acb);
+
+    acb->has_returned = true;
+    if (acb->rwco.ret != NOT_DONE) {
+        acb->bh = aio_bh_new(blk_get_aio_context(blk), blk_aio_complete_bh, acb);
+        qemu_bh_schedule(acb->bh);
+    }
+
+    return &acb->common;
+}
+
+static void blk_aio_read_entry(void *opaque)
+{
+    BlkAioEmAIOCB *acb = opaque;
+    BlkRwCo *rwco = &acb->rwco;
+
+    rwco->ret = blk_co_preadv(rwco->blk, rwco->offset, rwco->qiov->size,
+                              rwco->qiov, rwco->flags);
+    blk_aio_complete(acb);
+}
+
+static void blk_aio_write_entry(void *opaque)
+{
+    BlkAioEmAIOCB *acb = opaque;
+    BlkRwCo *rwco = &acb->rwco;
+
+    rwco->ret = blk_co_pwritev(rwco->blk, rwco->offset, rwco->qiov->size,
+                               rwco->qiov, rwco->flags);
+    blk_aio_complete(acb);
+}
+
 BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
                                  int nb_sectors, BdrvRequestFlags flags,
                                  BlockCompletionFunc *cb, void *opaque)
 {
-    int ret = blk_check_request(blk, sector_num, nb_sectors);
-    if (ret < 0) {
-        return blk_abort_aio_request(blk, cb, opaque, ret);
+    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
+        return blk_abort_aio_request(blk, cb, opaque, -EINVAL);
     }
 
-    return bdrv_aio_write_zeroes(blk_bs(blk), sector_num, nb_sectors, flags,
-                                 cb, opaque);
+    return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, NULL,
+                        blk_aio_write_entry, BDRV_REQ_ZERO_WRITE, cb, opaque);
 }
 
 int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
@@ -921,24 +999,24 @@ BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
                           QEMUIOVector *iov, int nb_sectors,
                           BlockCompletionFunc *cb, void *opaque)
 {
-    int ret = blk_check_request(blk, sector_num, nb_sectors);
-    if (ret < 0) {
-        return blk_abort_aio_request(blk, cb, opaque, ret);
+    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
+        return blk_abort_aio_request(blk, cb, opaque, -EINVAL);
     }
 
-    return bdrv_aio_readv(blk_bs(blk), sector_num, iov, nb_sectors, cb, opaque);
+    return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, iov,
+                        blk_aio_read_entry, 0, cb, opaque);
 }
 
 BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
                            QEMUIOVector *iov, int nb_sectors,
                            BlockCompletionFunc *cb, void *opaque)
 {
-    int ret = blk_check_request(blk, sector_num, nb_sectors);
-    if (ret < 0) {
-        return blk_abort_aio_request(blk, cb, opaque, ret);
+    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
+        return blk_abort_aio_request(blk, cb, opaque, -EINVAL);
     }
 
-    return bdrv_aio_writev(blk_bs(blk), sector_num, iov, nb_sectors, cb, opaque);
+    return blk_aio_prwv(blk, sector_num << BDRV_SECTOR_BITS, iov,
+                        blk_aio_write_entry, 0, cb, opaque);
 }
 
 BlockAIOCB *blk_aio_flush(BlockBackend *blk,
Signature · 1 line
1.8.3.1
author
#8 lore raw

PATCH 8/8 block: Use blk_co_pwritev() in blk_co_write_zeroes()

In reply to Kevin Wolf
---
 block/block-backend.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
block/block-backend.c +5 -4
index 2947e8c..1c2ea72 100644
@@ -1465,12 +1465,13 @@ void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
 int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
                                      int nb_sectors, BdrvRequestFlags flags)
 {
-    int ret = blk_check_request(blk, sector_num, nb_sectors);
-    if (ret < 0) {
-        return ret;
+    if (nb_sectors < 0 || nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
+        return -EINVAL;
     }
 
-    return bdrv_co_write_zeroes(blk_bs(blk), sector_num, nb_sectors, flags);
+    return blk_co_pwritev(blk, sector_num << BDRV_SECTOR_BITS,
+                          nb_sectors << BDRV_SECTOR_BITS, NULL,
+                          BDRV_REQ_ZERO_WRITE);
 }
 
 int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
Signature · 1 line
1.8.3.1
author Applied
#9 lore raw
In reply to Kevin Wolf

Am 08.03.2016 um 13:47 hat Kevin Wolf geschrieben:

Quoted 9 lines

BlockBackends support a few different interfaces for reads and writes. Until now they used to forward the requests directly to the BDS layer, which implemented wrappers around one central common read/write function that contained the actual implementation of features provided by the block layer.

This only works as long as none of the features are actually on the BlockBackend level. As it happens, we have features (writethrough cache, I/O throttling) that are currently implemented in the BDS, but must move to the BB.

Quoted 7 lines

As a preparation, this series introduces the mapping of the existing APIs to a single coroutine based preadv/pwritev function (as we already have on the BDS level) to the BB layer. The BDS version of the emulation can't go away just yet because there are internal users of them, but the goal is to remove them in the long term.

Depends on Max's "blockdev: Further BlockBackend work".

Applied to the block branch.

Kevin

Reviewed-by on the v1 series
#10 lore raw
In reply to Kevin Wolf
Quoted 27 lines · Kevin Wolf

BlockBackends support a few different interfaces for reads and writes. Until now they used to forward the requests directly to the BDS layer, which implemented wrappers around one central common read/write function that contained the actual implementation of features provided by the block layer.

This only works as long as none of the features are actually on the BlockBackend level. As it happens, we have features (writethrough cache, I/O throttling) that are currently implemented in the BDS, but must move to the BB.

As a preparation, this series introduces the mapping of the existing APIs to a single coroutine based preadv/pwritev function (as we already have on the BDS level) to the BB layer. The BDS version of the emulation can't go away just yet because there are internal users of them, but the goal is to remove them in the long term.

Depends on Max's "blockdev: Further BlockBackend work".

Kevin Wolf (8):
block: Use BdrvChild in BlockBackend
block: Use blk_co_preadv() for blk_read()
block: Use blk_co_pwritev() for blk_write()
block: Pull up blk_read_unthrottled() implementation
block: Use blk_co_pwritev() in blk_write_zeroes()
block: Use blk_prw() in blk_pread()/blk_pwrite()
block: Use blk_aio_prwv() for aio_read/write/write_zeroes
block: Use blk_co_pwritev() in blk_co_write_zeroes()

Quoted 6 lines
 block.c                   |  47 +++--
 block/block-backend.c     | 485 +++++++++++++++++++++++++++++++++-------------
 block/io.c                |  24 +--
 include/block/block.h     |   2 -
 include/block/block_int.h |  12 ++
 5 files changed, 401 insertions(+), 169 deletions(-)

This series conflicts with Paolo's "AioContext fine-grained locking, part 1 of 3, including bdrv_drain rewrite".

We should merge both of them soon to prevent further conflicts.