1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-01-25 10:03:49 +03:00

qemu_conf.c: introduce qemuAddRemoveSharedHostdevInternal

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@gmail.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Daniel Henrique Barboza 2019-09-03 20:06:05 -03:00 committed by Pavel Hrdina
parent 2029f8269b
commit b2de989b9d

View File

@ -1729,35 +1729,6 @@ qemuGetHostdevPath(virDomainHostdevDefPtr hostdev)
}
static int
qemuAddSharedHostdev(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 = qemuSharedDeviceEntryInsert(driver, key, name);
qemuDriverUnlock(driver);
cleanup:
VIR_FREE(dev_path);
VIR_FREE(key);
return ret;
}
static int
qemuSharedDeviceEntryRemove(virQEMUDriverPtr driver,
const char *key,
@ -1782,6 +1753,42 @@ qemuSharedDeviceEntryRemove(virQEMUDriverPtr driver,
}
static int
qemuAddRemoveSharedHostdevInternal(virQEMUDriverPtr driver,
virDomainHostdevDefPtr hostdev,
const char *name,
bool addDevice)
{
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);
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;
}
/* qemuAddSharedDevice:
* @driver: Pointer to qemu driver struct
* @dev: The device def
@ -1803,7 +1810,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;
}
@ -1838,33 +1846,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:
@ -1884,7 +1865,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;
}