From patchwork Wed Nov 4 17:19:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev" X-Patchwork-Id: 540103 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 06ECB140D9D for ; Thu, 5 Nov 2015 04:27:30 +1100 (AEDT) Received: from localhost ([::1]:56227 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zu1qJ-00019D-5x for incoming@patchwork.ozlabs.org; Wed, 04 Nov 2015 12:27:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60707) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zu1j4-0005Tj-1f for qemu-devel@nongnu.org; Wed, 04 Nov 2015 12:20:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zu1j2-0003al-V0 for qemu-devel@nongnu.org; Wed, 04 Nov 2015 12:19:58 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:19165 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zu1j2-0003aZ-Jk for qemu-devel@nongnu.org; Wed, 04 Nov 2015 12:19:56 -0500 Received: from hades.sw.ru ([10.30.8.132]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id tA4HJgKi020281; Wed, 4 Nov 2015 20:19:54 +0300 (MSK) From: "Denis V. Lunev" To: Date: Wed, 4 Nov 2015 20:19:39 +0300 Message-Id: <1446657582-21619-9-git-send-email-den@openvz.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1446657582-21619-1-git-send-email-den@openvz.org> References: <1446657582-21619-1-git-send-email-den@openvz.org> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x X-Received-From: 195.214.232.25 Cc: Kevin Wolf , "Denis V. Lunev" , qemu-devel@nongnu.org, Stefan Hajnoczi , Juan Quintela Subject: [Qemu-devel] [PATCH 08/11] migration: implement bdrv_all_find_vmstate_bs and bdrv_unlock helpers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org bdrv_all_find_vmstate_bs will return locked BlockDriverState. The code written in the way to avoid nested locking taking into account that patch for locking inside qemu_fopen_bdrv/qemu_fclose is acceptable. Signed-off-by: Denis V. Lunev CC: Juan Quintela CC: Stefan Hajnoczi CC: Kevin Wolf --- block/snapshot.c | 21 +++++++++++++++++++++ include/block/snapshot.h | 3 +++ migration/savevm.c | 23 ++++++++--------------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/block/snapshot.c b/block/snapshot.c index bb3bfd5..e196c9b 100644 --- a/block/snapshot.c +++ b/block/snapshot.c @@ -446,3 +446,24 @@ int bdrv_all_find_snapshot(const char *name, BlockDriverState **first_bad_bs) *first_bad_bs = bs; return err; } + +BlockDriverState *bdrv_all_find_vmstate_bs(void) +{ + BlockDriverState *bs = NULL; + + while ((bs = bdrv_next(bs))) { + AioContext *ctx = bdrv_get_aio_context(bs); + + aio_context_acquire(ctx); + if (bdrv_can_snapshot(bs)) { + return bs; + } + aio_context_release(ctx); + } + return NULL; +} + +void bdrv_unlock(BlockDriverState *bs) +{ + aio_context_release(bdrv_get_aio_context(bs)); +} diff --git a/include/block/snapshot.h b/include/block/snapshot.h index 44c3a38..1f45f51 100644 --- a/include/block/snapshot.h +++ b/include/block/snapshot.h @@ -88,4 +88,7 @@ int bdrv_all_delete_snapshot(const char *name, BlockDriverState **first_bsd_bs, int bdrv_all_goto_snapshot(const char *name, BlockDriverState **first_bsd_bs); int bdrv_all_find_snapshot(const char *name, BlockDriverState **first_bad_bs); +BlockDriverState *bdrv_all_find_vmstate_bs(void); +void bdrv_unlock(BlockDriverState *bs); + #endif diff --git a/migration/savevm.c b/migration/savevm.c index dbcb313..9339f2e 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -1237,17 +1237,6 @@ out: return ret; } -static BlockDriverState *find_vmstate_bs(void) -{ - BlockDriverState *bs = NULL; - while ((bs = bdrv_next(bs))) { - if (bdrv_can_snapshot(bs)) { - return bs; - } - } - return NULL; -} - void hmp_savevm(Monitor *mon, const QDict *qdict) { BlockDriverState *bs, *bs1; @@ -1293,8 +1282,8 @@ void hmp_savevm(Monitor *mon, const QDict *qdict) sn->date_nsec = tv.tv_usec * 1000; sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - bs = find_vmstate_bs(); - if (!bs) { + bs = bdrv_all_find_vmstate_bs(); + if (bs == NULL) { monitor_printf(mon, "No block device can accept snapshots\n"); goto the_end; } @@ -1312,6 +1301,7 @@ void hmp_savevm(Monitor *mon, const QDict *qdict) localtime_r((const time_t *)&tv.tv_sec, &tm); strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); } + bdrv_unlock(bs); /* save the VM state */ f = qemu_fopen_bdrv(bs, 1); @@ -1395,7 +1385,7 @@ int load_vmstate(const char *name) return ret; } - bs_vm_state = find_vmstate_bs(); + bs_vm_state = bdrv_all_find_vmstate_bs(); if (!bs_vm_state) { error_report("No block device supports snapshots"); return -ENOTSUP; @@ -1403,6 +1393,8 @@ int load_vmstate(const char *name) /* Don't even try to load empty VM states */ ret = bdrv_snapshot_find(bs_vm_state, &sn, name); + bdrv_unlock(bs_vm_state); + if (ret < 0) { return ret; } else if (sn.vm_state_size == 0) { @@ -1464,13 +1456,14 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict) int total; int *available_snapshots; - bs = find_vmstate_bs(); + bs = bdrv_all_find_vmstate_bs(); if (!bs) { monitor_printf(mon, "No available block device supports snapshots\n"); return; } nb_sns = bdrv_snapshot_list(bs, &sn_tab); + bdrv_unlock(bs); if (nb_sns < 0) { monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); return;