From nobody Mon Nov 25 11:54:36 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1715582057; cv=none; d=zohomail.com; s=zohoarc; b=bw/aqRaA/gNo7yp7GzFd0/dME8zAEP0qCUycUfOT3sV9n1GIPTgejce2EOCNJZ6qojojA51+JkV4dZ1YDmcSwlrqWy9exarrXf49QVvT0bM5+T791kg63hT+TeC2GDU98uywa4mnzzmEpJvAPUslcxYpR3dC29+7BAY/jW/IR9o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1715582057; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cX31PH9jtDeIuw0XEzjsMlgVyqFrOzL4wkS6kSgnYr8=; b=lkwuK+FCP0UuufGo9fj8E4V7DNAmE0g2tatmsUV7UZe21QIbR3jnP0cO3Y3HbHReqPcVHQNBJsYHoVlbNJ34xo596XpFfN2bxRwzQz0V8bUO1RvkFKFb8v3TK0IpWe58Lch3MOoBedi8TDFhgrVNPH8krnPrKrYCGKPTrL/ILg0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1715582057647557.1344033074568; Sun, 12 May 2024 23:34:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s6PEo-0007iV-CC; Mon, 13 May 2024 02:32:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s6PEO-000799-51; Mon, 13 May 2024 02:32:16 -0400 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s6PEI-0003wY-N0; Mon, 13 May 2024 02:32:15 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1s6PAR-000qpR-2f; Mon, 13 May 2024 08:31:56 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=cX31PH9jtDeIuw0XEzjsMlgVyqFrOzL4wkS6kSgnYr8=; b=Pdc2PB/pWR/g MyVEAYLuocbuhkIMKii/M9GgacX80f7c/wJ0YPY1+xoIlKH4rm9kT1wzIMRsT5xEjOv1sIa1/xdb/ 9bpNhUOfJq1TZmMOxJPR33jkT9QauOmWGvuHkZbDFAmbUvgHOxv3EMy1jWssPsoDhilDDef3VXP8e TLDemKwyFHrEU/koJXF/B03I4r5atvZZjy1KFUe76xUbO9YA41uylzj5tBoFAwedxN0/dHWmeqxTo 9J+zzQr0ToLGjA0h7ap4Zs/vBjtsjU2Lfm2+JPV6bWz85p8z5ujQo5D2F3m59a1WmPEWn/qE4H872 l6ufpY8aOce9K0jNVMNbMg==; From: Andrey Drobyshev To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, hreitz@redhat.com, kwolf@redhat.com, eblake@redhat.com, berto@igalia.com, jean-louis@dupond.be, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v2 07/11] qcow2: add get_sc_range_info() helper for working with subcluster ranges Date: Mon, 13 May 2024 09:31:59 +0300 Message-Id: <20240513063203.113911-8-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240513063203.113911-1-andrey.drobyshev@virtuozzo.com> References: <20240513063203.113911-1-andrey.drobyshev@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=130.117.225.111; envelope-from=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1715582057908100004 Content-Type: text/plain; charset="utf-8" This helper simply obtains the l2 table parameters of the cluster which contains the given subclusters range. Right now this info is being obtained and used by zero_l2_subclusters(). As we're about to introduce the subclusters discard operation, this helper would let us avoid code duplication. Also introduce struct SubClusterRangeInfo, which would contain all the needed params. Signed-off-by: Andrey Drobyshev Reviewed-by: Alexander Ivanov --- block/qcow2-cluster.c | 140 ++++++++++++++++++++++++++++++++---------- 1 file changed, 108 insertions(+), 32 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 7dff0bd5a1..475f167035 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -1915,6 +1915,103 @@ discard_no_unref_any_file(BlockDriverState *bs, uin= t64_t offset, } } =20 +/* + * Structure containing info about the subclusters range within one cluste= r. + * + * Since @l2_slice is a strong reference to the l2 table slice containing + * the corresponding l2 entry, it must be explicitly released by + * qcow2_cache_put(). Thus the user must either declare it with g_auto() + * (in which case sc_range_info_cleanup() is called automatically) or do + * the cleanup themselves. + */ +typedef struct SubClusterRangeInfo { + uint64_t *l2_slice; + int l2_index; + uint64_t l2_entry; + uint64_t l2_bitmap; + QCow2ClusterType ctype; + Qcow2Cache *l2_table_cache; +} SubClusterRangeInfo; + +static void sc_range_info_cleanup(SubClusterRangeInfo *scri) +{ + if (scri->l2_table_cache && scri->l2_slice) { + qcow2_cache_put(scri->l2_table_cache, (void **) &scri->l2_slice); + } +} + +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(SubClusterRangeInfo, sc_range_info_cleanu= p); + +/* + * For a given @offset and @nb_subclusters, fill out the SubClusterRangeIn= fo + * structure describing the subclusters range and referred to by @scri. + * Only the subclusters which can be independently discarded/zeroized + * (i.e. not compressed or invalid) are considered to be valid here. + * + * The subclusters range is denoted by @offset and @nb_subclusters and must + * not cross the cluster boundary. @offset must be aligned to the subclus= ter + * size. + * + * Return: 0 if the SubClusterRangeInfo is successfully filled out and the + * subclusters within the given range might be discarded/zeroized; + * -EINVAL if any of the subclusters within the range is invalid; + * -ENOTSUP if the range is contained within a compressed cluster. + */ +static int GRAPH_RDLOCK +get_sc_range_info(BlockDriverState *bs, uint64_t offset, + unsigned nb_subclusters, SubClusterRangeInfo *scri) +{ + BDRVQcow2State *s =3D bs->opaque; + int ret, sc_cleared, sc_index =3D offset_to_sc_index(s, offset); + QCow2SubclusterType sctype; + + /* Here we only work with the subclusters within single cluster. */ + assert(nb_subclusters > 0 && nb_subclusters < s->subclusters_per_clust= er); + assert(sc_index + nb_subclusters <=3D s->subclusters_per_cluster); + assert(offset_into_subcluster(s, offset) =3D=3D 0); + + scri->l2_table_cache =3D s->l2_table_cache; + + ret =3D get_cluster_table(bs, offset, &scri->l2_slice, &scri->l2_index= ); + if (ret < 0) { + goto cleanup; + } + + scri->l2_entry =3D get_l2_entry(s, scri->l2_slice, scri->l2_index); + scri->l2_bitmap =3D get_l2_bitmap(s, scri->l2_slice, scri->l2_index); + scri->ctype =3D qcow2_get_cluster_type(bs, scri->l2_entry); + + sc_cleared =3D 0; + do { + ret =3D qcow2_get_subcluster_range_type( + bs, scri->l2_entry, scri->l2_bitmap, sc_index + sc_cleared, + &sctype); + if (ret < 0) { + goto cleanup; + } + + switch (sctype) { + case QCOW2_SUBCLUSTER_COMPRESSED: + /* We cannot partially zeroize/discard compressed clusters. */ + ret =3D -ENOTSUP; + goto cleanup; + case QCOW2_SUBCLUSTER_INVALID: + ret =3D -EINVAL; + goto cleanup; + default: + break; + } + + sc_cleared +=3D ret; + } while (sc_cleared < nb_subclusters); + + return 0; + +cleanup: + sc_range_info_cleanup(scri); + return ret; +} + /* * This discards as many clusters of nb_clusters as possible at once (i.e. * all clusters in the same L2 slice) and returns the number of discarded @@ -2127,46 +2224,25 @@ zero_l2_subclusters(BlockDriverState *bs, uint64_t = offset, unsigned nb_subclusters) { BDRVQcow2State *s =3D bs->opaque; - uint64_t *l2_slice; - uint64_t old_l2_bitmap, l2_bitmap; - int l2_index, ret, sc =3D offset_to_sc_index(s, offset); - - /* For full clusters use zero_in_l2_slice() instead */ - assert(nb_subclusters > 0 && nb_subclusters < s->subclusters_per_clust= er); - assert(sc + nb_subclusters <=3D s->subclusters_per_cluster); - assert(offset_into_subcluster(s, offset) =3D=3D 0); + uint64_t new_l2_bitmap; + int ret, sc =3D offset_to_sc_index(s, offset); + g_auto(SubClusterRangeInfo) scri =3D { 0 }; =20 - ret =3D get_cluster_table(bs, offset, &l2_slice, &l2_index); + ret =3D get_sc_range_info(bs, offset, nb_subclusters, &scri); if (ret < 0) { return ret; } =20 - switch (qcow2_get_cluster_type(bs, get_l2_entry(s, l2_slice, l2_index)= )) { - case QCOW2_CLUSTER_COMPRESSED: - ret =3D -ENOTSUP; /* We cannot partially zeroize compressed cluste= rs */ - goto out; - case QCOW2_CLUSTER_NORMAL: - case QCOW2_CLUSTER_UNALLOCATED: - break; - default: - g_assert_not_reached(); - } - - old_l2_bitmap =3D l2_bitmap =3D get_l2_bitmap(s, l2_slice, l2_index); - - l2_bitmap |=3D QCOW_OFLAG_SUB_ZERO_RANGE(sc, sc + nb_subclusters); - l2_bitmap &=3D ~QCOW_OFLAG_SUB_ALLOC_RANGE(sc, sc + nb_subclusters); + new_l2_bitmap =3D scri.l2_bitmap; + new_l2_bitmap |=3D QCOW_OFLAG_SUB_ZERO_RANGE(sc, sc + nb_subclusters); + new_l2_bitmap &=3D ~QCOW_OFLAG_SUB_ALLOC_RANGE(sc, sc + nb_subclusters= ); =20 - if (old_l2_bitmap !=3D l2_bitmap) { - set_l2_bitmap(s, l2_slice, l2_index, l2_bitmap); - qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice); + if (new_l2_bitmap !=3D scri.l2_bitmap) { + set_l2_bitmap(s, scri.l2_slice, scri.l2_index, new_l2_bitmap); + qcow2_cache_entry_mark_dirty(s->l2_table_cache, scri.l2_slice); } =20 - ret =3D 0; -out: - qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice); - - return ret; + return 0; } =20 int coroutine_fn qcow2_subcluster_zeroize(BlockDriverState *bs, uint64_t o= ffset, --=20 2.39.3