2011-07-06 16:30:08 +00:00
|
|
|
/*
|
|
|
|
* lock_driver_lockd.c: A lock driver which locks nothing
|
|
|
|
*
|
2014-10-28 20:17:04 +00:00
|
|
|
* Copyright (C) 2010-2011, 2014 Red Hat, Inc.
|
2011-07-06 16:30:08 +00:00
|
|
|
*
|
|
|
|
* 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 "lock_driver.h"
|
2012-12-12 16:35:35 +00:00
|
|
|
#include "virconf.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2014-03-05 12:39:00 +00:00
|
|
|
#include "vircrypto.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:01:25 +00:00
|
|
|
#include "viruuid.h"
|
2011-07-06 16:30:08 +00:00
|
|
|
#include "virfile.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2011-07-06 16:30:08 +00:00
|
|
|
#include "rpc/virnetclient.h"
|
|
|
|
#include "lock_protocol.h"
|
|
|
|
#include "configmake.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2016-01-12 15:47:13 +00:00
|
|
|
#include "lock_driver_lockd.h"
|
|
|
|
|
2011-07-06 16:30:08 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_LOCKING
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("locking.lock_driver_lockd");
|
|
|
|
|
2011-07-06 16:30:08 +00:00
|
|
|
typedef struct _virLockManagerLockDaemonPrivate virLockManagerLockDaemonPrivate;
|
|
|
|
typedef virLockManagerLockDaemonPrivate *virLockManagerLockDaemonPrivatePtr;
|
|
|
|
|
|
|
|
typedef struct _virLockManagerLockDaemonResource virLockManagerLockDaemonResource;
|
|
|
|
typedef virLockManagerLockDaemonResource *virLockManagerLockDaemonResourcePtr;
|
|
|
|
|
|
|
|
typedef struct _virLockManagerLockDaemonDriver virLockManagerLockDaemonDriver;
|
|
|
|
typedef virLockManagerLockDaemonDriver *virLockManagerLockDaemonDriverPtr;
|
|
|
|
|
|
|
|
struct _virLockManagerLockDaemonResource {
|
|
|
|
char *lockspace;
|
|
|
|
char *name;
|
|
|
|
unsigned int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virLockManagerLockDaemonPrivate {
|
2018-10-02 13:10:11 +00:00
|
|
|
unsigned char uuid[VIR_UUID_BUFLEN];
|
|
|
|
char *name;
|
|
|
|
int id;
|
|
|
|
pid_t pid;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
size_t nresources;
|
|
|
|
virLockManagerLockDaemonResourcePtr resources;
|
2018-10-02 13:10:03 +00:00
|
|
|
|
|
|
|
bool hasRWDisks;
|
2011-07-06 16:30:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct _virLockManagerLockDaemonDriver {
|
|
|
|
bool autoDiskLease;
|
|
|
|
bool requireLeaseForDisks;
|
2012-08-14 15:46:08 +00:00
|
|
|
|
|
|
|
char *fileLockSpaceDir;
|
2012-12-11 16:39:50 +00:00
|
|
|
char *lvmLockSpaceDir;
|
2012-12-11 16:39:50 +00:00
|
|
|
char *scsiLockSpaceDir;
|
2011-07-06 16:30:08 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 20:17:04 +00:00
|
|
|
static virLockManagerLockDaemonDriverPtr driver;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
static int virLockManagerLockDaemonLoadConfig(const char *configFile)
|
|
|
|
{
|
2019-09-09 15:56:26 +00:00
|
|
|
VIR_AUTOPTR(virConf) conf = NULL;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
if (access(configFile, R_OK) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to access config file %s"),
|
|
|
|
configFile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(conf = virConfReadFile(configFile, 0)))
|
|
|
|
return -1;
|
|
|
|
|
2016-07-08 12:53:28 +00:00
|
|
|
if (virConfGetValueBool(conf, "auto_disk_leases", &driver->autoDiskLease) < 0)
|
2019-09-09 15:56:26 +00:00
|
|
|
return -1;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2016-07-08 12:53:28 +00:00
|
|
|
if (virConfGetValueString(conf, "file_lockspace_dir", &driver->fileLockSpaceDir) < 0)
|
2019-09-09 15:56:26 +00:00
|
|
|
return -1;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2016-07-08 12:53:28 +00:00
|
|
|
if (virConfGetValueString(conf, "lvm_lockspace_dir", &driver->lvmLockSpaceDir) < 0)
|
2019-09-09 15:56:26 +00:00
|
|
|
return -1;
|
2012-08-14 15:46:08 +00:00
|
|
|
|
2016-07-08 12:53:28 +00:00
|
|
|
if (virConfGetValueString(conf, "scsi_lockspace_dir", &driver->scsiLockSpaceDir) < 0)
|
2019-09-09 15:56:26 +00:00
|
|
|
return -1;
|
2012-12-11 16:39:50 +00:00
|
|
|
|
2016-07-08 12:53:28 +00:00
|
|
|
driver->requireLeaseForDisks = !driver->autoDiskLease;
|
|
|
|
if (virConfGetValueBool(conf, "require_lease_for_disks", &driver->requireLeaseForDisks) < 0)
|
2019-09-09 15:56:26 +00:00
|
|
|
return -1;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2019-09-09 15:56:26 +00:00
|
|
|
return 0;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *virLockManagerLockDaemonPath(bool privileged)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
if (privileged) {
|
2019-08-20 15:05:12 +00:00
|
|
|
if (VIR_STRDUP(path, RUNSTATEDIR "/libvirt/virtlockd-sock") < 0)
|
2011-07-06 16:30:08 +00:00
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
char *rundir = NULL;
|
|
|
|
|
|
|
|
if (!(rundir = virGetUserRuntimeDirectory()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&path, "%s/virtlockd-sock", rundir) < 0) {
|
|
|
|
VIR_FREE(rundir);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-24 13:16:23 +00:00
|
|
|
VIR_FREE(rundir);
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLockManagerLockDaemonConnectionRegister(virLockManagerPtr lock,
|
|
|
|
virNetClientPtr client,
|
|
|
|
virNetClientProgramPtr program,
|
|
|
|
int *counter)
|
|
|
|
{
|
|
|
|
virLockManagerLockDaemonPrivatePtr priv = lock->privateData;
|
|
|
|
virLockSpaceProtocolRegisterArgs args;
|
|
|
|
int rv = -1;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
|
|
|
|
args.flags = 0;
|
2018-10-02 13:10:11 +00:00
|
|
|
memcpy(args.owner.uuid, priv->uuid, VIR_UUID_BUFLEN);
|
|
|
|
args.owner.name = priv->name;
|
|
|
|
args.owner.id = priv->id;
|
|
|
|
args.owner.pid = priv->pid;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
if (virNetClientProgramCall(program,
|
|
|
|
client,
|
|
|
|
(*counter)++,
|
|
|
|
VIR_LOCK_SPACE_PROTOCOL_PROC_REGISTER,
|
|
|
|
0, NULL, NULL, NULL,
|
|
|
|
(xdrproc_t)xdr_virLockSpaceProtocolRegisterArgs, (char*)&args,
|
|
|
|
(xdrproc_t)xdr_void, NULL) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
cleanup:
|
2011-07-06 16:30:08 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLockManagerLockDaemonConnectionRestrict(virLockManagerPtr lock ATTRIBUTE_UNUSED,
|
|
|
|
virNetClientPtr client,
|
|
|
|
virNetClientProgramPtr program,
|
|
|
|
int *counter)
|
|
|
|
{
|
|
|
|
virLockSpaceProtocolRestrictArgs args;
|
|
|
|
int rv = -1;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
|
|
|
|
args.flags = 0;
|
|
|
|
|
|
|
|
if (virNetClientProgramCall(program,
|
|
|
|
client,
|
|
|
|
(*counter)++,
|
|
|
|
VIR_LOCK_SPACE_PROTOCOL_PROC_RESTRICT,
|
|
|
|
0, NULL, NULL, NULL,
|
|
|
|
(xdrproc_t)xdr_virLockSpaceProtocolRestrictArgs, (char*)&args,
|
|
|
|
(xdrproc_t)xdr_void, NULL) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
cleanup:
|
2011-07-06 16:30:08 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virNetClientPtr virLockManagerLockDaemonConnectionNew(bool privileged,
|
|
|
|
virNetClientProgramPtr *prog)
|
|
|
|
{
|
|
|
|
virNetClientPtr client = NULL;
|
|
|
|
char *lockdpath;
|
2014-07-15 11:36:00 +00:00
|
|
|
char *daemonPath = NULL;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
*prog = NULL;
|
|
|
|
|
|
|
|
if (!(lockdpath = virLockManagerLockDaemonPath(privileged)))
|
|
|
|
goto error;
|
|
|
|
|
2014-04-25 10:55:32 +00:00
|
|
|
if (!privileged &&
|
|
|
|
!(daemonPath = virFileFindResourceFull("virtlockd",
|
|
|
|
NULL, NULL,
|
2019-03-12 14:11:47 +00:00
|
|
|
abs_top_builddir "/src",
|
2014-04-25 10:55:32 +00:00
|
|
|
SBINDIR,
|
|
|
|
"VIRTLOCKD_PATH")))
|
|
|
|
goto error;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
if (!(client = virNetClientNewUNIX(lockdpath,
|
|
|
|
daemonPath != NULL,
|
|
|
|
daemonPath)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(*prog = virNetClientProgramNew(VIR_LOCK_SPACE_PROTOCOL_PROGRAM,
|
|
|
|
VIR_LOCK_SPACE_PROTOCOL_PROGRAM_VERSION,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NULL)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (virNetClientAddProgram(client, *prog) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2014-07-07 06:18:44 +00:00
|
|
|
VIR_FREE(daemonPath);
|
2011-07-06 16:30:08 +00:00
|
|
|
VIR_FREE(lockdpath);
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2014-07-07 06:18:44 +00:00
|
|
|
VIR_FREE(daemonPath);
|
2011-07-06 16:30:08 +00:00
|
|
|
VIR_FREE(lockdpath);
|
|
|
|
virNetClientClose(client);
|
|
|
|
virObjectUnref(client);
|
|
|
|
virObjectUnref(*prog);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virNetClientPtr
|
|
|
|
virLockManagerLockDaemonConnect(virLockManagerPtr lock,
|
|
|
|
virNetClientProgramPtr *program,
|
|
|
|
int *counter)
|
|
|
|
{
|
|
|
|
virNetClientPtr client;
|
|
|
|
|
2013-10-09 11:13:45 +00:00
|
|
|
if (!(client = virLockManagerLockDaemonConnectionNew(geteuid() == 0, program)))
|
2011-07-06 16:30:08 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virLockManagerLockDaemonConnectionRegister(lock,
|
|
|
|
client,
|
|
|
|
*program,
|
|
|
|
counter) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2011-07-06 16:30:08 +00:00
|
|
|
virNetClientClose(client);
|
|
|
|
virObjectUnref(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-14 15:46:08 +00:00
|
|
|
static int virLockManagerLockDaemonSetupLockspace(const char *path)
|
|
|
|
{
|
|
|
|
virNetClientPtr client;
|
|
|
|
virNetClientProgramPtr program = NULL;
|
|
|
|
virLockSpaceProtocolCreateLockSpaceArgs args;
|
|
|
|
int rv = -1;
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
args.path = (char*)path;
|
|
|
|
|
2013-10-09 11:13:45 +00:00
|
|
|
if (!(client = virLockManagerLockDaemonConnectionNew(geteuid() == 0, &program)))
|
2012-08-14 15:46:08 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virNetClientProgramCall(program,
|
|
|
|
client,
|
|
|
|
counter++,
|
|
|
|
VIR_LOCK_SPACE_PROTOCOL_PROC_CREATE_LOCKSPACE,
|
|
|
|
0, NULL, NULL, NULL,
|
|
|
|
(xdrproc_t)xdr_virLockSpaceProtocolCreateLockSpaceArgs, (char*)&args,
|
|
|
|
(xdrproc_t)xdr_void, NULL) < 0) {
|
2018-05-05 12:04:21 +00:00
|
|
|
if (virGetLastErrorCode() == VIR_ERR_OPERATION_INVALID) {
|
2012-08-14 15:46:08 +00:00
|
|
|
/* The lockspace already exists */
|
|
|
|
virResetLastError();
|
|
|
|
rv = 0;
|
|
|
|
} else {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
cleanup:
|
2012-08-14 15:46:08 +00:00
|
|
|
virObjectUnref(program);
|
|
|
|
virNetClientClose(client);
|
|
|
|
virObjectUnref(client);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-06 16:30:08 +00:00
|
|
|
static int virLockManagerLockDaemonDeinit(void);
|
|
|
|
|
|
|
|
static int virLockManagerLockDaemonInit(unsigned int version,
|
|
|
|
const char *configFile,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
2017-09-25 10:43:33 +00:00
|
|
|
VIR_DEBUG("version=%u configFile=%s flags=0x%x", version, NULLSTR(configFile), flags);
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
|
|
|
if (driver)
|
|
|
|
return 0;
|
|
|
|
|
2013-07-04 10:11:02 +00:00
|
|
|
if (VIR_ALLOC(driver) < 0)
|
2011-07-06 16:30:08 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
driver->requireLeaseForDisks = true;
|
|
|
|
driver->autoDiskLease = true;
|
|
|
|
|
2018-10-02 13:09:45 +00:00
|
|
|
if (virLockManagerLockDaemonLoadConfig(configFile) < 0)
|
2011-07-06 16:30:08 +00:00
|
|
|
goto error;
|
|
|
|
|
2012-08-14 15:46:08 +00:00
|
|
|
if (driver->autoDiskLease) {
|
|
|
|
if (driver->fileLockSpaceDir &&
|
|
|
|
virLockManagerLockDaemonSetupLockspace(driver->fileLockSpaceDir) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2012-12-11 16:39:50 +00:00
|
|
|
if (driver->lvmLockSpaceDir &&
|
|
|
|
virLockManagerLockDaemonSetupLockspace(driver->lvmLockSpaceDir) < 0)
|
|
|
|
goto error;
|
2012-12-11 16:39:50 +00:00
|
|
|
|
|
|
|
if (driver->scsiLockSpaceDir &&
|
|
|
|
virLockManagerLockDaemonSetupLockspace(driver->scsiLockSpaceDir) < 0)
|
|
|
|
goto error;
|
2012-12-11 16:39:50 +00:00
|
|
|
}
|
2012-08-14 15:46:08 +00:00
|
|
|
|
2011-07-06 16:30:08 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2011-07-06 16:30:08 +00:00
|
|
|
virLockManagerLockDaemonDeinit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virLockManagerLockDaemonDeinit(void)
|
|
|
|
{
|
|
|
|
if (!driver)
|
|
|
|
return 0;
|
|
|
|
|
2018-08-21 12:06:44 +00:00
|
|
|
VIR_FREE(driver->scsiLockSpaceDir);
|
|
|
|
VIR_FREE(driver->lvmLockSpaceDir);
|
2012-08-14 15:46:08 +00:00
|
|
|
VIR_FREE(driver->fileLockSpaceDir);
|
2011-07-06 16:30:08 +00:00
|
|
|
VIR_FREE(driver);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:58:32 +00:00
|
|
|
static void
|
|
|
|
virLockManagerLockDaemonPrivateFree(virLockManagerLockDaemonPrivatePtr priv)
|
2011-07-06 16:30:08 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
return;
|
|
|
|
|
2013-05-21 08:06:00 +00:00
|
|
|
for (i = 0; i < priv->nresources; i++) {
|
2011-07-06 16:30:08 +00:00
|
|
|
VIR_FREE(priv->resources[i].lockspace);
|
|
|
|
VIR_FREE(priv->resources[i].name);
|
|
|
|
}
|
|
|
|
VIR_FREE(priv->resources);
|
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
VIR_FREE(priv->name);
|
2011-07-06 16:30:08 +00:00
|
|
|
VIR_FREE(priv);
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:58:32 +00:00
|
|
|
static void virLockManagerLockDaemonFree(virLockManagerPtr lock)
|
|
|
|
{
|
|
|
|
if (!lock)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virLockManagerLockDaemonPrivateFree(lock->privateData);
|
|
|
|
lock->privateData = NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
static int virLockManagerLockDaemonNew(virLockManagerPtr lock,
|
|
|
|
unsigned int type,
|
|
|
|
size_t nparams,
|
|
|
|
virLockManagerParamPtr params,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
2018-08-22 06:58:32 +00:00
|
|
|
virLockManagerLockDaemonPrivatePtr priv = NULL;
|
2011-07-06 16:30:08 +00:00
|
|
|
size_t i;
|
2018-08-22 06:58:32 +00:00
|
|
|
int ret = -1;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2015-03-06 14:58:55 +00:00
|
|
|
virCheckFlags(VIR_LOCK_MANAGER_NEW_STARTED, -1);
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2013-07-04 10:11:02 +00:00
|
|
|
if (VIR_ALLOC(priv) < 0)
|
2011-07-06 16:30:08 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
switch (type) {
|
2011-07-06 16:30:08 +00:00
|
|
|
case VIR_LOCK_MANAGER_OBJECT_TYPE_DOMAIN:
|
2013-05-21 08:06:00 +00:00
|
|
|
for (i = 0; i < nparams; i++) {
|
2011-07-06 16:30:08 +00:00
|
|
|
if (STREQ(params[i].key, "uuid")) {
|
2018-10-02 13:10:11 +00:00
|
|
|
memcpy(priv->uuid, params[i].value.uuid, VIR_UUID_BUFLEN);
|
2011-07-06 16:30:08 +00:00
|
|
|
} else if (STREQ(params[i].key, "name")) {
|
2018-10-02 13:10:11 +00:00
|
|
|
if (VIR_STRDUP(priv->name, params[i].value.str) < 0)
|
2018-08-22 06:58:32 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
} else if (STREQ(params[i].key, "id")) {
|
2018-10-02 13:10:11 +00:00
|
|
|
priv->id = params[i].value.iv;
|
2011-07-06 16:30:08 +00:00
|
|
|
} else if (STREQ(params[i].key, "pid")) {
|
2018-10-02 13:10:11 +00:00
|
|
|
priv->pid = params[i].value.iv;
|
2013-05-03 13:23:03 +00:00
|
|
|
} else if (STREQ(params[i].key, "uri")) {
|
|
|
|
/* ignored */
|
2011-07-06 16:30:08 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2018-10-02 13:10:11 +00:00
|
|
|
_("Unexpected parameter %s for object"),
|
2011-07-06 16:30:08 +00:00
|
|
|
params[i].key);
|
2018-08-22 06:58:32 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-02 13:10:11 +00:00
|
|
|
if (priv->id == 0) {
|
2011-07-06 16:30:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing ID parameter for domain object"));
|
2018-08-22 06:58:32 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
2018-10-02 13:10:11 +00:00
|
|
|
if (priv->pid == 0)
|
2015-04-16 17:32:42 +00:00
|
|
|
VIR_DEBUG("Missing PID parameter for domain object");
|
2018-10-02 13:10:11 +00:00
|
|
|
if (!priv->name) {
|
2011-07-06 16:30:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing name parameter for domain object"));
|
2018-08-22 06:58:32 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
2018-10-02 13:10:11 +00:00
|
|
|
if (!virUUIDIsValid(priv->uuid)) {
|
2011-07-06 16:30:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing UUID parameter for domain object"));
|
2018-08-22 06:58:32 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unknown lock manager object type %d"),
|
|
|
|
type);
|
2018-08-22 06:58:32 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 06:58:32 +00:00
|
|
|
VIR_STEAL_PTR(lock->privateData, priv);
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virLockManagerLockDaemonPrivateFree(priv);
|
|
|
|
return ret;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int virLockManagerLockDaemonAddResource(virLockManagerPtr lock,
|
|
|
|
unsigned int type,
|
|
|
|
const char *name,
|
|
|
|
size_t nparams,
|
|
|
|
virLockManagerParamPtr params,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virLockManagerLockDaemonPrivatePtr priv = lock->privateData;
|
2012-08-14 15:46:08 +00:00
|
|
|
char *newName = NULL;
|
2011-07-06 16:30:08 +00:00
|
|
|
char *newLockspace = NULL;
|
2018-10-02 13:09:58 +00:00
|
|
|
bool autoCreate = false;
|
2018-08-22 07:56:07 +00:00
|
|
|
int ret = -1;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
virCheckFlags(VIR_LOCK_MANAGER_RESOURCE_READONLY |
|
|
|
|
VIR_LOCK_MANAGER_RESOURCE_SHARED, -1);
|
|
|
|
|
|
|
|
if (flags & VIR_LOCK_MANAGER_RESOURCE_READONLY)
|
|
|
|
return 0;
|
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
switch (type) {
|
|
|
|
case VIR_LOCK_MANAGER_RESOURCE_TYPE_DISK:
|
|
|
|
if (params || nparams) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Unexpected parameters for disk resource"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!driver->autoDiskLease) {
|
|
|
|
if (!(flags & (VIR_LOCK_MANAGER_RESOURCE_SHARED |
|
|
|
|
VIR_LOCK_MANAGER_RESOURCE_READONLY)))
|
|
|
|
priv->hasRWDisks = true;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-14 15:46:08 +00:00
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
/* XXX we should somehow pass in TYPE=BLOCK info
|
|
|
|
* from the domain_lock code, instead of assuming /dev
|
|
|
|
*/
|
|
|
|
if (STRPREFIX(name, "/dev") &&
|
|
|
|
driver->lvmLockSpaceDir) {
|
|
|
|
VIR_DEBUG("Trying to find an LVM UUID for %s", name);
|
|
|
|
if (virStorageFileGetLVMKey(name, &newName) < 0)
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2012-12-11 16:39:50 +00:00
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
if (newName) {
|
|
|
|
VIR_DEBUG("Got an LVM UUID %s for %s", newName, name);
|
|
|
|
if (VIR_STRDUP(newLockspace, driver->lvmLockSpaceDir) < 0)
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2018-10-02 13:10:11 +00:00
|
|
|
autoCreate = true;
|
|
|
|
break;
|
2012-12-11 16:39:50 +00:00
|
|
|
}
|
2018-10-02 13:10:11 +00:00
|
|
|
virResetLastError();
|
|
|
|
/* Fallback to generic non-block code */
|
|
|
|
}
|
2012-12-11 16:39:50 +00:00
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
if (STRPREFIX(name, "/dev") &&
|
|
|
|
driver->scsiLockSpaceDir) {
|
|
|
|
VIR_DEBUG("Trying to find an SCSI ID for %s", name);
|
2019-01-15 20:59:21 +00:00
|
|
|
if (virStorageFileGetSCSIKey(name, &newName, false) < 0)
|
2018-10-02 13:10:11 +00:00
|
|
|
goto cleanup;
|
2012-12-11 16:39:50 +00:00
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
if (newName) {
|
|
|
|
VIR_DEBUG("Got an SCSI ID %s for %s", newName, name);
|
|
|
|
if (VIR_STRDUP(newLockspace, driver->scsiLockSpaceDir) < 0)
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2018-10-02 13:09:58 +00:00
|
|
|
autoCreate = true;
|
2018-10-02 13:10:11 +00:00
|
|
|
break;
|
2012-12-11 16:39:50 +00:00
|
|
|
}
|
2018-10-02 13:10:11 +00:00
|
|
|
virResetLastError();
|
|
|
|
/* Fallback to generic non-block code */
|
|
|
|
}
|
2012-12-11 16:39:50 +00:00
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
if (driver->fileLockSpaceDir) {
|
|
|
|
if (VIR_STRDUP(newLockspace, driver->fileLockSpaceDir) < 0)
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2018-10-02 13:10:11 +00:00
|
|
|
if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, name, &newName) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
autoCreate = true;
|
|
|
|
VIR_DEBUG("Using indirect lease %s for %s", newName, name);
|
|
|
|
} else {
|
|
|
|
if (VIR_STRDUP(newLockspace, "") < 0)
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2013-05-03 12:43:12 +00:00
|
|
|
if (VIR_STRDUP(newName, name) < 0)
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2018-10-02 13:10:11 +00:00
|
|
|
VIR_DEBUG("Using direct lease for %s", name);
|
|
|
|
}
|
2012-08-14 15:46:08 +00:00
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
break;
|
|
|
|
case VIR_LOCK_MANAGER_RESOURCE_TYPE_LEASE: {
|
|
|
|
size_t i;
|
|
|
|
char *path = NULL;
|
|
|
|
char *lockspace = NULL;
|
|
|
|
for (i = 0; i < nparams; i++) {
|
|
|
|
if (STREQ(params[i].key, "offset")) {
|
|
|
|
if (params[i].value.ul != 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Offset must be zero for this lock manager"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
} else if (STREQ(params[i].key, "lockspace")) {
|
|
|
|
lockspace = params[i].value.str;
|
|
|
|
} else if (STREQ(params[i].key, "path")) {
|
|
|
|
path = params[i].value.str;
|
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unexpected parameter %s for lease resource"),
|
|
|
|
params[i].key);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!path || !lockspace) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing path or lockspace for lease resource"));
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
2018-10-02 13:10:11 +00:00
|
|
|
if (virAsprintf(&newLockspace, "%s/%s",
|
|
|
|
path, lockspace) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (VIR_STRDUP(newName, name) < 0)
|
|
|
|
goto cleanup;
|
2012-08-14 15:46:08 +00:00
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
} break;
|
2011-07-06 16:30:08 +00:00
|
|
|
default:
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unknown lock manager object type %d"),
|
|
|
|
type);
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_EXPAND_N(priv->resources, priv->nresources, 1) < 0)
|
2018-08-22 07:56:07 +00:00
|
|
|
goto cleanup;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2018-08-22 07:56:07 +00:00
|
|
|
VIR_STEAL_PTR(priv->resources[priv->nresources-1].lockspace, newLockspace);
|
|
|
|
VIR_STEAL_PTR(priv->resources[priv->nresources-1].name, newName);
|
2018-10-02 13:09:58 +00:00
|
|
|
|
|
|
|
if (flags & VIR_LOCK_MANAGER_RESOURCE_SHARED)
|
|
|
|
priv->resources[priv->nresources-1].flags |=
|
|
|
|
VIR_LOCK_SPACE_PROTOCOL_ACQUIRE_RESOURCE_SHARED;
|
|
|
|
|
|
|
|
if (autoCreate)
|
|
|
|
priv->resources[priv->nresources-1].flags |=
|
|
|
|
VIR_LOCK_SPACE_PROTOCOL_ACQUIRE_RESOURCE_AUTOCREATE;
|
2012-08-14 15:46:08 +00:00
|
|
|
|
2018-08-22 07:56:07 +00:00
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2012-08-14 15:46:08 +00:00
|
|
|
VIR_FREE(newLockspace);
|
2011-07-06 16:30:08 +00:00
|
|
|
VIR_FREE(newName);
|
2018-08-22 07:56:07 +00:00
|
|
|
return ret;
|
2011-07-06 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int virLockManagerLockDaemonAcquire(virLockManagerPtr lock,
|
|
|
|
const char *state ATTRIBUTE_UNUSED,
|
|
|
|
unsigned int flags,
|
|
|
|
virDomainLockFailureAction action ATTRIBUTE_UNUSED,
|
|
|
|
int *fd)
|
|
|
|
{
|
|
|
|
virNetClientPtr client = NULL;
|
|
|
|
virNetClientProgramPtr program = NULL;
|
|
|
|
int counter = 0;
|
|
|
|
int rv = -1;
|
|
|
|
virLockManagerLockDaemonPrivatePtr priv = lock->privateData;
|
|
|
|
|
|
|
|
virCheckFlags(VIR_LOCK_MANAGER_ACQUIRE_REGISTER_ONLY |
|
2018-10-02 13:09:53 +00:00
|
|
|
VIR_LOCK_MANAGER_ACQUIRE_RESTRICT, -1);
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2018-10-02 13:10:11 +00:00
|
|
|
if (priv->nresources == 0 &&
|
2018-10-02 13:10:03 +00:00
|
|
|
priv->hasRWDisks &&
|
2011-07-06 16:30:08 +00:00
|
|
|
driver->requireLeaseForDisks) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Read/write, exclusive access, disks were present, but no leases specified"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(client = virLockManagerLockDaemonConnect(lock, &program, &counter)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (fd &&
|
|
|
|
(*fd = virNetClientDupFD(client, false)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(flags & VIR_LOCK_MANAGER_ACQUIRE_REGISTER_ONLY)) {
|
2018-10-02 13:09:53 +00:00
|
|
|
size_t i;
|
2013-05-21 08:06:00 +00:00
|
|
|
for (i = 0; i < priv->nresources; i++) {
|
2011-07-06 16:30:08 +00:00
|
|
|
virLockSpaceProtocolAcquireResourceArgs args;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
|
|
|
|
2018-08-22 07:51:30 +00:00
|
|
|
args.path = priv->resources[i].lockspace;
|
2011-07-06 16:30:08 +00:00
|
|
|
args.name = priv->resources[i].name;
|
|
|
|
args.flags = priv->resources[i].flags;
|
|
|
|
|
|
|
|
if (virNetClientProgramCall(program,
|
|
|
|
client,
|
|
|
|
counter++,
|
|
|
|
VIR_LOCK_SPACE_PROTOCOL_PROC_ACQUIRE_RESOURCE,
|
|
|
|
0, NULL, NULL, NULL,
|
|
|
|
(xdrproc_t)xdr_virLockSpaceProtocolAcquireResourceArgs, &args,
|
|
|
|
(xdrproc_t)xdr_void, NULL) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & VIR_LOCK_MANAGER_ACQUIRE_RESTRICT) &&
|
|
|
|
virLockManagerLockDaemonConnectionRestrict(lock, client, program, &counter) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
cleanup:
|
2018-10-02 13:09:53 +00:00
|
|
|
if (rv != 0 && fd)
|
|
|
|
VIR_FORCE_CLOSE(*fd);
|
2011-07-06 16:30:08 +00:00
|
|
|
virNetClientClose(client);
|
|
|
|
virObjectUnref(client);
|
|
|
|
virObjectUnref(program);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virLockManagerLockDaemonRelease(virLockManagerPtr lock,
|
|
|
|
char **state,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virNetClientPtr client = NULL;
|
|
|
|
virNetClientProgramPtr program = NULL;
|
|
|
|
int counter = 0;
|
|
|
|
int rv = -1;
|
2013-05-03 11:00:15 +00:00
|
|
|
size_t i;
|
|
|
|
virLockManagerLockDaemonPrivatePtr priv = lock->privateData;
|
2011-07-06 16:30:08 +00:00
|
|
|
|
2013-05-03 11:00:15 +00:00
|
|
|
virCheckFlags(0, -1);
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
if (state)
|
|
|
|
*state = NULL;
|
|
|
|
|
|
|
|
if (!(client = virLockManagerLockDaemonConnect(lock, &program, &counter)))
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-05-21 08:06:00 +00:00
|
|
|
for (i = 0; i < priv->nresources; i++) {
|
2018-10-02 13:09:53 +00:00
|
|
|
virLockSpaceProtocolReleaseResourceArgs args;
|
|
|
|
|
|
|
|
memset(&args, 0, sizeof(args));
|
2013-05-03 11:00:15 +00:00
|
|
|
|
2018-10-02 13:09:53 +00:00
|
|
|
if (priv->resources[i].lockspace)
|
|
|
|
args.path = priv->resources[i].lockspace;
|
|
|
|
args.name = priv->resources[i].name;
|
|
|
|
args.flags = priv->resources[i].flags;
|
|
|
|
|
|
|
|
args.flags &=
|
|
|
|
~(VIR_LOCK_SPACE_PROTOCOL_ACQUIRE_RESOURCE_SHARED |
|
2018-10-02 13:10:18 +00:00
|
|
|
VIR_LOCK_SPACE_PROTOCOL_ACQUIRE_RESOURCE_AUTOCREATE);
|
2018-10-02 13:09:53 +00:00
|
|
|
|
|
|
|
if (virNetClientProgramCall(program,
|
|
|
|
client,
|
|
|
|
counter++,
|
|
|
|
VIR_LOCK_SPACE_PROTOCOL_PROC_RELEASE_RESOURCE,
|
|
|
|
0, NULL, NULL, NULL,
|
|
|
|
(xdrproc_t)xdr_virLockSpaceProtocolReleaseResourceArgs, &args,
|
|
|
|
(xdrproc_t)xdr_void, NULL) < 0)
|
2013-05-03 11:00:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-07-06 16:30:08 +00:00
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
cleanup:
|
2011-07-06 16:30:08 +00:00
|
|
|
virNetClientClose(client);
|
|
|
|
virObjectUnref(client);
|
|
|
|
virObjectUnref(program);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int virLockManagerLockDaemonInquire(virLockManagerPtr lock ATTRIBUTE_UNUSED,
|
|
|
|
char **state,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
|
|
|
if (state)
|
|
|
|
*state = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virLockDriver virLockDriverImpl =
|
|
|
|
{
|
|
|
|
.version = VIR_LOCK_MANAGER_VERSION,
|
|
|
|
.flags = 0,
|
|
|
|
|
|
|
|
.drvInit = virLockManagerLockDaemonInit,
|
|
|
|
.drvDeinit = virLockManagerLockDaemonDeinit,
|
|
|
|
|
|
|
|
.drvNew = virLockManagerLockDaemonNew,
|
|
|
|
.drvFree = virLockManagerLockDaemonFree,
|
|
|
|
|
|
|
|
.drvAddResource = virLockManagerLockDaemonAddResource,
|
|
|
|
|
|
|
|
.drvAcquire = virLockManagerLockDaemonAcquire,
|
|
|
|
.drvRelease = virLockManagerLockDaemonRelease,
|
|
|
|
|
|
|
|
.drvInquire = virLockManagerLockDaemonInquire,
|
|
|
|
};
|