qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Charlie Shepherd <charlie@ctshepherd.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, stefanha@gmail.com, gabriel@kerneis.info,
	Charlie Shepherd <charlie@ctshepherd.com>,
	pbonzini@redhat.com
Subject: [Qemu-devel] [RFC v2 13/15] Introduce a run_handler function in qemu-img.c
Date: Fri,  9 Aug 2013 19:44:03 +0200	[thread overview]
Message-ID: <1376070245-22557-13-git-send-email-charlie@ctshepherd.com> (raw)
In-Reply-To: <1376070245-22557-1-git-send-email-charlie@ctshepherd.com>

qemu-img doesn't run in a coroutine context, but uses block layer functions
which do need to run in a coroutine context. This patch converts qemu-img to
run the various qemu-img functions in a coroutine context correctly.

Signed-off-by: Charlie Shepherd <charlie@ctshepherd.com>
---
 qemu-img.c | 54 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 43 insertions(+), 11 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index b9a848d..615f273 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -324,7 +324,7 @@ static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
     return 0;
 }
 
-static int img_create(int argc, char **argv)
+static int coroutine_fn img_create(int argc, char **argv)
 {
     int c;
     uint64_t img_size = -1;
@@ -522,7 +522,7 @@ static int collect_image_check(BlockDriverState *bs,
  * 2 - Check completed, image is corrupted
  * 3 - Check completed, image has leaked clusters, but is good otherwise
  */
-static int img_check(int argc, char **argv)
+static int coroutine_fn img_check(int argc, char **argv)
 {
     int c, ret;
     OutputFormat output_format = OFORMAT_HUMAN;
@@ -657,7 +657,7 @@ fail:
     return ret;
 }
 
-static int img_commit(int argc, char **argv)
+static int coroutine_fn img_commit(int argc, char **argv)
 {
     int c, ret, flags;
     const char *filename, *fmt, *cache;
@@ -854,7 +854,7 @@ static int64_t sectors_to_process(int64_t total, int64_t from)
  * @param buffer: Allocated buffer for storing read data
  * @param quiet: Flag for quiet mode
  */
-static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
+static int coroutine_fn check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
                                int sect_count, const char *filename,
                                uint8_t *buffer, bool quiet)
 {
@@ -882,7 +882,7 @@ static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
  * 1 - Images differ
  * >1 - Error occurred
  */
-static int img_compare(int argc, char **argv)
+static int coroutine_fn img_compare(int argc, char **argv)
 {
     const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
     BlockDriverState *bs1, *bs2;
@@ -1114,7 +1114,7 @@ out3:
     return ret;
 }
 
-static int img_convert(int argc, char **argv)
+static int coroutine_fn img_convert(int argc, char **argv)
 {
     int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
     int progress = 0, flags;
@@ -1704,7 +1704,7 @@ err:
     return NULL;
 }
 
-static int img_info(int argc, char **argv)
+static int coroutine_fn img_info(int argc, char **argv)
 {
     int c;
     OutputFormat output_format = OFORMAT_HUMAN;
@@ -1785,7 +1785,7 @@ static int img_info(int argc, char **argv)
 #define SNAPSHOT_APPLY  3
 #define SNAPSHOT_DELETE 4
 
-static int img_snapshot(int argc, char **argv)
+static int coroutine_fn img_snapshot(int argc, char **argv)
 {
     BlockDriverState *bs;
     QEMUSnapshotInfo sn;
@@ -1902,7 +1902,7 @@ static int img_snapshot(int argc, char **argv)
     return 0;
 }
 
-static int img_rebase(int argc, char **argv)
+static int coroutine_fn img_rebase(int argc, char **argv)
 {
     BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
     BlockDriver *old_backing_drv, *new_backing_drv;
@@ -2184,7 +2184,7 @@ out:
     return 0;
 }
 
-static int img_resize(int argc, char **argv)
+static int coroutine_fn img_resize(int argc, char **argv)
 {
     int c, ret, relative;
     const char *filename, *fmt, *size;
@@ -2317,6 +2317,38 @@ static const img_cmd_t img_cmds[] = {
     { NULL, NULL, },
 };
 
+struct HandlerCo {
+    int coroutine_fn (*handler)(int, char **);
+    int argc;
+    char **argv;
+    int ret;
+};
+
+
+static void coroutine_fn handler_entry(void *opaque)
+{
+    struct HandlerCo *hco = opaque;
+    hco->ret = hco->handler(hco->argc, hco->argv);
+    hco->handler = NULL;
+}
+
+static int run_handler(int coroutine_fn (*handler)(int, char **), int argc, char **argv)
+{
+    struct HandlerCo hco = {
+        .handler = handler,
+        .argc = argc,
+        .argv = argv,
+    };
+
+    Coroutine *co = qemu_coroutine_create(handler_entry);
+    qemu_coroutine_enter(co, &hco);
+    while (hco.handler) {
+        qemu_aio_wait();
+    }
+
+    return hco.ret;
+}
+
 int main(int argc, char **argv)
 {
     const img_cmd_t *cmd;
@@ -2338,7 +2370,7 @@ int main(int argc, char **argv)
     /* find the command */
     for(cmd = img_cmds; cmd->name != NULL; cmd++) {
         if (!strcmp(cmdname, cmd->name)) {
-            return cmd->handler(argc, argv);
+            return run_handler(cmd->handler, argc, argv);
         }
     }
 
-- 
1.8.3.2

  parent reply	other threads:[~2013-08-09 17:45 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-09 17:43 [Qemu-devel] [RFC v2 01/15] Add an explanation of when a function should be marked coroutine_fn Charlie Shepherd
2013-08-09 17:43 ` [Qemu-devel] [RFC v2 02/15] Rename qemu_coroutine_self to qemu_coroutine_self_int and add an annotated wrapper Charlie Shepherd
2013-08-14 14:21   ` Stefan Hajnoczi
2013-08-09 17:43 ` [Qemu-devel] [RFC v2 03/15] Explicitly mark BlockDriver function .bdrv_create as coroutine and rename it bdrv_co_create Charlie Shepherd
2013-08-14 14:26   ` Stefan Hajnoczi
2013-08-09 17:43 ` [Qemu-devel] [RFC v2 04/15] Convert .bdrv_open and .bdrv_file_open to coroutine_fn Charlie Shepherd
2013-08-29 12:11   ` Stefan Hajnoczi
2013-08-29 12:16     ` Charlie Shepherd
2013-08-09 17:43 ` [Qemu-devel] [RFC v2 05/15] Make qcow2_open synchronous Charlie Shepherd
2013-08-29 12:29   ` Stefan Hajnoczi
2013-08-29 12:33   ` Stefan Hajnoczi
2013-08-29 12:43     ` Charlie Shepherd
2013-09-04 11:39       ` Stefan Hajnoczi
2013-08-09 17:43 ` [Qemu-devel] [RFC v2 06/15] Explicitly mark BlockDriver functions .bdrv_write and .bdrv_read as coroutine functions Charlie Shepherd
2013-08-09 17:43 ` [Qemu-devel] [RFC v2 07/15] Call bdrv->open via a synchronous wrapper in block/snapshot.c Charlie Shepherd
2013-08-29 15:14   ` Stefan Hajnoczi
2013-08-09 17:43 ` [Qemu-devel] [RFC v2 08/15] Convert bdrv_create and associated functions to be coroutine_fn Charlie Shepherd
2013-08-29 15:31   ` Stefan Hajnoczi
2013-08-09 17:43 ` [Qemu-devel] [RFC v2 09/15] Add a synchronous wrapper bdrv_sync_rwco Charlie Shepherd
2013-08-29 15:32   ` Stefan Hajnoczi
2013-08-09 17:44 ` [Qemu-devel] [RFC v2 10/15] Convert bdrv_read, bdrv_write and associated functions to coroutine functions Charlie Shepherd
2013-08-09 17:44 ` [Qemu-devel] [RFC v2 11/15] Make bdrv_discard coroutine only and add bdrv_sync_discard Charlie Shepherd
2013-08-09 17:44 ` [Qemu-devel] [RFC v2 12/15] Make bdrv_flush coroutine only and add bdrv_sync_flush Charlie Shepherd
2013-08-09 17:44 ` Charlie Shepherd [this message]
2013-08-09 17:44 ` [Qemu-devel] [RFC v2 14/15] Add coroutine annotations for qemu_co_rwlock_rdlock and qemu_co_rwlock_wrlock Charlie Shepherd
2013-08-09 17:44 ` [Qemu-devel] [RFC v2 15/15] Add coroutine_fn annotations to nbd_co_* functions Charlie Shepherd
2013-08-14 14:14 ` [Qemu-devel] [RFC v2 01/15] Add an explanation of when a function should be marked coroutine_fn Stefan Hajnoczi
2013-08-29 15:34 ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1376070245-22557-13-git-send-email-charlie@ctshepherd.com \
    --to=charlie@ctshepherd.com \
    --cc=gabriel@kerneis.info \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).