From nobody Mon Feb 9 03:14:07 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 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=1611590757; cv=none; d=zohomail.com; s=zohoarc; b=TXB6thpnIt7pmXLa60BYVFApeyO/zM6GUyg2G/wU9PlttiNQRrIrFiHGwmq4a7yVJ8J/oSa9T9dGBuaL30nMPhqDjDSBasD/ySBXaiQ52iK/avpfSln4DnNQrRzONWHHiY9gtXxkgKmbdPB8hHlAK64Wy9fsZPqys0D9F0cnF9A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611590757; 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=7ZD1JFcAKSF51TnonKxKdcFAYukgCUGdDBfaR1zJop8=; b=nEa0OTgPYB1OHIk7Mm/XV6k9U3kp4jD1i4aAOd0oqrBWkdgs+URpSWxXpibqy8ff0RxU/R/HJSzYAyiy5h/rQTdIr4+/YZtOuKnfEVxbO9eQtaoLWkSAEoHjhJPkHLulitsJ7RSqubjxPrKtO7MywjuKp5RnBDnTjVW8KYqMnxE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 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-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1611590757450481.65896065733875; Mon, 25 Jan 2021 08:05:57 -0800 (PST) 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-142-4u6BovnnMTacFsgOnWyFdg-1; Mon, 25 Jan 2021 11:05:51 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 22FC1AFA81; Mon, 25 Jan 2021 16:05:45 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F32655D72F; Mon, 25 Jan 2021 16:05:44 +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 BEC504EBC6; Mon, 25 Jan 2021 16:05:44 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 10PG5fGb018303 for ; Mon, 25 Jan 2021 11:05:41 -0500 Received: by smtp.corp.redhat.com (Postfix) id CA0BC5D9DE; Mon, 25 Jan 2021 16:05:41 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1684E5D9DB for ; Mon, 25 Jan 2021 16:05:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611590755; 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=7ZD1JFcAKSF51TnonKxKdcFAYukgCUGdDBfaR1zJop8=; b=Xu0NP2pE9/PsvhwqFokerVTXipH3ZLaV9QTYvY6zz22cItPTSmOd77CbvO+MPXyLMJQJH/ RIlEIPX2ZDdr1TxYcYK/8kXUewNYLGW1bniF/II17iX7Lx8vDZpG25kzq8wTxPemaCWRXY wDA4jDV/KIEXGGH3XOx1W31oD7WNP0w= X-MC-Unique: 4u6BovnnMTacFsgOnWyFdg-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 10/12] virStorageSourceChainLookup: Handle names like 'vda[4]' internally Date: Mon, 25 Jan 2021 17:05:22 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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.79 on 10.5.11.15 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" All callers of this function called virStorageFileParseChainIndex before. Internalize the logic of that function to prevent multiple calls and passing around unnecessary temporary variables. This is achieved by calling virStorageFileParseBackingStoreStr and using it to fill the values internally. Signed-off-by: Peter Krempa --- src/qemu/qemu_driver.c | 19 ++++-------- src/storage_file/storage_source.c | 49 ++++++++++++++++++++++++------- src/storage_file/storage_source.h | 2 +- tests/virstoragetest.c | 2 +- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index ed966cf7e3..b3e663c9d5 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -14412,7 +14412,6 @@ qemuDomainBlockPullCommon(virDomainObjPtr vm, const char *jobname =3D NULL; virDomainDiskDefPtr disk; virStorageSourcePtr baseSource =3D NULL; - unsigned int baseIndex =3D 0; g_autofree char *basePath =3D NULL; g_autofree char *backingPath =3D NULL; unsigned long long speed =3D bandwidth; @@ -14448,9 +14447,8 @@ qemuDomainBlockPullCommon(virDomainObjPtr vm, goto endjob; if (base && - (virStorageFileParseChainIndex(disk->dst, base, &baseIndex) < 0 || - !(baseSource =3D virStorageSourceChainLookup(disk->src, disk->src, - base, baseIndex, NULL)= ))) + !(baseSource =3D virStorageSourceChainLookup(disk->src, disk->src, + base, disk->dst, NULL))) goto endjob; if (baseSource) { @@ -15465,9 +15463,7 @@ qemuDomainBlockCommit(virDomainPtr dom, int ret =3D -1; virDomainDiskDefPtr disk =3D NULL; virStorageSourcePtr topSource; - unsigned int topIndex =3D 0; virStorageSourcePtr baseSource =3D NULL; - unsigned int baseIndex =3D 0; virStorageSourcePtr top_parent =3D NULL; bool clean_access =3D false; g_autofree char *topPath =3D NULL; @@ -15540,10 +15536,8 @@ qemuDomainBlockCommit(virDomainPtr dom, if (!top || STREQ(top, disk->dst)) topSource =3D disk->src; - else if (virStorageFileParseChainIndex(disk->dst, top, &topIndex) < 0 = || - !(topSource =3D virStorageSourceChainLookup(disk->src, NULL, - top, topIndex, - &top_parent))) + else if (!(topSource =3D virStorageSourceChainLookup(disk->src, NULL, = top, + disk->dst, &top_par= ent))) goto endjob; if (topSource =3D=3D disk->src) { @@ -15575,9 +15569,8 @@ qemuDomainBlockCommit(virDomainPtr dom, if (!base && (flags & VIR_DOMAIN_BLOCK_COMMIT_SHALLOW)) baseSource =3D topSource->backingStore; - else if (virStorageFileParseChainIndex(disk->dst, base, &baseIndex) < = 0 || - !(baseSource =3D virStorageSourceChainLookup(disk->src, topSo= urce, - base, baseIndex, N= ULL))) + else if (!(baseSource =3D virStorageSourceChainLookup(disk->src, topSo= urce, + base, disk->dst, N= ULL))) goto endjob; if ((flags & VIR_DOMAIN_BLOCK_COMMIT_SHALLOW) && diff --git a/src/storage_file/storage_source.c b/src/storage_file/storage_s= ource.c index 52edb91112..4b46cc4e84 100644 --- a/src/storage_file/storage_source.c +++ b/src/storage_file/storage_source.c @@ -198,32 +198,59 @@ virStorageSourceGetMetadataFromFD(const char *path, } -/* Given a @chain, look for the backing store @name that is a backing file - * of @startFrom (or any member of @chain if @startFrom is NULL) and return - * that location within the chain. @chain must always point to the top of - * the chain. Pass NULL for @name and 0 for @idx to find the base of the - * chain. Pass nonzero @idx to find the backing source according to its - * position in the backing chain. If @parent is not NULL, set *@parent to - * the preferred name of the parent (or to NULL if @name matches the start - * of the chain). Since the results point within @chain, they must not be - * independently freed. Reports an error and returns NULL if @name is not - * found. +/** + * virStorageSourceChainLookup: + * @chain: chain top to look in + * @startFrom: move the starting point of @chain if non-NULL + * @name: name of the file to look for in @chain + * @diskTarget: optional disk target to validate against + * @parent: Filled with parent virStorageSource of the returned value if n= on-NULL. + * + * Looks up a storage source definition corresponding to @name in @chain a= nd + * returns the corresponding virStorageSource. If @startFrom is non-NULL, = the + * lookup starts from that virStorageSource. + * + * @name can be: + * - NULL: the end of the source chain is returned + * - "vda[4]": Storage source with 'id' =3D=3D 4 is returned. If @diskTar= get is + * non-NULL it's also validated that the part before the squa= re + * bracket matches the requested target + * - "/path/to/file": Literal path is matched. Symlink resolution is atte= mpted + * if the filename doesn't string-match with the path. */ virStorageSourcePtr virStorageSourceChainLookup(virStorageSourcePtr chain, virStorageSourcePtr startFrom, const char *name, - unsigned int idx, + const char *diskTarget, virStorageSourcePtr *parent) { virStorageSourcePtr prev; const char *start =3D chain->path; bool nameIsFile =3D virStorageSourceBackinStoreStringIsFile(name); + g_autofree char *target =3D NULL; + unsigned int idx =3D 0; + + if (diskTarget) + start =3D diskTarget; if (!parent) parent =3D &prev; *parent =3D NULL; + /* parse the "vda[4]" type string */ + if (name && + virStorageFileParseBackingStoreStr(name, &target, &idx) =3D=3D 0) { + if (diskTarget && + idx !=3D 0 && + STRNEQ(diskTarget, target)) { + virReportError(VIR_ERR_INVALID_ARG, + _("requested target '%s' does not match target = '%s'"), + target, diskTarget); + return NULL; + } + } + if (startFrom) { while (virStorageSourceIsBacking(chain) && chain !=3D startFrom->backingStore) diff --git a/src/storage_file/storage_source.h b/src/storage_file/storage_s= ource.h index 58f88a3c01..6eb795b301 100644 --- a/src/storage_file/storage_source.h +++ b/src/storage_file/storage_source.h @@ -43,7 +43,7 @@ virStorageSourcePtr virStorageSourceChainLookup(virStorageSourcePtr chain, virStorageSourcePtr startFrom, const char *name, - unsigned int idx, + const char *diskTarget, virStorageSourcePtr *parent) ATTRIBUTE_NONNULL(1); diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index 99007dd662..c976a1c0d0 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -366,7 +366,7 @@ testStorageLookup(const void *args) } result =3D virStorageSourceChainLookup(data->chain, data->from, - data->name, idx, &actualParent); + data->name, data->target, &actual= Parent); if (!data->expResult) virResetLastError(); --=20 2.29.2