From nobody Sun Feb 8 22:00:28 2026 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; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1571153221; cv=none; d=zoho.com; s=zohoarc; b=IuAXeduKG98MJFVHdb3x631f7foO3A1/iAjnie35/G146/SDbCAIw+aY2czyvN1uFIpqxQ5P/7Q1bTDznbt/ccvKG+Ud8jre5HkgUe6uX4zHXq/jMrZLm0ZxYK1DEHwxB0Meer4q+juUPlJMZoC5P5BoFZXF8LL2w9aNPa8qBNc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571153221; 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=8hTqzhXCwSm+pnn+u/HHCmoHotoRRBHUylfmlt5TL7Y=; b=VYq5ATVh+qO/C5CIABjJIdCj5lKCftCA5PVNFgo0i5tkRzBDB/obRZm2UrSUGFWcyfv6lBwl5OVKs5lwGxQ/utKc6QDLPjAI9myHZZWsIT98ITBbTbqYWqCVt1kC5u9pmyHamhFBlyu8InjDaKH1XYpfl1Q3h/hH9e1iLg6IY4g= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571153220460297.03782369283954; Tue, 15 Oct 2019 08:27:00 -0700 (PDT) Received: from localhost ([::1]:48692 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iKOit-00057s-QY for importer@patchew.org; Tue, 15 Oct 2019 11:26:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48082) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iKOgy-0002ku-7a for qemu-devel@nongnu.org; Tue, 15 Oct 2019 11:24:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iKOgw-0003fX-R2 for qemu-devel@nongnu.org; Tue, 15 Oct 2019 11:24:56 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:47099) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iKOgw-0003Z7-Ct; Tue, 15 Oct 2019 11:24:54 -0400 Received: from 82-181-115-92.bb.dnainternet.fi ([82.181.115.92] helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1iKOgd-0003aN-W9; Tue, 15 Oct 2019 17:24:36 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1iKOfm-000615-GQ; Tue, 15 Oct 2019 18:23:42 +0300 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=8hTqzhXCwSm+pnn+u/HHCmoHotoRRBHUylfmlt5TL7Y=; b=B0OEIY5DHZpNmhFoZ8Q68Z5+5FnefWRcaPktAFNDPOFJE+7CH4n6pdL42MJactzJgZn0q8mfR1Kabe8/fJBDEpdCYOW62yJCybHVIkKVh0Ev+JNdymVn4ZZ6VUiaQ+dt1bTpZp/c48ZuyizfDh+0BtF+hx3yk9utQjsWbRmPTxTbTf2/L0nyUvAN0sQH+IUmMvoHLTVdTbHta5H0ouZ5+WE9NvX2BOGq08NxuvR40DlmcJ1SfNs2jPo7D5gDQDtnuzJFrB1zhSb/TDncKyBtr1Is6YeuDI0MaBR5YyZiwrcIfluo60SyJRETTj2rfWKnUHRtTixxBrLOOe+8bR/1fQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [RFC PATCH 01/23] qcow2: Add calculate_l2_meta() Date: Tue, 15 Oct 2019 18:23:12 +0300 Message-Id: <9a83358cfa0bf36b6fd7d6bb207511edee0260bc.1571152571.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 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 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 , Anton Nefedov , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , "Denis V . Lunev" 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 --- block/qcow2-cluster.c | 76 +++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 8d5fa1539c..fe2523ed66 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -1019,6 +1019,55 @@ void qcow2_alloc_cluster_abort(BlockDriverState *bs,= QCowL2Meta *m) QCOW2_DISCARD_NEVER); } =20 +/* + * For a given write request, create a new QCowL2Meta structure and + * add it to @m. + * + * @host_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_offset, + uint64_t guest_offset, uint64_t 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_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, @@ -1414,35 +1463,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 MIN(INT_MAX, 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