From nobody Sun Feb 8 22:43:03 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1593342320; cv=none; d=zohomail.com; s=zohoarc; b=jFpvBreoK2dLbzEPwQ5D1i4lzrdChBRopeoM8OfomnzZ13g5Xw+SmpMw9REOu7rcFlIVE/pJput9hQtXom0GLR1IscV2NTdO+LNlSLrtKs62UTEWSm4s1zhI/taYUtUQaKoKhtWu6Gzt64mrB+t/n44KSfDosKvt3u36rQUzizk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593342320; 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; bh=zhUI+SdcRuNKlLxl0eH0JWm96meGNa39l1+qfmyCMtQ=; b=fbSg5TxmPJJIsvBOyt9vkmYDYOoJzsQY7CMjDplXaWoEyl+Ql8YGthZGICf5l5hyw3G5i22ImbGe0c30Geuz422mgX0DL/FJ9rWjb/U/Qfm4g7pFD9bIfTnETWutlkTGt1A10N7ZfhKsSosF/GVOw3+UusjH5ZNcyKth22nQlb4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1593342319383625.6835814469148; Sun, 28 Jun 2020 04:05:19 -0700 (PDT) Received: from localhost ([::1]:45804 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jpV7e-0000uc-1K for importer@patchew.org; Sun, 28 Jun 2020 07:05:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47758) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jpV62-0006v3-KM; Sun, 28 Jun 2020 07:03:38 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:42243) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jpV5x-0000Wb-Ut; Sun, 28 Jun 2020 07:03:38 -0400 Received: from 26.red-79-158-236.dynamicip.rima-tde.net ([79.158.236.26] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1jpV5X-0002gN-Cc; Sun, 28 Jun 2020 13:03:07 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1jpV5H-00038D-H4; Sun, 28 Jun 2020 13:02:51 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=zhUI+SdcRuNKlLxl0eH0JWm96meGNa39l1+qfmyCMtQ=; b=RaAzkD7nNUYhv1iUDsw3stPCg4juLcrbuNRAmDsV5H0YxscReBBS2IvWelsBw33J9Ro0pooIhVf+YnEnQGn9nDLqEncoXqCcwUA8LegZUrzySeAh88QfG1a4GD8zKdA4eFFSPSzn794zq35XhkjytmMZGmDtmYNMkSdZGGws91MkOiaf6Fl82fpgnwouD3QEWHVGRdHi4FZ6eH/jUtniatxfIl9FRy4OcfXw+LOqyA/gYeSRTyZqwTJp8oO9jxHO/enRHA2ZfSp9rcOFy8WT+MH+koLjAnn1Sn6xllXttlLCYQ49RNcNS968UAm3cGzVDzpn1CG4EMS9fdrD6YD15w==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v9 03/34] qcow2: Add calculate_l2_meta() Date: Sun, 28 Jun 2020 13:02:12 +0200 Message-Id: <08a1848d974ee94f3074fd76132d30cd041d00f6.1593342067.git.berto@igalia.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: 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=178.60.130.6; envelope-from=berto@igalia.com; helo=fanzine.igalia.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/28 07:03:08 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , Alberto Garcia , qemu-block@nongnu.org, Derek Su , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" handle_alloc() creates a QCowL2Meta structure in order to update the image metadata and perform the necessary copy-on-write operations. This patch moves that code to a separate function so it can be used from other places. Signed-off-by: Alberto Garcia Reviewed-by: Max Reitz --- block/qcow2-cluster.c | 77 +++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 9ab41cb728..61ad638bdc 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -1037,6 +1037,56 @@ void qcow2_alloc_cluster_abort(BlockDriverState *bs,= QCowL2Meta *m) } } =20 +/* + * For a given write request, create a new QCowL2Meta structure, add + * it to @m and the BDRVQcow2State.cluster_allocs list. + * + * @host_cluster_offset points to the beginning of the first cluster. + * + * @guest_offset and @bytes indicate the offset and length of the + * request. + * + * If @keep_old is true it means that the clusters were already + * allocated and will be overwritten. If false then the clusters are + * new and we have to decrease the reference count of the old ones. + */ +static void calculate_l2_meta(BlockDriverState *bs, + uint64_t host_cluster_offset, + uint64_t guest_offset, unsigned bytes, + QCowL2Meta **m, bool keep_old) +{ + BDRVQcow2State *s =3D bs->opaque; + unsigned cow_start_from =3D 0; + unsigned cow_start_to =3D offset_into_cluster(s, guest_offset); + unsigned cow_end_from =3D cow_start_to + bytes; + unsigned cow_end_to =3D ROUND_UP(cow_end_from, s->cluster_size); + unsigned nb_clusters =3D size_to_clusters(s, cow_end_from); + QCowL2Meta *old_m =3D *m; + + *m =3D g_malloc0(sizeof(**m)); + **m =3D (QCowL2Meta) { + .next =3D old_m, + + .alloc_offset =3D host_cluster_offset, + .offset =3D start_of_cluster(s, guest_offset), + .nb_clusters =3D nb_clusters, + + .keep_old_clusters =3D keep_old, + + .cow_start =3D { + .offset =3D cow_start_from, + .nb_bytes =3D cow_start_to - cow_start_from, + }, + .cow_end =3D { + .offset =3D cow_end_from, + .nb_bytes =3D cow_end_to - cow_end_from, + }, + }; + + qemu_co_queue_init(&(*m)->dependent_requests); + QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight); +} + /* * Returns the number of contiguous clusters that can be used for an alloc= ating * write, but require COW to be performed (this includes yet unallocated s= pace, @@ -1435,35 +1485,14 @@ static int handle_alloc(BlockDriverState *bs, uint6= 4_t guest_offset, uint64_t requested_bytes =3D *bytes + offset_into_cluster(s, guest_off= set); int avail_bytes =3D nb_clusters << s->cluster_bits; int nb_bytes =3D MIN(requested_bytes, avail_bytes); - QCowL2Meta *old_m =3D *m; - - *m =3D g_malloc0(sizeof(**m)); - - **m =3D (QCowL2Meta) { - .next =3D old_m, - - .alloc_offset =3D alloc_cluster_offset, - .offset =3D start_of_cluster(s, guest_offset), - .nb_clusters =3D nb_clusters, - - .keep_old_clusters =3D keep_old_clusters, - - .cow_start =3D { - .offset =3D 0, - .nb_bytes =3D offset_into_cluster(s, guest_offset), - }, - .cow_end =3D { - .offset =3D nb_bytes, - .nb_bytes =3D avail_bytes - nb_bytes, - }, - }; - qemu_co_queue_init(&(*m)->dependent_requests); - QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight); =20 *host_offset =3D alloc_cluster_offset + offset_into_cluster(s, guest_o= ffset); *bytes =3D MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset)= ); assert(*bytes !=3D 0); =20 + calculate_l2_meta(bs, alloc_cluster_offset, guest_offset, *bytes, + m, keep_old_clusters); + return 1; =20 fail: --=20 2.20.1