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

[libvirt] [PATCH 4/7] qemu: Kill qemuDiskPathToAlias



The function has terrible semantics. Split it into two functions.
---
 src/qemu/qemu_alias.c  | 16 ++++++++
 src/qemu/qemu_alias.h  |  2 +
 src/qemu/qemu_domain.c | 16 ++++++++
 src/qemu/qemu_domain.h |  3 ++
 src/qemu/qemu_driver.c | 99 +++++++++++++++++++++-----------------------------
 5 files changed, 79 insertions(+), 57 deletions(-)

diff --git a/src/qemu/qemu_alias.c b/src/qemu/qemu_alias.c
index efd9222..9691223 100644
--- a/src/qemu/qemu_alias.c
+++ b/src/qemu/qemu_alias.c
@@ -484,3 +484,19 @@ qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps)

     return 0;
 }
+
+
+char *
+qemuAliasFromDisk(const virDomainDiskDef *disk)
+{
+    char *ret;
+
+    if (!disk->info.alias) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s", _("disk does not have an alias"));
+        return NULL;
+    }
+
+    ignore_value(virAsprintf(&ret, "drive-%s", disk->info.alias));
+
+    return ret;
+}
diff --git a/src/qemu/qemu_alias.h b/src/qemu/qemu_alias.h
index a2eaa27..714a526 100644
--- a/src/qemu/qemu_alias.h
+++ b/src/qemu/qemu_alias.h
@@ -61,4 +61,6 @@ int qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps);

 int qemuDomainDeviceAliasIndex(const virDomainDeviceInfo *info,
                                const char *prefix);
+
+char *qemuAliasFromDisk(const virDomainDiskDef *disk);
 #endif /* __QEMU_ALIAS_H__*/
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 594063e..0043e9e 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -4547,3 +4547,19 @@ qemuDomainNetVLAN(virDomainNetDefPtr def)
 {
     return qemuDomainDeviceAliasIndex(&def->info, "net");
 }
+
+
+virDomainDiskDefPtr
+qemuDomainDiskByName(virDomainDefPtr def,
+                     const char *name)
+{
+    virDomainDiskDefPtr ret;
+
+    if (!(ret = virDomainDiskByName(def, name, true))) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("No device found for specified path"));
+        return NULL;
+    }
+
+    return ret;
+}
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index 5976455..8292ca9 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -545,4 +545,7 @@ int qemuDomainSetPrivatePaths(char **domainLibDir,
                               const char *confChannelDir,
                               const char *domainName,
                               int domainId);
+
+virDomainDiskDefPtr qemuDomainDiskByName(virDomainDefPtr def, const char *name);
+
 #endif /* __QEMU_DOMAIN_H__ */
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 08e784b..6a008b4 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -16031,33 +16031,6 @@ qemuDomainOpenChannel(virDomainPtr dom,
     return ret;
 }

-static char *
-qemuDiskPathToAlias(virDomainObjPtr vm, const char *path, int *idxret)
-{
-    int idx;
-    char *ret = NULL;
-    virDomainDiskDefPtr disk;
-
-    idx = virDomainDiskIndexByName(vm->def, path, true);
-    if (idx < 0)
-        goto cleanup;
-
-    disk = vm->def->disks[idx];
-    if (idxret)
-        *idxret = idx;
-
-    if (virDomainDiskGetSource(disk)) {
-        if (virAsprintf(&ret, "drive-%s", disk->info.alias) < 0)
-            return NULL;
-    }
-
- cleanup:
-    if (!ret) {
-        virReportError(VIR_ERR_INVALID_ARG,
-                       "%s", _("No device found for specified path"));
-    }
-    return ret;
-}

 /* Called while holding the VM job lock, to implement a block job
  * abort with pivot; this updates the VM definition as appropriate, on
@@ -16179,7 +16152,6 @@ qemuDomainBlockPullCommon(virQEMUDriverPtr driver,
     qemuDomainObjPrivatePtr priv = vm->privateData;
     char *device = NULL;
     bool modern;
-    int idx;
     virDomainDiskDefPtr disk;
     virStorageSourcePtr baseSource = NULL;
     unsigned int baseIndex = 0;
@@ -16223,9 +16195,11 @@ qemuDomainBlockPullCommon(virQEMUDriverPtr driver,
         }
     }

-    if (!(device = qemuDiskPathToAlias(vm, path, &idx)))
+    if (!(disk = qemuDomainDiskByName(vm->def, path)))
+        goto endjob;
+
+    if (!(device = qemuAliasFromDisk(disk)))
         goto endjob;
-    disk = vm->def->disks[idx];

     if (qemuDomainDiskBlockJobIsActive(disk))
         goto endjob;
@@ -16306,7 +16280,6 @@ qemuDomainBlockJobAbort(virDomainPtr dom,
     virDomainDiskDefPtr disk = NULL;
     virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
     bool save = false;
-    int idx;
     bool modern;
     bool pivot = !!(flags & VIR_DOMAIN_BLOCK_JOB_ABORT_PIVOT);
     bool async = !!(flags & VIR_DOMAIN_BLOCK_JOB_ABORT_ASYNC);
@@ -16334,9 +16307,11 @@ qemuDomainBlockJobAbort(virDomainPtr dom,
     if (qemuDomainSupportsBlockJobs(vm, &modern) < 0)
         goto endjob;

-    if (!(device = qemuDiskPathToAlias(vm, path, &idx)))
+    if (!(disk = qemuDomainDiskByName(vm->def, path)))
+        goto endjob;
+
+    if (!(device = qemuAliasFromDisk(disk)))
         goto endjob;
-    disk = vm->def->disks[idx];

     if (disk->mirrorState != VIR_DOMAIN_DISK_MIRROR_STATE_NONE &&
         disk->mirrorState != VIR_DOMAIN_DISK_MIRROR_STATE_READY) {
@@ -16517,6 +16492,7 @@ qemuDomainBlockJobSetSpeed(virDomainPtr dom,
                            unsigned int flags)
 {
     virQEMUDriverPtr driver = dom->conn->privateData;
+    virDomainDiskDefPtr disk;
     int ret = -1;
     virDomainObjPtr vm;
     bool modern;
@@ -16554,7 +16530,10 @@ qemuDomainBlockJobSetSpeed(virDomainPtr dom,
     if (qemuDomainSupportsBlockJobs(vm, &modern) < 0)
         goto endjob;

-    if (!(device = qemuDiskPathToAlias(vm, path, NULL)))
+    if (!(disk = qemuDomainDiskByName(vm->def, path)))
+        goto endjob;
+
+    if (!(device = qemuAliasFromDisk(disk)))
         goto endjob;

     qemuDomainObjEnterMonitor(driver, vm);
@@ -16593,7 +16572,6 @@ qemuDomainBlockCopyCommon(virDomainObjPtr vm,
     char *device = NULL;
     virDomainDiskDefPtr disk = NULL;
     int ret = -1;
-    int idx;
     struct stat st;
     bool need_unlink = false;
     virQEMUDriverConfigPtr cfg = NULL;
@@ -16616,10 +16594,12 @@ qemuDomainBlockCopyCommon(virDomainObjPtr vm,
         goto endjob;
     }

-    device = qemuDiskPathToAlias(vm, path, &idx);
-    if (!device)
+    if (!(disk = qemuDomainDiskByName(vm->def, path)))
+        goto endjob;
+
+    if (!(device = qemuAliasFromDisk(disk)))
         goto endjob;
-    disk = vm->def->disks[idx];
+
     if (qemuDomainDiskBlockJobIsActive(disk))
         goto endjob;

@@ -16950,7 +16930,6 @@ qemuDomainBlockCommit(virDomainPtr dom,
     virDomainObjPtr vm = NULL;
     char *device = NULL;
     int ret = -1;
-    int idx;
     virDomainDiskDefPtr disk = NULL;
     virStorageSourcePtr topSource;
     unsigned int topIndex = 0;
@@ -17005,10 +16984,11 @@ qemuDomainBlockCommit(virDomainPtr dom,
         speed <<= 20;
     }

-    device = qemuDiskPathToAlias(vm, path, &idx);
-    if (!device)
+    if (!(disk = qemuDomainDiskByName(vm->def, path)))
+        goto endjob;
+
+    if (!(device = qemuAliasFromDisk(disk)))
         goto endjob;
-    disk = vm->def->disks[idx];

     if (!disk->src->path) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -17325,7 +17305,7 @@ qemuDomainOpenGraphicsFD(virDomainPtr dom,

 static int
 qemuDomainSetBlockIoTune(virDomainPtr dom,
-                         const char *disk,
+                         const char *path,
                          virTypedParameterPtr params,
                          int nparams,
                          unsigned int flags)
@@ -17339,8 +17319,8 @@ qemuDomainSetBlockIoTune(virDomainPtr dom,
     char *device = NULL;
     int ret = -1;
     size_t i;
-    int idx = -1;
     virDomainDiskDefPtr conf_disk = NULL;
+    virDomainDiskDefPtr disk;
     bool set_bytes = false;
     bool set_iops = false;
     bool set_bytes_max = false;
@@ -17409,7 +17389,7 @@ qemuDomainSetBlockIoTune(virDomainPtr dom,
         goto endjob;

     if (virTypedParamsAddString(&eventParams, &eventNparams, &eventMaxparams,
-                                VIR_DOMAIN_TUNABLE_BLKDEV_DISK, disk) < 0)
+                                VIR_DOMAIN_TUNABLE_BLKDEV_DISK, path) < 0)
         goto endjob;

     for (i = 0; i < nparams; i++) {
@@ -17574,10 +17554,10 @@ qemuDomainSetBlockIoTune(virDomainPtr dom,
     }

     if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
-        if (!(conf_disk = virDomainDiskByName(persistentDef, disk, true))) {
+        if (!(conf_disk = virDomainDiskByName(persistentDef, path, true))) {
             virReportError(VIR_ERR_INVALID_ARG,
                            _("missing persistent configuration for disk '%s'"),
-                           disk);
+                           path);
             goto endjob;
         }
     }
@@ -17600,13 +17580,16 @@ qemuDomainSetBlockIoTune(virDomainPtr dom,
              goto endjob;
         }

-        if (!(device = qemuDiskPathToAlias(vm, disk, &idx)))
+        if (!(disk = qemuDomainDiskByName(vm->def, path)))
+            goto endjob;
+
+        if (!(device = qemuAliasFromDisk(disk)))
             goto endjob;

         /* If the user didn't specify bytes limits, inherit previous
          * values; likewise if the user didn't specify iops
          * limits.  */
-        oldinfo = &vm->def->disks[idx]->blkdeviotune;
+        oldinfo = &disk->blkdeviotune;
         if (!set_bytes) {
             info.total_bytes_sec = oldinfo->total_bytes_sec;
             info.read_bytes_sec = oldinfo->read_bytes_sec;
@@ -17636,7 +17619,7 @@ qemuDomainSetBlockIoTune(virDomainPtr dom,
             ret = -1;
         if (ret < 0)
             goto endjob;
-        vm->def->disks[idx]->blkdeviotune = info;
+        disk->blkdeviotune = info;

         ret = virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps);
         if (ret < 0)
@@ -17683,11 +17666,12 @@ qemuDomainSetBlockIoTune(virDomainPtr dom,

 static int
 qemuDomainGetBlockIoTune(virDomainPtr dom,
-                         const char *disk,
+                         const char *path,
                          virTypedParameterPtr params,
                          int *nparams,
                          unsigned int flags)
 {
+    virDomainDiskDefPtr disk;
     virQEMUDriverPtr driver = dom->conn->privateData;
     virDomainObjPtr vm = NULL;
     qemuDomainObjPrivatePtr priv = NULL;
@@ -17747,8 +17731,10 @@ qemuDomainGetBlockIoTune(virDomainPtr dom,
     }

     if (flags & VIR_DOMAIN_AFFECT_LIVE) {
-        device = qemuDiskPathToAlias(vm, disk, NULL);
-        if (!device)
+        if (!(disk = qemuDomainDiskByName(vm->def, path)))
+            goto endjob;
+
+        if (!(device = qemuAliasFromDisk(disk)))
             goto endjob;
         qemuDomainObjEnterMonitor(driver, vm);
         ret = qemuMonitorGetBlockIoThrottle(priv->mon, device, &reply, supportMaxOptions);
@@ -17759,14 +17745,13 @@ qemuDomainGetBlockIoTune(virDomainPtr dom,
     }

     if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
-        virDomainDiskDefPtr diskDef;
-        if (!(diskDef = virDomainDiskByName(persistentDef, disk, true))) {
+        if (!(disk = virDomainDiskByName(persistentDef, path, true))) {
             virReportError(VIR_ERR_INVALID_ARG,
                            _("disk '%s' was not found in the domain config"),
-                           disk);
+                           path);
             goto endjob;
         }
-        reply = diskDef->blkdeviotune;
+        reply = disk->blkdeviotune;
     }

     for (i = 0; i < QEMU_NB_BLOCK_IO_TUNE_PARAM_MAX && i < *nparams; i++) {
-- 
2.7.3


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