qemu: Don't fail if the SCSI host device is shareable between domains

It doesn't make sense to fail if the SCSI host device is specified
as "shareable" explicitly between domains (NB, it works if and only
if the device is specified as "shareable" for *all* domains,
otherwise it fails).

To fix the problem, this patch introduces an array for virSCSIDevice
struct, which records all the names of domain which are using the
device (note that the recorded domains must specify the device as
shareable).  And the change on the data struct brings on many
subsequent changes in the code.

Prior to this patch, the "shareable" tag didn't work as expected,
it actually work like "non-shareable".  So this patch also added notes
in formatdomain.html to declare the fact.

* src/util/virscsi.h:
  - Remove virSCSIDeviceGetUsedBy
  - Change definition of virSCSIDeviceGetUsedBy and virSCSIDeviceListDel
  - Add virSCSIDeviceIsAvailable

* src/util/virscsi.c:
  - struct virSCSIDevice: Change "used_by" to be an array; Add
    "n_used_by" as the array count
  - virSCSIDeviceGetUsedBy: Removed
  - virSCSIDeviceFree: frees the "used_by" array
  - virSCSIDeviceSetUsedBy: Copy the domain name to avoid potential
    memory corruption
  - virSCSIDeviceIsAvailable: New
  - virSCSIDeviceListDel: Change the logic, for device which is already
    in the list, just remove the corresponding entry in "used_by". And
    since it's only used in one place, we can safely removing the code
    to find out the dev in the list first.
  - Copyright updating

* src/libvirt_private.sys:
  - virSCSIDeviceGetUsedBy: Remove
  - virSCSIDeviceIsAvailable: New

* src/qemu/qemu_hostdev.c:
  - qemuUpdateActiveScsiHostdevs: Check if the device existing before
    adding it to the list;
  - qemuPrepareHostdevSCSIDevices: Error out if the not all domains
    use the device as "shareable"; Also don't try to add the device
    to the activeScsiHostdevs list if it already there; And make
    more sensible error w.r.t the current "shareable" value in
    driver->activeScsiHostdevs.
  - qemuDomainReAttachHostScsiDevices: Change the logic according
    to the changes on helpers.

Signed-off-by: Osier Yang <jyang@redhat.com>
This commit is contained in:
Osier Yang 2014-01-30 01:22:42 +08:00
parent d779d218d4
commit fd243fc4ad
5 changed files with 89 additions and 48 deletions

View File

@ -2798,6 +2798,11 @@
between domains (assuming the hypervisor and OS support this). between domains (assuming the hypervisor and OS support this).
Only supported by SCSI host device. Only supported by SCSI host device.
<span class="since">Since 1.0.6</span> <span class="since">Since 1.0.6</span>
<p>
Note: Although <code>shareable</code> was introduced
<span class="since">in 1.0.6</span>, it did not work as
as expected until <span class="since">1.2.2</span>.
</p>
</dd> </dd>
</dl> </dl>

View File

@ -1687,7 +1687,7 @@ virSCSIDeviceGetSgName;
virSCSIDeviceGetShareable; virSCSIDeviceGetShareable;
virSCSIDeviceGetTarget; virSCSIDeviceGetTarget;
virSCSIDeviceGetUnit; virSCSIDeviceGetUnit;
virSCSIDeviceGetUsedBy; virSCSIDeviceIsAvailable;
virSCSIDeviceListAdd; virSCSIDeviceListAdd;
virSCSIDeviceListCount; virSCSIDeviceListCount;
virSCSIDeviceListDel; virSCSIDeviceListDel;

View File

