Skip to contentSkip to conversation

add real vectored block I/O support

PATCH In review Christoph Hellwig 5 participants 20 messages Mar 14, 2009 – Mar 28, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

cover add real vectored block I/O support

This patch adds support to pass down vectored I/O requests from virtio and ide all the way down to the posix raw image format. Other image formats aren't updated to support vectored I/O yet.

The last patch in the series is rather experminental and requires unmerged Linux kernel patches to actually implement the preadv/pwritev syscalls.

Note that this series currently disables scsi generic support as I haven't had time to deal with the recent changes in that area yet. A working scsi generic support will be back in the next version of the patchset.

#1 lore raw

PATCH 1/6 more BlockDriver C99 initializers

Looks like the two bdrv_raw instances were missed last time.

block-raw-posix.c +14 -17
Index: qemu/block-raw-posix.c
===================================================================
@@ -847,27 +847,24 @@ static void raw_flush(BlockDriverState *
 }
 
 BlockDriver bdrv_raw = {
-    "raw",
-    sizeof(BDRVRawState),
-    NULL, /* no probe for protocols */
-    raw_open,
-    NULL,
-    NULL,
-    raw_close,
-    raw_create,
-    raw_flush,
+    .format_name	= "raw",
+    .instance_size	= sizeof(BDRVRawState),
+    .bdrv_open		= raw_open,
+    .bdrv_close		= raw_close,
+    .bdrv_create	= raw_create,
+    .bdrv_flush		= raw_flush,
 
 #ifdef CONFIG_AIO
-    .bdrv_aio_read = raw_aio_read,
-    .bdrv_aio_write = raw_aio_write,
-    .bdrv_aio_cancel = raw_aio_cancel,
-    .aiocb_size = sizeof(RawAIOCB),
+    .bdrv_aio_read	= raw_aio_read,
+    .bdrv_aio_write	= raw_aio_write,
+    .bdrv_aio_cancel	= raw_aio_cancel,
+    .aiocb_size		= sizeof(RawAIOCB),
 #endif
 
-    .bdrv_read = raw_read,
-    .bdrv_write = raw_write,
-    .bdrv_truncate = raw_truncate,
-    .bdrv_getlength = raw_getlength,
+    .bdrv_read		= raw_read,
+    .bdrv_write		= raw_write,
+    .bdrv_truncate	= raw_truncate,
+    .bdrv_getlength	= raw_getlength,
 };
 
 /***********************************************/
block-raw-win32.c +14 -17
Index: qemu/block-raw-win32.c
===================================================================
@@ -351,26 +351,23 @@ static int raw_create(const char *filena
 }
 
 BlockDriver bdrv_raw = {
-    "raw",
-    sizeof(BDRVRawState),
-    NULL, /* no probe for protocols */
-    raw_open,
-    NULL,
-    NULL,
-    raw_close,
-    raw_create,
-    raw_flush,
+    .format_name	= "raw",
+    .instance_size	= sizeof(BDRVRawState),
+    .bdrv_open		= raw_open,
+    .bdrv_close		= raw_close,
+    .bdrv_create	= raw_create,
+    .bdrv_flush		= raw_flush,
 
 #ifdef WIN32_AIO
-    .bdrv_aio_read = raw_aio_read,
-    .bdrv_aio_write = raw_aio_write,
-    .bdrv_aio_cancel = raw_aio_cancel,
-    .aiocb_size = sizeof(RawAIOCB);
+    .bdrv_aio_read	= raw_aio_read,
+    .bdrv_aio_write	= raw_aio_write,
+    .bdrv_aio_cancel	= raw_aio_cancel,
+    .aiocb_size		= sizeof(RawAIOCB);
 #endif
-    .bdrv_read = raw_read,
-    .bdrv_write = raw_write,
-    .bdrv_truncate = raw_truncate,
-    .bdrv_getlength = raw_getlength,
+    .bdrv_read		= raw_read,
+    .bdrv_write		= raw_write,
+    .bdrv_truncate	= raw_truncate,
+    .bdrv_getlength	= raw_getlength,
 };
 
 /***********************************************/
#2 lore raw

Re: [Qemu-devel] [PATCH 1/6] more BlockDriver C99 initializers

ping?

This is a simple preparatory patch not directly related to the rest of the series (which is beeing respun right now)

Quoted 89 lines · Christoph Hellwig

Looks like the two bdrv_raw instances were missed last time.

Index: qemu/block-raw-posix.c
===================================================================

block-raw-posix.c +14 -17
@@ -847,27 +847,24 @@ static void raw_flush(BlockDriverState *
 }
 
 BlockDriver bdrv_raw = {
-    "raw",
-    sizeof(BDRVRawState),
-    NULL, /* no probe for protocols */
-    raw_open,
-    NULL,
-    NULL,
-    raw_close,
-    raw_create,
-    raw_flush,
+    .format_name	= "raw",
+    .instance_size	= sizeof(BDRVRawState),
+    .bdrv_open		= raw_open,
+    .bdrv_close		= raw_close,
+    .bdrv_create	= raw_create,
+    .bdrv_flush		= raw_flush,
 
 #ifdef CONFIG_AIO
-    .bdrv_aio_read = raw_aio_read,
-    .bdrv_aio_write = raw_aio_write,
-    .bdrv_aio_cancel = raw_aio_cancel,
-    .aiocb_size = sizeof(RawAIOCB),
+    .bdrv_aio_read	= raw_aio_read,
+    .bdrv_aio_write	= raw_aio_write,
+    .bdrv_aio_cancel	= raw_aio_cancel,
+    .aiocb_size		= sizeof(RawAIOCB),
 #endif
 
-    .bdrv_read = raw_read,
-    .bdrv_write = raw_write,
-    .bdrv_truncate = raw_truncate,
-    .bdrv_getlength = raw_getlength,
+    .bdrv_read		= raw_read,
+    .bdrv_write		= raw_write,
+    .bdrv_truncate	= raw_truncate,
+    .bdrv_getlength	= raw_getlength,
 };
 
 /***********************************************/

Index: qemu/block-raw-win32.c
===================================================================

