2016-11-22 03:58:17 +00:00
|
|
|
/*
|
|
|
|
* virscsivhost.c: helper APIs for managing scsi_host devices
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 IBM Corporation
|
|
|
|
*
|
|
|
|
* 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, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "virscsivhost.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virstring.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
/* For virReportOOMError() and virReportSystemError() */
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
VIR_LOG_INIT("util.scsihost");
|
|
|
|
|
|
|
|
#define SYSFS_VHOST_SCSI_DEVICES "/sys/kernel/config/target/vhost/"
|
|
|
|
#define VHOST_SCSI_DEVICE "/dev/vhost-scsi"
|
|
|
|
|
|
|
|
struct _virSCSIVHostDevice {
|
|
|
|
char *name; /* naa.<wwn> */
|
|
|
|
char *path;
|
|
|
|
char *used_by_drvname;
|
|
|
|
char *used_by_domname;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virSCSIVHostDeviceList {
|
|
|
|
virObjectLockable parent;
|
|
|
|
size_t count;
|
|
|
|
virSCSIVHostDevicePtr *devs;
|
|
|
|
};
|
|
|
|
|
|
|
|
static virClassPtr virSCSIVHostDeviceListClass;
|
|
|
|
|
|
|
|
static void
|
|
|
|
virSCSIVHostDeviceListDispose(void *obj)
|
|
|
|
{
|
|
|
|
virSCSIVHostDeviceListPtr list = obj;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++)
|
|
|
|
virSCSIVHostDeviceFree(list->devs[i]);
|
|
|
|
|
|
|
|
VIR_FREE(list->devs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virSCSIVHostOnceInit(void)
|
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virSCSIVHostDeviceList, virClassForObjectLockable()))
|
2016-11-22 03:58:17 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virSCSIVHost);
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSCSIVHostOpenVhostSCSI(int *vhostfd)
|
|
|
|
{
|
2017-12-12 13:31:03 +00:00
|
|
|
if (!virFileExists(VHOST_SCSI_DEVICE)) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("vhost-scsi device file '%s' cannot be found"),
|
|
|
|
VHOST_SCSI_DEVICE);
|
2017-12-12 13:33:48 +00:00
|
|
|
return -1;
|
2017-12-12 13:31:03 +00:00
|
|
|
}
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
*vhostfd = open(VHOST_SCSI_DEVICE, O_RDWR);
|
|
|
|
|
|
|
|
if (*vhostfd < 0) {
|
|
|
|
virReportSystemError(errno, _("Failed to open %s"), VHOST_SCSI_DEVICE);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
VIR_FORCE_CLOSE(*vhostfd);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virSCSIVHostDeviceListDel(virSCSIVHostDeviceListPtr list,
|
|
|
|
virSCSIVHostDevicePtr dev)
|
|
|
|
{
|
2018-07-27 21:17:34 +00:00
|
|
|
virSCSIVHostDeviceFree(virSCSIVHostDeviceListSteal(list, dev));
|
2016-11-22 03:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virSCSIVHostDeviceListFindIndex(virSCSIVHostDeviceListPtr list,
|
|
|
|
virSCSIVHostDevicePtr dev)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
virSCSIVHostDevicePtr other = list->devs[i];
|
|
|
|
if (STREQ_NULLABLE(other->name, dev->name))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virSCSIVHostDevicePtr
|
|
|
|
virSCSIVHostDeviceListGet(virSCSIVHostDeviceListPtr list, int idx)
|
|
|
|
{
|
|
|
|
if (idx >= list->count || idx < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list->devs[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
virSCSIVHostDeviceListCount(virSCSIVHostDeviceListPtr list)
|
|
|
|
{
|
|
|
|
return list->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virSCSIVHostDevicePtr
|
|
|
|
virSCSIVHostDeviceListSteal(virSCSIVHostDeviceListPtr list,
|
|
|
|
virSCSIVHostDevicePtr dev)
|
|
|
|
{
|
|
|
|
virSCSIVHostDevicePtr ret = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
if (STREQ_NULLABLE(list->devs[i]->name, dev->name)) {
|
|
|
|
ret = list->devs[i];
|
|
|
|
VIR_DELETE_ELEMENT(list->devs, i, list->count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virSCSIVHostDevicePtr
|
|
|
|
virSCSIVHostDeviceListFind(virSCSIVHostDeviceListPtr list,
|
|
|
|
virSCSIVHostDevicePtr dev)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if ((idx = virSCSIVHostDeviceListFindIndex(list, dev)) >= 0)
|
|
|
|
return list->devs[idx];
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSCSIVHostDeviceListAdd(virSCSIVHostDeviceListPtr list,
|
|
|
|
virSCSIVHostDevicePtr dev)
|
|
|
|
{
|
|
|
|
if (virSCSIVHostDeviceListFind(list, dev)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Device %s is already in use"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return VIR_APPEND_ELEMENT(list->devs, list->count, dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virSCSIVHostDeviceListPtr
|
|
|
|
virSCSIVHostDeviceListNew(void)
|
|
|
|
{
|
|
|
|
if (virSCSIVHostInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virObjectLockableNew(virSCSIVHostDeviceListClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSCSIVHostDeviceSetUsedBy(virSCSIVHostDevicePtr dev,
|
|
|
|
const char *drvname,
|
|
|
|
const char *domname)
|
|
|
|
{
|
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
|
|
|
if (VIR_STRDUP(dev->used_by_drvname, drvname) < 0)
|
|
|
|
return -1;
|
|
|
|
if (VIR_STRDUP(dev->used_by_domname, domname) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virSCSIVHostDeviceGetUsedBy(virSCSIVHostDevicePtr dev,
|
|
|
|
const char **drv_name,
|
|
|
|
const char **dom_name)
|
|
|
|
{
|
|
|
|
*drv_name = dev->used_by_drvname;
|
|
|
|
*dom_name = dev->used_by_domname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virSCSIVHostDeviceFileIterate(virSCSIVHostDevicePtr dev,
|
|
|
|
virSCSIVHostDeviceFileActor actor,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
return (actor)(dev, dev->path, opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
virSCSIVHostDeviceGetName(virSCSIVHostDevicePtr dev)
|
|
|
|
{
|
|
|
|
return dev->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-06 15:06:02 +00:00
|
|
|
const char *
|
|
|
|
virSCSIVHostDeviceGetPath(virSCSIVHostDevicePtr dev)
|
|
|
|
{
|
|
|
|
return dev->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-22 03:58:17 +00:00
|
|
|
virSCSIVHostDevicePtr
|
|
|
|
virSCSIVHostDeviceNew(const char *name)
|
|
|
|
{
|
2018-07-24 15:52:31 +00:00
|
|
|
VIR_AUTOPTR(virSCSIVHostDevice) dev = NULL;
|
|
|
|
virSCSIVHostDevicePtr ret = NULL;
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
if (VIR_ALLOC(dev) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (VIR_STRDUP(dev->name, name) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("dev->name buffer overflow: %s"),
|
|
|
|
name);
|
2018-07-24 15:52:31 +00:00
|
|
|
return NULL;
|
2016-11-22 03:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virAsprintf(&dev->path, "%s/%s",
|
|
|
|
SYSFS_VHOST_SCSI_DEVICES, name) < 0)
|
2018-07-24 15:52:31 +00:00
|
|
|
return NULL;
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("%s: initialized", dev->name);
|
|
|
|
|
2018-07-24 15:52:31 +00:00
|
|
|
VIR_STEAL_PTR(ret, dev);
|
2016-11-22 03:58:17 +00:00
|
|
|
|
2018-07-24 15:52:31 +00:00
|
|
|
return ret;
|
2016-11-22 03:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virSCSIVHostDeviceFree(virSCSIVHostDevicePtr dev)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
VIR_DEBUG("%s: freeing", dev->name);
|
|
|
|
VIR_FREE(dev->name);
|
|
|
|
VIR_FREE(dev->path);
|
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
|
|
|
VIR_FREE(dev);
|
|
|
|
}
|