[PATCH v3 03/18] conf: Split out storage pool source adapter helpers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Split out the code that munges through the storage pool adapter into
helpers - it's about to be moved into it's own source file.

This is purely code motion at this point.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/conf/storage_conf.c | 455 ++++++++++++++++++++++++++----------------------
 1 file changed, 243 insertions(+), 212 deletions(-)

diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 8e3b175..1993d3a 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -463,6 +463,128 @@ virStoragePoolObjRemove(virStoragePoolObjListPtr pools,
 }
 
 static int
+virStoragePoolDefParseSourceAdapter(virStoragePoolSourcePtr source,
+                                    xmlXPathContextPtr ctxt)
+{
+    int ret = -1;
+    char *adapter_type = NULL;
+    char *managed = NULL;
+
+    if ((adapter_type = virXPathString("string(./adapter/@type)", ctxt))) {
+        if ((source->adapter.type =
+             virStoragePoolSourceAdapterTypeFromString(adapter_type)) <= 0) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("Unknown pool adapter type '%s'"),
+                           adapter_type);
+            goto cleanup;
+        }
+
+        if (source->adapter.type ==
+            VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
+            source->adapter.data.fchost.parent =
+                virXPathString("string(./adapter/@parent)", ctxt);
+            managed = virXPathString("string(./adapter/@managed)", ctxt);
+            if (managed) {
+                source->adapter.data.fchost.managed =
+                    virTristateBoolTypeFromString(managed);
+                if (source->adapter.data.fchost.managed < 0) {
+                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                                   _("unknown fc_host managed setting '%s'"),
+                                   managed);
+                    goto cleanup;
+                }
+            }
+
+            source->adapter.data.fchost.parent_wwnn =
+                virXPathString("string(./adapter/@parent_wwnn)", ctxt);
+            source->adapter.data.fchost.parent_wwpn =
+                virXPathString("string(./adapter/@parent_wwpn)", ctxt);
+            source->adapter.data.fchost.parent_fabric_wwn =
+                virXPathString("string(./adapter/@parent_fabric_wwn)", ctxt);
+
+            source->adapter.data.fchost.wwpn =
+                virXPathString("string(./adapter/@wwpn)", ctxt);
+            source->adapter.data.fchost.wwnn =
+                virXPathString("string(./adapter/@wwnn)", ctxt);
+        } else if (source->adapter.type ==
+                   VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
+
+            source->adapter.data.scsi_host.name =
+                virXPathString("string(./adapter/@name)", ctxt);
+            if (virXPathNode("./adapter/parentaddr", ctxt)) {
+                xmlNodePtr addrnode = virXPathNode("./adapter/parentaddr/address",
+                                                   ctxt);
+                virPCIDeviceAddressPtr addr =
+                    &source->adapter.data.scsi_host.parentaddr;
+
+                if (!addrnode) {
+                    virReportError(VIR_ERR_XML_ERROR, "%s",
+                                   _("Missing scsi_host PCI address element"));
+                    goto cleanup;
+                }
+                source->adapter.data.scsi_host.has_parent = true;
+                if (virPCIDeviceAddressParseXML(addrnode, addr) < 0)
+                    goto cleanup;
+                if ((virXPathInt("string(./adapter/parentaddr/@unique_id)",
+                                 ctxt,
+                                 &source->adapter.data.scsi_host.unique_id) < 0) ||
+                    (source->adapter.data.scsi_host.unique_id < 0)) {
+                    virReportError(VIR_ERR_XML_ERROR, "%s",
+                                   _("Missing or invalid scsi adapter "
+                                     "'unique_id' value"));
+                    goto cleanup;
+                }
+            }
+        }
+    } else {
+        char *wwnn = NULL;
+        char *wwpn = NULL;
+        char *parent = NULL;
+
+        /* "type" was not specified in the XML, so we must verify that
+         * "wwnn", "wwpn", "parent", or "parentaddr" are also not in the
+         * XML. If any are found, then we cannot just use "name" alone".
+         */
+        wwnn = virXPathString("string(./adapter/@wwnn)", ctxt);
+        wwpn = virXPathString("string(./adapter/@wwpn)", ctxt);
+        parent = virXPathString("string(./adapter/@parent)", ctxt);
+
+        if (wwnn || wwpn || parent) {
+            VIR_FREE(wwnn);
+            VIR_FREE(wwpn);
+            VIR_FREE(parent);
+            virReportError(VIR_ERR_XML_ERROR, "%s",
+                           _("Use of 'wwnn', 'wwpn', and 'parent' attributes "
+                             "requires use of the adapter 'type'"));
+            goto cleanup;
+        }
+
+        if (virXPathNode("./adapter/parentaddr", ctxt)) {
+            virReportError(VIR_ERR_XML_ERROR, "%s",
+                           _("Use of 'parent' element requires use "
+                             "of the adapter 'type'"));
+            goto cleanup;
+        }
+
+        /* To keep back-compat, 'type' is not required to specify
+         * for scsi_host adapter.
+         */
+        if ((source->adapter.data.scsi_host.name =
+             virXPathString("string(./adapter/@name)", ctxt)))
+            source->adapter.type =
+                VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST;
+    }
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(adapter_type);
+    VIR_FREE(managed);
+    return ret;
+}
+
+
+static int
 virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
                              virStoragePoolSourcePtr source,
                              int pool_type,
