From patchwork Fri Jun 10 16:05:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 9170063 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0D9A5604DB for ; Fri, 10 Jun 2016 16:11:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0262B2832C for ; Fri, 10 Jun 2016 16:11:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB43328367; Fri, 10 Jun 2016 16:11:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5627228342 for ; Fri, 10 Jun 2016 16:11:29 +0000 (UTC) Received: from localhost ([::1]:43196 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bBP1r-0002Hg-PH for patchwork-qemu-devel@patchwork.kernel.org; Fri, 10 Jun 2016 12:11:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60799) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bBOwM-000778-OW for qemu-devel@nongnu.org; Fri, 10 Jun 2016 12:05:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bBOwF-00026p-Hh for qemu-devel@nongnu.org; Fri, 10 Jun 2016 12:05:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56701) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bBOwC-00025L-AS; Fri, 10 Jun 2016 12:05:36 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D926E72D2C; Fri, 10 Jun 2016 16:05:35 +0000 (UTC) Received: from noname.redhat.com (ovpn-116-78.ams2.redhat.com [10.36.116.78]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5AG5PW9032495; Fri, 10 Jun 2016 12:05:34 -0400 From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 10 Jun 2016 18:05:20 +0200 Message-Id: <1465574722-27656-5-git-send-email-kwolf@redhat.com> In-Reply-To: <1465574722-27656-1-git-send-email-kwolf@redhat.com> References: <1465574722-27656-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 10 Jun 2016 16:05:35 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 4/6] block: Make bdrv_load/save_vmstate coroutine_fns X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This allows drivers to share code between normal I/O and vmstate accesses. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake --- block/io.c | 80 ++++++++++++++++++++++++++++++++++------------- include/block/block_int.h | 10 +++--- 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/block/io.c b/block/io.c index bca244c..adf2726 100644 --- a/block/io.c +++ b/block/io.c @@ -1831,6 +1831,62 @@ int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); } +typedef struct BdrvVmstateCo { + BlockDriverState *bs; + QEMUIOVector *qiov; + int64_t pos; + bool is_read; + int ret; +} BdrvVmstateCo; + +static int coroutine_fn +bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos, + bool is_read) +{ + BlockDriver *drv = bs->drv; + + if (!drv) { + return -ENOMEDIUM; + } else if (drv->bdrv_load_vmstate) { + return is_read ? drv->bdrv_load_vmstate(bs, qiov, pos) + : drv->bdrv_save_vmstate(bs, qiov, pos); + } else if (bs->file) { + return bdrv_co_rw_vmstate(bs->file->bs, qiov, pos, is_read); + } + + return -ENOTSUP; +} + +static void bdrv_co_rw_vmstate_entry(void *opaque) +{ + BdrvVmstateCo *co = opaque; + co->ret = bdrv_co_rw_vmstate(co->bs, co->qiov, co->pos, co->is_read); +} + +static inline int +bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos, + bool is_read) +{ + if (qemu_in_coroutine()) { + return bdrv_co_rw_vmstate(bs, qiov, pos, is_read); + } else { + BdrvVmstateCo data = { + .bs = bs, + .qiov = qiov, + .pos = pos, + .is_read = is_read, + .ret = -EINPROGRESS, + }; + Coroutine *co = qemu_coroutine_create(bdrv_co_rw_vmstate_entry); + + qemu_coroutine_enter(co, &data); + while (data.ret == -EINPROGRESS) { + aio_poll(bdrv_get_aio_context(bs), true); + } + return data.ret; + } +} + int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size) { @@ -1853,17 +1909,7 @@ int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) { - BlockDriver *drv = bs->drv; - - if (!drv) { - return -ENOMEDIUM; - } else if (drv->bdrv_save_vmstate) { - return drv->bdrv_save_vmstate(bs, qiov, pos); - } else if (bs->file) { - return bdrv_writev_vmstate(bs->file->bs, qiov, pos); - } - - return -ENOTSUP; + return bdrv_rw_vmstate(bs, qiov, pos, false); } int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, @@ -1887,17 +1933,7 @@ int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) { - BlockDriver *drv = bs->drv; - - if (!drv) { - return -ENOMEDIUM; - } else if (drv->bdrv_load_vmstate) { - return drv->bdrv_load_vmstate(bs, qiov, pos); - } else if (bs->file) { - return bdrv_readv_vmstate(bs->file->bs, qiov, pos); - } - - return -ENOTSUP; + return bdrv_rw_vmstate(bs, qiov, pos, true); } /**************************************************************/ diff --git a/include/block/block_int.h b/include/block/block_int.h index f9a32cc..1fe0811 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -224,10 +224,12 @@ struct BlockDriver { int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi); ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs); - int (*bdrv_save_vmstate)(BlockDriverState *bs, QEMUIOVector *qiov, - int64_t pos); - int (*bdrv_load_vmstate)(BlockDriverState *bs, QEMUIOVector *qiov, - int64_t pos); + int coroutine_fn (*bdrv_save_vmstate)(BlockDriverState *bs, + QEMUIOVector *qiov, + int64_t pos); + int coroutine_fn (*bdrv_load_vmstate)(BlockDriverState *bs, + QEMUIOVector *qiov, + int64_t pos); int (*bdrv_change_backing_file)(BlockDriverState *bs, const char *backing_file, const char *backing_fmt);