9119d9
From bc71e8453519c5123b23fcc295d90a4e788540ca Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <bc71e8453519c5123b23fcc295d90a4e788540ca@dist-git>
9119d9
From: John Ferlan <jferlan@redhat.com>
9119d9
Date: Tue, 28 Oct 2014 22:13:32 -0400
9119d9
Subject: [PATCH] virutil: Introduce virGetSCSIHostNameByParentaddr
9119d9
9119d9
https://bugzilla.redhat.com/show_bug.cgi?id=1146837
9119d9
9119d9
Create the function from the code in getAdapterName() in order to return
9119d9
the "host#" name for the provided parentaddr values.
9119d9
9119d9
(cherry picked from commit beff5d4e1bc72d673f6f0e61afeaa637eddc8e4d)
9119d9
Signed-off-by: John Ferlan <jferlan@redhat.com>
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 src/libvirt_private.syms           |  1 +
9119d9
 src/storage/storage_backend_scsi.c | 19 +++++----------
9119d9
 src/util/virutil.c                 | 50 ++++++++++++++++++++++++++++++++++++++
9119d9
 src/util/virutil.h                 |  6 +++++
9119d9
 4 files changed, 63 insertions(+), 13 deletions(-)
9119d9
9119d9
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
9119d9
index d14023e..401bbb5 100644
9119d9
--- a/src/libvirt_private.syms
9119d9
+++ b/src/libvirt_private.syms
9119d9
@@ -2120,6 +2120,7 @@ virGetGroupList;
9119d9
 virGetGroupName;
9119d9
 virGetHostname;
9119d9
 virGetListenFDs;
9119d9
+virGetSCSIHostNameByParentaddr;
9119d9
 virGetSCSIHostNumber;
9119d9
 virGetSelfLastChanged;
9119d9
 virGetUnprivSGIOSysfsPath;
9119d9
diff --git a/src/storage/storage_backend_scsi.c b/src/storage/storage_backend_scsi.c
9119d9
index d9f3ff2..02160bc 100644
9119d9
--- a/src/storage/storage_backend_scsi.c
9119d9
+++ b/src/storage/storage_backend_scsi.c
9119d9
@@ -519,22 +519,15 @@ getAdapterName(virStoragePoolSourceAdapter adapter)
9119d9
 
9119d9
     if (adapter.type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
9119d9
         if (adapter.data.scsi_host.has_parent) {
9119d9
+            virDevicePCIAddress addr = adapter.data.scsi_host.parentaddr;
9119d9
             unsigned int unique_id = adapter.data.scsi_host.unique_id;
9119d9
 
9119d9
-            if (virAsprintf(&parentaddr, "%04x:%02x:%02x.%01x",
9119d9
-                            adapter.data.scsi_host.parentaddr.domain,
9119d9
-                            adapter.data.scsi_host.parentaddr.bus,
9119d9
-                            adapter.data.scsi_host.parentaddr.slot,
9119d9
-                            adapter.data.scsi_host.parentaddr.function) < 0)
9119d9
+            if (!(name = virGetSCSIHostNameByParentaddr(addr.domain,
9119d9
+                                                        addr.bus,
9119d9
+                                                        addr.slot,
9119d9
+                                                        addr.function,
9119d9
+                                                        unique_id)))
9119d9
                 goto cleanup;
9119d9
-            if (!(name = virFindSCSIHostByPCI(NULL, parentaddr,
9119d9
-                                              unique_id))) {
9119d9
-                virReportError(VIR_ERR_XML_ERROR,
9119d9
-                               _("Failed to find scsi_host using PCI '%s' "
9119d9
-                                 "and unique_id='%u'"),
9119d9
-                               parentaddr, unique_id);
9119d9
-                goto cleanup;
9119d9
-            }
9119d9
         } else {
9119d9
             ignore_value(VIR_STRDUP(name, adapter.data.scsi_host.name));
9119d9
         }
9119d9
diff --git a/src/util/virutil.c b/src/util/virutil.c
9119d9
index 0a69912..78952ec 100644
9119d9
--- a/src/util/virutil.c
9119d9
+++ b/src/util/virutil.c
9119d9
@@ -1878,6 +1878,45 @@ virGetSCSIHostNumber(const char *adapter_name,
9119d9
     return 0;
9119d9
 }
9119d9
 
9119d9
+/* virGetSCSIHostNameByParentaddr:
9119d9
+ * @domain: The domain from the scsi_host parentaddr
9119d9
+ * @bus: The bus from the scsi_host parentaddr
9119d9
+ * @slot: The slot from the scsi_host parentaddr
9119d9
+ * @function: The function from the scsi_host parentaddr
9119d9
+ * @unique_id: The unique id value for parentaddr
9119d9
+ *
9119d9
+ * Generate a parentaddr and find the scsi_host host# for
9119d9
+ * the provided parentaddr PCI address fields.
9119d9
+ *
9119d9
+ * Returns the "host#" string which must be free'd by
9119d9
+ * the caller or NULL on error
9119d9
+ */
9119d9
+char *
9119d9
+virGetSCSIHostNameByParentaddr(unsigned int domain,
9119d9
+                               unsigned int bus,
9119d9
+                               unsigned int slot,
9119d9
+                               unsigned int function,
9119d9
+                               unsigned int unique_id)
9119d9
+{
9119d9
+    char *name = NULL;
9119d9
+    char *parentaddr = NULL;
9119d9
+
9119d9
+    if (virAsprintf(&parentaddr, "%04x:%02x:%02x.%01x",
9119d9
+                    domain, bus, slot, function) < 0)
9119d9
+        goto cleanup;
9119d9
+    if (!(name = virFindSCSIHostByPCI(NULL, parentaddr, unique_id))) {
9119d9
+        virReportError(VIR_ERR_XML_ERROR,
9119d9
+                       _("Failed to find scsi_host using PCI '%s' "
9119d9
+                         "and unique_id='%u'"),
9119d9
+                       parentaddr, unique_id);
9119d9
+        goto cleanup;
9119d9
+    }
9119d9
+
9119d9
+ cleanup:
9119d9
+    VIR_FREE(parentaddr);
9119d9
+    return name;
9119d9
+}
9119d9
+
9119d9
 /* virReadFCHost:
9119d9
  * @sysfs_prefix: "fc_host" sysfs path, defaults to SYSFS_FC_HOST_PATH
9119d9
  * @host: Host number, E.g. 5 of "fc_host/host5"
9119d9
@@ -2256,6 +2295,17 @@ virGetSCSIHostNumber(const char *adapter_name ATTRIBUTE_UNUSED,
9119d9
     return NULL;
9119d9
 }
9119d9
 
9119d9
+char *
9119d9
+virGetSCSIHostNameByParentaddr(unsigned int domain ATTRIBUTE_UNUSED,
9119d9
+                               unsigned int bus ATTRIBUTE_UNUSED,
9119d9
+                               unsigned int slot ATTRIBUTE_UNUSED,
9119d9
+                               unsigned int function ATTRIBUTE_UNUSED,
9119d9
+                               unsigned int unique_id ATTRIBUTE_UNUSED)
9119d9
+{
9119d9
+    virReportSystemError(ENOSYS, "%s", _("Not supported on this platform"));
9119d9
+    return NULL;
9119d9
+}
9119d9
+
9119d9
 int
9119d9
 virReadFCHost(const char *sysfs_prefix ATTRIBUTE_UNUSED,
9119d9
               int host ATTRIBUTE_UNUSED,
9119d9
diff --git a/src/util/virutil.h b/src/util/virutil.h
9119d9
index 8b41063..105c519 100644
9119d9
--- a/src/util/virutil.h
9119d9
+++ b/src/util/virutil.h
9119d9
@@ -177,6 +177,12 @@ int
9119d9
 virGetSCSIHostNumber(const char *adapter_name,
9119d9
                      unsigned int *result)
9119d9
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
9119d9
+char *
9119d9
+virGetSCSIHostNameByParentaddr(unsigned int domain,
9119d9
+                               unsigned int bus,
9119d9
+                               unsigned int slot,
9119d9
+                               unsigned int function,
9119d9
+                               unsigned int unique_id);
9119d9
 int virReadFCHost(const char *sysfs_prefix,
9119d9
                   int host,
9119d9
                   const char *entry,
9119d9
-- 
9119d9
2.1.3
9119d9