[libvirt PATCH v2 12/31] storage_source: introduce virStorageSourceChainLookupBySource

Pavel Hrdina posted 31 patches 3 years, 1 month ago
There is a newer version of this series
[libvirt PATCH v2 12/31] storage_source: introduce virStorageSourceChainLookupBySource
Posted by Pavel Hrdina 3 years, 1 month ago
Looks up disk storage source within storage source chain using storage
source object instead of path to make it work with all disk types.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 src/libvirt_private.syms          |  1 +
 src/storage_file/storage_source.c | 39 +++++++++++++++++++++++++++++++
 src/storage_file/storage_source.h |  6 +++++
 3 files changed, 46 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ae746a2d51..6ec405ef67 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1778,6 +1778,7 @@ virStorageFileProbeGetMetadata;
 # storage_file/storage_source.h
 virStorageSourceAccess;
 virStorageSourceChainLookup;
+virStorageSourceChainLookupBySource;
 virStorageSourceChown;
 virStorageSourceCreate;
 virStorageSourceDeinit;
diff --git a/src/storage_file/storage_source.c b/src/storage_file/storage_source.c
index ab0cdf2b12..a15d766bec 100644
--- a/src/storage_file/storage_source.c
+++ b/src/storage_file/storage_source.c
@@ -322,6 +322,45 @@ virStorageSourceChainLookup(virStorageSource *chain,
 }
 
 
+/**
+ * virStorageSourceChainLookupBySource:
+ * @chain: chain top to look in
+ * @base: stourage source to look for in @chain
+ * @parent: Filled with parent virStorageSource of the returned value if non-NULL.
+ *
+ * Looks up a storage source definition corresponding to @base in @chain.
+ *
+ * Returns virStorageSource withing chain or NULL if not found.
+ */
+virStorageSource *
+virStorageSourceChainLookupBySource(virStorageSource *chain,
+                                    virStorageSource *base,
+                                    virStorageSource **parent)
+{
+    virStorageSource *prev = NULL;
+
+    if (parent)
+        *parent = NULL;
+
+    while (virStorageSourceIsBacking(chain)) {
+        if (virStorageSourceIsSameLocation(chain, base))
+            break;
+
+        prev = chain;
+        chain = chain->backingStore;
+    }
+
+    if (!virStorageSourceIsBacking(chain)) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("could not find base disk source in disk source chain"));
+        return NULL;
+    }
+
+    *parent = prev;
+    return chain;
+}
+
+
 static virStorageSource *
 virStorageSourceNewFromBackingRelative(virStorageSource *parent,
                                        const char *rel)
diff --git a/src/storage_file/storage_source.h b/src/storage_file/storage_source.h
index 0ae06f4e7d..63fefb6919 100644
--- a/src/storage_file/storage_source.h
+++ b/src/storage_file/storage_source.h
@@ -47,6 +47,12 @@ virStorageSourceChainLookup(virStorageSource *chain,
                             virStorageSource **parent)
     ATTRIBUTE_NONNULL(1);
 
+virStorageSource *
+virStorageSourceChainLookupBySource(virStorageSource *chain,
+                                    virStorageSource *base,
+                                    virStorageSource **parent)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 int
 virStorageSourceUpdatePhysicalSize(virStorageSource *src,
                                    int fd,
-- 
2.39.0
Re: [libvirt PATCH v2 12/31] storage_source: introduce virStorageSourceChainLookupBySource
Posted by Peter Krempa 3 years, 1 month ago
On Thu, Jan 05, 2023 at 12:46:48 +0100, Pavel Hrdina wrote:
> Looks up disk storage source within storage source chain using storage
> source object instead of path to make it work with all disk types.
> 
> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
> ---
>  src/libvirt_private.syms          |  1 +
>  src/storage_file/storage_source.c | 39 +++++++++++++++++++++++++++++++
>  src/storage_file/storage_source.h |  6 +++++
>  3 files changed, 46 insertions(+)

[...]

> diff --git a/src/storage_file/storage_source.c b/src/storage_file/storage_source.c
> index ab0cdf2b12..a15d766bec 100644
> --- a/src/storage_file/storage_source.c
> +++ b/src/storage_file/storage_source.c
> @@ -322,6 +322,45 @@ virStorageSourceChainLookup(virStorageSource *chain,
>  }
>  
>  
> +/**
> + * virStorageSourceChainLookupBySource:
> + * @chain: chain top to look in
> + * @base: stourage source to look for in @chain

s/stourage/storage/

> + * @parent: Filled with parent virStorageSource of the returned value if non-NULL.
> + *
> + * Looks up a storage source definition corresponding to @base in @chain.
> + *
> + * Returns virStorageSource withing chain or NULL if not found.
> + */
> +virStorageSource *
> +virStorageSourceChainLookupBySource(virStorageSource *chain,
> +                                    virStorageSource *base,
> +                                    virStorageSource **parent)
> +{
> +    virStorageSource *prev = NULL;
> +
> +    if (parent)
> +        *parent = NULL;
> +
> +    while (virStorageSourceIsBacking(chain)) {
> +        if (virStorageSourceIsSameLocation(chain, base))
> +            break;
> +
> +        prev = chain;
> +        chain = chain->backingStore;
> +    }
> +
> +    if (!virStorageSourceIsBacking(chain)) {
> +        virReportError(VIR_ERR_INVALID_ARG, "%s",
> +                       _("could not find base disk source in disk source chain"));
> +        return NULL;
> +    }
> +
> +    *parent = prev;

The comment specifies that passing a pointer for 'parent' is optional
but here you hard-dereference it always. Add the same condition as at
the beginning.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>