libvirt/src/locking/domain_lock.c

383 lines
10 KiB
C
Raw Normal View History

/*
* domain_lock.c: Locking for domain lifecycle operations
*
* Copyright (C) 2010-2014 Red Hat, Inc.
*
* 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 "domain_lock.h"
2012-12-12 18:06:53 +00:00
#include "viralloc.h"
2012-12-13 18:01:25 +00:00
#include "viruuid.h"
#include "virerror.h"
2012-12-12 17:59:27 +00:00
#include "virlog.h"
#define VIR_FROM_THIS VIR_FROM_LOCKING
VIR_LOG_INIT("locking.domain_lock");
static int virDomainLockManagerAddLease(virLockManagerPtr lock,
virDomainLeaseDefPtr lease)
{
unsigned int leaseFlags = 0;
virLockManagerParam lparams[] = {
{ .type = VIR_LOCK_MANAGER_PARAM_TYPE_STRING,
.key = "path",
.value = { .str = lease->path },
},
{ .type = VIR_LOCK_MANAGER_PARAM_TYPE_ULONG,
.key = "offset",
.value = { .ul = lease->offset },
},
{ .type = VIR_LOCK_MANAGER_PARAM_TYPE_STRING,
.key = "lockspace",
.value = { .str = lease->lockspace },
},
};
size_t nparams = ARRAY_CARDINALITY(lparams);
if (!lease->lockspace)
nparams--;
VIR_DEBUG("Add lease %s", lease->path);
if (virLockManagerAddResource(lock,
VIR_LOCK_MANAGER_RESOURCE_TYPE_LEASE,
lease->key,
nparams,
lparams,
leaseFlags) < 0) {
VIR_DEBUG("Failed to add lease %s", lease->path);
return -1;
}
return 0;
}
static int virDomainLockManagerAddImage(virLockManagerPtr lock,
virStorageSourcePtr src)
{
unsigned int diskFlags = 0;
int type = virStorageSourceGetActualType(src);
if (!src->path)
return 0;
conf: move host disk type to util/ A continuation of the migration of disk details to virstoragefile. This patch moves a single enum, but converting the name has quite a bit of fallout. * src/conf/domain_conf.h (virDomainDiskType): Move... * src/util/virstoragefile.h (virStorageType): ...and rename. * src/bhyve/bhyve_command.c (bhyveBuildDiskArgStr) (virBhyveProcessBuildLoadCmd): Update clients. * src/conf/domain_conf.c (virDomainDiskSourceDefParse) (virDomainDiskDefParseXML, virDomainDiskSourceDefFormatInternal) (virDomainDiskDefFormat, virDomainDiskGetActualType) (virDomainDiskDefForeachPath, virDomainDiskSourceIsBlockType): Likewise. * src/conf/snapshot_conf.h (_virDomainSnapshotDiskDef): Likewise. * src/conf/snapshot_conf.c (virDomainSnapshotDiskDefParseXML) (virDomainSnapshotAlignDisks, virDomainSnapshotDiskDefFormat): Likewise. * src/esx/esx_driver.c (esxAutodetectSCSIControllerModel) (esxDomainDefineXML): Likewise. * src/locking/domain_lock.c (virDomainLockManagerAddDisk): Likewise. * src/lxc/lxc_controller.c (virLXCControllerSetupLoopDeviceDisk) (virLXCControllerSetupNBDDeviceDisk) (virLXCControllerSetupLoopDevices, virLXCControllerSetupDisk): Likewise. * src/parallels/parallels_driver.c (parallelsGetHddInfo): Likewise. * src/phyp/phyp_driver.c (phypDiskType): Likewise. * src/qemu/qemu_command.c (qemuGetDriveSourceString) (qemuDomainDiskGetSourceString, qemuBuildDriveStr) (qemuBuildCommandLine, qemuParseCommandLineDisk) (qemuParseCommandLine): Likewise. * src/qemu/qemu_conf.c (qemuCheckSharedDevice) (qemuTranslateDiskSourcePool) (qemuTranslateSnapshotDiskSourcePool): Likewise. * src/qemu/qemu_domain.c (qemuDomainDeviceDefPostParse) (qemuDomainDetermineDiskChain): Likewise. * src/qemu/qemu_driver.c (qemuDomainGetBlockInfo) (qemuDomainSnapshotPrepareDiskExternalBackingInactive) (qemuDomainSnapshotPrepareDiskExternalBackingActive) (qemuDomainSnapshotPrepareDiskExternalOverlayActive) (qemuDomainSnapshotPrepareDiskExternalOverlayInactive) (qemuDomainSnapshotPrepareDiskInternal) (qemuDomainSnapshotPrepare) (qemuDomainSnapshotCreateSingleDiskActive): Likewise. * src/qemu/qemu_hotplug.c (qemuDomainChangeEjectableMedia): Likewise. * src/qemu/qemu_migration.c (qemuMigrationIsSafe): Likewise. * src/security/security_apparmor.c (AppArmorRestoreSecurityImageLabel) (AppArmorSetSecurityImageLabel): Likewise. * src/security/security_dac.c (virSecurityDACSetSecurityImageLabel) (virSecurityDACRestoreSecurityImageLabelInt) (virSecurityDACSetSecurityAllLabel): Likewise. * src/security/security_selinux.c (virSecuritySELinuxRestoreSecurityImageLabelInt) (virSecuritySELinuxSetSecurityImageLabel) (virSecuritySELinuxSetSecurityAllLabel): Likewise. * src/storage/storage_backend.c (virStorageFileBackendForType): Likewise. * src/storage/storage_backend_fs.c (virStorageFileBackendFile) (virStorageFileBackendBlock): Likewise. * src/storage/storage_backend_gluster.c (virStorageFileBackendGluster): Likewise. * src/vbox/vbox_tmpl.c (vboxDomainGetXMLDesc, vboxAttachDrives) (vboxDomainAttachDeviceImpl, vboxDomainDetachDevice): Likewise. * src/vmware/vmware_conf.c (vmwareVmxPath): Likewise. * src/vmx/vmx.c (virVMXParseDisk, virVMXFormatDisk) (virVMXFormatFloppy): Likewise. * src/xenxs/xen_sxpr.c (xenParseSxprDisks, xenParseSxpr) (xenFormatSxprDisk): Likewise. * src/xenxs/xen_xm.c (xenParseXM, xenFormatXMDisk): Likewise. * tests/securityselinuxlabeltest.c (testSELinuxLoadDef): Likewise. * src/libvirt_private.syms (domain_conf.h): Move symbols... (virstoragefile.h): ...as appropriate. Signed-off-by: Eric Blake <eblake@redhat.com>
2014-03-27 21:57:49 +00:00
if (!(type == VIR_STORAGE_TYPE_BLOCK ||
type == VIR_STORAGE_TYPE_FILE ||
type == VIR_STORAGE_TYPE_DIR))
return 0;
if (src->readonly)
diskFlags |= VIR_LOCK_MANAGER_RESOURCE_READONLY;
if (src->shared)
diskFlags |= VIR_LOCK_MANAGER_RESOURCE_SHARED;
VIR_DEBUG("Add disk %s", src->path);
if (virLockManagerAddResource(lock,
VIR_LOCK_MANAGER_RESOURCE_TYPE_DISK,
src->path,
0,
NULL,
diskFlags) < 0) {
VIR_DEBUG("Failed add disk %s", src->path);
return -1;
}
return 0;
}
static virLockManagerPtr virDomainLockManagerNew(virLockManagerPluginPtr plugin,
const char *uri,
virDomainObjPtr dom,
bool withResources,
unsigned int flags)
{
virLockManagerPtr lock;
size_t i;
virLockManagerParam params[] = {
{ .type = VIR_LOCK_MANAGER_PARAM_TYPE_UUID,
.key = "uuid",
},
{ .type = VIR_LOCK_MANAGER_PARAM_TYPE_STRING,
.key = "name",
.value = { .str = dom->def->name },
},
{ .type = VIR_LOCK_MANAGER_PARAM_TYPE_UINT,
.key = "id",
.value = { .iv = dom->def->id },
},
{ .type = VIR_LOCK_MANAGER_PARAM_TYPE_UINT,
.key = "pid",
.value = { .iv = dom->pid },
},
{ .type = VIR_LOCK_MANAGER_PARAM_TYPE_CSTRING,
.key = "uri",
.value = { .cstr = uri },
},
};
VIR_DEBUG("plugin=%p dom=%p withResources=%d",
plugin, dom, withResources);
memcpy(params[0].value.uuid, dom->def->uuid, VIR_UUID_BUFLEN);
if (!(lock = virLockManagerNew(virLockManagerPluginGetDriver(plugin),
VIR_LOCK_MANAGER_OBJECT_TYPE_DOMAIN,
ARRAY_CARDINALITY(params),
params,
flags)))
goto error;
if (withResources) {
VIR_DEBUG("Adding leases");
for (i = 0; i < dom->def->nleases; i++)
if (virDomainLockManagerAddLease(lock, dom->def->leases[i]) < 0)
goto error;
VIR_DEBUG("Adding disks");
for (i = 0; i < dom->def->ndisks; i++) {
virDomainDiskDefPtr disk = dom->def->disks[i];
if (virDomainLockManagerAddImage(lock, disk->src) < 0)
goto error;
}
}
return lock;
error:
virLockManagerFree(lock);
return NULL;
}
int virDomainLockProcessStart(virLockManagerPluginPtr plugin,
const char *uri,
virDomainObjPtr dom,
bool paused,
int *fd)
{
virLockManagerPtr lock;
int ret;
int flags = VIR_LOCK_MANAGER_ACQUIRE_RESTRICT;
VIR_DEBUG("plugin=%p dom=%p paused=%d fd=%p",
plugin, dom, paused, fd);
if (!(lock = virDomainLockManagerNew(plugin, uri, dom, true,
VIR_LOCK_MANAGER_NEW_STARTED)))
return -1;
if (paused)
flags |= VIR_LOCK_MANAGER_ACQUIRE_REGISTER_ONLY;
ret = virLockManagerAcquire(lock, NULL, flags,
dom->def->onLockFailure, fd);
virLockManagerFree(lock);
return ret;
}
int virDomainLockProcessPause(virLockManagerPluginPtr plugin,
virDomainObjPtr dom,
char **state)
{
virLockManagerPtr lock;
int ret;
VIR_DEBUG("plugin=%p dom=%p state=%p",
plugin, dom, state);
if (!(lock = virDomainLockManagerNew(plugin, NULL, dom, true, 0)))
return -1;
ret = virLockManagerRelease(lock, state, 0);
virLockManagerFree(lock);
return ret;
}
int virDomainLockProcessResume(virLockManagerPluginPtr plugin,
const char *uri,
virDomainObjPtr dom,
const char *state)
{
virLockManagerPtr lock;
int ret;
VIR_DEBUG("plugin=%p dom=%p state=%s",
plugin, dom, NULLSTR(state));
if (!(lock = virDomainLockManagerNew(plugin, uri, dom, true, 0)))
return -1;
ret = virLockManagerAcquire(lock, state, 0, dom->def->onLockFailure, NULL);
virLockManagerFree(lock);
return ret;
}
int virDomainLockProcessInquire(virLockManagerPluginPtr plugin,
virDomainObjPtr dom,
char **state)
{
virLockManagerPtr lock;
int ret;
VIR_DEBUG("plugin=%p dom=%p state=%p",
plugin, dom, state);
if (!(lock = virDomainLockManagerNew(plugin, NULL, dom, true, 0)))
return -1;
ret = virLockManagerInquire(lock, state, 0);
virLockManagerFree(lock);
return ret;
}
int virDomainLockImageAttach(virLockManagerPluginPtr plugin,
const char *uri,
virDomainObjPtr dom,
virStorageSourcePtr src)
{
virLockManagerPtr lock;
int ret = -1;
VIR_DEBUG("plugin=%p dom=%p src=%p", plugin, dom, src);
if (!(lock = virDomainLockManagerNew(plugin, uri, dom, false, 0)))
return -1;
if (virDomainLockManagerAddImage(lock, src) < 0)
goto cleanup;
if (virLockManagerAcquire(lock, NULL, 0,
dom->def->onLockFailure, NULL) < 0)
goto cleanup;
ret = 0;
cleanup:
virLockManagerFree(lock);
return ret;
}
int virDomainLockDiskAttach(virLockManagerPluginPtr plugin,
const char *uri,
virDomainObjPtr dom,
virDomainDiskDefPtr disk)
{
return virDomainLockImageAttach(plugin, uri, dom, disk->src);
}
int virDomainLockImageDetach(virLockManagerPluginPtr plugin,
virDomainObjPtr dom,
virStorageSourcePtr src)
{
virLockManagerPtr lock;
int ret = -1;
VIR_DEBUG("plugin=%p dom=%p src=%p", plugin, dom, src);
if (!(lock = virDomainLockManagerNew(plugin, NULL, dom, false, 0)))
return -1;
if (virDomainLockManagerAddImage(lock, src) < 0)
goto cleanup;
if (virLockManagerRelease(lock, NULL, 0) < 0)
goto cleanup;
ret = 0;
cleanup:
virLockManagerFree(lock);
return ret;
}
int virDomainLockDiskDetach(virLockManagerPluginPtr plugin,
virDomainObjPtr dom,
virDomainDiskDefPtr disk)
{
return virDomainLockImageDetach(plugin, dom, disk->src);
}
int virDomainLockLeaseAttach(virLockManagerPluginPtr plugin,
const char *uri,
virDomainObjPtr dom,
virDomainLeaseDefPtr lease)
{
virLockManagerPtr lock;
int ret = -1;
VIR_DEBUG("plugin=%p dom=%p lease=%p",
plugin, dom, lease);
if (!(lock = virDomainLockManagerNew(plugin, uri, dom, false, 0)))
return -1;
if (virDomainLockManagerAddLease(lock, lease) < 0)
goto cleanup;
if (virLockManagerAcquire(lock, NULL, 0,
dom->def->onLockFailure, NULL) < 0)
goto cleanup;
ret = 0;
cleanup:
virLockManagerFree(lock);
return ret;
}
int virDomainLockLeaseDetach(virLockManagerPluginPtr plugin,
virDomainObjPtr dom,
virDomainLeaseDefPtr lease)
{
virLockManagerPtr lock;
int ret = -1;
VIR_DEBUG("plugin=%p dom=%p lease=%p",
plugin, dom, lease);
if (!(lock = virDomainLockManagerNew(plugin, NULL, dom, false, 0)))
return -1;
if (virDomainLockManagerAddLease(lock, lease) < 0)
goto cleanup;
if (virLockManagerRelease(lock, NULL, 0) < 0)
goto cleanup;
ret = 0;
cleanup:
virLockManagerFree(lock);
return ret;
}