@ -250,13 +250,14 @@ qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver,
virDomainHostdevDefPtr hostdev = NULL; virDomainHostdevDefPtr hostdev = NULL;
size_t i; size_t i;
int ret = -1; int ret = -1;
virSCSIDevicePtr scsi = NULL;
virSCSIDevicePtr tmp = NULL;
if (!def->nhostdevs) if (!def->nhostdevs)
return 0; return 0;
virObjectLock(driver->activeScsiHostdevs); virObjectLock(driver->activeScsiHostdevs);
for (i = 0; i < def->nhostdevs; i++) { for (i = 0; i < def->nhostdevs; i++) {
virSCSIDevicePtr scsi = NULL;
hostdev = def->hostdevs[i]; hostdev = def->hostdevs[i];
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS || if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
@ -271,11 +272,18 @@ qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver,
hostdev->shareable))) hostdev->shareable)))
goto cleanup; goto cleanup;
virSCSIDeviceSetUsedBy(scsi, def->name); if ((tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) {
if (virSCSIDeviceSetUsedBy(tmp, def->name) < 0) {
if (virSCSIDeviceListAdd(driver->activeScsiHostdevs, scsi) < 0) { virSCSIDeviceFree(scsi);
goto cleanup;
}
virSCSIDeviceFree(scsi); virSCSIDeviceFree(scsi);
goto cleanup; } else {
if (virSCSIDeviceSetUsedBy(scsi, def->name) < 0 ||
virSCSIDeviceListAdd(driver->activeScsiHostdevs, scsi) < 0) {
virSCSIDeviceFree(scsi);
goto cleanup;
}
} }
} }
ret = 0; ret = 0;
@ -1118,24 +1126,29 @@ qemuPrepareHostdevSCSIDevices(virQEMUDriverPtr driver,
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
virSCSIDevicePtr scsi = virSCSIDeviceListGet(list, i); virSCSIDevicePtr scsi = virSCSIDeviceListGet(list, i);
if ((tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) { if ((tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) {
const char *other_name = virSCSIDeviceGetUsedBy(tmp); bool scsi_shareable = virSCSIDeviceGetShareable(scsi);
bool tmp_shareable = virSCSIDeviceGetShareable(tmp);
if (other_name) if (!(scsi_shareable && tmp_shareable)) {
virReportError(VIR_ERR_OPERATION_INVALID, virReportError(VIR_ERR_OPERATION_INVALID,
_("SCSI device %s is in use by domain %s"), _("SCSI device %s is already in use by "
virSCSIDeviceGetName(tmp), other_name); "other domain(s) as '%s'"),
else tmp_shareable ? "shareable" : "non-shareable",
virReportError(VIR_ERR_OPERATION_INVALID,
_("SCSI device %s is already in use"),
virSCSIDeviceGetName(tmp)); virSCSIDeviceGetName(tmp));
goto error; goto error;
}
if (virSCSIDeviceSetUsedBy(tmp, name) < 0)
goto error;
} else {
if (virSCSIDeviceSetUsedBy(scsi, name) < 0)
goto error;
VIR_DEBUG("Adding %s to activeScsiHostdevs", virSCSIDeviceGetName(scsi));
if (virSCSIDeviceListAdd(driver->activeScsiHostdevs, scsi) < 0)
goto error;
} }
virSCSIDeviceSetUsedBy(scsi, name);
VIR_DEBUG("Adding %s to activeScsiHostdevs", virSCSIDeviceGetName(scsi));
if (virSCSIDeviceListAdd(driver->activeScsiHostdevs, scsi) < 0)
goto error;
} }
virObjectUnlock(driver->activeScsiHostdevs); virObjectUnlock(driver->activeScsiHostdevs);
@ -1380,8 +1393,8 @@ qemuDomainReAttachHostScsiDevices(virQEMUDriverPtr driver,
virObjectLock(driver->activeScsiHostdevs); virObjectLock(driver->activeScsiHostdevs);
for (i = 0; i < nhostdevs; i++) { for (i = 0; i < nhostdevs; i++) {
virDomainHostdevDefPtr hostdev = hostdevs[i]; virDomainHostdevDefPtr hostdev = hostdevs[i];
virSCSIDevicePtr scsi, tmp; virSCSIDevicePtr scsi;
const char *used_by = NULL; virSCSIDevicePtr tmp;
virDomainDeviceDef dev; virDomainDeviceDef dev;
dev.type = VIR_DOMAIN_DEVICE_HOSTDEV; dev.type = VIR_DOMAIN_DEVICE_HOSTDEV;
@ -1411,30 +1424,26 @@ qemuDomainReAttachHostScsiDevices(virQEMUDriverPtr driver,
/* Only delete the devices which are marked as being used by @name, /* Only delete the devices which are marked as being used by @name,
* because qemuProcessStart could fail on the half way. */ * because qemuProcessStart could fail on the half way. */
tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi); if (!(tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) {
virSCSIDeviceFree(scsi);
if (!tmp) {
VIR_WARN("Unable to find device %s:%d:%d:%d " VIR_WARN("Unable to find device %s:%d:%d:%d "
"in list of active SCSI devices", "in list of active SCSI devices",
hostdev->source.subsys.u.scsi.adapter, hostdev->source.subsys.u.scsi.adapter,
hostdev->source.subsys.u.scsi.bus, hostdev->source.subsys.u.scsi.bus,
hostdev->source.subsys.u.scsi.target, hostdev->source.subsys.u.scsi.target,
hostdev->source.subsys.u.scsi.unit); hostdev->source.subsys.u.scsi.unit);
virSCSIDeviceFree(scsi);
continue; continue;
} }
used_by = virSCSIDeviceGetUsedBy(tmp); VIR_DEBUG("Removing %s:%d:%d:%d dom=%s from activeScsiHostdevs",
if (STREQ_NULLABLE(used_by, name)) { hostdev->source.subsys.u.scsi.adapter,
VIR_DEBUG("Removing %s:%d:%d:%d dom=%s from activeScsiHostdevs", hostdev->source.subsys.u.scsi.bus,
hostdev->source.subsys.u.scsi.adapter, hostdev->source.subsys.u.scsi.target,
hostdev->source.subsys.u.scsi.bus, hostdev->source.subsys.u.scsi.unit,
hostdev->source.subsys.u.scsi.target, name);
hostdev->source.subsys.u.scsi.unit,
name);
virSCSIDeviceListDel(driver->activeScsiHostdevs, tmp); virSCSIDeviceListDel(driver->activeScsiHostdevs, tmp, name);
} virSCSIDeviceFree(scsi);
} }
virObjectUnlock(driver->activeScsiHostdevs); virObjectUnlock(driver->activeScsiHostdevs);
} }

View File

@ -1,6 +1,7 @@
/* /*
* virscsi.c: helper APIs for managing host SCSI devices * virscsi.c: helper APIs for managing host SCSI devices
* *
* Copyright (C) 2013-2014 Red Hat, Inc.
* Copyright (C) 2013 Fujitsu, Inc. * Copyright (C) 2013 Fujitsu, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -19,6 +20,7 @@
* *
* Authors: * Authors:
* Han Cheng <hanc.fnst@cn.fujitsu.com> * Han Cheng <hanc.fnst@cn.fujitsu.com>
* Osier Yang <jyang@redhat.com>
*/ */
#include <config.h> #include <config.h>
@ -55,7 +57,8 @@ struct _virSCSIDevice {
char *name; /* adapter:bus:target:unit */ char *name; /* adapter:bus:target:unit */
char *id; /* model:vendor */ char *id; /* model:vendor */
char *sg_path; /* e.g. /dev/sg2 */ char *sg_path; /* e.g. /dev/sg2 */
const char *used_by; /* name of the domain using this dev */ char **used_by; /* name of the domains using this dev */
size_t n_used_by; /* how many domains are using this dev */
bool readonly; bool readonly;
bool shareable; bool shareable;
@ -256,26 +259,36 @@ cleanup:
void void
virSCSIDeviceFree(virSCSIDevicePtr dev) virSCSIDeviceFree(virSCSIDevicePtr dev)
{ {
size_t i;
if (!dev) if (!dev)
return; return;
VIR_FREE(dev->id); VIR_FREE(dev->id);
VIR_FREE(dev->name); VIR_FREE(dev->name);
VIR_FREE(dev->sg_path); VIR_FREE(dev->sg_path);
for (i = 0; i < dev->n_used_by; i++)
VIR_FREE(dev->used_by[i]);
VIR_FREE(dev->used_by);
VIR_FREE(dev); VIR_FREE(dev);
} }
void int
virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev, virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev,
const char *name) const char *name)
{ {
dev->used_by = name; char *copy = NULL;
if (VIR_STRDUP(copy, name) < 0)
return -1;
return VIR_APPEND_ELEMENT(dev->used_by, dev->n_used_by, copy);
} }
const char * bool
virSCSIDeviceGetUsedBy(virSCSIDevicePtr dev) virSCSIDeviceIsAvailable(virSCSIDevicePtr dev)
{ {
return dev->used_by; return dev->n_used_by == 0;
} }
const char * const char *
@ -406,10 +419,23 @@ virSCSIDeviceListSteal(virSCSIDeviceListPtr list,
void void
virSCSIDeviceListDel(virSCSIDeviceListPtr list, virSCSIDeviceListDel(virSCSIDeviceListPtr list,
virSCSIDevicePtr dev) virSCSIDevicePtr dev,
const char *name)
{ {
virSCSIDevicePtr ret = virSCSIDeviceListSteal(list, dev); virSCSIDevicePtr tmp = NULL;
virSCSIDeviceFree(ret); size_t i;
for (i = 0; i < dev->n_used_by; i++) {
if (STREQ_NULLABLE(dev->used_by[i], name)) {
if (dev->n_used_by > 1) {
VIR_DELETE_ELEMENT(dev->used_by, i, dev->n_used_by);
} else {
tmp = virSCSIDeviceListSteal(list, dev);
virSCSIDeviceFree(tmp);
}
break;
}
}
} }
virSCSIDevicePtr virSCSIDevicePtr

View File

@ -50,8 +50,8 @@ virSCSIDevicePtr virSCSIDeviceNew(const char *adapter,
bool shareable); bool shareable);
void virSCSIDeviceFree(virSCSIDevicePtr dev); void virSCSIDeviceFree(virSCSIDevicePtr dev);
void virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev, const char *name); int virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev, const char *name);
const char *virSCSIDeviceGetUsedBy(virSCSIDevicePtr dev); bool virSCSIDeviceIsAvailable(virSCSIDevicePtr dev);
const char *virSCSIDeviceGetName(virSCSIDevicePtr dev); const char *virSCSIDeviceGetName(virSCSIDevicePtr dev);
unsigned int virSCSIDeviceGetAdapter(virSCSIDevicePtr dev); unsigned int virSCSIDeviceGetAdapter(virSCSIDevicePtr dev);
unsigned int virSCSIDeviceGetBus(virSCSIDevicePtr dev); unsigned int virSCSIDeviceGetBus(virSCSIDevicePtr dev);
@ -83,7 +83,8 @@ size_t virSCSIDeviceListCount(virSCSIDeviceListPtr list);
virSCSIDevicePtr virSCSIDeviceListSteal(virSCSIDeviceListPtr list, virSCSIDevicePtr virSCSIDeviceListSteal(virSCSIDeviceListPtr list,
virSCSIDevicePtr dev); virSCSIDevicePtr dev);
void virSCSIDeviceListDel(virSCSIDeviceListPtr list, void virSCSIDeviceListDel(virSCSIDeviceListPtr list,
virSCSIDevicePtr dev); virSCSIDevicePtr dev,
const char *name);
virSCSIDevicePtr virSCSIDeviceListFind(virSCSIDeviceListPtr list, virSCSIDevicePtr virSCSIDeviceListFind(virSCSIDeviceListPtr list,
virSCSIDevicePtr dev); virSCSIDevicePtr dev);