From patchwork Mon Feb 18 14:09:30 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 221419 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 134F12C0092 for ; Tue, 19 Feb 2013 03:42:04 +1100 (EST) Received: from localhost ([::1]:39662 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7RTZ-0007og-K0 for incoming@patchwork.ozlabs.org; Mon, 18 Feb 2013 09:13:49 -0500 Received: from eggs.gnu.org ([208.118.235.92]:60586) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7RSt-0006ym-Ni for qemu-devel@nongnu.org; Mon, 18 Feb 2013 09:13:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U7RSn-0002LB-54 for qemu-devel@nongnu.org; Mon, 18 Feb 2013 09:13:07 -0500 Received: from e28smtp06.in.ibm.com ([122.248.162.6]:36082) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7RSm-0002Km-DB for qemu-devel@nongnu.org; Mon, 18 Feb 2013 09:13:01 -0500 Received: from /spool/local by e28smtp06.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 18 Feb 2013 19:40:04 +0530 Received: from d28dlp01.in.ibm.com (9.184.220.126) by e28smtp06.in.ibm.com (192.168.1.136) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 18 Feb 2013 19:40:03 +0530 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id 997EAE004F for ; Mon, 18 Feb 2013 19:43:52 +0530 (IST) Received: from d28av02.in.ibm.com (d28av02.in.ibm.com [9.184.220.64]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r1IECq1k37814308 for ; Mon, 18 Feb 2013 19:42:52 +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 r1IECtud009377 for ; Tue, 19 Feb 2013 01:12:56 +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 r1IEA8vr030211; Tue, 19 Feb 2013 01:12:53 +1100 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Mon, 18 Feb 2013 22:09:30 +0800 Message-Id: <1361196578-19016-7-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-9574-0000-0000-000006A3CF06 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.6 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 06/14] block: add image info query function bdrv_query_image_info() 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_image_info(), which will return image info in qmp object format. The implementation code are based on the code moved from qemu-img.c, but use block layer function to get snapshot info. A check with bdrv_can_read_snapshot(), was done before collecting snapshot info. Signed-off-by: Wenchao Xia Reviewed-by: Eric Blake --- block.c | 43 +++++++++++++++++++++++++++++++++++++------ include/block/block.h | 5 +---- qemu-img.c | 13 +++++-------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/block.c b/block.c index 8d0145a..ef1f7b9 100644 --- a/block.c +++ b/block.c @@ -2649,6 +2649,11 @@ void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) } } +static const char *bdrv_get_filename(const BlockDriverState *bs) +{ + return bs->filename; +} + const char *bdrv_get_device_name(BlockDriverState *bs) { return bs->device_name; @@ -2880,15 +2885,33 @@ SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs, return head; } -void collect_image_info(BlockDriverState *bs, - ImageInfo *info, - const char *filename) +/* collect all internal snapshot info in a image for ImageInfo */ +static void collect_snapshots_info(BlockDriverState *bs, + ImageInfo *info, + Error **errp) +{ + SnapshotInfoList *info_list; + + if (!bdrv_can_read_snapshot(bs)) { + return; + } + info_list = bdrv_query_snapshot_infolist(bs, NULL, NULL, errp); + if (info_list != NULL) { + info->has_snapshots = true; + info->snapshots = info_list; + } +} + +static void collect_image_info(BlockDriverState *bs, + ImageInfo *info) { uint64_t total_sectors; - char backing_filename[1024]; + const char *backing_filename; char backing_filename2[1024]; BlockDriverInfo bdi; + const char *filename; + filename = bdrv_get_filename(bs); bdrv_get_geometry(bs, &total_sectors); info->filename = g_strdup(filename); @@ -2908,8 +2931,8 @@ void collect_image_info(BlockDriverState *bs, info->dirty_flag = bdi.is_dirty; info->has_dirty_flag = true; } - bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename)); - if (backing_filename[0] != '\0') { + backing_filename = bs->backing_file; + if (backing_filename && backing_filename[0] != '\0') { info->backing_filename = g_strdup(backing_filename); info->has_backing_filename = true; bdrv_get_full_backing_filename(bs, backing_filename2, @@ -2928,6 +2951,14 @@ void collect_image_info(BlockDriverState *bs, } } +ImageInfo *bdrv_query_image_info(BlockDriverState *bs, Error **errp) +{ + ImageInfo *info = g_new0(ImageInfo, 1); + collect_image_info(bs, info); + collect_snapshots_info(bs, info, errp); + return info; +} + BlockInfo *bdrv_query_info(BlockDriverState *bs) { BlockInfo *info = g_malloc0(sizeof(*info)); diff --git a/include/block/block.h b/include/block/block.h index 51a14f2..f033807 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -326,6 +326,7 @@ SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs, SnapshotFilterFunc filter, void *opaque, Error **errp); +ImageInfo *bdrv_query_image_info(BlockDriverState *bs, Error **errp); BlockInfo *bdrv_query_info(BlockDriverState *s); BlockStats *bdrv_query_stats(const BlockDriverState *bs); bool bdrv_can_read_snapshot(BlockDriverState *bs); @@ -456,8 +457,4 @@ int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, const char *tag); int bdrv_debug_resume(BlockDriverState *bs, const char *tag); bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag); - -void collect_image_info(BlockDriverState *bs, - ImageInfo *info, - const char *filename); #endif diff --git a/qemu-img.c b/qemu-img.c index 1034cc5..90f4bf4 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1257,6 +1257,7 @@ static ImageInfoList *collect_image_info_list(const char *filename, ImageInfoList *head = NULL; ImageInfoList **last = &head; GHashTable *filenames; + Error *err = NULL; filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); @@ -1278,14 +1279,10 @@ static ImageInfoList *collect_image_info_list(const char *filename, goto err; } - info = g_new0(ImageInfo, 1); - collect_image_info(bs, info, filename); - if (bdrv_can_read_snapshot(bs)) { - info->snapshots = bdrv_query_snapshot_infolist(bs, NULL, - NULL, NULL); - if (info->snapshots) { - info->has_snapshots = true; - } + info = bdrv_query_image_info(bs, &err); + if (error_is_set(&err)) { + bdrv_delete(bs); + goto err; } elem = g_new0(ImageInfoList, 1);