From nobody Fri Nov 14 16:59:39 2025 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=1588700464; cv=none; d=zohomail.com; s=zohoarc; b=Czzr5/kD9FyPNGV9Hejn0r/5aSGQSun7EX8YoWQ42mm3hUS7i6jRI2nUZoI+ihZXR/PVjdaoFNDCXt/PqDevoIcDTmbUlclsLb/Z+impvSV6Q7saJV8IxIThN5JWebTIW3skTXO2d+V4weqe7sK+EdPmLu4g5jtxLQB/Jju9atM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1588700464; 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=L1ObEPw7stN5DLgFQxEeefkoPMnXSN1fNO9+u/YWUFbD1kak2o45RRFahMUxTatcEVjdS7A6RndzeCsUiNYHNXkFQ+3h1lcY36sBgItT1/qxHyLnsKjwZwvKBUxeBgdyZyfSipebxdVSthnAMDmfgTMlEB1t3IFTYXVDCFUHLbA= 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 1588700464507616.4579021431482; Tue, 5 May 2020 10:41:04 -0700 (PDT) Received: from localhost ([::1]:38222 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jW1Z1-0008U3-90 for importer@patchew.org; Tue, 05 May 2020 13:41:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44000) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jW1XM-0006Gh-1p; Tue, 05 May 2020 13:39:20 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:38783) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jW1XJ-00089R-7b; Tue, 05 May 2020 13:39:19 -0400 Received: from static.160.43.0.81.ibercom.com ([81.0.43.160] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1jW1Ws-00025N-86; Tue, 05 May 2020 19:38:50 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1jW1Wc-00043c-GJ; Tue, 05 May 2020 19:38:34 +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=sMFcPUPCMOyUtQYy+S2TbLttTZg+auV9i7lze306tcC4Ahv5HnyUrcGxoM0saQw5+hQ+Atmy6rEkekf9B5eEcQWHSkG7i3HhGsVo0VA49OSdjcE8LmD8pF948pg/L6J7LGiqQec0cScmuAwZ9cNgPKzi6T/+LMajgKJX5gEUdhAVMBXixeMQUayt7HDmIVESF2aBZWT/9Hiue5RV46DUOApVJhkfFOsQQUc4WaWOq02UB2bAMnl22HZJBdCYQMImz9RQcXkEqNvan1W3DLdWi+YaSGFf1uYWnQVpTBC9/jdyf2rudxGmRjKsJvmED+n/new3xvbOlMXOWX6hM9/Bsg==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v5 03/31] qcow2: Add calculate_l2_meta() Date: Tue, 5 May 2020 19:38:03 +0200 Message-Id: <0a4e60dc71a2b1569501e2afa0113873509aa386.1588699789.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/05/05 13:38:50 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_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, 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