2011-01-18 18:37:45 +00:00
|
|
|
/*
|
|
|
|
* lock_driver_sanlock.c: A lock driver for Sanlock
|
|
|
|
*
|
2014-03-17 09:38:38 +00:00
|
|
|
* Copyright (C) 2010-2014 Red Hat, Inc.
|
2011-01-18 18:37:45 +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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2011-01-18 18:37:45 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
2011-06-14 08:29:00 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2011-01-18 18:37:45 +00:00
|
|
|
|
|
|
|
#include <sanlock.h>
|
|
|
|
#include <sanlock_resource.h>
|
2011-06-14 08:29:00 +00:00
|
|
|
#include <sanlock_admin.h>
|
2011-01-18 18:37:45 +00:00
|
|
|
|
2012-08-21 13:27:10 +00:00
|
|
|
#include "dirname.h"
|
2011-01-18 18:37:45 +00:00
|
|
|
#include "lock_driver.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2014-03-05 12:39:00 +00:00
|
|
|
#include "vircrypto.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2012-12-12 16:35:35 +00:00
|
|
|
#include "virconf.h"
|
2013-05-02 17:24:02 +00:00
|
|
|
#include "virstring.h"
|
2011-06-14 08:29:00 +00:00
|
|
|
|
|
|
|
#include "configmake.h"
|
2011-01-18 18:37:45 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_LOCKING
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("locking.lock_driver_sanlock");
|
|
|
|
|
2011-06-14 08:29:00 +00:00
|
|
|
#define VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE "__LIBVIRT__DISKS__"
|
2012-09-18 11:41:26 +00:00
|
|
|
#define VIR_LOCK_MANAGER_SANLOCK_KILLPATH LIBEXECDIR "/libvirt_sanlock_helper"
|
2011-06-14 08:29:00 +00:00
|
|
|
|
2012-06-25 10:31:09 +00:00
|
|
|
/*
|
|
|
|
* temporary fix for the case where the sanlock devel package is
|
|
|
|
* too old to provide that define, and probably the functionality too
|
|
|
|
*/
|
|
|
|
#ifndef SANLK_RES_SHARED
|
2012-06-25 10:46:21 +00:00
|
|
|
# define SANLK_RES_SHARED 0x4
|
2012-06-25 10:31:09 +00:00
|
|
|
#endif
|
|
|
|
|
2011-06-14 08:20:49 +00:00
|
|
|
typedef struct _virLockManagerSanlockDriver virLockManagerSanlockDriver;
|
|
|
|
typedef virLockManagerSanlockDriver *virLockManagerSanlockDriverPtr;
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
typedef struct _virLockManagerSanlockPrivate virLockManagerSanlockPrivate;
|
|
|
|
typedef virLockManagerSanlockPrivate *virLockManagerSanlockPrivatePtr;
|
|
|
|
|
2011-06-14 08:20:49 +00:00
|
|
|
struct _virLockManagerSanlockDriver {
|
|
|
|
bool requireLeaseForDisks;
|
2011-06-14 08:29:00 +00:00
|
|
|
int hostID;
|
|
|
|
bool autoDiskLease;
|
|
|
|
char *autoDiskLeasePath;
|
2012-10-23 14:34:21 +00:00
|
|
|
|
|
|
|
/* under which permissions does sanlock run */
|
|
|
|
uid_t user;
|
|
|
|
gid_t group;
|
2011-06-14 08:20:49 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 20:17:04 +00:00
|
|
|
static virLockManagerSanlockDriver *driver;
|
2011-06-14 08:20:49 +00:00
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
struct _virLockManagerSanlockPrivate {
|
2012-09-18 11:41:26 +00:00
|
|
|
const char *vm_uri;
|
2014-02-27 08:32:41 +00:00
|
|
|
char *vm_name;
|
2012-09-18 11:41:26 +00:00
|
|
|
unsigned char vm_uuid[VIR_UUID_BUFLEN];
|
2011-01-18 18:37:45 +00:00
|
|
|
unsigned int vm_id;
|
|
|
|
unsigned int vm_pid;
|
|
|
|
unsigned int flags;
|
|
|
|
bool hasRWDisks;
|
|
|
|
int res_count;
|
|
|
|
struct sanlk_resource *res_args[SANLK_MAX_RESOURCES];
|
2014-05-12 09:38:47 +00:00
|
|
|
|
|
|
|
/* whether the VM was registered or not */
|
|
|
|
bool registered;
|
2011-01-18 18:37:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sanlock plugin for the libvirt virLockManager API
|
|
|
|
*/
|
2011-06-14 08:20:49 +00:00
|
|
|
static int virLockManagerSanlockLoadConfig(const char *configFile)
|
|
|
|
{
|
|
|
|
virConfPtr conf;
|
|
|
|
virConfValuePtr p;
|
2012-10-23 14:34:21 +00:00
|
|
|
char *tmp;
|
2011-06-14 08:20:49 +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;
|
|
|
|
|
2014-08-22 12:59:28 +00:00
|
|
|
#define CHECK_TYPE(name, typ) if (p && p->type != (typ)) { \
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, \
|
|
|
|
"%s: %s: expected type " #typ, \
|
|
|
|
configFile, (name)); \
|
2011-06-14 08:20:49 +00:00
|
|
|
virConfFree(conf); \
|
|
|
|
return -1; \
|
|
|
|
}
|
|
|
|
|
2011-06-14 08:29:00 +00:00
|
|
|
p = virConfGetValue(conf, "auto_disk_leases");
|
|
|
|
CHECK_TYPE("auto_disk_leases", VIR_CONF_LONG);
|
|
|
|
if (p) driver->autoDiskLease = p->l;
|
|
|
|
|
|
|
|
p = virConfGetValue(conf, "disk_lease_dir");
|
|
|
|
CHECK_TYPE("disk_lease_dir", VIR_CONF_STRING);
|
|
|
|
if (p && p->str) {
|
|
|
|
VIR_FREE(driver->autoDiskLeasePath);
|
2013-05-03 12:43:12 +00:00
|
|
|
if (VIR_STRDUP(driver->autoDiskLeasePath, p->str) < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
virConfFree(conf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p = virConfGetValue(conf, "host_id");
|
|
|
|
CHECK_TYPE("host_id", VIR_CONF_LONG);
|
|
|
|
if (p) driver->hostID = p->l;
|
|
|
|
|
2011-06-14 08:20:49 +00:00
|
|
|
p = virConfGetValue(conf, "require_lease_for_disks");
|
|
|
|
CHECK_TYPE("require_lease_for_disks", VIR_CONF_LONG);
|
|
|
|
if (p)
|
|
|
|
driver->requireLeaseForDisks = p->l;
|
2011-06-14 08:29:00 +00:00
|
|
|
else
|
|
|
|
driver->requireLeaseForDisks = !driver->autoDiskLease;
|
2011-06-14 08:20:49 +00:00
|
|
|
|
2012-10-23 14:34:21 +00:00
|
|
|
p = virConfGetValue(conf, "user");
|
|
|
|
CHECK_TYPE("user", VIR_CONF_STRING);
|
|
|
|
if (p) {
|
2013-05-03 12:43:12 +00:00
|
|
|
if (VIR_STRDUP(tmp, p->str) < 0) {
|
2012-10-23 14:34:21 +00:00
|
|
|
virConfFree(conf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virGetUserID(tmp, &driver->user) < 0) {
|
|
|
|
VIR_FREE(tmp);
|
|
|
|
virConfFree(conf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
VIR_FREE(tmp);
|
|
|
|
}
|
|
|
|
|
2012-10-17 09:23:12 +00:00
|
|
|
p = virConfGetValue(conf, "group");
|
|
|
|
CHECK_TYPE("group", VIR_CONF_STRING);
|
2012-10-23 14:34:21 +00:00
|
|
|
if (p) {
|
2013-05-03 12:43:12 +00:00
|
|
|
if (VIR_STRDUP(tmp, p->str) < 0) {
|
2012-10-23 14:34:21 +00:00
|
|
|
virConfFree(conf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (virGetGroupID(tmp, &driver->group) < 0) {
|
|
|
|
VIR_FREE(tmp);
|
|
|
|
virConfFree(conf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
VIR_FREE(tmp);
|
|
|
|
}
|
|
|
|
|
2011-06-14 08:20:49 +00:00
|
|
|
virConfFree(conf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-15 10:19:08 +00:00
|
|
|
/* How much ms sleep before retrying to add a lockspace? */
|
|
|
|
#define LOCKSPACE_SLEEP 100
|
|
|
|
/* How many times try adding a lockspace? */
|
|
|
|
#define LOCKSPACE_RETRIES 10
|
|
|
|
|
2011-06-14 08:29:00 +00:00
|
|
|
static int virLockManagerSanlockSetupLockspace(void)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
struct stat st;
|
|
|
|
int rv;
|
|
|
|
struct sanlk_lockspace ls;
|
|
|
|
char *path = NULL;
|
2012-08-21 13:27:10 +00:00
|
|
|
char *dir = NULL;
|
2012-11-15 10:19:08 +00:00
|
|
|
int retries = LOCKSPACE_RETRIES;
|
2011-06-14 08:29:00 +00:00
|
|
|
|
|
|
|
if (virAsprintf(&path, "%s/%s",
|
|
|
|
driver->autoDiskLeasePath,
|
2013-07-04 10:11:02 +00:00
|
|
|
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE) < 0)
|
2011-06-14 08:29:00 +00:00
|
|
|
goto error;
|
|
|
|
|
2013-01-18 13:50:37 +00:00
|
|
|
if (!virStrcpyStatic(ls.name,
|
|
|
|
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Lockspace path '%s' exceeded %d characters"),
|
|
|
|
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE,
|
|
|
|
SANLK_PATH_LEN);
|
|
|
|
goto error;
|
|
|
|
}
|
2011-06-14 08:29:00 +00:00
|
|
|
ls.host_id = 0; /* Doesn't matter for initialization */
|
|
|
|
ls.flags = 0;
|
2011-08-30 16:24:06 +00:00
|
|
|
if (!virStrcpy(ls.host_id_disk.path, path, SANLK_PATH_LEN)) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Lockspace path '%s' exceeded %d characters"),
|
|
|
|
path, SANLK_PATH_LEN);
|
2011-06-14 08:29:00 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
ls.host_id_disk.offset = 0;
|
|
|
|
|
|
|
|
/* Stage 1: Ensure the lockspace file exists on disk, has
|
|
|
|
* space allocated for it and is initialized with lease
|
|
|
|
*/
|
|
|
|
if (stat(path, &st) < 0) {
|
2012-10-23 14:34:21 +00:00
|
|
|
int perms = 0600;
|
2011-06-14 08:29:00 +00:00
|
|
|
VIR_DEBUG("Lockspace %s does not yet exist", path);
|
2012-08-21 13:27:10 +00:00
|
|
|
|
|
|
|
if (!(dir = mdir_name(path))) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (stat(dir, &st) < 0 || !S_ISDIR(st.st_mode)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to create lockspace %s: parent directory"
|
|
|
|
" does not exist or is not a directory"),
|
|
|
|
path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-01-07 22:50:41 +00:00
|
|
|
if (driver->group != (gid_t) -1)
|
2012-10-23 14:34:21 +00:00
|
|
|
perms |= 0060;
|
|
|
|
|
|
|
|
if ((fd = open(path, O_WRONLY|O_CREAT|O_EXCL, perms)) < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
if (errno != EEXIST) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to create lockspace %s"),
|
|
|
|
path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Someone else just created lockspace %s", path);
|
|
|
|
} else {
|
2012-10-23 14:34:21 +00:00
|
|
|
/* chown() the path to make sure sanlock can access it */
|
2013-01-07 22:50:41 +00:00
|
|
|
if ((driver->user != (uid_t) -1 || driver->group != (gid_t) -1) &&
|
2012-10-23 14:34:21 +00:00
|
|
|
(fchown(fd, driver->user, driver->group) < 0)) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot chown '%s' to (%u, %u)"),
|
|
|
|
path,
|
|
|
|
(unsigned int) driver->user,
|
|
|
|
(unsigned int) driver->group);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
|
2011-09-19 10:04:59 +00:00
|
|
|
if ((rv = sanlock_align(&ls.host_id_disk)) < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to query sector size %s: error %d"),
|
|
|
|
path, rv);
|
2011-06-14 08:29:00 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv,
|
|
|
|
_("Unable to query sector size %s"),
|
|
|
|
path);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pre allocate enough data for 1 block of leases at preferred alignment
|
|
|
|
*/
|
2011-07-06 22:32:10 +00:00
|
|
|
if (safezero(fd, 0, rv) < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to allocate lockspace %s"),
|
|
|
|
path);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to save lockspace %s"),
|
|
|
|
path);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
2011-01-18 18:37:45 +00:00
|
|
|
|
2011-09-19 10:04:59 +00:00
|
|
|
if ((rv = sanlock_init(&ls, NULL, 0, 0)) < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to initialize lockspace %s: error %d"),
|
|
|
|
path, rv);
|
2011-06-14 08:29:00 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv,
|
|
|
|
_("Unable to initialize lockspace %s"),
|
|
|
|
path);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Lockspace %s has been initialized", path);
|
|
|
|
}
|
2012-10-23 14:34:21 +00:00
|
|
|
} else if (S_ISREG(st.st_mode)) {
|
|
|
|
/* okay, the lease file exists. Check the permissions */
|
2013-01-07 22:50:41 +00:00
|
|
|
if (((driver->user != (uid_t) -1 && driver->user != st.st_uid) ||
|
|
|
|
(driver->group != (gid_t) -1 && driver->group != st.st_gid)) &&
|
2012-10-23 14:34:21 +00:00
|
|
|
(chown(path, driver->user, driver->group) < 0)) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot chown '%s' to (%u, %u)"),
|
|
|
|
path,
|
|
|
|
(unsigned int) driver->user,
|
|
|
|
(unsigned int) driver->group);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-01-07 22:50:41 +00:00
|
|
|
if ((driver->group != (gid_t) -1 && (st.st_mode & 0060) != 0060) &&
|
2012-10-23 14:34:21 +00:00
|
|
|
chmod(path, 0660) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot chmod '%s' to 0660"),
|
|
|
|
path);
|
|
|
|
goto error;
|
|
|
|
}
|
2011-06-14 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ls.host_id = driver->hostID;
|
2012-11-15 10:19:08 +00:00
|
|
|
/* Stage 2: Try to register the lockspace with the daemon. If the lockspace
|
|
|
|
* is already registered, we should get EEXIST back in which case we can
|
|
|
|
* just carry on with life. If EINPROGRESS is returned, we have two options:
|
|
|
|
* either call a sanlock API that blocks us until lockspace changes state,
|
|
|
|
* or we can fallback to polling.
|
2011-06-14 08:29:00 +00:00
|
|
|
*/
|
2014-03-25 06:51:47 +00:00
|
|
|
retry:
|
2011-06-14 08:29:00 +00:00
|
|
|
if ((rv = sanlock_add_lockspace(&ls, 0)) < 0) {
|
2012-12-14 11:17:55 +00:00
|
|
|
if (-rv == EINPROGRESS && --retries) {
|
2012-11-15 10:19:08 +00:00
|
|
|
#ifdef HAVE_SANLOCK_INQ_LOCKSPACE
|
|
|
|
/* we have this function which blocks until lockspace change the
|
|
|
|
* state. It returns 0 if lockspace has been added, -ENOENT if it
|
2012-12-14 11:17:55 +00:00
|
|
|
* hasn't. */
|
2012-11-15 10:19:08 +00:00
|
|
|
VIR_DEBUG("Inquiring lockspace");
|
2012-12-14 11:17:55 +00:00
|
|
|
if (sanlock_inq_lockspace(&ls, SANLK_INQ_WAIT) < 0)
|
|
|
|
VIR_DEBUG("Unable to inquire lockspace");
|
2012-11-15 10:19:08 +00:00
|
|
|
#else
|
|
|
|
/* fall back to polling */
|
2012-12-14 11:17:55 +00:00
|
|
|
VIR_DEBUG("Sleeping for %dms", LOCKSPACE_SLEEP);
|
|
|
|
usleep(LOCKSPACE_SLEEP * 1000);
|
2012-11-15 10:19:08 +00:00
|
|
|
#endif
|
2012-12-14 11:17:55 +00:00
|
|
|
VIR_DEBUG("Retrying to add lockspace (left %d)", retries);
|
|
|
|
goto retry;
|
2012-11-15 10:19:08 +00:00
|
|
|
}
|
2011-06-14 08:29:00 +00:00
|
|
|
if (-rv != EEXIST) {
|
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to add lockspace %s: error %d"),
|
|
|
|
path, rv);
|
2011-06-14 08:29:00 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv,
|
|
|
|
_("Unable to add lockspace %s"),
|
|
|
|
path);
|
2012-08-13 20:21:10 +00:00
|
|
|
goto error;
|
2011-06-14 08:29:00 +00:00
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Lockspace %s is already registered", path);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Lockspace %s has been registered", path);
|
|
|
|
}
|
|
|
|
|
2013-01-15 14:23:29 +00:00
|
|
|
VIR_FREE(path);
|
|
|
|
VIR_FREE(dir);
|
2011-06-14 08:29:00 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error_unlink:
|
2011-09-21 20:22:57 +00:00
|
|
|
unlink(path);
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2011-06-14 08:29:00 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
VIR_FREE(path);
|
2012-08-21 13:27:10 +00:00
|
|
|
VIR_FREE(dir);
|
2011-06-14 08:29:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int virLockManagerSanlockDeinit(void);
|
2011-06-14 08:20:49 +00:00
|
|
|
static int virLockManagerSanlockInit(unsigned int version,
|
|
|
|
const char *configFile,
|
2011-01-18 18:37:45 +00:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
2011-07-08 15:18:48 +00:00
|
|
|
VIR_DEBUG("version=%u configFile=%s flags=%x",
|
|
|
|
version, NULLSTR(configFile), flags);
|
2011-01-18 18:37:45 +00:00
|
|
|
virCheckFlags(0, -1);
|
2011-06-14 08:20:49 +00:00
|
|
|
|
|
|
|
if (driver)
|
|
|
|
return 0;
|
|
|
|
|
2013-07-04 10:11:02 +00:00
|
|
|
if (VIR_ALLOC(driver) < 0)
|
2011-06-14 08:20:49 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
driver->requireLeaseForDisks = true;
|
2011-06-14 08:29:00 +00:00
|
|
|
driver->hostID = 0;
|
|
|
|
driver->autoDiskLease = false;
|
2013-01-07 22:50:41 +00:00
|
|
|
driver->user = (uid_t) -1;
|
|
|
|
driver->group = (gid_t) -1;
|
2013-05-03 12:43:12 +00:00
|
|
|
if (VIR_STRDUP(driver->autoDiskLeasePath, LOCALSTATEDIR "/lib/libvirt/sanlock") < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
VIR_FREE(driver);
|
|
|
|
goto error;
|
|
|
|
}
|
2011-06-14 08:20:49 +00:00
|
|
|
|
|
|
|
if (virLockManagerSanlockLoadConfig(configFile) < 0)
|
2011-06-14 08:29:00 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (driver->autoDiskLease && !driver->hostID) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Automatic disk lease mode enabled, but no host ID is set"));
|
2011-06-14 08:29:00 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-07-08 10:18:35 +00:00
|
|
|
if (driver->autoDiskLease) {
|
|
|
|
if (virLockManagerSanlockSetupLockspace() < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2011-06-14 08:20:49 +00:00
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
return 0;
|
2011-06-14 08:29:00 +00:00
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2011-06-14 08:29:00 +00:00
|
|
|
virLockManagerSanlockDeinit();
|
|
|
|
return -1;
|
2011-01-18 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int virLockManagerSanlockDeinit(void)
|
|
|
|
{
|
2011-06-14 08:20:49 +00:00
|
|
|
if (!driver)
|
|
|
|
return 0;
|
|
|
|
|
2011-06-14 08:29:00 +00:00
|
|
|
VIR_FREE(driver->autoDiskLeasePath);
|
|
|
|
VIR_FREE(driver);
|
|
|
|
|
|
|
|
return 0;
|
2011-01-18 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2011-06-14 08:20:49 +00:00
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
static int virLockManagerSanlockNew(virLockManagerPtr lock,
|
|
|
|
unsigned int type,
|
|
|
|
size_t nparams,
|
|
|
|
virLockManagerParamPtr params,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virLockManagerParamPtr param;
|
|
|
|
virLockManagerSanlockPrivatePtr priv;
|
Convert 'int i' to 'size_t i' in src/locking/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2014-05-12 09:38:47 +00:00
|
|
|
int resCount = 0;
|
2011-01-18 18:37:45 +00:00
|
|
|
|
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
2011-06-14 08:20:49 +00:00
|
|
|
if (!driver) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Sanlock plugin is not initialized"));
|
2011-06-14 08:20:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
if (type != VIR_LOCK_MANAGER_OBJECT_TYPE_DOMAIN) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unsupported object type %d"), type);
|
2011-01-18 18:37:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-04 10:11:02 +00:00
|
|
|
if (VIR_ALLOC(priv) < 0)
|
2011-01-18 18:37:45 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
priv->flags = flags;
|
|
|
|
|
|
|
|
for (i = 0; i < nparams; i++) {
|
|
|
|
param = ¶ms[i];
|
|
|
|
|
|
|
|
if (STREQ(param->key, "uuid")) {
|
|
|
|
memcpy(priv->vm_uuid, param->value.uuid, 16);
|
|
|
|
} else if (STREQ(param->key, "name")) {
|
2014-02-27 08:32:41 +00:00
|
|
|
if (VIR_STRDUP(priv->vm_name, param->value.str) < 0)
|
2011-01-18 18:37:45 +00:00
|
|
|
goto error;
|
|
|
|
} else if (STREQ(param->key, "pid")) {
|
|
|
|
priv->vm_pid = param->value.ui;
|
|
|
|
} else if (STREQ(param->key, "id")) {
|
|
|
|
priv->vm_id = param->value.ui;
|
2012-09-18 11:41:26 +00:00
|
|
|
} else if (STREQ(param->key, "uri")) {
|
|
|
|
priv->vm_uri = param->value.cstr;
|
2011-01-18 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 09:38:47 +00:00
|
|
|
/* Sanlock needs process registration, but the only way how to probe
|
|
|
|
* whether a process has been registered is to inquire the lock. If
|
|
|
|
* sanlock_inquire() returns -ESRCH, then it is not registered, but
|
|
|
|
* if it returns any other error (rv < 0), then we cannot fail due
|
|
|
|
* to back-compat. So this whole call is non-fatal, because it's
|
|
|
|
* called from all over the place (it will usually fail). It merely
|
|
|
|
* updates privateData. */
|
|
|
|
if (sanlock_inquire(-1, priv->vm_pid, 0, &resCount, NULL) >= 0)
|
|
|
|
priv->registered = true;
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
lock->privateData = priv;
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2011-01-18 18:37:45 +00:00
|
|
|
VIR_FREE(priv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virLockManagerSanlockFree(virLockManagerPtr lock)
|
|
|
|
{
|
|
|
|
virLockManagerSanlockPrivatePtr priv = lock->privateData;
|
Convert 'int i' to 'size_t i' in src/locking/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-01-18 18:37:45 +00:00
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
return;
|
|
|
|
|
2014-02-27 08:32:41 +00:00
|
|
|
VIR_FREE(priv->vm_name);
|
2011-01-18 18:37:45 +00:00
|
|
|
for (i = 0; i < priv->res_count; i++)
|
|
|
|
VIR_FREE(priv->res_args[i]);
|
|
|
|
VIR_FREE(priv);
|
|
|
|
lock->privateData = NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-14 08:29:00 +00:00
|
|
|
|
|
|
|
static int virLockManagerSanlockAddLease(virLockManagerPtr lock,
|
|
|
|
const char *name,
|
|
|
|
size_t nparams,
|
2012-06-21 14:34:46 +00:00
|
|
|
virLockManagerParamPtr params,
|
|
|
|
bool shared)
|
2011-06-14 08:29:00 +00:00
|
|
|
{
|
|
|
|
virLockManagerSanlockPrivatePtr priv = lock->privateData;
|
|
|
|
int ret = -1;
|
|
|
|
struct sanlk_resource *res = NULL;
|
Convert 'int i' to 'size_t i' in src/locking/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-01-18 18:37:45 +00:00
|
|
|
|
2013-07-04 10:11:02 +00:00
|
|
|
if (VIR_ALLOC_VAR(res, struct sanlk_disk, 1) < 0)
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
2011-01-18 18:37:45 +00:00
|
|
|
|
2012-06-21 14:34:46 +00:00
|
|
|
res->flags = shared ? SANLK_RES_SHARED : 0;
|
2011-01-18 18:37:45 +00:00
|
|
|
res->num_disks = 1;
|
|
|
|
if (!virStrcpy(res->name, name, SANLK_NAME_LEN)) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Resource name '%s' exceeds %d characters"),
|
|
|
|
name, SANLK_NAME_LEN);
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
2011-01-18 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nparams; i++) {
|
|
|
|
if (STREQ(params[i].key, "path")) {
|
|
|
|
if (!virStrcpy(res->disks[0].path, params[i].value.str, SANLK_PATH_LEN)) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Lease path '%s' exceeds %d characters"),
|
|
|
|
params[i].value.str, SANLK_PATH_LEN);
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
2011-01-18 18:37:45 +00:00
|
|
|
}
|
|
|
|
} else if (STREQ(params[i].key, "offset")) {
|
|
|
|
res->disks[0].offset = params[i].value.ul;
|
|
|
|
} else if (STREQ(params[i].key, "lockspace")) {
|
|
|
|
if (!virStrcpy(res->lockspace_name, params[i].value.str, SANLK_NAME_LEN)) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Resource lockspace '%s' exceeds %d characters"),
|
|
|
|
params[i].value.str, SANLK_NAME_LEN);
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
2011-01-18 18:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->res_args[priv->res_count] = res;
|
|
|
|
priv->res_count++;
|
2011-06-14 08:29:00 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
cleanup:
|
2011-06-14 08:29:00 +00:00
|
|
|
if (ret == -1)
|
|
|
|
VIR_FREE(res);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int virLockManagerSanlockAddDisk(virLockManagerPtr lock,
|
|
|
|
const char *name,
|
|
|
|
size_t nparams,
|
2012-06-21 14:34:46 +00:00
|
|
|
virLockManagerParamPtr params ATTRIBUTE_UNUSED,
|
|
|
|
bool shared)
|
2011-06-14 08:29:00 +00:00
|
|
|
{
|
|
|
|
virLockManagerSanlockPrivatePtr priv = lock->privateData;
|
|
|
|
int ret = -1;
|
|
|
|
struct sanlk_resource *res = NULL;
|
|
|
|
char *path = NULL;
|
2014-03-05 12:39:00 +00:00
|
|
|
char *hash = NULL;
|
2011-06-14 08:29:00 +00:00
|
|
|
|
|
|
|
if (nparams) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Unexpected lock parameters for disk resource"));
|
2011-06-14 08:29:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-04 10:11:02 +00:00
|
|
|
if (VIR_ALLOC_VAR(res, struct sanlk_disk, 1) < 0)
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2012-06-21 14:34:46 +00:00
|
|
|
res->flags = shared ? SANLK_RES_SHARED : 0;
|
2011-06-14 08:29:00 +00:00
|
|
|
res->num_disks = 1;
|
2014-03-05 12:39:00 +00:00
|
|
|
if (virCryptoHashString(VIR_CRYPTO_HASH_MD5, name, &hash) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (!virStrcpy(res->name, hash, SANLK_NAME_LEN)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("MD5 hash '%s' unexpectedly larger than %d characters"),
|
|
|
|
hash, (SANLK_NAME_LEN - 1));
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
2014-03-05 12:39:00 +00:00
|
|
|
}
|
2011-06-14 08:29:00 +00:00
|
|
|
|
|
|
|
if (virAsprintf(&path, "%s/%s",
|
2013-07-04 10:11:02 +00:00
|
|
|
driver->autoDiskLeasePath, res->name) < 0)
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
if (!virStrcpy(res->disks[0].path, path, SANLK_PATH_LEN)) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Lease path '%s' exceeds %d characters"),
|
|
|
|
path, SANLK_PATH_LEN);
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStrcpy(res->lockspace_name,
|
|
|
|
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE,
|
|
|
|
SANLK_NAME_LEN)) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Resource lockspace '%s' exceeds %d characters"),
|
|
|
|
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE, SANLK_NAME_LEN);
|
2011-06-14 08:29:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->res_args[priv->res_count] = res;
|
|
|
|
priv->res_count++;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
cleanup:
|
2011-06-14 08:29:00 +00:00
|
|
|
if (ret == -1)
|
|
|
|
VIR_FREE(res);
|
|
|
|
VIR_FREE(path);
|
2014-03-05 12:39:00 +00:00
|
|
|
VIR_FREE(hash);
|
2011-06-14 08:29:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int virLockManagerSanlockCreateLease(struct sanlk_resource *res)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
struct stat st;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (stat(res->disks[0].path, &st) < 0) {
|
|
|
|
VIR_DEBUG("Lockspace %s does not yet exist", res->disks[0].path);
|
|
|
|
if ((fd = open(res->disks[0].path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to create lockspace %s"),
|
|
|
|
res->disks[0].path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Someone else just created lockspace %s", res->disks[0].path);
|
|
|
|
} else {
|
2012-12-21 11:35:21 +00:00
|
|
|
/* chown() the path to make sure sanlock can access it */
|
2013-01-07 22:50:41 +00:00
|
|
|
if ((driver->user != (uid_t) -1 || driver->group != (gid_t) -1) &&
|
2012-12-21 11:35:21 +00:00
|
|
|
(fchown(fd, driver->user, driver->group) < 0)) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot chown '%s' to (%u, %u)"),
|
|
|
|
res->disks[0].path,
|
|
|
|
(unsigned int) driver->user,
|
|
|
|
(unsigned int) driver->group);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
|
2011-09-19 10:04:59 +00:00
|
|
|
if ((rv = sanlock_align(&res->disks[0])) < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to query sector size %s: error %d"),
|
|
|
|
res->disks[0].path, rv);
|
2011-06-14 08:29:00 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv,
|
|
|
|
_("Unable to query sector size %s"),
|
|
|
|
res->disks[0].path);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pre allocate enough data for 1 block of leases at preferred alignment
|
|
|
|
*/
|
2011-07-06 22:32:10 +00:00
|
|
|
if (safezero(fd, 0, rv) < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to allocate lease %s"),
|
|
|
|
res->disks[0].path);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to save lease %s"),
|
|
|
|
res->disks[0].path);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
|
2011-09-19 10:04:59 +00:00
|
|
|
if ((rv = sanlock_init(NULL, res, 0, 0)) < 0) {
|
2011-06-14 08:29:00 +00:00
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to initialize lease %s: error %d"),
|
|
|
|
res->disks[0].path, rv);
|
2011-06-14 08:29:00 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv,
|
|
|
|
_("Unable to initialize lease %s"),
|
|
|
|
res->disks[0].path);
|
|
|
|
goto error_unlink;
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Lease %s has been initialized", res->disks[0].path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error_unlink:
|
2011-06-14 08:29:00 +00:00
|
|
|
unlink(res->disks[0].path);
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
2011-01-18 18:37:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-14 08:29:00 +00:00
|
|
|
|
|
|
|
static int virLockManagerSanlockAddResource(virLockManagerPtr lock,
|
|
|
|
unsigned int type,
|
|
|
|
const char *name,
|
|
|
|
size_t nparams,
|
|
|
|
virLockManagerParamPtr params,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virLockManagerSanlockPrivatePtr priv = lock->privateData;
|
|
|
|
|
|
|
|
virCheckFlags(VIR_LOCK_MANAGER_RESOURCE_READONLY |
|
|
|
|
VIR_LOCK_MANAGER_RESOURCE_SHARED, -1);
|
|
|
|
|
|
|
|
if (priv->res_count == SANLK_MAX_RESOURCES) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Too many resources %d for object"),
|
|
|
|
SANLK_MAX_RESOURCES);
|
2011-06-14 08:29:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-06-21 14:34:46 +00:00
|
|
|
/* Treat R/O resources as a no-op lock request */
|
|
|
|
if (flags & VIR_LOCK_MANAGER_RESOURCE_READONLY)
|
|
|
|
return 0;
|
2011-06-14 08:29:00 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case VIR_LOCK_MANAGER_RESOURCE_TYPE_DISK:
|
|
|
|
if (driver->autoDiskLease) {
|
2012-06-21 14:34:46 +00:00
|
|
|
if (virLockManagerSanlockAddDisk(lock, name, nparams, params,
|
|
|
|
!!(flags & VIR_LOCK_MANAGER_RESOURCE_SHARED)) < 0)
|
2011-06-14 08:29:00 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virLockManagerSanlockCreateLease(priv->res_args[priv->res_count-1]) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (!(flags & (VIR_LOCK_MANAGER_RESOURCE_SHARED |
|
|
|
|
VIR_LOCK_MANAGER_RESOURCE_READONLY)))
|
|
|
|
priv->hasRWDisks = true;
|
|
|
|
/* Ignore disk resources without error */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_LOCK_MANAGER_RESOURCE_TYPE_LEASE:
|
2012-06-21 14:34:46 +00:00
|
|
|
if (virLockManagerSanlockAddLease(lock, name, nparams, params,
|
|
|
|
!!(flags & VIR_LOCK_MANAGER_RESOURCE_SHARED)) < 0)
|
2011-06-14 08:29:00 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Ignore other resources, without error */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-16 10:41:20 +00:00
|
|
|
#if HAVE_SANLOCK_KILLPATH
|
2012-09-18 11:41:26 +00:00
|
|
|
static int
|
|
|
|
virLockManagerSanlockRegisterKillscript(int sock,
|
|
|
|
const char *vmuri,
|
|
|
|
const char *uuidstr,
|
|
|
|
virDomainLockFailureAction action)
|
|
|
|
{
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
char *path;
|
|
|
|
char *args = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
int rv;
|
|
|
|
|
2014-03-24 13:22:36 +00:00
|
|
|
switch (action) {
|
|
|
|
case VIR_DOMAIN_LOCK_FAILURE_DEFAULT:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VIR_DOMAIN_LOCK_FAILURE_POWEROFF:
|
|
|
|
case VIR_DOMAIN_LOCK_FAILURE_PAUSE:
|
|
|
|
break;
|
|
|
|
|
2014-03-24 13:23:09 +00:00
|
|
|
case VIR_DOMAIN_LOCK_FAILURE_RESTART:
|
2014-03-24 13:22:36 +00:00
|
|
|
case VIR_DOMAIN_LOCK_FAILURE_IGNORE:
|
|
|
|
case VIR_DOMAIN_LOCK_FAILURE_LAST:
|
2012-09-18 11:41:26 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Failure action %s is not supported by sanlock"),
|
|
|
|
virDomainLockFailureTypeToString(action));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
virBufferEscape(&buf, '\\', "\\ ", "%s", vmuri);
|
|
|
|
virBufferAddLit(&buf, " ");
|
|
|
|
virBufferEscape(&buf, '\\', "\\ ", "%s", uuidstr);
|
|
|
|
virBufferAddLit(&buf, " ");
|
|
|
|
virBufferEscape(&buf, '\\', "\\ ", "%s",
|
|
|
|
virDomainLockFailureTypeToString(action));
|
|
|
|
|
2014-06-27 08:40:15 +00:00
|
|
|
if (virBufferCheckError(&buf) < 0)
|
2012-09-18 11:41:26 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* Unfortunately, sanlock_killpath() does not use const for either
|
|
|
|
* path or args even though it will just copy them into its own
|
|
|
|
* buffers.
|
|
|
|
*/
|
|
|
|
path = (char *) VIR_LOCK_MANAGER_SANLOCK_KILLPATH;
|
|
|
|
args = virBufferContentAndReset(&buf);
|
|
|
|
|
|
|
|
VIR_DEBUG("Register sanlock killpath: %s %s", path, args);
|
|
|
|
|
|
|
|
/* sanlock_killpath() would just crop the strings */
|
|
|
|
if (strlen(path) >= SANLK_HELPER_PATH_LEN) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Sanlock helper path is longer than %d: '%s'"),
|
|
|
|
SANLK_HELPER_PATH_LEN - 1, path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (strlen(args) >= SANLK_HELPER_ARGS_LEN) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Sanlock helper arguments are longer than %d:"
|
|
|
|
" '%s'"),
|
|
|
|
SANLK_HELPER_ARGS_LEN - 1, args);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rv = sanlock_killpath(sock, 0, path, args)) < 0) {
|
|
|
|
if (rv <= -200) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to register lock failure action:"
|
|
|
|
" error %d"), rv);
|
|
|
|
} else {
|
|
|
|
virReportSystemError(-rv, "%s",
|
|
|
|
_("Failed to register lock failure"
|
|
|
|
" action"));
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
cleanup:
|
2012-09-18 11:41:26 +00:00
|
|
|
VIR_FREE(args);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-10-16 10:41:20 +00:00
|
|
|
#else
|
|
|
|
static int
|
|
|
|
virLockManagerSanlockRegisterKillscript(int sock ATTRIBUTE_UNUSED,
|
|
|
|
const char *vmuri ATTRIBUTE_UNUSED,
|
|
|
|
const char *uuidstr ATTRIBUTE_UNUSED,
|
|
|
|
virDomainLockFailureAction action ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("sanlock is too old to support lock failure action"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-18 11:41:26 +00:00
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
|
|
|
|
const char *state,
|
2011-06-24 14:14:41 +00:00
|
|
|
unsigned int flags,
|
2012-09-18 11:41:26 +00:00
|
|
|
virDomainLockFailureAction action,
|
2011-06-24 14:14:41 +00:00
|
|
|
int *fd)
|
2011-01-18 18:37:45 +00:00
|
|
|
{
|
|
|
|
virLockManagerSanlockPrivatePtr priv = lock->privateData;
|
2014-09-03 11:18:59 +00:00
|
|
|
struct sanlk_options *opt = NULL;
|
2011-01-18 18:37:45 +00:00
|
|
|
struct sanlk_resource **res_args;
|
|
|
|
int res_count;
|
|
|
|
bool res_free = false;
|
|
|
|
int sock = -1;
|
|
|
|
int rv;
|
Convert 'int i' to 'size_t i' in src/locking/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-01-18 18:37:45 +00:00
|
|
|
|
2011-06-02 10:46:56 +00:00
|
|
|
virCheckFlags(VIR_LOCK_MANAGER_ACQUIRE_RESTRICT |
|
|
|
|
VIR_LOCK_MANAGER_ACQUIRE_REGISTER_ONLY, -1);
|
2011-01-18 18:37:45 +00:00
|
|
|
|
|
|
|
if (priv->res_count == 0 &&
|
2011-06-14 08:20:49 +00:00
|
|
|
priv->hasRWDisks &&
|
|
|
|
driver->requireLeaseForDisks) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Read/write, exclusive access, disks were present, but no leases specified"));
|
2011-01-18 18:37:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We only initialize 'sock' if we are in the real
|
|
|
|
* child process and we need it to be inherited
|
|
|
|
*
|
2014-03-17 09:38:38 +00:00
|
|
|
* If sock == -1, then sanlock auto-open/closes a
|
2011-01-18 18:37:45 +00:00
|
|
|
* temporary sock
|
|
|
|
*/
|
2012-09-18 11:41:26 +00:00
|
|
|
if (priv->vm_pid == getpid()) {
|
|
|
|
VIR_DEBUG("Register sanlock %d", flags);
|
|
|
|
if ((sock = sanlock_register()) < 0) {
|
|
|
|
if (sock <= -200)
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to open socket to sanlock daemon: error %d"),
|
|
|
|
sock);
|
|
|
|
else
|
|
|
|
virReportSystemError(-sock, "%s",
|
|
|
|
_("Failed to open socket to sanlock daemon"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-05-12 09:38:47 +00:00
|
|
|
/* Mark the pid as registered */
|
|
|
|
priv->registered = true;
|
|
|
|
|
2012-09-18 11:41:26 +00:00
|
|
|
if (action != VIR_DOMAIN_LOCK_FAILURE_DEFAULT) {
|
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
|
|
|
virUUIDFormat(priv->vm_uuid, uuidstr);
|
|
|
|
if (virLockManagerSanlockRegisterKillscript(sock, priv->vm_uri,
|
|
|
|
uuidstr, action) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2014-05-12 09:38:47 +00:00
|
|
|
} else if (!priv->registered) {
|
|
|
|
VIR_DEBUG("Process not registered, not acquiring lock");
|
|
|
|
return 0;
|
2011-01-18 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 11:40:38 +00:00
|
|
|
if (VIR_ALLOC(opt) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2014-05-12 09:38:08 +00:00
|
|
|
/* sanlock doesn't use owner_name for anything, so it's safe to take just
|
|
|
|
* the first SANLK_NAME_LEN - 1 characters from vm_name */
|
|
|
|
ignore_value(virStrncpy(opt->owner_name, priv->vm_name,
|
|
|
|
SANLK_NAME_LEN - 1, SANLK_NAME_LEN));
|
|
|
|
|
|
|
|
if (state && STRNEQ(state, "")) {
|
|
|
|
if ((rv = sanlock_state_to_args((char *)state,
|
|
|
|
&res_count,
|
|
|
|
&res_args)) < 0) {
|
|
|
|
if (rv <= -200)
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse lock state %s: error %d"),
|
|
|
|
state, rv);
|
|
|
|
else
|
|
|
|
virReportSystemError(-rv,
|
|
|
|
_("Unable to parse lock state %s"),
|
|
|
|
state);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
res_free = true;
|
|
|
|
} else {
|
|
|
|
res_args = priv->res_args;
|
|
|
|
res_count = priv->res_count;
|
|
|
|
}
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
if (!(flags & VIR_LOCK_MANAGER_ACQUIRE_REGISTER_ONLY)) {
|
|
|
|
VIR_DEBUG("Acquiring object %u", priv->res_count);
|
|
|
|
if ((rv = sanlock_acquire(sock, priv->vm_pid, 0,
|
|
|
|
priv->res_count, priv->res_args,
|
|
|
|
opt)) < 0) {
|
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to acquire lock: error %d"), rv);
|
2011-01-18 18:37:45 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv, "%s",
|
|
|
|
_("Failed to acquire lock"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(opt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are *intentionally* "leaking" sock file descriptor
|
|
|
|
* because we want it to be inherited by QEMU. When the
|
|
|
|
* sock FD finally closes upon QEMU exit (or crash) then
|
|
|
|
* sanlock will notice EOF and release the lock
|
|
|
|
*/
|
|
|
|
if (sock != -1 &&
|
|
|
|
virSetInherit(sock, true) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2011-06-02 10:46:56 +00:00
|
|
|
if (flags & VIR_LOCK_MANAGER_ACQUIRE_RESTRICT) {
|
|
|
|
if ((rv = sanlock_restrict(sock, SANLK_RESTRICT_ALL)) < 0) {
|
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to restrict process: error %d"), rv);
|
2011-06-02 10:46:56 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv, "%s",
|
|
|
|
_("Failed to restrict process"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
VIR_DEBUG("Acquire completed fd=%d", sock);
|
|
|
|
|
|
|
|
if (res_free) {
|
2014-11-13 14:27:11 +00:00
|
|
|
for (i = 0; i < res_count; i++)
|
2011-01-18 18:37:45 +00:00
|
|
|
VIR_FREE(res_args[i]);
|
|
|
|
VIR_FREE(res_args);
|
|
|
|
}
|
|
|
|
|
2011-06-24 14:14:41 +00:00
|
|
|
if (fd)
|
|
|
|
*fd = sock;
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2011-01-18 18:37:45 +00:00
|
|
|
if (res_free) {
|
2014-11-13 14:27:11 +00:00
|
|
|
for (i = 0; i < res_count; i++)
|
2011-01-18 18:37:45 +00:00
|
|
|
VIR_FREE(res_args[i]);
|
|
|
|
VIR_FREE(res_args);
|
|
|
|
}
|
|
|
|
VIR_FREE(opt);
|
|
|
|
VIR_FORCE_CLOSE(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int virLockManagerSanlockRelease(virLockManagerPtr lock,
|
|
|
|
char **state,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virLockManagerSanlockPrivatePtr priv = lock->privateData;
|
2012-08-13 10:54:38 +00:00
|
|
|
int res_count = priv->res_count;
|
2011-01-18 18:37:45 +00:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
2014-05-12 09:38:47 +00:00
|
|
|
if (!priv->registered) {
|
|
|
|
VIR_DEBUG("Process not registered, skipping release");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-02 13:44:10 +00:00
|
|
|
if (state) {
|
|
|
|
if ((rv = sanlock_inquire(-1, priv->vm_pid, 0, &res_count, state)) < 0) {
|
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to inquire lock: error %d"), rv);
|
2011-06-02 13:44:10 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv, "%s",
|
|
|
|
_("Failed to inquire lock"));
|
|
|
|
return -1;
|
|
|
|
}
|
2011-01-18 18:37:45 +00:00
|
|
|
|
2012-03-07 14:04:33 +00:00
|
|
|
if (STREQ_NULLABLE(*state, ""))
|
2011-06-02 13:44:10 +00:00
|
|
|
VIR_FREE(*state);
|
|
|
|
}
|
2011-01-18 18:37:45 +00:00
|
|
|
|
2012-08-13 10:54:38 +00:00
|
|
|
if ((rv = sanlock_release(-1, priv->vm_pid, 0, res_count,
|
|
|
|
priv->res_args)) < 0) {
|
2011-01-18 18:37:45 +00:00
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to release lock: error %d"), rv);
|
2011-01-18 18:37:45 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv, "%s",
|
|
|
|
_("Failed to release lock"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virLockManagerSanlockInquire(virLockManagerPtr lock,
|
|
|
|
char **state,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virLockManagerSanlockPrivatePtr priv = lock->privateData;
|
|
|
|
int rv, res_count;
|
|
|
|
|
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
2011-06-02 13:44:10 +00:00
|
|
|
if (!state) {
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2011-06-02 13:44:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
VIR_DEBUG("pid=%d", priv->vm_pid);
|
|
|
|
|
2014-05-12 09:38:47 +00:00
|
|
|
if (!priv->registered) {
|
|
|
|
VIR_DEBUG("Process not registered, skipping inquiry");
|
|
|
|
VIR_FREE(*state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-18 18:37:45 +00:00
|
|
|
if ((rv = sanlock_inquire(-1, priv->vm_pid, 0, &res_count, state)) < 0) {
|
|
|
|
if (rv <= -200)
|
2012-07-18 13:37:08 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to inquire lock: error %d"), rv);
|
2011-01-18 18:37:45 +00:00
|
|
|
else
|
|
|
|
virReportSystemError(-rv, "%s",
|
|
|
|
_("Failed to inquire lock"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-07 14:04:33 +00:00
|
|
|
if (STREQ_NULLABLE(*state, ""))
|
2011-01-18 18:37:45 +00:00
|
|
|
VIR_FREE(*state);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virLockDriver virLockDriverImpl =
|
|
|
|
{
|
|
|
|
.version = VIR_LOCK_MANAGER_VERSION,
|
|
|
|
|
|
|
|
.flags = VIR_LOCK_MANAGER_USES_STATE,
|
|
|
|
|
|
|
|
.drvInit = virLockManagerSanlockInit,
|
|
|
|
.drvDeinit = virLockManagerSanlockDeinit,
|
|
|
|
|
|
|
|
.drvNew = virLockManagerSanlockNew,
|
|
|
|
.drvFree = virLockManagerSanlockFree,
|
|
|
|
|
|
|
|
.drvAddResource = virLockManagerSanlockAddResource,
|
|
|
|
|
|
|
|
.drvAcquire = virLockManagerSanlockAcquire,
|
|
|
|
.drvRelease = virLockManagerSanlockRelease,
|
|
|
|
.drvInquire = virLockManagerSanlockInquire,
|
|
|
|
};
|