From nobody Wed Apr 16 03:53:27 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1558369140; cv=none; d=zoho.com; s=zohoarc; b=hUurYI1KNt80kN66/7+4ZvDxwLBR5z1ZkmF2LHAzhKiJKSrOjclFVfavFKbiusL/YYmGGJDxTteycxz6TL0dBr4jKdPBFuNW2CL/PwOo4kuKYRMA8ZA1lwQ7jJSxLTVzLcGE/2vWmYNYfp5D3WS0rhgfuA5UDwdysmzSxpgieWo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1558369140; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=OP6eHF3UQulH9aZlV5dapKoXiPVt6ikGyPLwouuvLl8=; b=PX5KEQRCs/0IjZAAGmpw4SieTfSSa2uSZfp7Nbw6MxAXufDeHzzS9PC9R+SGpNSp+MH+UpXLTjn8pxQ4GvHAOLhLsZ2ghwJpczHayfQcqZcDBO+VTCWvFQ7E4VVl36PR643N1VbPzu30p7B11vTMBy7Se4tjs8nvO7PhuUihq0I= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1558369139997968.1161967593458; Mon, 20 May 2019 09:18:59 -0700 (PDT) Received: from localhost ([127.0.0.1]:38029 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSkzi-00024Y-Kn for importer@patchew.org; Mon, 20 May 2019 12:18:34 -0400 Received: from eggs.gnu.org ([209.51.188.92]:38419) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSkwq-0008EB-HD for qemu-devel@nongnu.org; Mon, 20 May 2019 12:15:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hSkwp-0003Xe-7z for qemu-devel@nongnu.org; Mon, 20 May 2019 12:15:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56804) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hSkwk-0003HA-8v; Mon, 20 May 2019 12:15:31 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 774563091851; Mon, 20 May 2019 16:15:02 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-208.ams2.redhat.com [10.36.117.208]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8E53FD1E4; Mon, 20 May 2019 16:14:59 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Mon, 20 May 2019 18:14:31 +0200 Message-Id: <20190520161453.30723-3-kwolf@redhat.com> In-Reply-To: <20190520161453.30723-1-kwolf@redhat.com> References: <20190520161453.30723-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Mon, 20 May 2019 16:15:07 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 02/24] qcow2: Define and use QCOW2_COMPRESSED_SECTOR_SIZE X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Alberto Garcia When an L2 table entry points to a compressed cluster the space used by the data is specified in 512-byte sectors. This size is independent from BDRV_SECTOR_SIZE and is specific to the qcow2 file format. The QCOW2_COMPRESSED_SECTOR_SIZE constant defined in this patch makes this explicit. Signed-off-by: Alberto Garcia Signed-off-by: Kevin Wolf --- block/qcow2.h | 4 ++++ block/qcow2-cluster.c | 5 +++-- block/qcow2-refcount.c | 25 ++++++++++++++----------- block/qcow2.c | 3 ++- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/block/qcow2.h b/block/qcow2.h index e62508d1ce..8d92ef1fee 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -74,6 +74,10 @@ #define MIN_CLUSTER_BITS 9 #define MAX_CLUSTER_BITS 21 =20 +/* Defined in the qcow2 spec (compressed cluster descriptor) */ +#define QCOW2_COMPRESSED_SECTOR_SIZE 512U +#define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1)) + /* Must be at least 2 to cover COW */ #define MIN_L2_CACHE_SIZE 2 /* cache entries */ =20 diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 974a4e8656..b36f4aa84a 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -796,8 +796,9 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverSt= ate *bs, return cluster_offset; } =20 - nb_csectors =3D ((cluster_offset + compressed_size - 1) >> 9) - - (cluster_offset >> 9); + nb_csectors =3D + (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_S= IZE - + (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE); =20 cluster_offset |=3D QCOW_OFLAG_COMPRESSED | ((uint64_t)nb_csectors << s->csize_shift); diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 7481903396..0b09d6838b 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1172,12 +1172,11 @@ void qcow2_free_any_clusters(BlockDriverState *bs, = uint64_t l2_entry, switch (ctype) { case QCOW2_CLUSTER_COMPRESSED: { - int nb_csectors; - nb_csectors =3D ((l2_entry >> s->csize_shift) & - s->csize_mask) + 1; - qcow2_free_clusters(bs, - (l2_entry & s->cluster_offset_mask) & ~511, - nb_csectors * 512, type); + int64_t offset =3D (l2_entry & s->cluster_offset_mask) + & QCOW2_COMPRESSED_SECTOR_MASK; + int size =3D QCOW2_COMPRESSED_SECTOR_SIZE * + (((l2_entry >> s->csize_shift) & s->csize_mask) + 1); + qcow2_free_clusters(bs, offset, size, type); } break; case QCOW2_CLUSTER_NORMAL: @@ -1317,9 +1316,12 @@ int qcow2_update_snapshot_refcount(BlockDriverState = *bs, nb_csectors =3D ((entry >> s->csize_shift) & s->csize_mask) + 1; if (addend !=3D 0) { + uint64_t coffset =3D (entry & s->cluster_offse= t_mask) + & QCOW2_COMPRESSED_SECTOR_MASK; ret =3D update_refcount( - bs, (entry & s->cluster_offset_mask) & ~51= 1, - nb_csectors * 512, abs(addend), addend < 0, + bs, coffset, + nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE, + abs(addend), addend < 0, QCOW2_DISCARD_SNAPSHOT); if (ret < 0) { goto fail; @@ -1635,9 +1637,10 @@ static int check_refcounts_l2(BlockDriverState *bs, = BdrvCheckResult *res, nb_csectors =3D ((l2_entry >> s->csize_shift) & s->csize_mask) + 1; l2_entry &=3D s->cluster_offset_mask; - ret =3D qcow2_inc_refcounts_imrt(bs, res, - refcount_table, refcount_table_= size, - l2_entry & ~511, nb_csectors * = 512); + ret =3D qcow2_inc_refcounts_imrt( + bs, res, refcount_table, refcount_table_size, + l2_entry & QCOW2_COMPRESSED_SECTOR_MASK, + nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE); if (ret < 0) { goto fail; } diff --git a/block/qcow2.c b/block/qcow2.c index 8e024007db..d39882785d 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -4187,7 +4187,8 @@ qcow2_co_preadv_compressed(BlockDriverState *bs, =20 coffset =3D file_cluster_offset & s->cluster_offset_mask; nb_csectors =3D ((file_cluster_offset >> s->csize_shift) & s->csize_ma= sk) + 1; - csize =3D nb_csectors * 512 - (coffset & 511); + csize =3D nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE - + (coffset & ~QCOW2_COMPRESSED_SECTOR_MASK); =20 buf =3D g_try_malloc(csize); if (!buf) { --=20 2.20.1