block-raw-win32.c +10 -14
@@ -351,26 +351,23 @@ static int raw_create(const char *filena
 }
 
 BlockDriver bdrv_raw = {
-    "raw",
-    sizeof(BDRVRawState),
-    NULL, /* no probe for protocols */
-    raw_open,
-    NULL,
-    NULL,
-    raw_close,
-    raw_create,
-    raw_flush,
+    .format_name	= "raw",
+    .instance_size	= sizeof(BDRVRawState),
+    .bdrv_open		= raw_open,
+    .bdrv_close		= raw_close,
+    .bdrv_create	= raw_create,
+    .bdrv_flush		= raw_flush,
 
 #ifdef WIN32_AIO
-    .bdrv_aio_read = raw_aio_read,
-    .bdrv_aio_write = raw_aio_write,
-    .bdrv_aio_cancel = raw_aio_cancel,
-    .aiocb_size = sizeof(RawAIOCB);
+    .bdrv_aio_read	= raw_aio_read,
+    .bdrv_aio_write	= raw_aio_write,
+    .bdrv_aio_cancel	= raw_aio_cancel,
+    .aiocb_size		= sizeof(RawAIOCB);
 #endif
-    .bdrv_read = raw_read,

block-raw-win32.c static int raw_create(const char *filena · line 373

Quoted 10 lines
block-raw-win32.c +4 -3
-    .bdrv_write = raw_write,
-    .bdrv_truncate = raw_truncate,
-    .bdrv_getlength = raw_getlength,
+    .bdrv_read		= raw_read,
+    .bdrv_write		= raw_write,
+    .bdrv_truncate	= raw_truncate,
+    .bdrv_getlength	= raw_getlength,
 };
 
 /***********************************************/

---end quoted text---

#3 lore raw

PATCH 2/6 change vectored block I/O API to plain iovecs

In reply to Christoph Hellwig

QEMUIOVector is a useful helper for the dma-helper.c internals but for a generic block API it's more of hindrance. Some top-level consumers like virtio-blk already have the plain iovec and segment number at hand and can pass it down directly, and for those that just have a single element and need to fake up a vector the plain iovec also is a lot easier.

Last but not leas we want to push down vectored I/O to the lowest level, and if posix-aio-compat.c wants to stay somewhat true to it's goal of beeing like an ehanced posix AIO API it should stick to posix types.

block.c +14 -9
Index: qemu/block.c
===================================================================
@@ -1253,7 +1253,9 @@ char *bdrv_snapshot_dump(char *buf, int 
 /* async I/Os */
 
 typedef struct VectorTranslationState {
-    QEMUIOVector *iov;
+    struct iovec *iov;
+    int nr_iov;
+    int size;
     uint8_t *bounce;
     int is_write;
     BlockDriverAIOCB *aiocb;
@@ -1265,7 +1267,7 @@ static void bdrv_aio_rw_vector_cb(void *
     VectorTranslationState *s = opaque;
 
     if (!s->is_write) {
-        qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
+        iovec_from_buffer(s->iov, s->nr_iov, s->bounce, s->size);
     }
     qemu_vfree(s->bounce);
     s->this_aiocb->cb(s->this_aiocb->opaque, ret);
@@ -1274,7 +1276,8 @@ static void bdrv_aio_rw_vector_cb(void *
 
 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
                                             int64_t sector_num,
-                                            QEMUIOVector *iov,
+                                            struct iovec *iov,
+					    int nr_iov,
                                             int nb_sectors,
                                             BlockDriverCompletionFunc *cb,
                                             void *opaque,
@@ -1286,10 +1289,12 @@ static BlockDriverAIOCB *bdrv_aio_rw_vec
 
     s->this_aiocb = aiocb;
     s->iov = iov;
-    s->bounce = qemu_memalign(512, nb_sectors * 512);
+    s->nr_iov = nr_iov;
+    s->size = nb_sectors * 512;
+    s->bounce = qemu_memalign(512, s->size);
     s->is_write = is_write;
     if (is_write) {
-        qemu_iovec_to_buffer(s->iov, s->bounce);
+        iovec_to_buffer(s->iov, s->nr_iov, s->bounce);
         s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
                                   bdrv_aio_rw_vector_cb, s);
     } else {
@@ -1300,24 +1305,24 @@ static BlockDriverAIOCB *bdrv_aio_rw_vec
 }
 
 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
-                                 QEMUIOVector *iov, int nb_sectors,
+                                 struct iovec *iov, int nr_iov, int nb_sectors,
                                  BlockDriverCompletionFunc *cb, void *opaque)
 {
     if (bdrv_check_request(bs, sector_num, nb_sectors))
         return NULL;
 
-    return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
+    return bdrv_aio_rw_vector(bs, sector_num, iov, nr_iov, nb_sectors,
                               cb, opaque, 0);
 }
 
 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
-                                  QEMUIOVector *iov, int nb_sectors,
+                                  struct iovec *iov, int nr_iov, int nb_sectors,
                                   BlockDriverCompletionFunc *cb, void *opaque)
 {
     if (bdrv_check_request(bs, sector_num, nb_sectors))
         return NULL;
 
-    return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
+    return bdrv_aio_rw_vector(bs, sector_num, iov, nr_iov, nb_sectors,
                               cb, opaque, 1);
 }
 
block.h +2 -2
Index: qemu/block.h
===================================================================
@@ -87,10 +87,10 @@ typedef struct BlockDriverAIOCB BlockDri
 typedef void BlockDriverCompletionFunc(void *opaque, int ret);
 
 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
-                                 QEMUIOVector *iov, int nb_sectors,
+                                 struct iovec *iov, int nr_iov, int nb_sectors,
                                  BlockDriverCompletionFunc *cb, void *opaque);
 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
-                                  QEMUIOVector *iov, int nb_sectors,
+                                  struct iovec *iov, int nr_iov, int nb_sectors,
                                   BlockDriverCompletionFunc *cb, void *opaque);
 
 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
cutils.c +10 -9
Index: qemu/cutils.c
===================================================================
@@ -135,28 +135,29 @@ void qemu_iovec_reset(QEMUIOVector *qiov
     qiov->size = 0;
 }
 
