2008-11-21 12:27:11 +00:00
|
|
|
/*
|
|
|
|
* node_device.c: node device enumeration
|
|
|
|
*
|
2010-08-18 19:43:26 +00:00
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
2008-11-21 12:27:11 +00:00
|
|
|
* Copyright (C) 2008 Virtual Iron Software, Inc.
|
|
|
|
* Copyright (C) 2008 David F. Lively
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Author: David F. Lively <dlively@virtualiron.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2009-06-02 15:12:53 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
2010-08-18 19:43:26 +00:00
|
|
|
#include <sys/stat.h>
|
2008-11-21 12:27:11 +00:00
|
|
|
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
#include "datatypes.h"
|
|
|
|
#include "memory.h"
|
2008-12-04 21:48:31 +00:00
|
|
|
#include "logging.h"
|
2008-11-21 12:27:11 +00:00
|
|
|
#include "node_device_conf.h"
|
2009-06-26 14:09:01 +00:00
|
|
|
#include "node_device_hal.h"
|
2009-09-15 17:30:17 +00:00
|
|
|
#include "node_device_driver.h"
|
2009-09-10 13:21:10 +00:00
|
|
|
#include "util.h"
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2009-01-29 12:10:32 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NODEDEV
|
|
|
|
|
2009-06-26 14:09:01 +00:00
|
|
|
|
|
|
|
static int update_caps(virNodeDeviceObjPtr dev)
|
|
|
|
{
|
|
|
|
virNodeDevCapsDefPtr cap = dev->def->caps;
|
|
|
|
|
|
|
|
while (cap) {
|
2010-06-03 03:24:51 +00:00
|
|
|
/* The only caps that currently need updating are FC related. */
|
2009-06-26 14:09:01 +00:00
|
|
|
if (cap->type == VIR_NODE_DEV_CAP_SCSI_HOST) {
|
2010-06-03 03:24:51 +00:00
|
|
|
check_fc_host(&dev->def->caps->data);
|
2009-06-26 14:09:01 +00:00
|
|
|
}
|
2009-07-01 21:30:41 +00:00
|
|
|
cap = cap->next;
|
2009-06-26 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-12 21:48:24 +00:00
|
|
|
#if defined (__linux__) && defined (HAVE_HAL)
|
|
|
|
/* Under libudev changes to the driver name should be picked up as
|
|
|
|
* "change" events, so we don't call update driver name unless we're
|
|
|
|
* using the HAL backend. */
|
2010-02-04 20:02:58 +00:00
|
|
|
static int update_driver_name(virNodeDeviceObjPtr dev)
|
2009-06-12 13:12:55 +00:00
|
|
|
{
|
|
|
|
char *driver_link = NULL;
|
2009-12-23 21:18:04 +00:00
|
|
|
char *devpath = NULL;
|
2009-06-12 13:12:55 +00:00
|
|
|
char *p;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
VIR_FREE(dev->def->driver);
|
|
|
|
|
2009-11-19 15:02:18 +00:00
|
|
|
if (virAsprintf(&driver_link, "%s/driver", dev->def->sysfs_path) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-06-12 13:12:55 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some devices don't have an explicit driver, so just return
|
|
|
|
without a name */
|
|
|
|
if (access(driver_link, R_OK) < 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2009-12-14 11:05:38 +00:00
|
|
|
if (virFileResolveLink(driver_link, &devpath) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-06-12 13:12:55 +00:00
|
|
|
_("cannot resolve driver link %s"), driver_link);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strrchr(devpath, '/');
|
|
|
|
if (p) {
|
|
|
|
dev->def->driver = strdup(p+1);
|
|
|
|
if (!dev->def->driver) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-06-12 13:12:55 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(driver_link);
|
2009-12-23 21:18:04 +00:00
|
|
|
VIR_FREE(devpath);
|
2009-06-12 13:12:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* XXX: Implement me for non-linux */
|
2010-02-04 20:02:58 +00:00
|
|
|
static int update_driver_name(virNodeDeviceObjPtr dev ATTRIBUTE_UNUSED)
|
2009-06-12 13:12:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
void nodeDeviceLock(virDeviceMonitorStatePtr driver)
|
|
|
|
{
|
2009-01-15 19:56:05 +00:00
|
|
|
virMutexLock(&driver->lock);
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
|
|
|
void nodeDeviceUnlock(virDeviceMonitorStatePtr driver)
|
|
|
|
{
|
2009-01-15 19:56:05 +00:00
|
|
|
virMutexUnlock(&driver->lock);
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
|
|
|
nodeNumOfDevices(virConnectPtr conn,
|
|
|
|
const char *cap,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
|
|
|
int ndevs = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
2009-10-01 18:51:39 +00:00
|
|
|
nodeDeviceLock(driver);
|
|
|
|
for (i = 0; i < driver->devs.count; i++) {
|
|
|
|
virNodeDeviceObjLock(driver->devs.objs[i]);
|
2008-11-21 12:27:11 +00:00
|
|
|
if ((cap == NULL) ||
|
2009-10-01 18:53:43 +00:00
|
|
|
virNodeDeviceHasCap(driver->devs.objs[i], cap))
|
2008-11-21 12:27:11 +00:00
|
|
|
++ndevs;
|
2009-10-01 18:51:39 +00:00
|
|
|
virNodeDeviceObjUnlock(driver->devs.objs[i]);
|
|
|
|
}
|
|
|
|
nodeDeviceUnlock(driver);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
|
|
|
return ndevs;
|
|
|
|
}
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
2008-11-21 12:27:11 +00:00
|
|
|
nodeListDevices(virConnectPtr conn,
|
|
|
|
const char *cap,
|
|
|
|
char **const names, int maxnames,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
|
|
|
int ndevs = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
|
|
|
for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) {
|
|
|
|
virNodeDeviceObjLock(driver->devs.objs[i]);
|
2008-11-21 12:27:11 +00:00
|
|
|
if (cap == NULL ||
|
2009-10-01 18:53:43 +00:00
|
|
|
virNodeDeviceHasCap(driver->devs.objs[i], cap)) {
|
2008-12-04 21:48:31 +00:00
|
|
|
if ((names[ndevs++] = strdup(driver->devs.objs[i]->def->name)) == NULL) {
|
|
|
|
virNodeDeviceObjUnlock(driver->devs.objs[i]);
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2008-11-21 12:27:11 +00:00
|
|
|
goto failure;
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
virNodeDeviceObjUnlock(driver->devs.objs[i]);
|
|
|
|
}
|
|
|
|
nodeDeviceUnlock(driver);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
|
|
|
return ndevs;
|
|
|
|
|
|
|
|
failure:
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
2008-11-21 12:27:11 +00:00
|
|
|
--ndevs;
|
|
|
|
while (--ndevs >= 0)
|
|
|
|
VIR_FREE(names[ndevs]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
virNodeDevicePtr
|
|
|
|
nodeDeviceLookupByName(virConnectPtr conn, const char *name)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
virNodeDevicePtr ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = virGetNodeDevice(conn, name);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
static virNodeDevicePtr
|
|
|
|
nodeDeviceLookupByWWN(virConnectPtr conn,
|
|
|
|
const char *wwnn,
|
|
|
|
const char *wwpn)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
|
|
|
virNodeDeviceObjListPtr devs = &driver->devs;
|
|
|
|
virNodeDevCapsDefPtr cap = NULL;
|
|
|
|
virNodeDeviceObjPtr obj = NULL;
|
|
|
|
virNodeDevicePtr dev = NULL;
|
|
|
|
|
|
|
|
nodeDeviceLock(driver);
|
|
|
|
|
|
|
|
for (i = 0; i < devs->count; i++) {
|
|
|
|
|
|
|
|
obj = devs->objs[i];
|
|
|
|
virNodeDeviceObjLock(obj);
|
|
|
|
cap = obj->def->caps;
|
|
|
|
|
|
|
|
while (cap) {
|
|
|
|
|
|
|
|
if (cap->type == VIR_NODE_DEV_CAP_SCSI_HOST) {
|
2010-06-03 03:24:51 +00:00
|
|
|
check_fc_host(&cap->data);
|
2009-06-02 15:12:53 +00:00
|
|
|
if (cap->data.scsi_host.flags &
|
|
|
|
VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
|
|
|
|
|
|
|
|
if (STREQ(cap->data.scsi_host.wwnn, wwnn) &&
|
|
|
|
STREQ(cap->data.scsi_host.wwpn, wwpn)) {
|
|
|
|
dev = virGetNodeDevice(conn, obj->def->name);
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cap = cap->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
char *
|
|
|
|
nodeDeviceGetXMLDesc(virNodeDevicePtr dev,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
char *ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
2009-07-08 10:01:35 +00:00
|
|
|
dev->name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2010-02-04 20:02:58 +00:00
|
|
|
update_driver_name(obj);
|
2009-06-26 14:09:01 +00:00
|
|
|
update_caps(obj);
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
ret = virNodeDeviceDefFormat(obj->def);
|
2008-12-04 21:46:34 +00:00
|
|
|
|
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
char *
|
|
|
|
nodeDeviceGetParent(virNodeDevicePtr dev)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
char *ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
2009-07-08 10:01:35 +00:00
|
|
|
dev->name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2009-04-01 10:21:34 +00:00
|
|
|
if (obj->def->parent) {
|
|
|
|
ret = strdup(obj->def->parent);
|
|
|
|
if (!ret)
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-04-01 10:21:34 +00:00
|
|
|
} else {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-04-01 10:21:34 +00:00
|
|
|
"%s", _("no parent for this device"));
|
|
|
|
}
|
2008-12-04 21:46:34 +00:00
|
|
|
|
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
|
|
|
nodeDeviceNumOfCaps(virNodeDevicePtr dev)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
2008-11-21 12:27:11 +00:00
|
|
|
virNodeDevCapsDefPtr caps;
|
|
|
|
int ncaps = 0;
|
2008-12-04 21:46:34 +00:00
|
|
|
int ret = -1;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
2009-07-08 10:01:35 +00:00
|
|
|
dev->name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (caps = obj->def->caps; caps; caps = caps->next)
|
|
|
|
++ncaps;
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = ncaps;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
2008-11-21 12:27:11 +00:00
|
|
|
nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
2008-11-21 12:27:11 +00:00
|
|
|
virNodeDevCapsDefPtr caps;
|
|
|
|
int ncaps = 0;
|
2008-12-04 21:46:34 +00:00
|
|
|
int ret = -1;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
2009-07-08 10:01:35 +00:00
|
|
|
dev->name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
|
|
|
|
names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type));
|
2009-11-19 14:44:00 +00:00
|
|
|
if (names[ncaps++] == NULL) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2009-11-19 14:44:00 +00:00
|
|
|
}
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = ncaps;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
--ncaps;
|
|
|
|
while (--ncaps >= 0)
|
|
|
|
VIR_FREE(names[ncaps]);
|
|
|
|
}
|
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-02 15:12:53 +00:00
|
|
|
static int
|
2010-02-10 10:40:18 +00:00
|
|
|
nodeDeviceVportCreateDelete(const int parent_host,
|
2009-06-02 15:12:53 +00:00
|
|
|
const char *wwpn,
|
|
|
|
const char *wwnn,
|
|
|
|
int operation)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
char *operation_path = NULL, *vport_name = NULL;
|
|
|
|
const char *operation_file = NULL;
|
2010-08-17 15:21:16 +00:00
|
|
|
struct stat st;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case VPORT_CREATE:
|
|
|
|
operation_file = LINUX_SYSFS_VPORT_CREATE_POSTFIX;
|
|
|
|
break;
|
|
|
|
case VPORT_DELETE:
|
|
|
|
operation_file = LINUX_SYSFS_VPORT_DELETE_POSTFIX;
|
|
|
|
break;
|
|
|
|
default:
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-06-02 15:12:53 +00:00
|
|
|
_("Invalid vport operation (%d)"), operation);
|
|
|
|
retval = -1;
|
|
|
|
goto cleanup;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virAsprintf(&operation_path,
|
|
|
|
"%shost%d%s",
|
|
|
|
LINUX_SYSFS_FC_HOST_PREFIX,
|
|
|
|
parent_host,
|
|
|
|
operation_file) < 0) {
|
|
|
|
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-06-02 15:12:53 +00:00
|
|
|
retval = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-08-17 15:21:16 +00:00
|
|
|
if (stat(operation_path, &st) != 0) {
|
|
|
|
VIR_FREE(operation_path);
|
|
|
|
if (virAsprintf(&operation_path,
|
|
|
|
"%shost%d%s",
|
|
|
|
LINUX_SYSFS_SCSI_HOST_PREFIX,
|
|
|
|
parent_host,
|
|
|
|
operation_file) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
retval = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(operation_path, &st) != 0) {
|
|
|
|
VIR_ERROR(_("No vport operation path found for host%d"),
|
|
|
|
parent_host);
|
|
|
|
retval = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-20 06:57:06 +00:00
|
|
|
VIR_DEBUG("Vport operation path is '%s'", operation_path);
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
if (virAsprintf(&vport_name,
|
|
|
|
"%s:%s",
|
|
|
|
wwpn,
|
|
|
|
wwnn) < 0) {
|
|
|
|
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-06-02 15:12:53 +00:00
|
|
|
retval = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-12-03 09:47:08 +00:00
|
|
|
if (virFileWriteStr(operation_path, vport_name, 0) == -1) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-06-02 15:12:53 +00:00
|
|
|
_("Write of '%s' to '%s' during "
|
|
|
|
"vport create/delete failed"),
|
|
|
|
vport_name, operation_path);
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(vport_name);
|
|
|
|
VIR_FREE(operation_path);
|
|
|
|
VIR_DEBUG("%s", _("Vport operation complete"));
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2010-02-10 10:40:18 +00:00
|
|
|
get_time(time_t *t)
|
2009-06-02 15:12:53 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
*t = time(NULL);
|
|
|
|
if (*t == (time_t)-1) {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-06-02 15:12:53 +00:00
|
|
|
"%s", _("Could not get current time"));
|
|
|
|
|
|
|
|
*t = 0;
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* When large numbers of devices are present on the host, it's
|
|
|
|
* possible for udev not to realize that it has work to do before we
|
|
|
|
* get here. We thus keep trying to find the new device we just
|
|
|
|
* created for up to LINUX_NEW_DEVICE_WAIT_TIME. Note that udev's
|
|
|
|
* default settle time is 180 seconds, so once udev realizes that it
|
|
|
|
* has work to do, it might take that long for the udev wait to
|
|
|
|
* return. Thus the total maximum time for this function to return is
|
|
|
|
* the udev settle time plus LINUX_NEW_DEVICE_WAIT_TIME.
|
|
|
|
*
|
|
|
|
* This whole area is a race, but if we retry the udev wait for
|
|
|
|
* LINUX_NEW_DEVICE_WAIT_TIME seconds and there's still no device,
|
|
|
|
* it's probably safe to assume it's not going to appear.
|
|
|
|
*/
|
|
|
|
static virNodeDevicePtr
|
|
|
|
find_new_device(virConnectPtr conn, const char *wwnn, const char *wwpn)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
|
|
|
virNodeDevicePtr dev = NULL;
|
|
|
|
time_t start = 0, now = 0;
|
|
|
|
|
|
|
|
/* The thread that creates the device takes the driver lock, so we
|
|
|
|
* must release it in order to allow the device to be created.
|
|
|
|
* We're not doing anything with the driver pointer at this point,
|
|
|
|
* so it's safe to release it, assuming that the pointer itself
|
|
|
|
* doesn't become invalid. */
|
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
get_time(&start);
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
while ((now - start) < LINUX_NEW_DEVICE_WAIT_TIME) {
|
|
|
|
|
2010-02-04 22:41:52 +00:00
|
|
|
virFileWaitForDevices();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
dev = nodeDeviceLookupByWWN(conn, wwnn, wwpn);
|
|
|
|
|
|
|
|
if (dev != NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(5);
|
2010-02-10 10:40:18 +00:00
|
|
|
if (get_time(&now) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeDeviceLock(driver);
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
virNodeDevicePtr
|
2009-06-02 15:12:53 +00:00
|
|
|
nodeDeviceCreateXML(virConnectPtr conn,
|
|
|
|
const char *xmlDesc,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
|
|
|
virNodeDeviceDefPtr def = NULL;
|
|
|
|
char *wwnn = NULL, *wwpn = NULL;
|
|
|
|
int parent_host = -1;
|
|
|
|
virNodeDevicePtr dev = NULL;
|
|
|
|
|
|
|
|
nodeDeviceLock(driver);
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE);
|
2009-06-02 15:12:53 +00:00
|
|
|
if (def == NULL) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
if (virNodeDeviceGetParentHost(&driver->devs,
|
2009-10-14 19:47:10 +00:00
|
|
|
def->name,
|
|
|
|
def->parent,
|
|
|
|
&parent_host) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
if (nodeDeviceVportCreateDelete(parent_host,
|
2009-06-02 15:12:53 +00:00
|
|
|
wwpn,
|
|
|
|
wwnn,
|
|
|
|
VPORT_CREATE) == -1) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = find_new_device(conn, wwnn, wwpn);
|
|
|
|
/* We don't check the return value, because one way or another,
|
|
|
|
* we're returning what we get... */
|
|
|
|
|
|
|
|
if (dev == NULL) {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
|
2009-06-02 15:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
virNodeDeviceDefFree(def);
|
|
|
|
VIR_FREE(wwnn);
|
|
|
|
VIR_FREE(wwpn);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
2009-06-02 15:12:53 +00:00
|
|
|
nodeDeviceDestroy(virNodeDevicePtr dev)
|
|
|
|
{
|
2010-06-03 01:59:16 +00:00
|
|
|
int ret = -1;
|
2009-06-02 15:12:53 +00:00
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
|
|
|
virNodeDeviceObjPtr obj = NULL;
|
|
|
|
char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
|
|
|
|
int parent_host = -1;
|
|
|
|
|
|
|
|
nodeDeviceLock(driver);
|
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
|
|
|
if (!obj) {
|
2010-02-10 10:40:18 +00:00
|
|
|
virNodeDeviceReportError(VIR_ERR_NO_NODE_DEVICE, NULL);
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_name = strdup(obj->def->parent);
|
|
|
|
|
2009-10-14 19:47:10 +00:00
|
|
|
/* virNodeDeviceGetParentHost will cause the device object's lock to be
|
2009-06-02 15:12:53 +00:00
|
|
|
* taken, so we have to dup the parent's name and drop the lock
|
|
|
|
* before calling it. We don't need the reference to the object
|
|
|
|
* any more once we have the parent's name. */
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
|
|
|
obj = NULL;
|
|
|
|
|
|
|
|
if (parent_name == NULL) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
if (virNodeDeviceGetParentHost(&driver->devs,
|
2009-10-14 19:47:10 +00:00
|
|
|
dev->name,
|
|
|
|
parent_name,
|
|
|
|
&parent_host) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
if (nodeDeviceVportCreateDelete(parent_host,
|
2009-06-02 15:12:53 +00:00
|
|
|
wwpn,
|
|
|
|
wwnn,
|
|
|
|
VPORT_DELETE) == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-06-03 01:59:16 +00:00
|
|
|
ret = 0;
|
2009-06-02 15:12:53 +00:00
|
|
|
out:
|
2009-10-14 20:15:13 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2009-06-02 15:12:53 +00:00
|
|
|
VIR_FREE(parent_name);
|
|
|
|
VIR_FREE(wwnn);
|
|
|
|
VIR_FREE(wwpn);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
int nodedevRegister(void) {
|
2009-11-12 21:48:24 +00:00
|
|
|
#if defined(HAVE_HAL) && defined(HAVE_UDEV)
|
2008-11-21 12:27:11 +00:00
|
|
|
/* Register only one of these two - they conflict */
|
2010-05-28 10:16:44 +00:00
|
|
|
if (udevNodeRegister() == -1)
|
|
|
|
return halNodeRegister();
|
2008-11-21 12:27:11 +00:00
|
|
|
return 0;
|
|
|
|
#else
|
2010-03-09 18:22:22 +00:00
|
|
|
# ifdef HAVE_HAL
|
2008-11-21 12:27:11 +00:00
|
|
|
return halNodeRegister();
|
2010-03-09 18:22:22 +00:00
|
|
|
# endif
|
|
|
|
# ifdef HAVE_UDEV
|
2009-11-12 21:48:24 +00:00
|
|
|
return udevNodeRegister();
|
2010-03-09 18:22:22 +00:00
|
|
|
# endif
|
2008-11-21 12:27:11 +00:00
|
|
|
#endif
|
|
|
|
}
|