@@ -476,8 +598,6 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
     virStorageAuthDefPtr authdef = NULL;
     char *name = NULL;
     char *port = NULL;
-    char *adapter_type = NULL;
-    char *managed = NULL;
     int n;
 
     relnode = ctxt->node;
@@ -583,110 +703,8 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
         VIR_STRDUP(source->dir, "/") < 0)
         goto cleanup;
 
-    if ((adapter_type = virXPathString("string(./adapter/@type)", ctxt))) {
-        if ((source->adapter.type =
-             virStoragePoolSourceAdapterTypeFromString(adapter_type)) <= 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("Unknown pool adapter type '%s'"),
-                           adapter_type);
-            goto cleanup;
-        }
-
-        if (source->adapter.type ==
-            VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
-            source->adapter.data.fchost.parent =
-                virXPathString("string(./adapter/@parent)", ctxt);
-            managed = virXPathString("string(./adapter/@managed)", ctxt);
-            if (managed) {
-                source->adapter.data.fchost.managed =
-                    virTristateBoolTypeFromString(managed);
-                if (source->adapter.data.fchost.managed < 0) {
-                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                                   _("unknown fc_host managed setting '%s'"),
-                                   managed);
-                    goto cleanup;
-                }
-            }
-
-            source->adapter.data.fchost.parent_wwnn =
-                virXPathString("string(./adapter/@parent_wwnn)", ctxt);
-            source->adapter.data.fchost.parent_wwpn =
-                virXPathString("string(./adapter/@parent_wwpn)", ctxt);
-            source->adapter.data.fchost.parent_fabric_wwn =
-                virXPathString("string(./adapter/@parent_fabric_wwn)", ctxt);
-
-            source->adapter.data.fchost.wwpn =
-                virXPathString("string(./adapter/@wwpn)", ctxt);
-            source->adapter.data.fchost.wwnn =
-                virXPathString("string(./adapter/@wwnn)", ctxt);
-        } else if (source->adapter.type ==
-                   VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
-
-            source->adapter.data.scsi_host.name =
-                virXPathString("string(./adapter/@name)", ctxt);
-            if (virXPathNode("./adapter/parentaddr", ctxt)) {
-                xmlNodePtr addrnode = virXPathNode("./adapter/parentaddr/address",
-                                                   ctxt);
-                virPCIDeviceAddressPtr addr =
-                    &source->adapter.data.scsi_host.parentaddr;
-
-                if (!addrnode) {
-                    virReportError(VIR_ERR_XML_ERROR, "%s",
-                                   _("Missing scsi_host PCI address element"));
-                    goto cleanup;
-                }
-                source->adapter.data.scsi_host.has_parent = true;
-                if (virPCIDeviceAddressParseXML(addrnode, addr) < 0)
-                    goto cleanup;
-                if ((virXPathInt("string(./adapter/parentaddr/@unique_id)",
-                                 ctxt,
-                                 &source->adapter.data.scsi_host.unique_id) < 0) ||
-                    (source->adapter.data.scsi_host.unique_id < 0)) {
-                    virReportError(VIR_ERR_XML_ERROR, "%s",
-                                   _("Missing or invalid scsi adapter "
-                                     "'unique_id' value"));
-                    goto cleanup;
-                }
-            }
-        }
-    } else {
-        char *wwnn = NULL;
-        char *wwpn = NULL;
-        char *parent = NULL;
-
-        /* "type" was not specified in the XML, so we must verify that
-         * "wwnn", "wwpn", "parent", or "parentaddr" are also not in the
-         * XML. If any are found, then we cannot just use "name" alone".
-         */
-        wwnn = virXPathString("string(./adapter/@wwnn)", ctxt);
-        wwpn = virXPathString("string(./adapter/@wwpn)", ctxt);
-        parent = virXPathString("string(./adapter/@parent)", ctxt);
-
-        if (wwnn || wwpn || parent) {
-            VIR_FREE(wwnn);
-            VIR_FREE(wwpn);
-            VIR_FREE(parent);
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("Use of 'wwnn', 'wwpn', and 'parent' attributes "
-                             "requires use of the adapter 'type'"));
-            goto cleanup;
-        }
-
-        if (virXPathNode("./adapter/parentaddr", ctxt)) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("Use of 'parent' element requires use "
-                             "of the adapter 'type'"));
-            goto cleanup;
-        }
-
-        /* To keep back-compat, 'type' is not required to specify
-         * for scsi_host adapter.
-         */
-        if ((source->adapter.data.scsi_host.name =
-             virXPathString("string(./adapter/@name)", ctxt)))
-            source->adapter.type =
-                VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST;
-    }
+    if (virStoragePoolDefParseSourceAdapter(source, ctxt) < 0)
+        goto cleanup;
 
     if ((authnode = virXPathNode("./auth", ctxt))) {
         if (!(authdef = virStorageAuthDefParse(node->doc, authnode)))
@@ -711,8 +729,6 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
 
     VIR_FREE(port);
     VIR_FREE(nodeset);
-    VIR_FREE(adapter_type);
-    VIR_FREE(managed);
     virStorageAuthDefFree(authdef);
     return ret;
 }
