qemuAddSharedHostdev() has a code similar to qemuRemoveSharedHostdev(), with exception of one line that defines the operation (add or remove). This patch introduces a new function that aggregates the common code, using a flag to switch between the operations, avoiding code repetition. No functional change was made. Signed-off-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx> --- src/qemu/qemu_conf.c | 93 ++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 56 deletions(-) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index d771bb6916..a583440807 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1722,9 +1722,33 @@ qemuGetHostdevPath(virDomainHostdevDefPtr hostdev) static int -qemuAddSharedHostdev(virQEMUDriverPtr driver, - virDomainHostdevDefPtr hostdev, - const char *name) +qemuSharedDeviceEntryRemove(virQEMUDriverPtr driver, + const char *key, + const char *name) +{ + qemuSharedDeviceEntryPtr entry = NULL; + int idx; + + if (!(entry = virHashLookup(driver->sharedDevices, key))) + return -1; + + /* Nothing to do if the shared disk is not recored in the table. */ + if (!qemuSharedDeviceEntryDomainExists(entry, name, &idx)) + return 0; + + if (entry->ref != 1) + VIR_DELETE_ELEMENT(entry->domains, idx, entry->ref); + else + ignore_value(virHashRemoveEntry(driver->sharedDevices, key)); + + return 0; +} + + +static int +qemuAddRemoveSharedHostdevInternal(virQEMUDriverPtr driver, + virDomainHostdevDefPtr hostdev, + const char *name, bool addDevice) { char *dev_path = NULL; char *key = NULL; @@ -1740,37 +1764,19 @@ qemuAddSharedHostdev(virQEMUDriverPtr driver, goto cleanup; qemuDriverLock(driver); - ret = qemuSharedDeviceEntryInsert(driver, key, name); + + if (addDevice) + ret = qemuSharedDeviceEntryInsert(driver, key, name); + else + ret = qemuSharedDeviceEntryRemove(driver, key, name); + qemuDriverUnlock(driver); cleanup: VIR_FREE(dev_path); VIR_FREE(key); return ret; -} - - -static int -qemuSharedDeviceEntryRemove(virQEMUDriverPtr driver, - const char *key, - const char *name) -{ - qemuSharedDeviceEntryPtr entry = NULL; - int idx; - - if (!(entry = virHashLookup(driver->sharedDevices, key))) - return -1; - - /* Nothing to do if the shared disk is not recored in the table. */ - if (!qemuSharedDeviceEntryDomainExists(entry, name, &idx)) - return 0; - if (entry->ref != 1) - VIR_DELETE_ELEMENT(entry->domains, idx, entry->ref); - else - ignore_value(virHashRemoveEntry(driver->sharedDevices, key)); - - return 0; } @@ -1795,7 +1801,8 @@ qemuAddSharedDevice(virQEMUDriverPtr driver, if (dev->type == VIR_DOMAIN_DEVICE_DISK) return qemuAddSharedDisk(driver, dev->data.disk, name); else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) - return qemuAddSharedHostdev(driver, dev->data.hostdev, name); + return qemuAddRemoveSharedHostdevInternal(driver, dev->data.hostdev, + name, true); else return 0; } @@ -1830,33 +1837,6 @@ qemuRemoveSharedDisk(virQEMUDriverPtr driver, } -static int -qemuRemoveSharedHostdev(virQEMUDriverPtr driver, - virDomainHostdevDefPtr hostdev, - const char *name) -{ - char *dev_path = NULL; - char *key = NULL; - int ret = -1; - - if (!qemuIsSharedHostdev(hostdev)) - return 0; - - if (!(dev_path = qemuGetHostdevPath(hostdev))) - goto cleanup; - - if (!(key = qemuGetSharedDeviceKey(dev_path))) - goto cleanup; - - qemuDriverLock(driver); - ret = qemuSharedDeviceEntryRemove(driver, key, name); - qemuDriverUnlock(driver); - - cleanup: - VIR_FREE(dev_path); - VIR_FREE(key); - return ret; -} /* qemuRemoveSharedDevice: @@ -1876,7 +1856,8 @@ qemuRemoveSharedDevice(virQEMUDriverPtr driver, if (dev->type == VIR_DOMAIN_DEVICE_DISK) return qemuRemoveSharedDisk(driver, dev->data.disk, name); else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) - return qemuRemoveSharedHostdev(driver, dev->data.hostdev, name); + return qemuAddRemoveSharedHostdevInternal(driver, dev->data.hostdev, + name, false); else return 0; } -- 2.21.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list