From patchwork Mon Feb 18 14:09:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 221416 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2350C2C0095 for ; Tue, 19 Feb 2013 03:28:15 +1100 (EST) Received: from localhost ([::1]:34832 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7TZa-0008Rh-2i for incoming@patchwork.ozlabs.org; Mon, 18 Feb 2013 11:28:10 -0500 Received: from eggs.gnu.org ([208.118.235.92]:60601) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7RT1-0007EX-FL for qemu-devel@nongnu.org; Mon, 18 Feb 2013 09:13:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U7RSp-0002Lx-Em for qemu-devel@nongnu.org; Mon, 18 Feb 2013 09:13:11 -0500 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:46397) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7RSo-0002LV-T8 for qemu-devel@nongnu.org; Mon, 18 Feb 2013 09:13:03 -0500 Received: from /spool/local by e28smtp07.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 18 Feb 2013 19:40:16 +0530 Received: from d28dlp02.in.ibm.com (9.184.220.127) by e28smtp07.in.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 18 Feb 2013 19:40:09 +0530 Received: from d28relay01.in.ibm.com (d28relay01.in.ibm.com [9.184.220.58]) by d28dlp02.in.ibm.com (Postfix) with ESMTP id 5AB99394004F for ; Mon, 18 Feb 2013 19:42:53 +0530 (IST) Received: from d28av02.in.ibm.com (d28av02.in.ibm.com [9.184.220.64]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r1IECnb431981782 for ; Mon, 18 Feb 2013 19:42:49 +0530 Received: from d28av02.in.ibm.com (loopback [127.0.0.1]) by d28av02.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r1IECp2S009052 for ; Tue, 19 Feb 2013 01:12:52 +1100 Received: from RH63Wenchao ([9.77.177.33]) by d28av02.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r1IEA8vq030211; Tue, 19 Feb 2013 01:12:48 +1100 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Mon, 18 Feb 2013 22:09:29 +0800 Message-Id: <1361196578-19016-6-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1361196578-19016-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1361196578-19016-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13021814-8878-0000-0000-000005F00633 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.7 Cc: kwolf@redhat.com, aliguori@us.ibm.com, phrdina@redhat.com, stefanha@gmail.com, armbru@redhat.com, lcapitulino@redhat.com, pbonzini@redhat.com, Wenchao Xia Subject: [Qemu-devel] [PATCH V6 05/14] block: add snapshot info query function bdrv_query_snapshot_infolist() 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 This patch add function bdrv_query_snapshot_infolist(), which will return snapshot info of an image in qmp object format. The implementation code are based on the code moved from qemu-img.c with modification to fit more for qmp based block layer API. To help filter out snapshot info not needed, a call back function is added in bdrv_query_snapshot_infolist(). bdrv_can_read_snapshot() should be called before call this function, to avoid got *errp set unexpectedly. Signed-off-by: Wenchao Xia Reviewed-by: Eric Blake --- block.c | 60 ++++++++++++++++++++++++++++++++++++++---------- include/block/block.h | 8 +++++- qemu-img.c | 8 +++++- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/block.c b/block.c index 368b37c..8d0145a 100644 --- a/block.c +++ b/block.c @@ -2813,29 +2813,62 @@ int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top, return 0; } -void collect_snapshots(BlockDriverState *bs , ImageInfo *info) +SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs, + SnapshotFilterFunc filter, + void *opaque, + Error **errp) { int i, sn_count; QEMUSnapshotInfo *sn_tab = NULL; - SnapshotInfoList *info_list, *cur_item = NULL; + SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL; + SnapshotInfo *info; + sn_count = bdrv_snapshot_list(bs, &sn_tab); + if (sn_count < 0) { + /* Now bdrv_snapshot_list() use negative value to tip error, so a check + * of it was done here. In future errp can be set by that function + * itself, by changing the call back functions in c files in ./block. + */ + const char *dev = bdrv_get_device_name(bs); + switch (sn_count) { + case -ENOMEDIUM: + error_setg(errp, "Device '%s' is not inserted.", dev); + break; + case -ENOTSUP: + error_setg(errp, + "Device '%s' does not support internal snapshot.", + dev); + break; + default: + error_setg(errp, + "Device '%s' got '%d' for bdrv_snapshot_list(), " + "message '%s'.", + dev, sn_count, strerror(-sn_count)); + break; + } + return NULL; + } for (i = 0; i < sn_count; i++) { - info->has_snapshots = true; - info_list = g_new0(SnapshotInfoList, 1); + if (filter && filter(&sn_tab[i], opaque) != 0) { + continue; + } - info_list->value = g_new0(SnapshotInfo, 1); - info_list->value->id = g_strdup(sn_tab[i].id_str); - info_list->value->name = g_strdup(sn_tab[i].name); - info_list->value->vm_state_size = sn_tab[i].vm_state_size; - info_list->value->date_sec = sn_tab[i].date_sec; - info_list->value->date_nsec = sn_tab[i].date_nsec; - info_list->value->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; - info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; + info = g_new0(SnapshotInfo, 1); + info->id = g_strdup(sn_tab[i].id_str); + info->name = g_strdup(sn_tab[i].name); + info->vm_state_size = sn_tab[i].vm_state_size; + info->date_sec = sn_tab[i].date_sec; + info->date_nsec = sn_tab[i].date_nsec; + info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; + info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; + + info_list = g_new0(SnapshotInfoList, 1); + info_list->value = info; /* XXX: waiting for the qapi to support qemu-queue.h types */ if (!cur_item) { - info->snapshots = cur_item = info_list; + head = cur_item = info_list; } else { cur_item->next = info_list; cur_item = info_list; @@ -2844,6 +2877,7 @@ void collect_snapshots(BlockDriverState *bs , ImageInfo *info) } g_free(sn_tab); + return head; } void collect_image_info(BlockDriverState *bs, diff --git a/include/block/block.h b/include/block/block.h index e6d915c..51a14f2 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -319,6 +319,13 @@ void bdrv_get_backing_filename(BlockDriverState *bs, char *filename, int filename_size); void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz); + +typedef int (*SnapshotFilterFunc)(const QEMUSnapshotInfo *sn, void *opaque); +/* assume bs is already opened, use qapi_free_* to free returned value. */ +SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs, + SnapshotFilterFunc filter, + void *opaque, + Error **errp); BlockInfo *bdrv_query_info(BlockDriverState *s); BlockStats *bdrv_query_stats(const BlockDriverState *bs); bool bdrv_can_read_snapshot(BlockDriverState *bs); @@ -450,7 +457,6 @@ int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, int bdrv_debug_resume(BlockDriverState *bs, const char *tag); bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag); -void collect_snapshots(BlockDriverState *bs , ImageInfo *info); void collect_image_info(BlockDriverState *bs, ImageInfo *info, const char *filename); diff --git a/qemu-img.c b/qemu-img.c index b650d17..1034cc5 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1280,7 +1280,13 @@ static ImageInfoList *collect_image_info_list(const char *filename, info = g_new0(ImageInfo, 1); collect_image_info(bs, info, filename); - collect_snapshots(bs, info); + if (bdrv_can_read_snapshot(bs)) { + info->snapshots = bdrv_query_snapshot_infolist(bs, NULL, + NULL, NULL); + if (info->snapshots) { + info->has_snapshots = true; + } + } elem = g_new0(ImageInfoList, 1); elem->value = info;