From nobody Mon Nov 25 17:31:38 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600868224; cv=none; d=zohomail.com; s=zohoarc; b=PRETV505s20f64ByRQsKAFckp2FKzl8MymdcewmNMGIbHhy0tRMFUts6s6avnEZ0lJNkEa+ti8biXQOwnZweGWTm1Q/8O0bfL77GeFZDnwFYMcH2oSByBRMQdTqkNMtciDMluQB3MRYOYtwEjMhtYFOo0yU0h9przqC6AuoDZ/A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600868224; h=Content-Type:Content-Transfer-Encoding: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=MJNSfvpHuFIOCe7JtVnBLRCyllPWH05OKRo14D5TUUo=; b=NXt6DZT3XibbXu9RB8PiHHR6P7JfZJpThUSaIyn/uhIRVA/xRQ0l0+Q59NKCVUr6ChaBP1rhlbNZUBhrP3cfNLcnBajqRKvpkdAMlo04PaH2cQZgb1PIVjBtzRFj+Ghqo7gfZigAdooiZfCVi8rSP0NB/aHCGupyRUnKaOO2s7E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 16008682246801010.5464770529754; Wed, 23 Sep 2020 06:37:04 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-358-JjvTOsPGNI-SUdaEVbereg-1; Wed, 23 Sep 2020 09:34:14 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 26F5318BA298; Wed, 23 Sep 2020 13:34:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0145610021AA; Wed, 23 Sep 2020 13:34:08 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B881B183D04D; Wed, 23 Sep 2020 13:34:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08NDY3nN013962 for ; Wed, 23 Sep 2020 09:34:03 -0400 Received: by smtp.corp.redhat.com (Postfix) id 48A9310021AA; Wed, 23 Sep 2020 13:34:03 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F00D100238E for ; Wed, 23 Sep 2020 13:34:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600868223; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=MJNSfvpHuFIOCe7JtVnBLRCyllPWH05OKRo14D5TUUo=; b=Rxpa7g3yYJcsNVjwJm3LYUdz0urfwlkvG/aTifiMRALIM2wsAKHtWQ9mRQelJYSJ2xSR0F IXTKgNxDVdptXXX1HQRjoCzXvF1Z2wsh04Um6j8zg6FWH5srIYXe1eX/Wg0QcZI7PsUaNO AWyg7RC0feTUJ8ekNgRNtpiuVsnWa8U= X-MC-Unique: JjvTOsPGNI-SUdaEVbereg-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 12/13] virDomainSnapshotAlignDisks: refactor extension to all disks Date: Wed, 23 Sep 2020 15:33:43 +0200 Message-Id: <05eff9e35655a36dcc32739dbe8ca9dadc92d7b8.1600867955.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Last step of the algorithm in virDomainSnapshotAlignDisks is to extend the array of disks to all VM's disk and provide defaults. This was done by extending the array, adding defaults at the end and then sorting it. This requires the 'idx' variable and also a separate sorting function. If we store the pointer to existing snapshot disk definitions in a hash table and create a new array of snapshot disk definitions, we can fill the new array directly by either copying the definition from the old array or adding the default. This avoids the sorting step and thus even the need to store the index of the domain disk altogether. Signed-off-by: Peter Krempa Reviewed-by: Eric Blake Reviewed-by: J=C3=A1n Tomko --- src/conf/snapshot_conf.c | 48 +++++++++++++++------------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c index f6a827d2ff..7090e3a1b0 100644 --- a/src/conf/snapshot_conf.c +++ b/src/conf/snapshot_conf.c @@ -627,16 +627,6 @@ virDomainSnapshotDefAssignExternalNames(virDomainSnaps= hotDefPtr def) } -static int -virDomainSnapshotCompareDiskIndex(const void *a, const void *b) -{ - const virDomainSnapshotDiskDef *diska =3D a; - const virDomainSnapshotDiskDef *diskb =3D b; - - /* Integer overflow shouldn't be a problem here. */ - return diska->idx - diskb->idx; -} - /* Align def->disks to def->parent.dom. Sort the list of def->disks, * filling in any missing disks or snapshot state defaults given by * the domain, with a fallback to a passed in default. Convert paths @@ -650,9 +640,9 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr sna= pdef, bool require_match) { virDomainDefPtr domdef =3D snapdef->parent.dom; - g_autoptr(virBitmap) map =3D NULL; + g_autoptr(virHashTable) map =3D virHashNew(NULL); + g_autofree virDomainSnapshotDiskDefPtr olddisks =3D NULL; size_t i; - int ndisks; if (!domdef) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -670,9 +660,6 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr sna= pdef, if (!domdef->ndisks) return 0; - if (!(map =3D virBitmapNew(domdef->ndisks))) - return -1; - /* Double check requested disks. */ for (i =3D 0; i < snapdef->ndisks; i++) { virDomainSnapshotDiskDefPtr snapdisk =3D &snapdef->disks[i]; @@ -687,14 +674,15 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr s= napdef, domdisk =3D domdef->disks[idx]; - if (virBitmapIsBitSet(map, idx)) { + if (virHashHasEntry(map, domdisk->dst)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("disk '%s' specified twice"), snapdisk->name); return -1; } - ignore_value(virBitmapSetBit(map, idx)); - snapdisk->idx =3D idx; + + if (virHashAddEntry(map, domdisk->dst, snapdisk) < 0) + return -1; if (snapdisk->snapshot =3D=3D VIR_DOMAIN_SNAPSHOT_LOCATION_DEFAULT= ) { if (domdisk->snapshot !=3D VIR_DOMAIN_SNAPSHOT_LOCATION_DEFAUL= T && @@ -729,21 +717,24 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr s= napdef, } } - /* Provide defaults for all remaining disks. */ - ndisks =3D snapdef->ndisks; - if (VIR_EXPAND_N(snapdef->disks, snapdef->ndisks, - domdef->ndisks - snapdef->ndisks) < 0) - return -1; + olddisks =3D g_steal_pointer(&snapdef->disks); + snapdef->disks =3D g_new0(virDomainSnapshotDiskDef, domdef->ndisks); + snapdef->ndisks =3D domdef->ndisks; for (i =3D 0; i < domdef->ndisks; i++) { - virDomainSnapshotDiskDefPtr snapdisk; + virDomainDiskDefPtr domdisk =3D domdef->disks[i]; + virDomainSnapshotDiskDefPtr snapdisk =3D snapdef->disks + i; + virDomainSnapshotDiskDefPtr existing; - if (virBitmapIsBitSet(map, i)) + /* copy existing disks */ + if ((existing =3D virHashLookup(map, domdisk->dst))) { + memcpy(snapdisk, existing, sizeof(*snapdisk)); continue; - snapdisk =3D &snapdef->disks[ndisks++]; + } + + /* Provide defaults for all remaining disks. */ snapdisk->src =3D virStorageSourceNew(); snapdisk->name =3D g_strdup(domdef->disks[i]->dst); - snapdisk->idx =3D i; /* Don't snapshot empty drives */ if (virStorageSourceIsEmpty(domdef->disks[i]->src)) @@ -756,9 +747,6 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr sna= pdef, snapdisk->snapshot =3D default_snapshot; } - qsort(&snapdef->disks[0], snapdef->ndisks, sizeof(snapdef->disks[0]), - virDomainSnapshotCompareDiskIndex); - /* Generate default external file names for external snapshot location= s */ if (virDomainSnapshotDefAssignExternalNames(snapdef) < 0) return -1; --=20 2.26.2