@@ -831,6 +847,74 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt,
     return ret;
 }
 
+static int
+virStoragePoolSourceAdapterParseValidate(virStoragePoolDefPtr ret)
+{
+    if (!ret->source.adapter.type) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("missing storage pool source adapter"));
+        return -1;
+    }
+
+    if (ret->source.adapter.type ==
+        VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
+        if (!ret->source.adapter.data.fchost.wwnn ||
+            !ret->source.adapter.data.fchost.wwpn) {
+            virReportError(VIR_ERR_XML_ERROR, "%s",
+                           _("'wwnn' and 'wwpn' must be specified for adapter "
+                             "type 'fchost'"));
+            return -1;
+        }
+
+        if (!virValidateWWN(ret->source.adapter.data.fchost.wwnn) ||
+            !virValidateWWN(ret->source.adapter.data.fchost.wwpn))
+            return -1;
+
+        if ((ret->source.adapter.data.fchost.parent_wwnn &&
+             !ret->source.adapter.data.fchost.parent_wwpn) ||
+            (!ret->source.adapter.data.fchost.parent_wwnn &&
+             ret->source.adapter.data.fchost.parent_wwpn)) {
+            virReportError(VIR_ERR_XML_ERROR, "%s",
+                           _("must supply both parent_wwnn and "
+                             "parent_wwpn not just one or the other"));
+                return -1;
+        }
+
+        if (ret->source.adapter.data.fchost.parent_wwnn &&
+            !virValidateWWN(ret->source.adapter.data.fchost.parent_wwnn))
+            return -1;
+
+        if (ret->source.adapter.data.fchost.parent_wwpn &&
+            !virValidateWWN(ret->source.adapter.data.fchost.parent_wwpn))
+            return -1;
+
+        if (ret->source.adapter.data.fchost.parent_fabric_wwn &&
+            !virValidateWWN(ret->source.adapter.data.fchost.parent_fabric_wwn))
+            return -1;
+
+    } else if (ret->source.adapter.type ==
+               VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
+        if (!ret->source.adapter.data.scsi_host.name &&
+            !ret->source.adapter.data.scsi_host.has_parent) {
+            virReportError(VIR_ERR_XML_ERROR, "%s",
+                           _("Either 'name' or 'parent' must be specified "
+                             "for the 'scsi_host' adapter"));
+            return -1;
+        }
+
+        if (ret->source.adapter.data.scsi_host.name &&
+            ret->source.adapter.data.scsi_host.has_parent) {
+            virReportError(VIR_ERR_XML_ERROR, "%s",
+                           _("Both 'name' and 'parent' cannot be specified "
+                             "for the 'scsi_host' adapter"));
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+
 static virStoragePoolDefPtr
 virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
 {
@@ -921,68 +1005,9 @@ virStoragePoolDefParseXML(xmlXPathContextPtr ctxt)
         }
     }
 
-    if (options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) {
-        if (!ret->source.adapter.type) {
-            virReportError(VIR_ERR_XML_ERROR, "%s",
-                           _("missing storage pool source adapter"));
+    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
+        (virStoragePoolSourceAdapterParseValidate(ret)) < 0)
             goto error;
-        }
-
-        if (ret->source.adapter.type ==
-            VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
-            if (!ret->source.adapter.data.fchost.wwnn ||
-                !ret->source.adapter.data.fchost.wwpn) {
-                virReportError(VIR_ERR_XML_ERROR, "%s",
-                               _("'wwnn' and 'wwpn' must be specified for adapter "
-                                 "type 'fchost'"));
-                goto error;
-            }
-
-            if (!virValidateWWN(ret->source.adapter.data.fchost.wwnn) ||
-                !virValidateWWN(ret->source.adapter.data.fchost.wwpn))
-                goto error;
-
-            if ((ret->source.adapter.data.fchost.parent_wwnn &&
-                 !ret->source.adapter.data.fchost.parent_wwpn) ||
-                (!ret->source.adapter.data.fchost.parent_wwnn &&
-                 ret->source.adapter.data.fchost.parent_wwpn)) {
-                virReportError(VIR_ERR_XML_ERROR, "%s",
-                               _("must supply both parent_wwnn and "
-                                 "parent_wwpn not just one or the other"));
-                    goto error;
-            }
-
-            if (ret->source.adapter.data.fchost.parent_wwnn &&
-                !virValidateWWN(ret->source.adapter.data.fchost.parent_wwnn))
-                goto error;
-
-            if (ret->source.adapter.data.fchost.parent_wwpn &&
-                !virValidateWWN(ret->source.adapter.data.fchost.parent_wwpn))
-                goto error;
-
-            if (ret->source.adapter.data.fchost.parent_fabric_wwn &&
-                !virValidateWWN(ret->source.adapter.data.fchost.parent_fabric_wwn))
-                goto error;
-
-        } else if (ret->source.adapter.type ==
-                   VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
-            if (!ret->source.adapter.data.scsi_host.name &&
-                !ret->source.adapter.data.scsi_host.has_parent) {
-                virReportError(VIR_ERR_XML_ERROR, "%s",
-                               _("Either 'name' or 'parent' must be specified "
-                                 "for the 'scsi_host' adapter"));
-                goto error;
-            }
-
-            if (ret->source.adapter.data.scsi_host.name &&
-                ret->source.adapter.data.scsi_host.has_parent) {
-                virReportError(VIR_ERR_XML_ERROR, "%s",
-                               _("Both 'name' and 'parent' cannot be specified "
-                                 "for the 'scsi_host' adapter"));
-                goto error;
-            }
-        }
-    }
 
     /* If DEVICE is the only source type, then its required */
     if (options->flags == VIR_STORAGE_POOL_SOURCE_DEVICE) {
@@ -1086,6 +1111,51 @@ virStoragePoolDefParseFile(const char *filename)
     return virStoragePoolDefParse(NULL, filename);
 }
 
