From patchwork Wed Sep 11 08:00:30 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 274185 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)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 837742C01E6 for ; Wed, 11 Sep 2013 18:04:41 +1000 (EST) Received: from localhost ([::1]:33825 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJfPj-0006GS-Bc for incoming@patchwork.ozlabs.org; Wed, 11 Sep 2013 04:04:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46438) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJfMA-0001Es-J5 for qemu-devel@nongnu.org; Wed, 11 Sep 2013 04:01:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VJfM3-0005ZY-7A for qemu-devel@nongnu.org; Wed, 11 Sep 2013 04:00:58 -0400 Received: from mx1.redhat.com ([209.132.183.28]:15178) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJfM2-0005ZK-Os for qemu-devel@nongnu.org; Wed, 11 Sep 2013 04:00:50 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r8B80mJQ014486 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 11 Sep 2013 04:00:49 -0400 Received: from localhost (dhcp-200-247.str.redhat.com [10.33.200.247]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r8B80k57020959 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Wed, 11 Sep 2013 04:00:47 -0400 From: Max Reitz To: qemu-devel@nongnu.org Date: Wed, 11 Sep 2013 10:00:30 +0200 Message-Id: <1378886434-9411-3-git-send-email-mreitz@redhat.com> In-Reply-To: <1378886434-9411-1-git-send-email-mreitz@redhat.com> References: <1378886434-9411-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Fam Zheng , Stefan Hajnoczi , Max Reitz Subject: [Qemu-devel] [PATCH v4 2/6] block: Add ImageInfoSpecific to BlockDriverInfo 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 Add the new ImageInfoSpecific type also to BlockDriverInfo, as well as a bdrv_put_info function which releases all data allocated by bdrv_get_info from BlockDriverInfo (such as the new ImageInfoSpecific field). To prevent memory leaks, bdrv_put_info has to be called on every BlockDriverInfo object when it is no longer required (and bdrv_get_info has been successful). Signed-off-by: Max Reitz --- block.c | 16 +++++++++++++++- block/mirror.c | 16 +++++++++++----- block/qapi.c | 4 ++++ include/block/block.h | 3 +++ qemu-img.c | 1 + qemu-io-cmds.c | 2 ++ 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/block.c b/block.c index b81d1e2..604bef0 100644 --- a/block.c +++ b/block.c @@ -1953,8 +1953,10 @@ void bdrv_round_to_clusters(BlockDriverState *bs, int *cluster_nb_sectors) { BlockDriverInfo bdi; + int ret; - if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { + ret = bdrv_get_info(bs, &bdi); + if (ret < 0 || bdi.cluster_size == 0) { *cluster_sector_num = sector_num; *cluster_nb_sectors = nb_sectors; } else { @@ -1963,6 +1965,9 @@ void bdrv_round_to_clusters(BlockDriverState *bs, *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + nb_sectors, c); } + if (ret >= 0) { + bdrv_put_info(bs, &bdi); + } } static bool tracked_request_overlaps(BdrvTrackedRequest *req, @@ -3281,6 +3286,15 @@ int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) return drv->bdrv_get_info(bs, bdi); } +/** + * Releases all data which has been allocated through bdrv_get_info. This + * function should be called if and only if bdrv_get_info was successful. + */ +void bdrv_put_info(BlockDriverState *bs, BlockDriverInfo *bdi) +{ + qapi_free_ImageInfoSpecific(bdi->format_specific); +} + int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size) { diff --git a/block/mirror.c b/block/mirror.c index f61a779..7d3eb29 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -320,10 +320,12 @@ static void coroutine_fn mirror_run(void *opaque) bdrv_get_backing_filename(s->target, backing_filename, sizeof(backing_filename)); if (backing_filename[0] && !s->target->backing_hd) { - bdrv_get_info(s->target, &bdi); - if (s->granularity < bdi.cluster_size) { - s->buf_size = MAX(s->buf_size, bdi.cluster_size); - s->cow_bitmap = bitmap_new(length); + if (bdrv_get_info(s->target, &bdi) >= 0) { + if (s->granularity < bdi.cluster_size) { + s->buf_size = MAX(s->buf_size, bdi.cluster_size); + s->cow_bitmap = bitmap_new(length); + } + bdrv_put_info(s->target, &bdi); } } @@ -545,12 +547,16 @@ void mirror_start(BlockDriverState *bs, BlockDriverState *target, /* Choose the default granularity based on the target file's cluster * size, clamped between 4k and 64k. */ BlockDriverInfo bdi; - if (bdrv_get_info(target, &bdi) >= 0 && bdi.cluster_size != 0) { + int ret = bdrv_get_info(target, &bdi); + if (ret >= 0 && bdi.cluster_size != 0) { granularity = MAX(4096, bdi.cluster_size); granularity = MIN(65536, granularity); } else { granularity = 65536; } + if (ret >= 0) { + bdrv_put_info(target, &bdi); + } } assert ((granularity & (granularity - 1)) == 0); diff --git a/block/qapi.c b/block/qapi.c index 782051c..738469a 100644 --- a/block/qapi.c +++ b/block/qapi.c @@ -133,6 +133,10 @@ void bdrv_query_image_info(BlockDriverState *bs, } info->dirty_flag = bdi.is_dirty; info->has_dirty_flag = true; + if (bdi.format_specific) { + info->format_specific = bdi.format_specific; + info->has_format_specific = true; + } } backing_filename = bs->backing_file; if (backing_filename[0] != '\0') { diff --git a/include/block/block.h b/include/block/block.h index 1c5f939..a9b3b16 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -18,6 +18,8 @@ typedef struct BlockDriverInfo { /* offset at which the VM state can be saved (0 if not possible) */ int64_t vm_state_offset; bool is_dirty; + /* additional information; NULL if none */ + ImageInfoSpecific *format_specific; } BlockDriverInfo; typedef struct BlockFragInfo { @@ -334,6 +336,7 @@ int bdrv_get_flags(BlockDriverState *bs); int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, const uint8_t *buf, int nb_sectors); int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi); +void bdrv_put_info(BlockDriverState *bs, BlockDriverInfo *bdi); void bdrv_round_to_clusters(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int64_t *cluster_sector_num, diff --git a/qemu-img.c b/qemu-img.c index 0cf6be2..66bc696 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1393,6 +1393,7 @@ static int img_convert(int argc, char **argv) goto out; } cluster_size = bdi.cluster_size; + bdrv_put_info(out_bs, &bdi); if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) { error_report("invalid cluster size"); ret = -1; diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index 8565d49..5029b19 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -1699,6 +1699,8 @@ static int info_f(BlockDriverState *bs, int argc, char **argv) printf("cluster size: %s\n", s1); printf("vm state offset: %s\n", s2); + bdrv_put_info(bs, &bdi); + return 0; }