-void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf)
+void iovec_to_buffer(struct iovec *iov, int nr_iov, void *buf)
 {
     uint8_t *p = (uint8_t *)buf;
     int i;
 
-    for (i = 0; i < qiov->niov; ++i) {
-        memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
-        p += qiov->iov[i].iov_len;
+    for (i = 0; i < nr_iov; ++i) {
+        memcpy(p, iov[i].iov_base, iov[i].iov_len);
+        p += iov[i].iov_len;
     }
 }
 
-void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count)
+void iovec_from_buffer(struct iovec *iov, int nr_iov,
+        const void *buf, size_t count)
 {
     const uint8_t *p = (const uint8_t *)buf;
     size_t copy;
     int i;
 
-    for (i = 0; i < qiov->niov && count; ++i) {
+    for (i = 0; i < nr_iov && count; ++i) {
         copy = count;
-        if (copy > qiov->iov[i].iov_len)
-            copy = qiov->iov[i].iov_len;
-        memcpy(qiov->iov[i].iov_base, p, copy);
+        if (copy > iov[i].iov_len)
+            copy = iov[i].iov_len;
+        memcpy(iov[i].iov_base, p, copy);
         p     += copy;
         count -= copy;
     }
dma-helpers.c +2 -2
Index: qemu/dma-helpers.c
===================================================================
@@ -110,10 +110,10 @@ static void dma_bdrv_cb(void *opaque, in
     }
 
     if (dbs->is_write) {
-        bdrv_aio_writev(dbs->bs, dbs->sector_num, &dbs->iov,
+        bdrv_aio_writev(dbs->bs, dbs->sector_num, dbs->iov.iov, dbs->iov.niov,
                         dbs->iov.size / 512, dma_bdrv_cb, dbs);
     } else {
-        bdrv_aio_readv(dbs->bs, dbs->sector_num, &dbs->iov,
+        bdrv_aio_readv(dbs->bs, dbs->sector_num, dbs->iov.iov, dbs->iov.niov,
                        dbs->iov.size / 512, dma_bdrv_cb, dbs);
     }
 }
qemu-common.h +4 -2
Index: qemu/qemu-common.h
===================================================================
@@ -197,8 +197,10 @@ void qemu_iovec_init(QEMUIOVector *qiov,
 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
 void qemu_iovec_destroy(QEMUIOVector *qiov);
 void qemu_iovec_reset(QEMUIOVector *qiov);
-void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
-void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
+
+void iovec_to_buffer(struct iovec *iov, int nr_iov, void *buf);
+void iovec_from_buffer(struct iovec *iov, int nr_iov,
+        const void *buf, size_t count);
 
 struct Monitor;
 typedef struct Monitor Monitor;
#4 lore raw

Re: [Qemu-devel] [PATCH 2/6] change vectored block I/O API to plain iovecs

Quoted 6 lines · Christoph Hellwig

QEMUIOVector is a useful helper for the dma-helper.c internals but for a generic block API it's more of hindrance. Some top-level consumers like virtio-blk already have the plain iovec and segment number at hand and can pass it down directly, and for those that just have a single element and need to fake up a vector the plain iovec also is a lot easier.

virtio gets its iovecs through a hacky (and incorrect, try >=4G) method. IMO virtio should be fixed to use the dma api, at which point it will start to use QEMUIOVector anyway,

Quoted 4 lines

Last but not leas we want to push down vectored I/O to the lowest level, and if posix-aio-compat.c wants to stay somewhat true to it's goal of beeing like an ehanced posix AIO API it should stick to posix types.

Internally yes, but why should bdrv_* not use QEMUIOVector? That API isn't very interested in posix.

block.c static void bdrv_aio_rw_vector_cb(void * · line 1283

Quoted 9 lines

static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,

                                             int64_t sector_num,
block.c +2 -1
-                                            QEMUIOVector *iov,
+                                            struct iovec *iov,
+					    int nr_iov,
                                             int nb_sectors,
                                             BlockDriverCompletionFunc *cb,
                                             void *opaque,

Note qemu never uses tabs in source code.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw

Re: [Qemu-devel] [PATCH 2/6] change vectored block I/O API to plain iovecs

Quoted 10 lines · Avi Kivity
Quoted 6 lines · Christoph Hellwig

QEMUIOVector is a useful helper for the dma-helper.c internals but for a generic block API it's more of hindrance. Some top-level consumers like virtio-blk already have the plain iovec and segment number at hand and can pass it down directly, and for those that just have a single element and need to fake up a vector the plain iovec also is a lot easier.

virtio gets its iovecs through a hacky (and incorrect, try >=4G) method. IMO virtio should be fixed to use the dma api, at which point it will start to use QEMUIOVector anyway,

Quoted 8 lines

= 4GB should work fine in virtio. It basically replicates the DMA API today. It doesn't handle MMIO memory though.

Quoted 4 lines

Last but not leas we want to push down vectored I/O to the lowest level, and if posix-aio-compat.c wants to stay somewhat true to it's goal of beeing like an ehanced posix AIO API it should stick to posix types.

Internally yes, but why should bdrv_* not use QEMUIOVector? That API isn't very interested in posix.

Agreed.

Regards,

Anthony Liguori

#6 lore raw

Re: [Qemu-devel] [PATCH 2/6] change vectored block I/O API to plain iovecs

Quoted 5 lines · Anthony Liguori
Quoted 3 lines

virtio gets its iovecs through a hacky (and incorrect, try >=4G) method. IMO virtio should be fixed to use the dma api, at which point it will start to use QEMUIOVector anyway,

= 4GB should work fine in virtio. It basically replicates the DMA

API today. It doesn't handle MMIO memory though.

Right. I saw phys_ram_base somewhere and jumped to conclusions. Sorry about the noise.

Still, it should use the dma api to avoid code duplication.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw

Re: [Qemu-devel] [PATCH 2/6] change vectored block I/O API to plain iovecs

In reply to Avi Kivity
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Christoph Hellwig

QEMUIOVector is a useful helper for the dma-helper.c internals but for a generic
block API it's more of hindrance. Some top-level consumers like virtio-blk already have the plain iovec and segment number at hand and can pass it down
directly, and for those that just have a single element and need to fake up a vector the plain iovec also is a lot easier.

Quoted 3 lines

virtio gets its iovecs through a hacky (and incorrect, try >=4G) method. IMO virtio should be fixed to use the dma api, at which point it will start to use QEMUIOVector anyway,

I would not call it hacky an incorrect. And the current dma API certainly won't work due to the layering between the generic virtio layer and virtio-block.

Quoted 2 lines

Internally yes, but why should bdrv_* not use QEMUIOVector? That API isn't very interested in posix.

Because it makes life a lot easier. We already pass the length in sector units anyway. While QEMUIOVector could replace instead of currently duplicate it that would mean another translation between byte and sector units at the block level. And then comes the issue of feeding in iovecs - there is the case of iovecs coming from other layers like virtio-blk and the more important one of just creating one-entry static iovecs in many places. These would mean another dynamic allocation and lots of API churn.

Keep the QEMUIOVector as a nice abstraction for the memory-managment issues in dma-helper.c but I think as an API for passing data (which doesn't care about how the iovec array is allocated) they aren't very helpful.

Quoted 12 lines

block.c static void bdrv_aio_rw_vector_cb(void * · line 1283

Quoted 10 lines

static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,

                                            int64_t sector_num,
block.c +2 -1
-                                            QEMUIOVector *iov,
+                                            struct iovec *iov,
+					    int nr_iov,
                                            int nb_sectors,
                                            BlockDriverCompletionFunc *cb,
                                            void *opaque,

Note qemu never uses tabs in source code.

Sorry, tried to get rid of most of them, but it's a real pain in the ass writing code that way ;-) Guess I need to write me a little obsfucate.awk script before submitting patches..

#8 lore raw

Re: [Qemu-devel] [PATCH 2/6] change vectored block I/O API to plain iovecs

Quoted 7 lines · Christoph Hellwig
Quoted 2 lines

virtio gets its iovecs through a hacky (and incorrect, try >=4G) method. IMO virtio should be fixed to use the dma api, at which point it will start to use QEMUIOVector anyway,

I would not call it hacky an incorrect. And the current dma API certainly won't work due to the layering between the generic virtio layer and virtio-block.

It is not incorrect, as Anthony pointed out. It's hacky in that it touches deep qemu internals.

The dma api has two layers; while the upper layer (in dma-helpers.c) is too high level for virtio, the lower level ought to work.

Quoted 9 lines
Quoted 2 lines

Internally yes, but why should bdrv_* not use QEMUIOVector? That API isn't very interested in posix.

Because it makes life a lot easier. We already pass the length in sector units anyway. While QEMUIOVector could replace instead of currently duplicate it that would mean another translation between byte and sector units at the block level. And then comes the issue of feeding in iovecs - there is the case of iovecs coming from other layers like virtio-blk

virtio-blk could simply gather iovecs through QEMUIOVector

Quoted 4 lines

and the more important one of just creating
one-entry static iovecs in many places. These would mean another dynamic allocation and lots of API churn.

QEMUStaticIOVector qiov;

   qemu_static_iovector_init(&qiov, data, len);
   some_random_function(&qiov.iov, ...);

Of course we need not to free non-owned iovecs.

Quoted 5 lines

Keep the QEMUIOVector as a nice abstraction for the memory-managment issues in dma-helper.c but I think as an API for passing data (which doesn't care about how the iovec array is allocated) they aren't very helpful.

They allow dropping two extra parameters, but I agree no huge benefit. I still like them.

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw

Re: [Qemu-devel] [PATCH 2/6] change vectored block I/O API to plain iovecs

Quoted 6 lines · Avi Kivity
   QEMUStaticIOVector qiov;

   qemu_static_iovector_init(&qiov, data, len);
   some_random_function(&qiov.iov, ...);

Of course we need not to free non-owned iovecs.

That means more wasted space in every iocb, more special cases needed in the QEMUIOVector to deal with the static allocation special case, etc.

I still haven't seen an actual benefit of using the QEMUIOVector at that level.

Quoted 2 lines

They allow dropping two extra parameters, but I agree no huge benefit. I still like them.

I think they are good for the use case of actually dynamically building iovecs in the dma helper case. They just aren't the right interface for the block level API. Given that no one underneath that API is going to add more elements to the iovec there's no good reason to carry the memory allocation details around.

#10 lore raw

PATCH 3/6 virtio-blk: use generic vectored I/O APIs

In reply to Christoph Hellwig

Use the generic bdrv_aio_readv/bdrv_aio_writev APIs instead of linearizing buffers directly. This enables using the future native preadv/pwritev support.

hw/virtio-blk.c +15 -65
Index: qemu/hw/virtio-blk.c
===================================================================
@@ -36,7 +36,6 @@ typedef struct VirtIOBlockReq
     struct virtio_blk_inhdr *in;
     struct virtio_blk_outhdr *out;
     size_t size;
-    uint8_t *buffer;
     struct VirtIOBlockReq *next;
 } VirtIOBlockReq;
 
@@ -48,7 +47,6 @@ static void virtio_blk_req_complete(Virt
     virtqueue_push(s->vq, &req->elem, req->size + sizeof(*req->in));
     virtio_notify(&s->vdev, s->vq);
 
-    qemu_free(req->buffer);
     qemu_free(req);
 }
 
@@ -76,24 +74,7 @@ static void virtio_blk_rw_complete(void 
 {
     VirtIOBlockReq *req = opaque;
 
-    /* Copy read data to the guest */
-    if (!ret && !(req->out->type & VIRTIO_BLK_T_OUT)) {
-        size_t offset = 0;
-        int i;
-
-        for (i = 0; i < req->elem.in_num - 1; i++) {
-            size_t len;
-
-            /* Be pretty defensive wrt malicious guests */
-            len = MIN(req->elem.in_sg[i].iov_len,
-                      req->size - offset);
-
-            memcpy(req->elem.in_sg[i].iov_base,
-                   req->buffer + offset,
-                   len);
-            offset += len;
-        }
-    } else if (ret && (req->out->type & VIRTIO_BLK_T_OUT)) {
+    if (ret && (req->out->type & VIRTIO_BLK_T_OUT)) {
         if (virtio_blk_handle_write_error(req, -ret))
             return;
     }
@@ -122,39 +103,18 @@ static VirtIOBlockReq *virtio_blk_get_re
     return req;
 }
 
-static int virtio_blk_handle_write(VirtIOBlockReq *req)
+static void virtio_blk_handle_write(VirtIOBlockReq *req)
 {
-    if (!req->buffer) {
-        size_t offset = 0;
-        int i;
-
-        for (i = 1; i < req->elem.out_num; i++)
-            req->size += req->elem.out_sg[i].iov_len;
-
-        req->buffer = qemu_memalign(512, req->size);
-        if (req->buffer == NULL) {
-            qemu_free(req);
-            return -1;
-        }
-
-        /* We copy the data from the SG list to avoid splitting up the request.
-           This helps performance a lot until we can pass full sg lists as AIO
-           operations */
-        for (i = 1; i < req->elem.out_num; i++) {
-            size_t len;
-
-            len = MIN(req->elem.out_sg[i].iov_len,
-                    req->size - offset);
-            memcpy(req->buffer + offset,
-                    req->elem.out_sg[i].iov_base,
-                    len);
-            offset += len;
-        }
-    }
+    bdrv_aio_writev(req->dev->bs, req->out->sector, &req->elem.out_sg[0],
+                    req->elem.out_num, req->size / 512,
+                    virtio_blk_rw_complete, req);
+}
 
-    bdrv_aio_write(req->dev->bs, req->out->sector, req->buffer, req->size / 512,
-            virtio_blk_rw_complete, req);
-    return 0;
+static void virtio_blk_handle_read(VirtIOBlockReq *req)
+{
+    bdrv_aio_readv(req->dev->bs, req->out->sector, &req->elem.in_sg[0],
+                   req->elem.in_num, req->size / 512,
+                   virtio_blk_rw_complete, req);
 }
 
 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
@@ -187,23 +147,13 @@ static void virtio_blk_handle_output(Vir
             virtio_notify(vdev, vq);
             qemu_free(req);
         } else if (req->out->type & VIRTIO_BLK_T_OUT) {
-            if (virtio_blk_handle_write(req) < 0)
-                break;
+            for (i = 1; i < req->elem.out_num; i++)
+                req->size += req->elem.out_sg[i].iov_len;
+            virtio_blk_handle_write(req);
         } else {
             for (i = 0; i < req->elem.in_num - 1; i++)
                 req->size += req->elem.in_sg[i].iov_len;
-
-            req->buffer = qemu_memalign(512, req->size);
-            if (req->buffer == NULL) {
-                qemu_free(req);
-                break;
-            }
-
-            bdrv_aio_read(s->bs, req->out->sector,
-                          req->buffer,
-                          req->size / 512,
-                          virtio_blk_rw_complete,
-                          req);
+            virtio_blk_handle_read(req);
         }
     }
     /*
#11 lore raw

PATCH 4/6 remove bdrv_aio_read/bdrv_aio_write

In reply to Christoph Hellwig

Always use the vectored APIs to reduce code churn once we switch the BlockDriver API to be vectored.

hw/ide.c +5 -3
Index: qemu/hw/ide.c
===================================================================
@@ -496,6 +496,7 @@ typedef struct BMDMAState {
     IDEState *ide_if;
     BlockDriverCompletionFunc *dma_cb;
     BlockDriverAIOCB *aiocb;
+    struct iovec iov;
     int64_t sector_num;
     uint32_t nsector;
 } BMDMAState;
@@ -1467,9 +1468,10 @@ static void ide_atapi_cmd_read_dma_cb(vo
 #ifdef DEBUG_AIO
     printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
 #endif
-    bm->aiocb = bdrv_aio_read(s->bs, (int64_t)s->lba << 2,
-                              s->io_buffer + data_offset, n * 4,
-                              ide_atapi_cmd_read_dma_cb, bm);
+    bm->iov.iov_base = s->io_buffer + data_offset;
+    bm->iov.iov_len = n * 4 * 512;
+    bm->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2, &bm->iov, 1,
+                               n * 4, ide_atapi_cmd_read_dma_cb, bm);
     if (!bm->aiocb) {
         /* Note: media not present is the most likely case */
         ide_atapi_cmd_error(s, SENSE_NOT_READY,
hw/scsi-disk.c +60 -62

280 lines not on this page · raw message

block-qcow.c +16 -8
Index: qemu/block-qcow.c
===================================================================
@@ -530,6 +530,7 @@ typedef struct QCowAIOCB {
     int n;
     uint64_t cluster_offset;
     uint8_t *cluster_data;
+    struct iovec hd_iov;
     BlockDriverAIOCB *hd_aiocb;
 } QCowAIOCB;
 
@@ -584,8 +585,10 @@ static void qcow_aio_read_cb(void *opaqu
     if (!acb->cluster_offset) {
         if (bs->backing_hd) {
             /* read from the base image */
-            acb->hd_aiocb = bdrv_aio_read(bs->backing_hd,
-                acb->sector_num, acb->buf, acb->n, qcow_aio_read_cb, acb);
+	    acb->hd_iov.iov_base = acb->buf;
+	    acb->hd_iov.iov_len = acb->n * 512;
+            acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
+                &acb->hd_iov, 1, acb->n, qcow_aio_read_cb, acb);
             if (acb->hd_aiocb == NULL)
                 goto fail;
         } else {
@@ -605,9 +608,11 @@ static void qcow_aio_read_cb(void *opaqu
             ret = -EIO;
             goto fail;
         }
-        acb->hd_aiocb = bdrv_aio_read(s->hd,
+	acb->hd_iov.iov_base = acb->buf;
+	acb->hd_iov.iov_len = acb->n * 512;
+        acb->hd_aiocb = bdrv_aio_readv(s->hd,
                             (acb->cluster_offset >> 9) + index_in_cluster,
-                            acb->buf, acb->n, qcow_aio_read_cb, acb);
+                            &acb->hd_iov, 1, acb->n, qcow_aio_read_cb, acb);
         if (acb->hd_aiocb == NULL)
             goto fail;
     }
@@ -687,10 +692,13 @@ static void qcow_aio_write_cb(void *opaq
     } else {
         src_buf = acb->buf;
     }
-    acb->hd_aiocb = bdrv_aio_write(s->hd,
-                                   (cluster_offset >> 9) + index_in_cluster,
-                                   src_buf, acb->n,
-                                   qcow_aio_write_cb, acb);
+
+    acb->hd_iov.iov_base = (void *)src_buf;
+    acb->hd_iov.iov_len = acb->n * 512;
+    acb->hd_aiocb = bdrv_aio_writev(s->hd,
+                                    (cluster_offset >> 9) + index_in_cluster,
+                                    &acb->hd_iov, 1, acb->n,
+                                    qcow_aio_write_cb, acb);
     if (acb->hd_aiocb == NULL)
         goto fail;
 }
block-qcow2.c +17 -8
Index: qemu/block-qcow2.c
===================================================================
@@ -1175,6 +1175,7 @@ typedef struct QCowAIOCB {
     uint64_t cluster_offset;
     uint8_t *cluster_data;
     BlockDriverAIOCB *hd_aiocb;
+    struct iovec hd_iov;
     QEMUBH *bh;
     QCowL2Meta l2meta;
 } QCowAIOCB;
@@ -1252,8 +1253,11 @@ fail:
             n1 = backing_read1(bs->backing_hd, acb->sector_num,
                                acb->buf, acb->n);
             if (n1 > 0) {
-                acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
-                                    acb->buf, acb->n, qcow_aio_read_cb, acb);
+	        acb->hd_iov.iov_base = acb->buf;
+                acb->hd_iov.iov_len = acb->n * 512;
+                acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num,
+                                    &acb->hd_iov, 1, acb->n,
+				    qcow_aio_read_cb, acb);
                 if (acb->hd_aiocb == NULL)
                     goto fail;
             } else {
@@ -1282,9 +1286,12 @@ fail:
             ret = -EIO;
             goto fail;
         }
-        acb->hd_aiocb = bdrv_aio_read(s->hd,
+
+	acb->hd_iov.iov_base = acb->buf;
+        acb->hd_iov.iov_len = acb->n * 512;
+        acb->hd_aiocb = bdrv_aio_readv(s->hd,
                             (acb->cluster_offset >> 9) + index_in_cluster,
-                            acb->buf, acb->n, qcow_aio_read_cb, acb);
+                            &acb->hd_iov, 1, acb->n, qcow_aio_read_cb, acb);
         if (acb->hd_aiocb == NULL)
             goto fail;
     }
@@ -1381,10 +1388,12 @@ static void qcow_aio_write_cb(void *opaq
     } else {
         src_buf = acb->buf;
     }
-    acb->hd_aiocb = bdrv_aio_write(s->hd,
-                                   (acb->cluster_offset >> 9) + index_in_cluster,
-                                   src_buf, acb->n,
-                                   qcow_aio_write_cb, acb);
+    acb->hd_iov.iov_base = (void *)src_buf;
+    acb->hd_iov.iov_len = acb->n * 512;
+    acb->hd_aiocb = bdrv_aio_writev(s->hd,
+                                    (acb->cluster_offset >> 9) + index_in_cluster,
+                                    &acb->hd_iov, 1, acb->n,
+                                    qcow_aio_write_cb, acb);
     if (acb->hd_aiocb == NULL)
         goto fail;
 }
block.c +8 -2
Index: qemu/block.c
===================================================================
@@ -53,6 +53,12 @@ typedef struct BlockDriverAIOCBSync {
     int ret;
 } BlockDriverAIOCBSync;
 
+static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs,
+        int64_t sector_num, uint8_t *buf, int nb_sectors,
+        BlockDriverCompletionFunc *cb, void *opaque);
+static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs,
+        int64_t sector_num, const uint8_t *buf, int nb_sectors,
+        BlockDriverCompletionFunc *cb, void *opaque);
 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
         int64_t sector_num, uint8_t *buf, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
@@ -1318,7 +1324,7 @@ BlockDriverAIOCB *bdrv_aio_writev(BlockD
                               cb, opaque, 1);
 }
 
-BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
+static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
                                 uint8_t *buf, int nb_sectors,
                                 BlockDriverCompletionFunc *cb, void *opaque)
 {
@@ -1341,7 +1347,7 @@ BlockDriverAIOCB *bdrv_aio_read(BlockDri
     return ret;
 }
 
-BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
+static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
                                  const uint8_t *buf, int nb_sectors,
                                  BlockDriverCompletionFunc *cb, void *opaque)
 {
block.h +0 -7
Index: qemu/block.h
===================================================================
@@ -92,13 +92,6 @@ BlockDriverAIOCB *bdrv_aio_readv(BlockDr
 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
                                   struct iovec *iov, int nr_iov, int nb_sectors,
                                   BlockDriverCompletionFunc *cb, void *opaque);
-
-BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
-                                uint8_t *buf, int nb_sectors,
-                                BlockDriverCompletionFunc *cb, void *opaque);
-BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
-                                 const uint8_t *buf, int nb_sectors,
-                                 BlockDriverCompletionFunc *cb, void *opaque);
 void bdrv_aio_cancel(BlockDriverAIOCB *acb);
 
 /* sg packet commands */
