From patchwork Mon Nov 10 13:45:39 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 408939 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 D44441400F4 for ; Tue, 11 Nov 2014 00:47:03 +1100 (AEDT) Received: from localhost ([::1]:43205 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnpJ7-0005hm-OY for incoming@patchwork.ozlabs.org; Mon, 10 Nov 2014 08:47:01 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58047) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnpIO-0004XG-4Z for qemu-devel@nongnu.org; Mon, 10 Nov 2014 08:46:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XnpII-0007fo-05 for qemu-devel@nongnu.org; Mon, 10 Nov 2014 08:46:16 -0500 Received: from mx1.redhat.com ([209.132.183.28]:42462) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnpIH-0007fb-OY for qemu-devel@nongnu.org; Mon, 10 Nov 2014 08:46:09 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sAADk4jH013442 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 10 Nov 2014 08:46:04 -0500 Received: from localhost (dhcp-192-247.str.redhat.com [10.33.192.247]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sAADk2QO024338 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO); Mon, 10 Nov 2014 08:46:03 -0500 From: Max Reitz To: qemu-devel@nongnu.org Date: Mon, 10 Nov 2014 14:45:39 +0100 Message-Id: <1415627159-15941-2-git-send-email-mreitz@redhat.com> In-Reply-To: <1415627159-15941-1-git-send-email-mreitz@redhat.com> References: <1415627159-15941-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Peter Lieven , Stefan Hajnoczi , Max Reitz Subject: [Qemu-devel] [PATCH 01/21] qcow2: Add two new fields to BDRVQcowState 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 two new fields regarding refcount information (the bit width of every entry and the maximum refcount value) to the BDRVQcowState. Signed-off-by: Max Reitz Reviewed-by: Eric Blake --- block/qcow2-refcount.c | 2 +- block/qcow2.c | 9 +++++++++ block/qcow2.h | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 9afdb40..6016211 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -584,7 +584,7 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, refcount = be16_to_cpu(refcount_block[block_index]); refcount += addend; - if (refcount < 0 || refcount > 0xffff) { + if (refcount < 0 || refcount > s->refcount_max) { ret = -EINVAL; goto fail; } diff --git a/block/qcow2.c b/block/qcow2.c index d120494..f57aff9 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -684,6 +684,15 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } s->refcount_order = header.refcount_order; + s->refcount_bits = 1 << s->refcount_order; + if (s->refcount_order < 6) { + s->refcount_max = (UINT64_C(1) << s->refcount_bits) - 1; + } else { + /* The above shift would overflow with s->refcount_bits == 64; + * furthermore, we do not want to use UINT64_MAX because refcounts will + * be passed around in int64_ts (negative values for -errno) */ + s->refcount_max = INT64_MAX; + } if (header.crypt_method > QCOW_CRYPT_AES) { error_setg(errp, "Unsupported encryption method: %" PRIu32, diff --git a/block/qcow2.h b/block/qcow2.h index 6e39a1b..4d8c902 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -258,6 +258,8 @@ typedef struct BDRVQcowState { int qcow_version; bool use_lazy_refcounts; int refcount_order; + int refcount_bits; + uint64_t refcount_max; bool discard_passthrough[QCOW2_DISCARD_MAX];