mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
qemu: shared: Split out insertion code to the shared device list
To allow reuse split the code into a separate function and refactor it. To update an existing entry there's no need to copy it first, just update it inplace.
This commit is contained in:
parent
b57ca1320c
commit
f36a94f236
@ -949,6 +949,47 @@ qemuSharedDeviceEntryCopy(const qemuSharedDeviceEntry *entry)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
qemuSharedDeviceEntryInsert(virQEMUDriverPtr driver,
|
||||||
|
const char *key,
|
||||||
|
const char *name)
|
||||||
|
{
|
||||||
|
qemuSharedDeviceEntry *entry = NULL;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if ((entry = virHashLookup(driver->sharedDevices, key))) {
|
||||||
|
/* Nothing to do if the shared scsi host device is already
|
||||||
|
* recorded in the table.
|
||||||
|
*/
|
||||||
|
if (qemuSharedDeviceEntryDomainExists(entry, name, NULL)) {
|
||||||
|
ret = 0;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (VIR_EXPAND_N(entry->domains, entry->ref, 1) < 0 ||
|
||||||
|
VIR_STRDUP(entry->domains[entry->ref - 1], name) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
} else {
|
||||||
|
if (VIR_ALLOC(entry) < 0 ||
|
||||||
|
VIR_ALLOC_N(entry->domains, 1) < 0 ||
|
||||||
|
VIR_STRDUP(entry->domains[0], name) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
entry->ref = 1;
|
||||||
|
|
||||||
|
if (virHashAddEntry(driver->sharedDevices, key, entry))
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
qemuSharedDeviceEntryFree(entry, NULL);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/* qemuAddSharedDevice:
|
/* qemuAddSharedDevice:
|
||||||
* @driver: Pointer to qemu driver struct
|
* @driver: Pointer to qemu driver struct
|
||||||
* @dev: The device def
|
* @dev: The device def
|
||||||
@ -963,8 +1004,6 @@ qemuAddSharedDevice(virQEMUDriverPtr driver,
|
|||||||
virDomainDeviceDefPtr dev,
|
virDomainDeviceDefPtr dev,
|
||||||
const char *name)
|
const char *name)
|
||||||
{
|
{
|
||||||
qemuSharedDeviceEntry *entry = NULL;
|
|
||||||
qemuSharedDeviceEntry *new_entry = NULL;
|
|
||||||
virDomainDiskDefPtr disk = NULL;
|
virDomainDiskDefPtr disk = NULL;
|
||||||
virDomainHostdevDefPtr hostdev = NULL;
|
virDomainHostdevDefPtr hostdev = NULL;
|
||||||
char *dev_name = NULL;
|
char *dev_name = NULL;
|
||||||
@ -1016,43 +1055,11 @@ qemuAddSharedDevice(virQEMUDriverPtr driver,
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((entry = virHashLookup(driver->sharedDevices, key))) {
|
if (qemuSharedDeviceEntryInsert(driver, key, name) < 0)
|
||||||
/* Nothing to do if the shared scsi host device is already
|
|
||||||
* recorded in the table.
|
|
||||||
*/
|
|
||||||
if (qemuSharedDeviceEntryDomainExists(entry, name, NULL)) {
|
|
||||||
ret = 0;
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
|
||||||
|
|
||||||
if (!(new_entry = qemuSharedDeviceEntryCopy(entry)))
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
if (VIR_EXPAND_N(new_entry->domains, new_entry->ref, 1) < 0 ||
|
|
||||||
VIR_STRDUP(new_entry->domains[new_entry->ref - 1], name) < 0) {
|
|
||||||
qemuSharedDeviceEntryFree(new_entry, NULL);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (virHashUpdateEntry(driver->sharedDevices, key, new_entry) < 0) {
|
|
||||||
qemuSharedDeviceEntryFree(new_entry, NULL);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (VIR_ALLOC(entry) < 0 ||
|
|
||||||
VIR_ALLOC_N(entry->domains, 1) < 0 ||
|
|
||||||
VIR_STRDUP(entry->domains[0], name) < 0) {
|
|
||||||
qemuSharedDeviceEntryFree(entry, NULL);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
entry->ref = 1;
|
|
||||||
|
|
||||||
if (virHashAddEntry(driver->sharedDevices, key, entry))
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
qemuDriverUnlock(driver);
|
qemuDriverUnlock(driver);
|
||||||
VIR_FREE(dev_name);
|
VIR_FREE(dev_name);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user