From nobody Wed Dec 17 04:03:06 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1507836331600114.62869135510766; Thu, 12 Oct 2017 12:25:31 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 978CB7D0C2; Thu, 12 Oct 2017 19:25:30 +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 79B4B17AD4; Thu, 12 Oct 2017 19:25:30 +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 2FBB918355D9; Thu, 12 Oct 2017 19:25:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v9CJ9OXd001857 for ; Thu, 12 Oct 2017 15:09:24 -0400 Received: by smtp.corp.redhat.com (Postfix) id 91A5F5EDF5; Thu, 12 Oct 2017 19:09:24 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 226655EDF2; Thu, 12 Oct 2017 19:09:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 978CB7D0C2 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 12 Oct 2017 21:07:39 +0200 Message-Id: <9b9d29a70a98c40c0f8d62c09aaa4581bd4c6f85.1507835192.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 8/9] util: storagefile: Add helpers to check presence of backing store 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 12 Oct 2017 19:25:31 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add a helper that will retrun true if a virStorageSource has backing store. This will also aid in refactoring of the code further down. Reviewed-by: Eric Blake --- src/conf/domain_conf.c | 2 +- src/conf/storage_conf.c | 5 +++-- src/libvirt_private.syms | 2 ++ src/qemu/qemu_block.c | 4 ++-- src/qemu/qemu_cgroup.c | 4 ++-- src/qemu/qemu_domain.c | 6 ++--- src/qemu/qemu_driver.c | 9 ++++---- src/security/security_dac.c | 2 +- src/security/security_selinux.c | 4 ++-- src/security/virt-aa-helper.c | 2 +- src/storage/storage_backend_logical.c | 2 +- src/storage/storage_source.c | 2 +- src/storage/storage_util.c | 14 ++++++------ src/util/virstoragefile.c | 42 +++++++++++++++++++++++++++++--= ---- src/util/virstoragefile.h | 6 +++++ tests/virstoragetest.c | 2 +- 16 files changed, 73 insertions(+), 35 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 45fa57a14..3eb6c7f6f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -26607,7 +26607,7 @@ virDomainDiskDefForeachPath(virDomainDiskDefPtr dis= k, } } - for (tmp =3D disk->src; tmp; tmp =3D tmp->backingStore) { + for (tmp =3D disk->src; virStorageSourceIsBacking(tmp); tmp =3D tmp->b= ackingStore) { /* execute the callback only for local storage */ if (virStorageSourceIsLocalStorage(tmp) && tmp->path) { diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c index 7c373e781..f808cd291 100644 --- a/src/conf/storage_conf.c +++ b/src/conf/storage_conf.c @@ -1169,7 +1169,8 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool, if (virStorageSize(unit, capacity, &ret->target.capacity) < 0) goto error; } else if (!(flags & VIR_VOL_XML_PARSE_NO_CAPACITY) && - !((flags & VIR_VOL_XML_PARSE_OPT_CAPACITY) && ret->target.b= ackingStore)) { + !((flags & VIR_VOL_XML_PARSE_OPT_CAPACITY) && + virStorageSourceHasBacking(&ret->target))) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing capacity elemen= t")); goto error; } @@ -1497,7 +1498,7 @@ virStorageVolDefFormat(virStoragePoolDefPtr pool, &def->target, "target") < 0) goto cleanup; - if (def->target.backingStore && + if (virStorageSourceHasBacking(&def->target) && virStorageVolTargetDefFormat(options, &buf, def->target.backingStore, "backingStore") < 0) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 26c5ddb40..4d44c401b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2692,7 +2692,9 @@ virStorageSourceFindByNodeName; virStorageSourceFree; virStorageSourceGetActualType; virStorageSourceGetSecurityLabelDef; +virStorageSourceHasBacking; virStorageSourceInitChainElement; +virStorageSourceIsBacking; virStorageSourceIsBlockLocal; virStorageSourceIsEmpty; virStorageSourceIsLocalStorage; diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index 8d232de3e..544b4893b 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -257,7 +257,7 @@ qemuBlockDiskClearDetectedNodes(virDomainDiskDefPtr dis= k) { virStorageSourcePtr next =3D disk->src; - while (next) { + while (virStorageSourceIsBacking(next)) { VIR_FREE(next->nodeformat); VIR_FREE(next->nodestorage); @@ -287,7 +287,7 @@ qemuBlockDiskDetectNodes(virDomainDiskDefPtr disk, goto cleanup; } - while (src && entry) { + while (virStorageSourceIsBacking(src) && entry) { if (src->nodeformat || src->nodestorage) { if (STRNEQ_NULLABLE(src->nodeformat, entry->nodeformat) || STRNEQ_NULLABLE(src->nodestorage, entry->nodestorage)) diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index 6fc413098..0f75e22f9 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -142,7 +142,7 @@ qemuSetupDiskCgroup(virDomainObjPtr vm, virStorageSourcePtr next; bool forceReadonly =3D false; - for (next =3D disk->src; next; next =3D next->backingStore) { + for (next =3D disk->src; virStorageSourceIsBacking(next); next =3D nex= t->backingStore) { if (qemuSetupImageCgroupInternal(vm, next, forceReadonly) < 0) return -1; @@ -160,7 +160,7 @@ qemuTeardownDiskCgroup(virDomainObjPtr vm, { virStorageSourcePtr next; - for (next =3D disk->src; next; next =3D next->backingStore) { + for (next =3D disk->src; virStorageSourceIsBacking(next); next =3D nex= t->backingStore) { if (qemuTeardownImageCgroup(vm, next) < 0) return -1; } diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index ed27a91fa..173fbca6e 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -5929,7 +5929,7 @@ qemuDomainDetermineDiskChain(virQEMUDriverPtr driver, if (virStorageSourceIsEmpty(disk->src)) goto cleanup; - if (disk->src->backingStore) { + if (virStorageSourceHasBacking(disk->src)) { if (force_probe) virStorageSourceBackingStoreClear(disk->src); else @@ -8548,7 +8548,7 @@ qemuDomainSetupDisk(virQEMUDriverConfigPtr cfg ATTRIB= UTE_UNUSED, char *dst =3D NULL; int ret =3D -1; - for (next =3D disk->src; next; next =3D next->backingStore) { + for (next =3D disk->src; virStorageSourceIsBacking(next); next =3D nex= t->backingStore) { if (!next->path || !virStorageSourceIsLocalStorage(next)) { /* Not creating device. Just continue. */ continue; @@ -9449,7 +9449,7 @@ qemuDomainNamespaceSetupDisk(virQEMUDriverPtr driver, &ndevMountsPath) < 0) goto cleanup; - for (next =3D src; next; next =3D next->backingStore) { + for (next =3D src; virStorageSourceIsBacking(next); next =3D next->bac= kingStore) { if (virStorageSourceIsEmpty(next) || !virStorageSourceIsLocalStorage(next)) { /* Not creating device. Just continue. */ diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 0260c40ef..803df9f06 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -14448,7 +14448,7 @@ qemuDomainSnapshotUpdateDiskSourcesRenumber(virStor= ageSourcePtr src) virStorageSourcePtr next; unsigned int idx =3D 1; - for (next =3D src->backingStore; next; next =3D next->backingStore) + for (next =3D src->backingStore; virStorageSourceIsBacking(next); next= =3D next->backingStore) next->id =3D idx++; } @@ -17048,7 +17048,7 @@ qemuDomainBlockCopyCommon(virDomainObjPtr vm, } /* clear the _SHALLOW flag if there is only one layer */ - if (!disk->src->backingStore) + if (!virStorageSourceHasBacking(disk->src)) flags &=3D ~VIR_DOMAIN_BLOCK_COPY_SHALLOW; /* unless the user provides a pre-created file, shallow copy into a raw @@ -17439,7 +17439,7 @@ qemuDomainBlockCommit(virDomainPtr dom, goto endjob; } - if (!topSource->backingStore) { + if (!virStorageSourceHasBacking(topSource)) { virReportError(VIR_ERR_INVALID_ARG, _("top '%s' in chain for '%s' has no backing file"), topSource->path, path); @@ -19887,7 +19887,8 @@ qemuDomainGetStatsBlock(virQEMUDriverPtr driver, virStorageSourcePtr src =3D disk->src; unsigned int backing_idx =3D 0; - while (src && (backing_idx =3D=3D 0 || visitBacking)) { + while (virStorageSourceIsBacking(src) && + (backing_idx =3D=3D 0 || visitBacking)) { if (qemuDomainGetStatsOneBlock(driver, cfg, dom, record, maxpa= rams, disk, src, visited, backing_idx, stats, nodestats) < 0) diff --git a/src/security/security_dac.c b/src/security/security_dac.c index 349dbe81d..244b300a9 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -730,7 +730,7 @@ virSecurityDACSetDiskLabel(virSecurityManagerPtr mgr, { virStorageSourcePtr next; - for (next =3D disk->src; next; next =3D next->backingStore) { + for (next =3D disk->src; virStorageSourceIsBacking(next); next =3D nex= t->backingStore) { if (virSecurityDACSetImageLabel(mgr, def, next) < 0) return -1; } diff --git a/src/security/security_selinux.c b/src/security/security_selinu= x.c index 2e3082b7a..cd3e41193 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -1539,7 +1539,7 @@ virSecuritySELinuxRestoreImageLabelInt(virSecurityMan= agerPtr mgr, * be tracked in domain XML, at which point labelskip should be a * per-file attribute instead of a disk attribute. */ if (disk_seclabel && disk_seclabel->labelskip && - !src->backingStore) + !virStorageSourceHasBacking(src)) return 0; /* Don't restore labels on readonly/shared disks, because other VMs may @@ -1673,7 +1673,7 @@ virSecuritySELinuxSetDiskLabel(virSecurityManagerPtr = mgr, bool first =3D true; virStorageSourcePtr next; - for (next =3D disk->src; next; next =3D next->backingStore) { + for (next =3D disk->src; virStorageSourceIsBacking(next); next =3D nex= t->backingStore) { if (virSecuritySELinuxSetImageLabelInternal(mgr, def, next, first)= < 0) return -1; diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c index 95906e68a..ef1bf0136 100644 --- a/src/security/virt-aa-helper.c +++ b/src/security/virt-aa-helper.c @@ -942,7 +942,7 @@ get_files(vahControl * ctl) /* XXX - if we knew the qemu user:group here we could send it in * so that the open could be re-tried as that user:group. */ - if (!disk->src->backingStore) { + if (!virStorageSourceHasBacking(disk->src)) { bool probe =3D ctl->allowDiskFormatProbing; virStorageFileGetMetadata(disk->src, -1, -1, probe, false); } diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_ba= ckend_logical.c index 1e0f04e4e..a872a2f88 100644 --- a/src/storage/storage_backend_logical.c +++ b/src/storage/storage_backend_logical.c @@ -977,7 +977,7 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn, } virCommandAddArgFormat(cmd, "%lluK", VIR_DIV_UP(vol->target.capacity, 1024)); - if (vol->target.backingStore) + if (virStorageSourceHasBacking(&vol->target)) virCommandAddArgList(cmd, "-s", vol->target.backingStore->path, NU= LL); else virCommandAddArg(cmd, def->source.name); diff --git a/src/storage/storage_source.c b/src/storage/storage_source.c index 864c69928..47b08f416 100644 --- a/src/storage/storage_source.c +++ b/src/storage/storage_source.c @@ -490,7 +490,7 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr sr= c, ret =3D 0; cleanup: - if (src->backingStore) + if (virStorageSourceHasBacking(src)) src->backingStore->id =3D depth; VIR_FREE(buf); virStorageFileDeinit(src); diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index a10e4590f..5252e429f 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -415,7 +415,7 @@ storageBackendCreateRaw(virConnectPtr conn ATTRIBUTE_UN= USED, goto cleanup; } - if (vol->target.backingStore) { + if (virStorageSourceHasBacking(&vol->target)) { virReportError(VIR_ERR_NO_SUPPORT, "%s", _("backing storage not supported for raw volumes")); goto cleanup; @@ -722,7 +722,7 @@ storageBackendCreatePloop(virConnectPtr conn ATTRIBUTE_= UNUSED, return -1; } - if (vol->target.backingStore !=3D NULL) { + if (virStorageSourceHasBacking(&vol->target)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("copy-on-write ploop volumes are not yet supporte= d")); return -1; @@ -1055,7 +1055,7 @@ storageBackendCreateQemuImgSetBacking(virStoragePoolO= bjPtr pool, * backing store, not really sure what use it serves though, and it * may cause issues with lvm. Untested essentially. */ - if (inputvol && inputvol->target.backingStore && + if (inputvol && virStorageSourceHasBacking(&inputvol->target) && STRNEQ_NULLABLE(inputvol->target.backingStore->path, info->backingPath)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -1230,7 +1230,7 @@ virStorageBackendCreateQemuImgCmdFromVol(virConnectPt= r conn, storageBackendCreateQemuImgSetInput(inputvol, &info) < 0) return NULL; - if (vol->target.backingStore && + if (virStorageSourceHasBacking(&vol->target) && storageBackendCreateQemuImgSetBacking(pool, vol, inputvol, &info) = < 0) return NULL; @@ -1840,7 +1840,7 @@ virStorageBackendUpdateVolInfo(virStorageVolDefPtr vo= l, openflags, readflags)) < = 0) return ret; - if (vol->target.backingStore && + if (virStorageSourceHasBacking(&vol->target) && (ret =3D storageBackendUpdateVolTargetInfo(VIR_STORAGE_VOL_FILE, vol->target.backingStore, withBlockVolFormat, @@ -2035,7 +2035,7 @@ createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED, return -1; } - if (vol->target.backingStore) { + if (virStorageSourceHasBacking(&vol->target)) { virReportError(VIR_ERR_NO_SUPPORT, "%s", _("backing storage not supported for directories vo= lumes")); return -1; @@ -3561,7 +3561,7 @@ virStorageBackendRefreshVolTargetUpdate(virStorageVol= DefPtr vol) if (vol->target.format =3D=3D VIR_STORAGE_FILE_PLOOP) vol->type =3D VIR_STORAGE_VOL_PLOOP; - if (vol->target.backingStore) { + if (virStorageSourceHasBacking(&vol->target)) { ignore_value(storageBackendUpdateVolTargetInfo(VIR_STORAGE_VOL_FIL= E, vol->target.backing= Store, false, diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 818a679de..93995a331 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -1292,7 +1292,7 @@ virStorageFileChainGetBroken(virStorageSourcePtr chai= n, if (!chain) return 0; - for (tmp =3D chain; tmp; tmp =3D tmp->backingStore) { + for (tmp =3D chain; virStorageSourceIsBacking(tmp); tmp =3D tmp->backi= ngStore) { /* Break when we hit end of chain; report error if we detected * a missing backing file, infinite loop, or other error */ if (!tmp->backingStore && tmp->backingStoreRaw) { @@ -1566,6 +1566,33 @@ virStorageFileParseChainIndex(const char *diskTarget, return ret; } + +/** + * virStorageSourceIsBacking: + * @src: storage source + * + * Returns true if @src is a eligible backing store structure. Useful + * for iterators. + */ +bool +virStorageSourceIsBacking(const virStorageSource *src) +{ + return !!src; +} + +/** + * virStorageSourceHasBacking: + * @src: storage source + * + * Returns true if @src has backing store/chain. + */ +bool +virStorageSourceHasBacking(const virStorageSource *src) +{ + return virStorageSourceIsBacking(src) && src->backingStore; +} + + /* 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 @@ -1594,15 +1621,16 @@ virStorageFileChainLookup(virStorageSourcePtr chain, *parent =3D NULL; if (startFrom) { - while (chain && chain !=3D startFrom->backingStore) + while (virStorageSourceIsBacking(chain) && + chain !=3D startFrom->backingStore) chain =3D chain->backingStore; *parent =3D startFrom; } - while (chain) { + while (virStorageSourceIsBacking(chain)) { if (!name && !idx) { - if (!chain->backingStore) + if (!virStorageSourceHasBacking(chain)) break; } else if (idx) { VIR_DEBUG("%u: %s", chain->id, chain->path); @@ -1640,7 +1668,7 @@ virStorageFileChainLookup(virStorageSourcePtr chain, chain =3D chain->backingStore; } - if (!chain) + if (!virStorageSourceIsBacking(chain)) goto error; return chain; @@ -3854,7 +3882,7 @@ virStorageFileGetRelativeBackingPath(virStorageSource= Ptr top, *relpath =3D NULL; - for (next =3D top; next; next =3D next->backingStore) { + for (next =3D top; virStorageSourceIsBacking(next); next =3D next->bac= kingStore) { if (!next->relPath) { ret =3D 1; goto cleanup; @@ -3973,7 +4001,7 @@ virStorageSourceFindByNodeName(virStorageSourcePtr to= p, if (idx) *idx =3D 0; - for (tmp =3D top; tmp; tmp =3D tmp->backingStore) { + for (tmp =3D top; virStorageSourceIsBacking(tmp); tmp =3D tmp->backing= Store) { if ((tmp->nodeformat && STREQ(tmp->nodeformat, nodeName)) || (tmp->nodestorage && STREQ(tmp->nodestorage, nodeName))) return tmp; diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index d3bafefc3..86e60de2a 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -425,4 +425,10 @@ void virStorageSourceNetworkAssignDefaultPorts(virStorageSourcePtr src) ATTRIBUTE_NONNULL(1); +bool +virStorageSourceIsBacking(const virStorageSource *src); +bool +virStorageSourceHasBacking(const virStorageSource *src); + + #endif /* __VIR_STORAGE_FILE_H__ */ diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index ad4514871..35e97ff26 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -356,7 +356,7 @@ testStorageChain(const void *args) } elt =3D meta; - while (elt) { + while (virStorageSourceIsBacking(elt)) { char *expect =3D NULL; char *actual =3D NULL; --=20 2.14.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list