提交 b80bb2d3 编写于 作者: D Daniel Henrique Barboza 提交者: Pavel Hrdina

qemu_conf.c: introduce qemuAddRemoveSharedDiskInternal

Following the same idea of avoid code repetition from the
previous patch, this commit introduces a new function that
aggregates the functions of qemuAddSharedDisk() and
qemuRemoveSharedDisk() into a single place, using a flag to
switch between add/remove operations.

Both qemuAddSharedDisk() and qemuRemoveSharedDisk() are
public, so keep them around to avoid changing other files
due to an internal qemu_conf.c refactory.

No functional change was made.
Signed-off-by: NDaniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: NPavel Hrdina <phrdina@redhat.com>
上级 b2de989b
......@@ -1654,19 +1654,35 @@ qemuSharedDeviceEntryInsert(virQEMUDriverPtr driver,
}
/* qemuAddSharedDisk:
* @driver: Pointer to qemu driver struct
* @src: disk source
* @name: The domain name
*
* Increase ref count and add the domain name into the list which
* records all the domains that use the shared device if the entry
* already exists, otherwise add a new entry.
*/
int
qemuAddSharedDisk(virQEMUDriverPtr driver,
virDomainDiskDefPtr disk,
const char *name)
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;
}
static int
qemuAddRemoveSharedDiskInternal(virQEMUDriverPtr driver,
virDomainDiskDefPtr disk,
const char *name,
bool addDisk)
{
char *key = NULL;
int ret = -1;
......@@ -1678,17 +1694,21 @@ qemuAddSharedDisk(virQEMUDriverPtr driver,
qemuDriverLock(driver);
if (qemuCheckSharedDisk(driver->sharedDevices, disk) < 0)
goto cleanup;
if (!(key = qemuGetSharedDeviceKey(virDomainDiskGetSource(disk))))
goto cleanup;
if (qemuSharedDeviceEntryInsert(driver, key, name) < 0)
goto cleanup;
if (addDisk) {
if (qemuCheckSharedDisk(driver->sharedDevices, disk) < 0)
goto cleanup;
ret = 0;
if (qemuSharedDeviceEntryInsert(driver, key, name) < 0)
goto cleanup;
} else {
if (qemuSharedDeviceEntryRemove(driver, key, name) < 0)
goto cleanup;
}
ret = 0;
cleanup:
qemuDriverUnlock(driver);
VIR_FREE(key);
......@@ -1696,6 +1716,24 @@ qemuAddSharedDisk(virQEMUDriverPtr driver,
}
/* qemuAddSharedDisk:
* @driver: Pointer to qemu driver struct
* @src: disk source
* @name: The domain name
*
* Increase ref count and add the domain name into the list which
* records all the domains that use the shared device if the entry
* already exists, otherwise add a new entry.
*/
int
qemuAddSharedDisk(virQEMUDriverPtr driver,
virDomainDiskDefPtr disk,
const char *name)
{
return qemuAddRemoveSharedDiskInternal(driver, disk, name, true);
}
static bool
qemuIsSharedHostdev(virDomainHostdevDefPtr hostdev)
{
......@@ -1729,30 +1767,6 @@ qemuGetHostdevPath(virDomainHostdevDefPtr hostdev)
}
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;
}
static int
qemuAddRemoveSharedHostdevInternal(virQEMUDriverPtr driver,
virDomainHostdevDefPtr hostdev,
......@@ -1808,7 +1822,8 @@ qemuAddSharedDevice(virQEMUDriverPtr driver,
* which is only valid for block disk and scsi host device.
*/
if (dev->type == VIR_DOMAIN_DEVICE_DISK)
return qemuAddSharedDisk(driver, dev->data.disk, name);
return qemuAddRemoveSharedDiskInternal(driver, dev->data.disk,
name, true);
else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV)
return qemuAddRemoveSharedHostdevInternal(driver, dev->data.hostdev,
name, true);
......@@ -1822,32 +1837,10 @@ qemuRemoveSharedDisk(virQEMUDriverPtr driver,
virDomainDiskDefPtr disk,
const char *name)
{
char *key = NULL;
int ret = -1;
if (virStorageSourceIsEmpty(disk->src) ||
!disk->src->shared ||
!virStorageSourceIsBlockLocal(disk->src))
return 0;
qemuDriverLock(driver);
if (!(key = qemuGetSharedDeviceKey(virDomainDiskGetSource(disk))))
goto cleanup;
if (qemuSharedDeviceEntryRemove(driver, key, name) < 0)
goto cleanup;
ret = 0;
cleanup:
qemuDriverUnlock(driver);
VIR_FREE(key);
return ret;
return qemuAddRemoveSharedDiskInternal(driver, disk, name, false);
}
/* qemuRemoveSharedDevice:
* @driver: Pointer to qemu driver struct
* @device: The device def
......@@ -1863,7 +1856,8 @@ qemuRemoveSharedDevice(virQEMUDriverPtr driver,
const char *name)
{
if (dev->type == VIR_DOMAIN_DEVICE_DISK)
return qemuRemoveSharedDisk(driver, dev->data.disk, name);
return qemuAddRemoveSharedDiskInternal(driver, dev->data.disk,
name, false);
else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV)
return qemuAddRemoveSharedHostdevInternal(driver, dev->data.hostdev,
name, false);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册