+static void
+virStoragePoolSourceAdapterFormat(virBufferPtr buf,
+                                  virStoragePoolSourcePtr src)
+{
+    virBufferAsprintf(buf, "<adapter type='%s'",
+                      virStoragePoolSourceAdapterTypeToString(src->adapter.type));
+
+    if (src->adapter.type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
+        virBufferEscapeString(buf, " parent='%s'",
+                              src->adapter.data.fchost.parent);
+        if (src->adapter.data.fchost.managed)
+            virBufferAsprintf(buf, " managed='%s'",
+                              virTristateBoolTypeToString(src->adapter.data.fchost.managed));
+        virBufferEscapeString(buf, " parent_wwnn='%s'",
+                              src->adapter.data.fchost.parent_wwnn);
+        virBufferEscapeString(buf, " parent_wwpn='%s'",
+                              src->adapter.data.fchost.parent_wwpn);
+        virBufferEscapeString(buf, " parent_fabric_wwn='%s'",
+                              src->adapter.data.fchost.parent_fabric_wwn);
+
+        virBufferAsprintf(buf, " wwnn='%s' wwpn='%s'/>\n",
+                          src->adapter.data.fchost.wwnn,
+                          src->adapter.data.fchost.wwpn);
+    } else if (src->adapter.type ==
+               VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
+        if (src->adapter.data.scsi_host.name) {
+            virBufferAsprintf(buf, " name='%s'/>\n",
+                              src->adapter.data.scsi_host.name);
+        } else {
+            virPCIDeviceAddress addr;
+            virBufferAddLit(buf, ">\n");
+            virBufferAdjustIndent(buf, 2);
+            virBufferAsprintf(buf, "<parentaddr unique_id='%d'>\n",
+                              src->adapter.data.scsi_host.unique_id);
+            virBufferAdjustIndent(buf, 2);
+            addr = src->adapter.data.scsi_host.parentaddr;
+            ignore_value(virPCIDeviceAddressFormat(buf, addr, false));
+            virBufferAdjustIndent(buf, -2);
+            virBufferAddLit(buf, "</parentaddr>\n");
+            virBufferAdjustIndent(buf, -2);
+            virBufferAddLit(buf, "</adapter>\n");
+        }
+    }
+}
+
 static int
 virStoragePoolSourceFormat(virBufferPtr buf,
                            virStoragePoolOptionsPtr options,
@@ -1135,49 +1205,10 @@ virStoragePoolSourceFormat(virBufferPtr buf,
     if (options->flags & VIR_STORAGE_POOL_SOURCE_DIR)
         virBufferEscapeString(buf, "<dir path='%s'/>\n", src->dir);
 
-    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER)) {
-        if (src->adapter.type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST ||
-            src->adapter.type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST)
-            virBufferAsprintf(buf, "<adapter type='%s'",
-                              virStoragePoolSourceAdapterTypeToString(src->adapter.type));
-
-        if (src->adapter.type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
-            virBufferEscapeString(buf, " parent='%s'",
-                                  src->adapter.data.fchost.parent);
-            if (src->adapter.data.fchost.managed)
-                virBufferAsprintf(buf, " managed='%s'",
-                                  virTristateBoolTypeToString(src->adapter.data.fchost.managed));
-            virBufferEscapeString(buf, " parent_wwnn='%s'",
-                                  src->adapter.data.fchost.parent_wwnn);
-            virBufferEscapeString(buf, " parent_wwpn='%s'",
-                                  src->adapter.data.fchost.parent_wwpn);
-            virBufferEscapeString(buf, " parent_fabric_wwn='%s'",
-                                  src->adapter.data.fchost.parent_fabric_wwn);
-
-            virBufferAsprintf(buf, " wwnn='%s' wwpn='%s'/>\n",
-                              src->adapter.data.fchost.wwnn,
-                              src->adapter.data.fchost.wwpn);
-        } else if (src->adapter.type ==
-                   VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST) {
-            if (src->adapter.data.scsi_host.name) {
-                virBufferAsprintf(buf, " name='%s'/>\n",
-                                  src->adapter.data.scsi_host.name);
-            } else {
-                virPCIDeviceAddress addr;
-                virBufferAddLit(buf, ">\n");
-                virBufferAdjustIndent(buf, 2);
-                virBufferAsprintf(buf, "<parentaddr unique_id='%d'>\n",
-                                  src->adapter.data.scsi_host.unique_id);
-                virBufferAdjustIndent(buf, 2);
-                addr = src->adapter.data.scsi_host.parentaddr;
-                ignore_value(virPCIDeviceAddressFormat(buf, addr, false));
-                virBufferAdjustIndent(buf, -2);
-                virBufferAddLit(buf, "</parentaddr>\n");
-                virBufferAdjustIndent(buf, -2);
-                virBufferAddLit(buf, "</adapter>\n");
-            }
-        }
-    }
+    if ((options->flags & VIR_STORAGE_POOL_SOURCE_ADAPTER) &&
+        (src->adapter.type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST ||
+         src->adapter.type == VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_SCSI_HOST))
+        virStoragePoolSourceAdapterFormat(buf, src);
 
     if (options->flags & VIR_STORAGE_POOL_SOURCE_NAME)
         virBufferEscapeString(buf, "<name>%s</name>\n", src->name);
-- 
2.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux