Skip to contentSkip to conversation

Fix bdrv_read/write emulation

PATCH New Kevin Wolf 1 participant 9 messages Oct 22, 2009

Subsystems (2)
lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover Fix bdrv_read/write emulation

The other day we re-introduced synchronous qcow2 read/write functions to work around the buggy bdrv_read/write emulation, this series implements the proper fix for it.

Basically what it's doing is to ensure that the emulation only runs its own AIO callbacks and bottom halves. Callbacks from outside need to wait so they can't corrupt the internal state of the driver calling bdrv_read/write (and assuming that nobody else runs in between).

Kevin Wolf (8):
posix-aio-compat: Split out posix_aio_process_queue Add qemu_aio_process_queue()
Split out bottom halves
Introduce contexts for asynchronous callbacks
block: Use new AsyncContext for bdrv_read/write emulation
posix-aio-compat: Honour AsyncContext
linux-aio: Honour AsyncContext
Revert "qcow2: Bring synchronous read/write back to life"

 Makefile.target       |    2 +-
 aio.c                 |   30 +++++++
 async.c               |  216 +++++++++++++++++++++++++++++++++++++++++++++++++
 block.c               |   22 ++++-
 block/curl.c          |    8 +-
 block/qcow2-cluster.c |    6 +-
 block/qcow2.c         |   51 +-----------
 block/qcow2.h         |    3 -
 linux-aio.c           |   87 ++++++++++++++++----
 posix-aio-compat.c    |   58 +++++++++----
 qemu-aio.h            |   13 +++
 qemu-common.h         |    5 +
 qemu-tool.c           |   13 +++
 vl.c                  |  104 -----------------------
 14 files changed, 418 insertions(+), 200 deletions(-)
 create mode 100644 async.c
author
#1 lore raw

PATCH 1/8 posix-aio-compat: Split out posix_aio_process_queue

We need to process the request queue and run callbacks separately from reading out the queue in a later patch, so split it out.

---
 posix-aio-compat.c |   43 +++++++++++++++++++++++++++----------------
 1 files changed, 27 insertions(+), 16 deletions(-)
posix-aio-compat.c +27 -16
index 400d898..e9f789c 100644
@@ -413,36 +413,25 @@ static int qemu_paio_error(struct qemu_paiocb *aiocb)
     return ret;
 }
 
-static void posix_aio_read(void *opaque)
+static int posix_aio_process_queue(void *opaque)
 {
     PosixAioState *s = opaque;
     struct qemu_paiocb *acb, **pacb;
     int ret;
-    ssize_t len;
-
-    /* read all bytes from signal pipe */
-    for (;;) {
-        char bytes[16];
-
-        len = read(s->rfd, bytes, sizeof(bytes));
-        if (len == -1 && errno == EINTR)
-            continue; /* try again */
-        if (len == sizeof(bytes))
-            continue; /* more to read */
-        break;
-    }
+    int result = 0;
 
     for(;;) {
         pacb = &s->first_aio;
         for(;;) {
             acb = *pacb;
             if (!acb)
-                goto the_end;
+                return result;
             ret = qemu_paio_error(acb);
             if (ret == ECANCELED) {
                 /* remove the request */
                 *pacb = acb->next;
                 qemu_aio_release(acb);
+                result = 1;
             } else if (ret != EINPROGRESS) {
                 /* end of aio */
                 if (ret == 0) {
@@ -459,13 +448,35 @@ static void posix_aio_read(void *opaque)
                 /* call the callback */
                 acb->common.cb(acb->common.opaque, ret);
                 qemu_aio_release(acb);
+                result = 1;
                 break;
             } else {
                 pacb = &acb->next;
             }
         }
     }
- the_end: ;
+
+    return result;
+}
+
+static void posix_aio_read(void *opaque)
+{
+    PosixAioState *s = opaque;
+    ssize_t len;
+
+    /* read all bytes from signal pipe */
+    for (;;) {
+        char bytes[16];
+
+        len = read(s->rfd, bytes, sizeof(bytes));
+        if (len == -1 && errno == EINTR)
+            continue; /* try again */
+        if (len == sizeof(bytes))
+            continue; /* more to read */
+        break;
+    }
+
+    posix_aio_process_queue(s);
 }
 
 static int posix_aio_flush(void *opaque)
Signature · 1 line
1.6.2.5
author
#2 lore raw

PATCH 2/8 Add qemu_aio_process_queue()

In reply to Kevin Wolf

We'll leave some AIO completions unhandled when we can't call the callback. qemu_aio_process_queue() is used later to run any callbacks that are left and can be run then.

---
 aio.c              |   30 ++++++++++++++++++++++++++++++
 block/curl.c       |    8 ++++----
 linux-aio.c        |    2 +-
 posix-aio-compat.c |    3 ++-
 qemu-aio.h         |   13 +++++++++++++
 5 files changed, 50 insertions(+), 6 deletions(-)
aio.c +30 -0
index da18443..f164a47 100644
@@ -33,6 +33,7 @@ struct AioHandler
     IOHandler *io_read;
     IOHandler *io_write;
     AioFlushHandler *io_flush;
+    AioProcessQueue *io_process_queue;
     int deleted;
     void *opaque;
     QLIST_ENTRY(AioHandler) node;
@@ -55,6 +56,7 @@ int qemu_aio_set_fd_handler(int fd,
                             IOHandler *io_read,
                             IOHandler *io_write,
                             AioFlushHandler *io_flush,
+                            AioProcessQueue *io_process_queue,
                             void *opaque)
 {
     AioHandler *node;
@@ -87,6 +89,7 @@ int qemu_aio_set_fd_handler(int fd,
         node->io_read = io_read;
         node->io_write = io_write;
         node->io_flush = io_flush;
+        node->io_process_queue = io_process_queue;
         node->opaque = opaque;
     }
 
@@ -115,6 +118,26 @@ void qemu_aio_flush(void)
     } while (qemu_bh_poll() || ret > 0);
 }
 
+int qemu_aio_process_queue(void)
+{
+    AioHandler *node;
+    int ret = 0;
+
+    walking_handlers = 1;
+
+    QLIST_FOREACH(node, &aio_handlers, node) {
+        if (node->io_process_queue) {
+            if (node->io_process_queue(node->opaque)) {
+                ret = 1;
+            }
+        }
+    }
+
+    walking_handlers = 0;
+
+    return ret;
+}
+
 void qemu_aio_wait(void)
 {
     int ret;
@@ -122,6 +145,13 @@ void qemu_aio_wait(void)
     if (qemu_bh_poll())
         return;
 
+    /*
+     * If there are callbacks left that have been queued, we need to call then.
+     * Return afterwards to avoid waiting needlessly in select().
+     */
+    if (qemu_aio_process_queue())
+        return;
+
     do {
         AioHandler *node;
         fd_set rdfds, wrfds;
block/curl.c +4 -4
index 3caa9be..5223ce8 100644
@@ -83,17 +83,17 @@ static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
     dprintf("CURL (AIO): Sock action %d on fd %d\n", action, fd);
     switch (action) {
         case CURL_POLL_IN:
-            qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, NULL, s);
+            qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, NULL, NULL, s);
             break;
         case CURL_POLL_OUT:
-            qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, NULL, s);
+            qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, NULL, NULL, s);
             break;
         case CURL_POLL_INOUT:
             qemu_aio_set_fd_handler(fd, curl_multi_do,
-                                    curl_multi_do, NULL, s);
+                                    curl_multi_do, NULL, NULL, s);
             break;
         case CURL_POLL_REMOVE:
-            qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL);
+            qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL, NULL);
             break;
     }
 
linux-aio.c +1 -1
index f53a08c..19ad36d 100644
@@ -192,7 +192,7 @@ void *laio_init(void)
         goto out_close_efd;
 
     qemu_aio_set_fd_handler(s->efd, qemu_laio_completion_cb,
-                            NULL, qemu_laio_flush_cb, s);
+                            NULL, qemu_laio_flush_cb, NULL, s);
 
     return s;
 
posix-aio-compat.c +2 -1
index e9f789c..4901539 100644
@@ -624,7 +624,8 @@ void *paio_init(void)
     fcntl(s->rfd, F_SETFL, O_NONBLOCK);
     fcntl(s->wfd, F_SETFL, O_NONBLOCK);
 
-    qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
+    qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush,
+        posix_aio_process_queue, s);
 
     ret = pthread_attr_init(&attr);
     if (ret)
qemu-aio.h +13 -0
index f262344..3bdd749 100644
@@ -20,6 +20,11 @@
 /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
 typedef int (AioFlushHandler)(void *opaque);
 
+/* Runs all currently allowed AIO callbacks of completed requests in the
+ * respective AIO backend. Returns 0 if no requests was handled, non-zero
+ * if at least one queued request was handled. */
+typedef int (AioProcessQueue)(void *opaque);
+
 /* Flush any pending AIO operation. This function will block until all
  * outstanding AIO operations have been completed or cancelled. */
 void qemu_aio_flush(void);
@@ -30,6 +35,13 @@ void qemu_aio_flush(void);
  * result of executing I/O completion or bh callbacks. */
 void qemu_aio_wait(void);
 
+/*
+ * Runs all currently allowed AIO callbacks of completed requests. Returns 0
+ * if no requests were handled, non-zero if at least one request was
+ * processed.
+ */
+int qemu_aio_process_queue(void);
+
 /* Register a file descriptor and associated callbacks.  Behaves very similarly
  * to qemu_set_fd_handler2.  Unlike qemu_set_fd_handler2, these callbacks will
  * be invoked when using either qemu_aio_wait() or qemu_aio_flush().
@@ -41,6 +53,7 @@ int qemu_aio_set_fd_handler(int fd,
                             IOHandler *io_read,
                             IOHandler *io_write,
                             AioFlushHandler *io_flush,
+                            AioProcessQueue *io_process_queue,
                             void *opaque);
 
 #endif
Signature · 1 line
1.6.2.5
author
#3 lore raw

PATCH 3/8 Split out bottom halves

In reply to Kevin Wolf

Instead of putting more and more stuff into vl.c, let's have the generic functions that deal with asynchronous callbacks in their own file.

---
 Makefile.target |    2 +-
 async.c         |  130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-common.h   |    1 +
 vl.c            |  104 --------------------------------------------
 4 files changed, 132 insertions(+), 105 deletions(-)
 create mode 100644 async.c
Makefile.target +1 -1
index 8d146c5..617c236 100644
@@ -154,7 +154,7 @@ endif #CONFIG_BSD_USER
 # System emulator target
 ifdef CONFIG_SOFTMMU
 
-obj-y = vl.o monitor.o pci.o machine.o gdbstub.o
+obj-y = vl.o async.o monitor.o pci.o machine.o gdbstub.o
 # virtio has to be here due to weird dependency between PCI and virtio-net.
 # need to fix this properly
 obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-console.o virtio-pci.o
async.c new file +130 -0

130 lines not on this page · raw message

qemu-common.h +1 -0
index 47272f5..4f42f95 100644
@@ -105,6 +105,7 @@ void qemu_bh_schedule_idle(QEMUBH *bh);
 void qemu_bh_cancel(QEMUBH *bh);
 void qemu_bh_delete(QEMUBH *bh);
 int qemu_bh_poll(void);
+void qemu_bh_update_timeout(int *timeout);
 
 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
 
vl.c +0 -104

110 lines not on this page · raw message

Signature · 1 line
1.6.2.5
author
#4 lore raw

PATCH 4/8 Introduce contexts for asynchronous callbacks

In reply to Kevin Wolf

Add the possibility to use AIO and BHs without allowing foreign callbacks to be run. Basically, you put your own AIOs and BHs in a separate context. For details see the comments in the source.

---
 async.c       |  100 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 qemu-common.h |    4 ++
 qemu-tool.c   |   13 +++++++
 3 files changed, 110 insertions(+), 7 deletions(-)
async.c +93 -7
index 60c1cb3..57ac3a8 100644
@@ -23,6 +23,94 @@
  */
 
 #include "qemu-common.h"
+#include "qemu-aio.h"
+
+/*
+ * An AsyncContext protects the callbacks of AIO requests and Bottom Halves
+ * against interfering with each other. A typical example is qcow2 that accepts
+ * asynchronous requests, but relies for manipulation of its metadata on
+ * synchronous bdrv_read/write that doesn't trigger any callbacks.
+ *
+ * However, these functions are often emulated using AIO which means that AIO
+ * callbacks must be run - but at the same time we must not run callbacks of
+ * other requests as they might start to modify metadata and corrupt the
+ * internal state of the caller of bdrv_read/write.
+ *
+ * To achieve the desired semantics we switch into a new AsyncContext.
+ * Callbacks must only be run if they belong to the current AsyncContext.
+ * Otherwise they need to be queued until their own context is active again.
+ * This is how you can make qemu_aio_wait() wait only for your own callbacks.
+ *
+ * The AsyncContexts form a stack. When you leave a AsyncContexts, you always
+ * return to the old ("parent") context.
+ */
+struct AsyncContext {
+    /* Consecutive number of the AsyncContext (position in the stack) */
+    int id;
+
+    /* Anchor of the list of Bottom Halves belonging to the context */
+    struct QEMUBH *first_bh;
+
+    /* Link to parent context */
+    struct AsyncContext *parent;
+};
+
+/* The currently active AsyncContext */
+static struct AsyncContext *async_context = &(struct AsyncContext) { 0 };
+
+/*
+ * Enter a new AsyncContext. Already scheduled Bottom Halves and AIO callbacks
+ * won't be called until this context is left again.
+ */
+void async_context_push(void)
+{
+    struct AsyncContext *new = qemu_mallocz(sizeof(*new));
+    new->parent = async_context;
+    new->id = async_context->id + 1;
+    async_context = new;
+}
+
+/* Run queued AIO completions and destroy Bottom Half */
+static void bh_run_aio_completions(void *opaque)
+{
+    QEMUBH **bh = opaque;
+    qemu_bh_delete(*bh);
+    qemu_free(bh);
+    qemu_aio_process_queue();
+}
+/*
+ * Leave the currently active AsyncContext. All Bottom Halves belonging to the
+ * old context are executed before changing the context.
+ */
+void async_context_pop(void)
+{
+    struct AsyncContext *old = async_context;
+    QEMUBH **bh;
+
+    /* Flush the bottom halves, we don't want to lose them */
+    while (qemu_bh_poll());
+
+    /* Switch back to the parent context */
+    async_context = async_context->parent;
+    qemu_free(old);
+
+    if (async_context == NULL) {
+        abort();
+    }
+
+    /* Schedule BH to run any queued AIO completions as soon as possible */
+    bh = qemu_malloc(sizeof(*bh));
+    *bh = qemu_bh_new(bh_run_aio_completions, bh);
+    qemu_bh_schedule(*bh);
+}
+
+/*
+ * Returns the ID of the currently active AsyncContext
+ */
+int get_async_context_id(void)
+{
+    return async_context->id;
+}
 
 /***********************************************************/
 /* bottom halves (can be seen as timers which expire ASAP) */
@@ -36,16 +124,14 @@ struct QEMUBH {
     QEMUBH *next;
 };
 
-static QEMUBH *first_bh = NULL;
-
 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
 {
     QEMUBH *bh;
     bh = qemu_mallocz(sizeof(QEMUBH));
     bh->cb = cb;
     bh->opaque = opaque;
-    bh->next = first_bh;
-    first_bh = bh;
+    bh->next = async_context->first_bh;
+    async_context->first_bh = bh;
     return bh;
 }
 
@@ -55,7 +141,7 @@ int qemu_bh_poll(void)
     int ret;
 
     ret = 0;
-    for (bh = first_bh; bh; bh = bh->next) {
+    for (bh = async_context->first_bh; bh; bh = bh->next) {
         if (!bh->deleted && bh->scheduled) {
             bh->scheduled = 0;
             if (!bh->idle)
@@ -66,7 +152,7 @@ int qemu_bh_poll(void)
     }
 
     /* remove deleted bhs */
-    bhp = &first_bh;
+    bhp = &async_context->first_bh;
     while (*bhp) {
         bh = *bhp;
         if (bh->deleted) {
@@ -112,7 +198,7 @@ void qemu_bh_update_timeout(int *timeout)
 {
     QEMUBH *bh;
 
-    for (bh = first_bh; bh; bh = bh->next) {
+    for (bh = async_context->first_bh; bh; bh = bh->next) {
         if (!bh->deleted && bh->scheduled) {
             if (bh->idle) {
                 /* idle bottom halves will be polled at least
qemu-common.h +4 -0
index 4f42f95..f6c612a 100644
@@ -93,6 +93,10 @@ typedef struct QEMUBH QEMUBH;
 
 typedef void QEMUBHFunc(void *opaque);
 
+void async_context_push(void);
+void async_context_pop(void);
+int get_async_context_id(void);
+
 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
 void qemu_bh_schedule(QEMUBH *bh);
 /* Bottom halfs that are scheduled from a bottom half handler are instantly
qemu-tool.c +13 -0
index ba24aa2..a1af4ac 100644
@@ -43,6 +43,19 @@ void monitor_print_filename(Monitor *mon, const char *filename)
 {
 }
 
+void async_context_push(void)
+{
+}
+
+void async_context_pop(void)
+{
+}
+
+int get_async_context_id(void)
+{
+    return 0;
+}
+
 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
 {
     QEMUBH *bh;
Signature · 1 line
1.6.2.5
author
#5 lore raw

PATCH 5/8 block: Use new AsyncContext for bdrv_read/write emulation

In reply to Kevin Wolf

bdrv_read/write emulation is used as the perfect example why we need something like AsyncContexts. So maybe they better start using it.

---
 block.c |   22 ++++++++++++++++++----
 1 files changed, 18 insertions(+), 4 deletions(-)
block.c +18 -4
index 33f3d65..fa0de25 100644
@@ -1696,19 +1696,26 @@ static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
     struct iovec iov;
     QEMUIOVector qiov;
 
+    async_context_push();
+
     async_ret = NOT_DONE;
     iov.iov_base = (void *)buf;
     iov.iov_len = nb_sectors * 512;
     qemu_iovec_init_external(&qiov, &iov, 1);
     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
         bdrv_rw_em_cb, &async_ret);
-    if (acb == NULL)
-        return -1;
+    if (acb == NULL) {
+        async_ret = -1;
+        goto fail;
+    }
 
     while (async_ret == NOT_DONE) {
         qemu_aio_wait();
     }
 
+
+fail:
+    async_context_pop();
     return async_ret;
 }
 
@@ -1720,17 +1727,24 @@ static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
     struct iovec iov;
     QEMUIOVector qiov;
 
+    async_context_push();
+
     async_ret = NOT_DONE;
     iov.iov_base = (void *)buf;
     iov.iov_len = nb_sectors * 512;
     qemu_iovec_init_external(&qiov, &iov, 1);
     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
         bdrv_rw_em_cb, &async_ret);
-    if (acb == NULL)
-        return -1;
+    if (acb == NULL) {
+        async_ret = -1;
+        goto fail;
+    }
     while (async_ret == NOT_DONE) {
         qemu_aio_wait();
     }
+
+fail:
+    async_context_pop();
     return async_ret;
 }
 
Signature · 1 line
1.6.2.5
author
#6 lore raw

PATCH 6/8 posix-aio-compat: Honour AsyncContext

In reply to Kevin Wolf

Don't call callbacks that don't belong to the active AsyncContext.

---
 posix-aio-compat.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
posix-aio-compat.c +12 -0
index 4901539..ec58288 100644
@@ -48,6 +48,8 @@ struct qemu_paiocb {
     ssize_t ret;
     int active;
     struct qemu_paiocb *next;
+
+    int async_context_id;
 };
 
 typedef struct PosixAioState {
@@ -419,6 +421,7 @@ static int posix_aio_process_queue(void *opaque)
     struct qemu_paiocb *acb, **pacb;
     int ret;
     int result = 0;
+    int async_context_id = get_async_context_id();
 
     for(;;) {
         pacb = &s->first_aio;
@@ -426,6 +429,13 @@ static int posix_aio_process_queue(void *opaque)
             acb = *pacb;
             if (!acb)
                 return result;
+
+            /* we're only interested in requests in the right context */
+            if (acb->async_context_id != async_context_id) {
+                pacb = &acb->next;
+                continue;
+            }
+
             ret = qemu_paio_error(acb);
             if (ret == ECANCELED) {
                 /* remove the request */
@@ -558,6 +568,8 @@ BlockDriverAIOCB *paio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
     acb->aio_type = type;
     acb->aio_fildes = fd;
     acb->ev_signo = SIGUSR2;
+    acb->async_context_id = get_async_context_id();
+
     if (qiov) {
         acb->aio_iov = qiov->iov;
         acb->aio_niov = qiov->niov;
Signature · 1 line
1.6.2.5
author
#7 lore raw

PATCH 7/8 linux-aio: Honour AsyncContext

In reply to Kevin Wolf

Also for Linux AIO, don't call callbacks that don't belong to the active AsyncContext.

---
 linux-aio.c |   87 ++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 72 insertions(+), 15 deletions(-)
linux-aio.c +72 -15
index 19ad36d..5e892b0 100644
@@ -31,12 +31,15 @@ struct qemu_laiocb {
     struct iocb iocb;
     ssize_t ret;
     size_t nbytes;
+    int async_context_id;
+    QLIST_ENTRY(qemu_laiocb) node;
 };
 
 struct qemu_laio_state {
     io_context_t ctx;
     int efd;
     int count;
+    QLIST_HEAD(, qemu_laiocb) completed_reqs;
 };
 
 static inline ssize_t io_event_ret(struct io_event *ev)
@@ -44,6 +47,69 @@ static inline ssize_t io_event_ret(struct io_event *ev)
     return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
 }
 
+/*
+ * Completes an AIO request (calls the callback and frees the ACB).
+ * Be sure to be in the right AsyncContext before calling this function.
+ */
+static void qemu_laio_process_completion(struct qemu_laio_state *s,
+    struct qemu_laiocb *laiocb)
+{
+    int ret;
+
+    s->count--;
+
+    ret = laiocb->ret;
+    if (ret != -ECANCELED) {
+        if (ret == laiocb->nbytes)
+            ret = 0;
+        else if (ret >= 0)
+            ret = -EINVAL;
+
+        laiocb->common.cb(laiocb->common.opaque, ret);
+    }
+
+    qemu_aio_release(laiocb);
+}
+
+/*
+ * Processes all queued AIO requests, i.e. requests that have return from OS
+ * but their callback was not called yet. Requests that cannot have their
+ * callback called in the current AsyncContext, remain in the queue.
+ *
+ * Returns 1 if at least one request could be completed, 0 otherwise.
+ */
+static int qemu_laio_process_requests(void *opaque)
+{
+    struct qemu_laio_state *s = opaque;
+    struct qemu_laiocb *laiocb, *next;
+    int res = 0;
+
+    QLIST_FOREACH_SAFE (laiocb, &s->completed_reqs, node, next) {
+        if (laiocb->async_context_id == get_async_context_id()) {
+            qemu_laio_process_completion(s, laiocb);
+            QLIST_REMOVE(laiocb, node);
+            res = 1;
+        }
+    }
+
+    return res;
+}
+
+/*
+ * Puts a request in the completion queue so that its callback is called the
+ * next time when it's possible. If we already are in the right AsyncContext,
+ * the request is completed immediately instead.
+ */
+static void qemu_laio_enqueue_completed(struct qemu_laio_state *s,
+    struct qemu_laiocb* laiocb)
+{
+    if (laiocb->async_context_id == get_async_context_id()) {
+        qemu_laio_process_completion(s, laiocb);
+    } else {
+        QLIST_INSERT_HEAD(&s->completed_reqs, laiocb, node);
+    }
+}
+
 static void qemu_laio_completion_cb(void *opaque)
 {
     struct qemu_laio_state *s = opaque;
@@ -74,19 +140,8 @@ static void qemu_laio_completion_cb(void *opaque)
             struct qemu_laiocb *laiocb =
                     container_of(iocb, struct qemu_laiocb, iocb);
 
-            s->count--;
-
-            ret = laiocb->ret = io_event_ret(&events[i]);
-            if (ret != -ECANCELED) {
-                if (ret == laiocb->nbytes)
-                    ret = 0;
-                else if (ret >= 0)
-                    ret = -EINVAL;
-
-                laiocb->common.cb(laiocb->common.opaque, ret);
-            }
-
-            qemu_aio_release(laiocb);
+            laiocb->ret = io_event_ret(&events[i]);
+            qemu_laio_enqueue_completed(s, laiocb);
         }
     }
 }
@@ -149,6 +204,7 @@ BlockDriverAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
     laiocb->nbytes = nb_sectors * 512;
     laiocb->ctx = s;
     laiocb->ret = -EINPROGRESS;
+    laiocb->async_context_id = get_async_context_id();
 
     iocbs = &laiocb->iocb;
 
@@ -183,6 +239,7 @@ void *laio_init(void)
     struct qemu_laio_state *s;
 
     s = qemu_mallocz(sizeof(*s));
+    QLIST_INIT(&s->completed_reqs);
     s->efd = eventfd(0, 0);
     if (s->efd == -1)
         goto out_free_state;
@@ -191,8 +248,8 @@ void *laio_init(void)
     if (io_setup(MAX_EVENTS, &s->ctx) != 0)
         goto out_close_efd;
 
-    qemu_aio_set_fd_handler(s->efd, qemu_laio_completion_cb,
-                            NULL, qemu_laio_flush_cb, NULL, s);
+    qemu_aio_set_fd_handler(s->efd, qemu_laio_completion_cb, NULL,
+        qemu_laio_flush_cb, qemu_laio_process_requests, s);
 
     return s;
 
Signature · 1 line
1.6.2.5
author
#8 lore raw

PATCH 8/8 Revert "qcow2: Bring synchronous read/write back to life"

In reply to Kevin Wolf

It was merely a workaround and the real fix is done now. This reverts commit ef845c3bf421290153154635dc18eaa677cecb43.

---
 block/qcow2-cluster.c |    6 ++--
 block/qcow2.c         |   51 +-----------------------------------------------
 block/qcow2.h         |    3 --
 3 files changed, 5 insertions(+), 55 deletions(-)
block/qcow2-cluster.c +3 -3
index a7de820..e444e53 100644
@@ -306,8 +306,8 @@ void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
 }
 
 
-int qcow2_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
-    int nb_sectors)
+static int qcow_read(BlockDriverState *bs, int64_t sector_num,
+                     uint8_t *buf, int nb_sectors)
 {
     BDRVQcowState *s = bs->opaque;
     int ret, index_in_cluster, n, n1;
@@ -358,7 +358,7 @@ static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
     n = n_end - n_start;
     if (n <= 0)
         return 0;
-    ret = qcow2_read(bs, start_sect + n_start, s->cluster_data, n);
+    ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
     if (ret < 0)
         return ret;
     if (s->crypt_method) {
block/qcow2.c +2 -49

63 lines not on this page · raw message

block/qcow2.h +0 -3
index d3f690a..26ab5d9 100644
@@ -202,9 +202,6 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
     QCowL2Meta *m);
 
-int qcow2_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
-    int nb_sectors);
-
 /* qcow2-snapshot.c functions */
 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
Signature · 1 line
1.6.2.5