#12 lore raw

PATCH 5/6 push down vector linearization to posix-aio-compat.c

In reply to Christoph Hellwig

Make all AIO requests vectored and defer linearization until the actual I/O thread. This prepares for using native preadv/pwritev.

Also enables asynchronous direct I/O by handling that case in the I/O thread.

For now disables support for scsi-generic requests until I figure a way to fit it into the new world order.

Win32 support is untested and uncompiled. Then again I can't even find a know to turn on win32 aio support.

Qcow and qcow2 propably want to be adopted to directly deal with multi-segment requests, but that can be implemented later.

block.c +73 -120
Index: qemu/block.c
===================================================================
@@ -51,19 +51,19 @@ typedef struct BlockDriverAIOCBSync {
     BlockDriverAIOCB common;
     QEMUBH *bh;
     int ret;
+    /* vector translation state */
+    struct iovec *iov;
+    int nr_iov;
+    int size;
+    uint8_t *bounce;
+    int is_write;
 } BlockDriverAIOCBSync;
 
-static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs,
-        int64_t sector_num, uint8_t *buf, int nb_sectors,
-        BlockDriverCompletionFunc *cb, void *opaque);
-static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs,
-        int64_t sector_num, const uint8_t *buf, int nb_sectors,
+static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
+        int64_t sector_num, struct iovec *iov, int nr_iov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
-static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
-        int64_t sector_num, uint8_t *buf, int nb_sectors,
-        BlockDriverCompletionFunc *cb, void *opaque);
-static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
-        int64_t sector_num, const uint8_t *buf, int nb_sectors,
+static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, int64_t sector_num,
+        struct iovec *iov, int nr_iov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
@@ -142,10 +142,10 @@ void path_combine(char *dest, int dest_s
 
 static void bdrv_register(BlockDriver *bdrv)
 {
-    if (!bdrv->bdrv_aio_read) {
+    if (!bdrv->bdrv_aio_readv) {
         /* add AIO emulation layer */
-        bdrv->bdrv_aio_read = bdrv_aio_read_em;
-        bdrv->bdrv_aio_write = bdrv_aio_write_em;
+        bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
+        bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
         bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
         bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
     } else if (!bdrv->bdrv_read) {
@@ -1250,84 +1250,10 @@ char *bdrv_snapshot_dump(char *buf, int 
 /**************************************************************/
 /* async I/Os */
 
-typedef struct VectorTranslationState {
-    struct iovec *iov;
-    int nr_iov;
-    int size;
-    uint8_t *bounce;
-    int is_write;
-    BlockDriverAIOCB *aiocb;
-    BlockDriverAIOCB *this_aiocb;
-} VectorTranslationState;
-
-static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
-{
-    VectorTranslationState *s = opaque;
-
-    if (!s->is_write) {
-        iovec_from_buffer(s->iov, s->nr_iov, s->bounce, s->size);
-    }
-    qemu_vfree(s->bounce);
-    s->this_aiocb->cb(s->this_aiocb->opaque, ret);
-    qemu_aio_release(s->this_aiocb);
-}
-
-static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
-                                            int64_t sector_num,
-                                            struct iovec *iov,
-					    int nr_iov,
-                                            int nb_sectors,
-                                            BlockDriverCompletionFunc *cb,
-                                            void *opaque,
-                                            int is_write)
-
-{
-    VectorTranslationState *s = qemu_mallocz(sizeof(*s));
-    BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
-
-    s->this_aiocb = aiocb;
-    s->iov = iov;
-    s->nr_iov = nr_iov;
-    s->size = nb_sectors * 512;
-    s->bounce = qemu_memalign(512, s->size);
-    s->is_write = is_write;
-    if (is_write) {
-        iovec_to_buffer(s->iov, s->nr_iov, s->bounce);
-        s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
-                                  bdrv_aio_rw_vector_cb, s);
-    } else {
-        s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
-                                 bdrv_aio_rw_vector_cb, s);
-    }
-    return aiocb;
-}
-
 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
                                  struct iovec *iov, int nr_iov, int nb_sectors,
                                  BlockDriverCompletionFunc *cb, void *opaque)
 {
-    if (bdrv_check_request(bs, sector_num, nb_sectors))
-        return NULL;
-
-    return bdrv_aio_rw_vector(bs, sector_num, iov, nr_iov, nb_sectors,
-                              cb, opaque, 0);
-}
-
-BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
-                                  struct iovec *iov, int nr_iov, int nb_sectors,
-                                  BlockDriverCompletionFunc *cb, void *opaque)
-{
-    if (bdrv_check_request(bs, sector_num, nb_sectors))
-        return NULL;
-
-    return bdrv_aio_rw_vector(bs, sector_num, iov, nr_iov, nb_sectors,
-                              cb, opaque, 1);
-}
-
-static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
-                                uint8_t *buf, int nb_sectors,
-                                BlockDriverCompletionFunc *cb, void *opaque)
-{
     BlockDriver *drv = bs->drv;
     BlockDriverAIOCB *ret;
 
@@ -1336,7 +1262,8 @@ static BlockDriverAIOCB *bdrv_aio_read(B
     if (bdrv_check_request(bs, sector_num, nb_sectors))
         return NULL;
 
-    ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
+    ret = drv->bdrv_aio_readv(bs, sector_num, iov, nr_iov, nb_sectors,
+                              cb, opaque);
 
     if (ret) {
 	/* Update stats even though technically transfer has not happened. */
@@ -1347,9 +1274,9 @@ static BlockDriverAIOCB *bdrv_aio_read(B
     return ret;
 }
 
-static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
-                                 const uint8_t *buf, int nb_sectors,
-                                 BlockDriverCompletionFunc *cb, void *opaque)
+BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
+                                  struct iovec *iov, int nr_iov, int nb_sectors,
+                                  BlockDriverCompletionFunc *cb, void *opaque)
 {
     BlockDriver *drv = bs->drv;
     BlockDriverAIOCB *ret;
@@ -1361,7 +1288,8 @@ static BlockDriverAIOCB *bdrv_aio_write(
     if (bdrv_check_request(bs, sector_num, nb_sectors))
         return NULL;
 
-    ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
+    ret = drv->bdrv_aio_writev(bs, sector_num, iov, nr_iov, nb_sectors,
+                               cb, opaque);
 
     if (ret) {
 	/* Update stats even though technically transfer has not happened. */
@@ -1376,11 +1304,6 @@ void bdrv_aio_cancel(BlockDriverAIOCB *a
 {
     BlockDriver *drv = acb->bs->drv;
 
-    if (acb->cb == bdrv_aio_rw_vector_cb) {
-        VectorTranslationState *s = acb->opaque;
-        acb = s->aiocb;
-    }
-
     drv->bdrv_aio_cancel(acb);
 }
 
@@ -1391,42 +1314,66 @@ void bdrv_aio_cancel(BlockDriverAIOCB *a
 static void bdrv_aio_bh_cb(void *opaque)
 {
     BlockDriverAIOCBSync *acb = opaque;
+
+    qemu_vfree(acb->bounce);
+
+    if (!acb->is_write)
+        iovec_from_buffer(acb->iov, acb->nr_iov, acb->bounce, acb->size);
     acb->common.cb(acb->common.opaque, acb->ret);
+
     qemu_aio_release(acb);
 }
 
-static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
-        int64_t sector_num, uint8_t *buf, int nb_sectors,
-        BlockDriverCompletionFunc *cb, void *opaque)
+static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
+                                            int64_t sector_num,
+                                            struct iovec *iov,
+					    int nr_iov,
+                                            int nb_sectors,
+                                            BlockDriverCompletionFunc *cb,
+                                            void *opaque,
+                                            int is_write)
+
 {
-    BlockDriverAIOCBSync *acb;
-    int ret;
+    BlockDriverAIOCBSync *acb = qemu_aio_get(bs, cb, opaque);
+
+    acb->is_write = is_write;
+    acb->iov = iov;
+    acb->nr_iov = nr_iov;
+    acb->size = nb_sectors * 512;
+    acb->bounce = qemu_memalign(512, acb->size);
 
-    acb = qemu_aio_get(bs, cb, opaque);
     if (!acb->bh)
         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
-    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
-    acb->ret = ret;
+
+    if (is_write) {
+        iovec_to_buffer(acb->iov, acb->nr_iov, acb->bounce);
+        acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
+    } else {
+        acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
+    }
+
     qemu_bh_schedule(acb->bh);
+
     return &acb->common;
 }
 
-static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
-        int64_t sector_num, const uint8_t *buf, int nb_sectors,
+static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
+        int64_t sector_num, struct iovec *iov, int nr_iov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque)
 {
-    BlockDriverAIOCBSync *acb;
-    int ret;
+    return bdrv_aio_rw_vector(bs, sector_num, iov, nr_iov, nb_sectors,
+                              cb, opaque, 0);
+}
 
-    acb = qemu_aio_get(bs, cb, opaque);
-    if (!acb->bh)
-        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
-    ret = bdrv_write(bs, sector_num, buf, nb_sectors);
-    acb->ret = ret;
-    qemu_bh_schedule(acb->bh);
-    return &acb->common;
+static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, int64_t sector_num,
+        struct iovec *iov, int nr_iov, int nb_sectors,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    return bdrv_aio_rw_vector(bs, sector_num, iov, nr_iov, nb_sectors,
+                              cb, opaque, 1);
 }
 
+
 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
 {
     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
@@ -1449,10 +1396,13 @@ static int bdrv_read_em(BlockDriverState
 {
     int async_ret;
     BlockDriverAIOCB *acb;
+    struct iovec iov;
 
     async_ret = NOT_DONE;
-    acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
-                        bdrv_rw_em_cb, &async_ret);
+    iov.iov_base = buf;
+    iov.iov_len = nb_sectors * 512;
+    acb = bdrv_aio_readv(bs, sector_num, &iov, 1, nb_sectors,
+                         bdrv_rw_em_cb, &async_ret);
     if (acb == NULL)
         return -1;
 
@@ -1468,10 +1418,13 @@ static int bdrv_write_em(BlockDriverStat
 {
     int async_ret;
     BlockDriverAIOCB *acb;
+    struct iovec iov;
 
     async_ret = NOT_DONE;
-    acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
-                         bdrv_rw_em_cb, &async_ret);
+    iov.iov_base = (void *)buf;
+    iov.iov_len = nb_sectors * 512;
+    acb = bdrv_aio_writev(bs, sector_num, &iov, 1, nb_sectors,
+                          bdrv_rw_em_cb, &async_ret);
     if (acb == NULL)
         return -1;
     while (async_ret == NOT_DONE) {
block-qcow.c +54 -33

182 lines not on this page · raw message

block-qcow2.c +58 -39

208 lines not on this page · raw message

block-raw-posix.c +29 -54

151 lines not on this page · raw message

block-raw-win32.c +44 -14

123 lines not on this page · raw message

block_int.h +4 -4
Index: qemu/block_int.h
===================================================================
@@ -48,11 +48,11 @@ struct BlockDriver {
     int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
     int (*bdrv_make_empty)(BlockDriverState *bs);
     /* aio */
-    BlockDriverAIOCB *(*bdrv_aio_read)(BlockDriverState *bs,
-        int64_t sector_num, uint8_t *buf, int nb_sectors,
+    BlockDriverAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
+        int64_t sector_num, struct iovec *iov, int nr_iov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
-    BlockDriverAIOCB *(*bdrv_aio_write)(BlockDriverState *bs,
-        int64_t sector_num, const uint8_t *buf, int nb_sectors,
+    BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
+        int64_t sector_num, struct iovec *iov, int nr_iov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque);
     void (*bdrv_aio_cancel)(BlockDriverAIOCB *acb);
     int aiocb_size;
posix-aio-compat.c +85 -30

143 lines not on this page · raw message

posix-aio-compat.h +6 -1
Index: qemu/posix-aio-compat.h
===================================================================
@@ -24,13 +24,18 @@
 #define QEMU_PAIO_NOTCANCELED  0x02
 #define QEMU_PAIO_ALLDONE      0x03
 
+
 struct qemu_paiocb
 {
     int aio_fildes;
-    void *aio_buf;
+    struct iovec *aio_iov;
+    int aio_niov;
     size_t aio_nbytes;
     int ev_signo;
     off_t aio_offset;
+    unsigned aio_flags;
+/* 512 byte alignment required for buffer, offset and length */
+#define QEMU_AIO_SECTOR_ALIGNED	0x01
 
     /* private */
     TAILQ_ENTRY(qemu_paiocb) node;
#13 lore raw

PATCH 6/6 experimental native preadv/pwritev support for Linux

In reply to Christoph Hellwig

This ties up Gerd Hoffmann's unmegred preadv/pwritev syscalls to qemu. Use with care as the syscall numbers aren't finalized yet.

If someone of the BSD folks is interested it should be trivial to tie this up for the preadv/pwritev syscalls that have been around there for a while.

Probably wants some optimization to not try preadv/pwritev again once we got the first ENOSYS.

posix-aio-compat.c +83 -2
Index: qemu/posix-aio-compat.c
===================================================================
@@ -76,6 +76,45 @@ static void thread_create(pthread_t *thr
     if (ret) die2(ret, "pthread_create");
 }
 
+#if defined (__linux__) && defined(__i386__)
+
+#define __NR_preadv             333
+#define __NR_pwritev            334
+
+static ssize_t
+qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
+{
+    uint32_t pos_high = offset >> 32;
+    uint32_t pos_low = offset;
+
+    return syscall(__NR_preadv, fd, iov, (unsigned long)nr_iov, pos_high, pos_low);
+}
+
+static ssize_t
+qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
+{
+    uint32_t pos_high = offset >> 32;
+    uint32_t pos_low = offset;
+
+    return syscall(__NR_pwritev, fd, iov, (unsigned long)nr_iov, pos_high, pos_low);
+}
+
+#else
+
+static ssize_t
+qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
+{
+    return -ENOSYS;
+}
+
+static ssize_t
+qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
+{
+    return -ENOSYS;
+}
+
+#endif
+
 /*
  * Check if we need to copy the data in the aiocb into a new
  * properly aligned buffer.
@@ -93,6 +132,29 @@ static int aiocb_needs_copy(struct qemu_
     return 0;
 }
 
+static size_t handle_aiocb_vector(struct qemu_paiocb *aiocb)
+{
+    size_t offset = 0;
+    ssize_t len;
+
+    do {
+        if (aiocb->is_write)
+            len = qemu_pwritev(aiocb->aio_fildes,
+                               aiocb->aio_iov,
+                               aiocb->aio_niov,
+                               aiocb->aio_offset + offset);
+         else
+            len = qemu_preadv(aiocb->aio_fildes,
+                              aiocb->aio_iov,
+                              aiocb->aio_niov,
+                              aiocb->aio_offset + offset);
+    } while (len == -1 && errno == EINTR);
+
+    if (len == -1)
+        return -errno;
+    return len;
+}
+
 static size_t handle_aiocb_linear(struct qemu_paiocb *aiocb, char *buf)
 {
     size_t offset = 0;
@@ -129,12 +191,31 @@ static size_t handle_aiocb(struct qemu_p
     size_t nbytes;
     char *buf;
 
-    if (!aiocb_needs_copy(aiocb) && aiocb->aio_niov == 1) {
+    if (!aiocb_needs_copy(aiocb)) {
         /*
          * If there is just a single buffer, and it is properly aligned
          * we can just use plain pread/pwrite without any problems.
          */
-        return handle_aiocb_linear(aiocb, aiocb->aio_iov->iov_base);
+        if (aiocb->aio_niov == 1)
+             return handle_aiocb_linear(aiocb, aiocb->aio_iov->iov_base);
+
+        /*
+         * We have more than one iovec, and all are properly aligned.
+         *
+         * Try preadv/pwritev first and fall back to linearizing the
+         * buffer if it's not supported.
+         */
+         nbytes = handle_aiocb_vector(aiocb);
+	 if (nbytes == aiocb->aio_nbytes)
+	     return nbytes;
+         if (nbytes < 0 && nbytes != -ENOSYS)
+             return nbytes;
+
+         /*
+          * XXX(hch): short read/write.  no easy way to handle the reminder
+          * using these interfaces.  For now retry using plain
+          * pread/pwrite?
+          */
     }
 
     /*
#14 lore raw

Re: [Qemu-devel] [PATCH 6/6] experimental native preadv/pwritev support for Linux

Quoted 7 lines · Christoph Hellwig

This ties up Gerd Hoffmann's unmegred preadv/pwritev syscalls to qemu. Use with care as the syscall numbers aren't finalized yet.

If someone of the BSD folks is interested it should be trivial to tie this up for the preadv/pwritev syscalls that have been around there for a while.

At least OpenBSD has preadv & pwritev.

Quoted 2 lines

Probably wants some optimization to not try preadv/pwritev again once we got the first ENOSYS.

I'd add a check to configure to see if preadv exist and then define HAVE_PREADV. Though it's not possible to see if the kernel supports preadv if libc support does not exist (checking features by running programs breaks cross compilation).

#15 lore raw

Re: [Qemu-devel] [PATCH 6/6] experimental native preadv/pwritev support for Linux

Quoted 10 lines · Blue Swirl
Quoted 7 lines · Christoph Hellwig

This ties up Gerd Hoffmann's unmegred preadv/pwritev syscalls to qemu. Use with care as the syscall numbers aren't finalized yet.

If someone of the BSD folks is interested it should be trivial to tie this up for the preadv/pwritev syscalls that have been around there for a while.

At least OpenBSD has preadv & pwritev.

NetBSD also. But I don't have setup of either one, so I'll need some help on that front.

Quoted 4 lines

I'd add a check to configure to see if preadv exist and then define HAVE_PREADV. Though it's not possible to see if the kernel supports preadv if libc support does not exist (checking features by running programs breaks cross compilation).

The way glibc does it is only enable it if the underlying interface exists, and then have another shortcut if it exists in the headers but isn't supported by the underlying kernel. That's what I'll try to implement.

#16 lore raw

Re: [Qemu-devel] [PATCH 6/6] experimental native preadv/pwritev support for Linux

Quoted 23 lines · Christoph Hellwig
Quoted 10 lines · Blue Swirl
Quoted 7 lines · Christoph Hellwig

This ties up Gerd Hoffmann's unmegred preadv/pwritev syscalls to qemu. Use with care as the syscall numbers aren't finalized yet.

If someone of the BSD folks is interested it should be trivial to tie this up for the preadv/pwritev syscalls that have been around there for a while.

At least OpenBSD has preadv & pwritev.

NetBSD also. But I don't have setup of either one, so I'll need some
help on that front.

Quoted 4 lines

I'd add a check to configure to see if preadv exist and then define HAVE_PREADV. Though it's not possible to see if the kernel supports preadv if libc support does not exist (checking features by running programs breaks cross compilation).

Quoted 4 lines

The way glibc does it is only enable it if the underlying interface
exists, and then have another shortcut if it exists in the headers but isn't supported by the underlying kernel. That's what I'll try to implement.

This patch adds a check for preadv, it does not find it on Linux but does on OpenBSD.

configure +18 -0
Index: qemu/configure
===================================================================
@@ -1138,6 +1138,20 @@
   AIOLIBS="$AIOLIBS -lrt"
 fi
 
+##########################################
+# preadv probe
+cat > $TMPC <<EOF
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <unistd.h>
+int main(void) { return preadv(0, NULL, 0, 0); }
+EOF
+
+preadv=no
+if $cc $ARCH_CFLAGS -o $TMPE $TMPC > /dev/null 2> /dev/null ; then
+  preadv=yes
+fi
+
 if test "$mingw32" = "yes" ; then
   if test -z "$prefix" ; then
       prefix="c:\\\\Program Files\\\\Qemu"
@@ -1213,6 +1227,7 @@
 echo "Install blobs     $blobs"
 echo "KVM support       $kvm"
 echo "fdt support       $fdt"
+echo "preadv support    $preadv"
 
 if test $sdl_too_old = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -1516,6 +1531,9 @@
   echo "#define HAVE_FDT 1" >> $config_h
   echo "FDT_LIBS=-lfdt" >> $config_mak
 fi
+if test "$preadv" = "yes" ; then
+  echo "#define HAVE_PREADV 1" >> $config_h
+fi
 
 # XXX: suppress that
 if [ "$bsd" = "yes" ] ; then
#17 lore raw

Re: [Qemu-devel] [PATCH 6/6] experimental native preadv/pwritev support for Linux

Quoted 2 lines · Blue Swirl

This patch adds a check for preadv, it does not find it on Linux but does on OpenBSD.

Thanks, I'll add this to the next version of the patch series.

#18 lore raw

Re: [Qemu-devel] [PATCH 6/6] experimental native preadv/pwritev support for Linux

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

This ties up Gerd Hoffmann's unmegred preadv/pwritev syscalls to qemu. Use with care as the syscall numbers aren't finalized yet.

If someone of the BSD folks is interested it should be trivial to tie this up for the preadv/pwritev syscalls that have been around there for a while.

Probably wants some optimization to not try preadv/pwritev again once we got the first ENOSYS.

See $patch I've hacked up a while ago. I think it already ran over the list a while ago, /me was waiting for the preadv kernel patches hitting mainline before trying to re-submit with the final syscall numbers and the warning removed.

It provides qemu_p{read,write}v() functions. Adds autoconf check for *BSD, does linux syscall windup, runtime check and emulation for old kernels.

HTH,
Gerd

Quoted 1 line

From 5ab001859f3abeb792ef22d073d2bc38f6bb8604 Mon Sep 17 00:00:00 2001

From: Gerd Hoffmann <[email protected]>
Date: Thu, 22 Jan 2009 16:22:41 +0100
Subject: [PATCH] preadv/writev code for qemu.

---
 Makefile           |    1 +
 configure          |   14 +++++
 qemu-common.h      |    3 +
 qemu-io-syscalls.c |  140 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 158 insertions(+), 0 deletions(-)
 create mode 100644 qemu-io-syscalls.c
Makefile +1 -0
index 4f7a55a..7921d54 100644
@@ -85,6 +85,7 @@ OBJS+=sd.o ssi-sd.o
 OBJS+=bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o
 OBJS+=buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
 OBJS+=qemu-char.o aio.o net-checksum.o savevm.o cache-utils.o
+OBJS+=qemu-io-syscalls.o
 
 ifdef CONFIG_BRLAPI
 OBJS+= baum.o
configure +14 -0
index c3fbbbe..a873cf4 100755
@@ -1037,6 +1037,17 @@ if $cc $ARCH_CFLAGS -o $TMPE $TMPC > /dev/null 2> /dev/null ; then
 fi
 
 ##########################################
+# preadv probe
+cat > $TMPC <<EOF
+#include <unistd.h>
+int main(void) { preadv; }
+EOF
+preadv=no
+if $cc $ARCH_CFLAGS -o $TMPE $TMPC > /dev/null 2> /dev/null ; then
+  preadv=yes
+fi
+
+##########################################
 # fdt probe
 if test "$fdt" = "yes" ; then
     fdt=no
@@ -1432,6 +1443,9 @@ fi
 if test "$iovec" = "yes" ; then
   echo "#define HAVE_IOVEC 1" >> $config_h
 fi
+if test "$preadv" = "yes" ; then
+  echo "#define HAVE_PREADV 1" >> $config_h
+fi
 if test "$fdt" = "yes" ; then
   echo "#define HAVE_FDT 1" >> $config_h
   echo "FDT_LIBS=-lfdt" >> $config_mak
qemu-common.h +3 -0
index 42d5e49..0dfc575 100644
@@ -204,6 +204,9 @@ void qemu_iovec_destroy(QEMUIOVector *qiov);
 void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
 void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
 
+ssize_t qemu_preadv(int fd, QEMUIOVector *qiov, off_t offset);
+ssize_t qemu_pwritev(int fd, QEMUIOVector *qiov, off_t offset);
+
 #endif /* dyngen-exec.h hack */
 
 #endif
qemu-io-syscalls.c new file +140 -0

140 lines not on this page · raw message

Signature · 1 line
1.6.1.3
#19 lore raw

Re: [Qemu-devel] [PATCH 6/6] experimental native preadv/pwritev support for Linux

Quoted 8 lines · Gerd Hoffmann

See $patch I've hacked up a while ago. I think it already ran over the list a while ago, /me was waiting for the preadv kernel patches hitting mainline before trying to re-submit with the final syscall numbers and the warning removed.

It provides qemu_p{read,write}v() functions. Adds autoconf check for *BSD, does linux syscall windup, runtime check and emulation for old kernels.

Cool, I had missed that one. I'll throw it into the next version of the series that will go out once I'd either had a defintive NACK on the plain iovec APIs or someone backing up my point.