From: "Daniel P. Berrange" <berrange(a)redhat.com>
The previously introduced virFile{Lock,Unlock} APIs provide a
way to acquire/release fcntl() locks on individual files. For
unknown reason though, the POSIX spec says that fcntl() locks
are released when *any* file handle refering to the same path
is closed. In the following sequence
threadA: fd1 = open("foo")
threadB: fd2 = open("foo")
threadA: virFileLock(fd1)
threadB: virFileLock(fd2)
threadB: close(fd2)
you'd expect threadA to come out holding a lock on 'foo', and
indeed it does hold a lock for a very short time. Unfortunately
when threadB does close(fd2) this releases the lock associated
with fd1. For the current libvirt use case for virFileLock -
pidfiles - this doesn't matter since the lock is acquired
at startup while single threaded an never released until
exit.
To provide a more generally useful API though, it is neccessary
to introduce a slightly higher level abstraction, which is to
be referred to as a "lockspace". This is to be provided by
a virLockSpacePtr object in src/util/virlockspace.{c,h}. The
core idea is that the lockspace keeps track of what files are
already open+locked. This means that when a 2nd thread comes
along and tries to acquire a lock, it doesn't end up opening
and closing a new FD. The lockspace just checks the current
list of held locks and immediately returns VIR_ERR_RESOURCE_BUSY.
NB, the API as it stands is designed on the basis that the
files being locked are not being otherwise opened and used
by the application code. ie the files which are used to
maintain the locks are not the files associated with the
resources themselves. One approach to using this API is to
acquire locks based on a hash of the filepath.
eg to lock /var/lib/libvirt/images/foo.img the application
might do
virLockSpacePtr lockspace = virLockSpaceNew("/var/lib/libvirt/imagelocks");
lockname = md5sum("/var/lib/libvirt/images/foo.img")
virLockSpaceAcquireLock(lockspace, lockname)
This patch is the basis for the soon to be re-submitted
patch series providing a virtlockd daemon
---
.gitignore | 1 +
include/libvirt/virterror.h | 2 +
src/Makefile.am | 1 +
src/libvirt_private.syms | 9 +
src/util/virlockspace.c | 419 ++++++++++++++++++++++++++++++++++++++++++++
src/util/virlockspace.h | 51 ++++++
src/util/virterror.c | 9 +-
tests/Makefile.am | 7 +-
tests/virlockspacetest.c | 311 ++++++++++++++++++++++++++++++++
9 files changed, 808 insertions(+), 2 deletions(-)
create mode 100644 src/util/virlockspace.c
create mode 100644 src/util/virlockspace.h
create mode 100644 tests/virlockspacetest.c
diff --git a/.gitignore b/.gitignore
index 5ea281a..9e566c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -157,6 +157,7 @@
/tests/virdrivermoduletest
/tests/virhashtest
/tests/virkeyfiletest
+/tests/virlockspacetest
/tests/virnet*test
/tests/virshtest
/tests/virtimetest
diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h
index ad8e101..3e72cb7 100644
--- a/include/libvirt/virterror.h
+++ b/include/libvirt/virterror.h
@@ -110,6 +110,7 @@ typedef enum {
VIR_FROM_AUTH = 46, /* Error from auth handling */
VIR_FROM_DBUS = 47, /* Error from DBus */
VIR_FROM_PARALLELS = 48, /* Error from Parallels */
+ VIR_FROM_LOCKSPACE = 49, /* Error from lockspace */
# ifdef VIR_ENUM_SENTINELS
VIR_ERR_DOMAIN_LAST
@@ -277,6 +278,7 @@ typedef enum {
VIR_ERR_MIGRATE_UNSAFE = 81, /* Migration is not safe */
VIR_ERR_OVERFLOW = 82, /* integer overflow */
VIR_ERR_BLOCK_COPY_ACTIVE = 83, /* action prevented by block copy job */
+ VIR_ERR_RESOURCE_BUSY = 84, /* resource is already in use */
} virErrorNumber;
/**
diff --git a/src/Makefile.am b/src/Makefile.am
index b48ce65..527a2b8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -95,6 +95,7 @@ UTIL_SOURCES = \
util/virkeycode.c util/virkeycode.h \
util/virkeyfile.c util/virkeyfile.h \
util/virkeymaps.h \
+ util/virlockspace.c util/virlockspace.h \
util/virmacaddr.h util/virmacaddr.c \
util/virnetdev.h util/virnetdev.c \
util/virnetdevbandwidth.h util/virnetdevbandwidth.c \
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ac392fe..69f021d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1248,6 +1248,15 @@ virKeyFileHasGroup;
virKeyFileGetValueString;
+# virlockspace.h
+virLockSpaceNew;
+virLockSpaceFree;
+virLockSpaceCreateResource;
+virLockSpaceDeleteResource;
+virLockSpaceAcquireResource;
+virLockSpaceReleaseResource;
+
+
# virmacaddr.h
virMacAddrCmp;
virMacAddrCmpRaw;
diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
new file mode 100644
index 0000000..727036b
--- /dev/null
+++ b/src/util/virlockspace.c
@@ -0,0 +1,419 @@
+/*
+ * virlockspace.c: simple file based lockspaces
+ *
+ * Copyright (C) 2012 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 "virlockspace.h"
+#include "logging.h"
+#include "memory.h"
+#include "virterror_internal.h"
+#include "util.h"
+#include "virfile.h"
+#include "virhash.h"
+#include "threads.h"
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+#define VIR_FROM_THIS VIR_FROM_LOCKSPACE
+
+typedef struct _virLockSpaceResource virLockSpaceResource;
+typedef virLockSpaceResource *virLockSpaceResourcePtr;
+
+struct _virLockSpaceResource {
+ char *name;
+ char *path;
+ int fd;
+ int flags;
+ unsigned int holdCount;
+};
+
+struct _virLockSpace {
+ char *dir;
+ virMutex lock;
+
+ virHashTablePtr resources;
+};
+
+
+static char *virLockSpaceGetResourcePath(virLockSpacePtr lockspace,
+ const char *resname)
+{
+ char *ret;
+ if (virAsprintf(&ret, "%s/%s", lockspace->dir, resname) < 0) {
+ virReportOOMError();
+ return NULL;
+ }
+ return ret;
+}
+
+
+static void virLockSpaceResourceFree(virLockSpaceResourcePtr res)
+{
+ if (!res)
+ return;
+
+ if (res->holdCount &&
+ (res->flags & VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE)) {
+ if (res->flags & VIR_LOCK_SPACE_ACQUIRE_SHARED) {
+ /* We must upgrade to an exclusive lock to ensure
+ * no one else still has it before trying to delete */
+ if (virFileLock(res->fd, false, 0, 1) < 0) {
+ VIR_DEBUG("Could not upgrade shared lease to exclusive, not
deleting");
+ } else {
+ unlink(res->path);
+ }
+ } else {
+ unlink(res->path);
+ }
+ }
+
+ VIR_FORCE_CLOSE(res->fd);
+ VIR_FREE(res->path);
+ VIR_FREE(res->name);
+ VIR_FREE(res);
+}
+
+
+static virLockSpaceResourcePtr
+virLockSpaceResourceNew(virLockSpacePtr lockspace,
+ const char *resname,
+ unsigned int flags)
+{
+ virLockSpaceResourcePtr res;
+ bool shared = !!(flags & VIR_LOCK_SPACE_ACQUIRE_SHARED);
+
+ if (VIR_ALLOC(res) < 0)
+ return NULL;
+
+ res->fd = -1;
+ res->flags = flags;
+
+ if (!(res->name = strdup(resname)))
+ goto no_memory;
+
+ if (!(res->path = virLockSpaceGetResourcePath(lockspace, resname)))
+ goto no_memory;
+
+ if (flags & VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) {
+ while (1) {
+ struct stat a, b;
+ if ((res->fd = open(res->path, O_RDWR|O_CREAT, 0600)) < 0) {
+ virReportSystemError(errno,
+ _("Unable to open/create resource %s"),
+ res->path);
+ goto error;
+ }
+
+ if (virSetCloseExec(res->fd) < 0) {
+ virReportSystemError(errno,
+ _("Failed to set close-on-exec flag
'%s'"),
+ res->path);
+ goto error;
+ }
+
+ if (fstat(res->fd, &b) < 0) {
+ virReportSystemError(errno,
+ _("Unable to check status of pid file
'%s'"),
+ res->path);
+ goto error;
+ }
+
+ if (virFileLock(res->fd, shared, 0, 1) < 0) {
+ if (errno == EACCES || errno == EAGAIN) {
+ virReportError(VIR_ERR_RESOURCE_BUSY,
+ _("Lockspace resource '%s' is
locked"),
+ resname);
+ } else {
+ virReportSystemError(errno,
+ _("Unable to acquire lock on
'%s'"),
+ res->path);
+ }
+ goto error;
+ }
+
+ /* Now make sure the pidfile we locked is the same
+ * one that now exists on the filesystem
+ */
+ if (stat(res->path, &a) < 0) {
+ char ebuf[1024] ATTRIBUTE_UNUSED;
+ VIR_DEBUG("Resource '%s' disappeared: %s",
+ res->path, virStrerror(errno, ebuf, sizeof(ebuf)));
+ VIR_FORCE_CLOSE(res->fd);
+ /* Someone else must be racing with us, so try agin */
+ continue;
+ }
+
+ if (a.st_ino == b.st_ino)
+ break;
+
+ VIR_DEBUG("Resource '%s' was recreated", res->path);
+ VIR_FORCE_CLOSE(res->fd);
+ /* Someone else must be racing with us, so try agin */
+ }
+ } else {
+ if ((res->fd = open(res->path, O_RDWR)) < 0) {
+ virReportSystemError(errno,
+ _("Unable to open resource %s"),
+ res->path);
+ goto error;
+ }
+
+ if (virSetCloseExec(res->fd) < 0) {
+ virReportSystemError(errno,
+ _("Failed to set close-on-exec flag
'%s'"),
+ res->path);
+ goto error;
+ }
+
+ if (virFileLock(res->fd, !!(flags & VIR_LOCK_SPACE_ACQUIRE_SHARED), 0, 0)
< 0) {
+ if (errno == EACCES || errno == EAGAIN) {
+ virReportError(VIR_ERR_RESOURCE_BUSY,
+ _("Lockspace resource '%s' is locked"),
+ resname);
+ } else {
+ virReportSystemError(errno,
+ _("Unable to acquire lock on
'%s'"),
+ res->path);
+ }
+ goto error;
+ }
+ }
+ res->holdCount = 1;
+
+ return res;
+
+no_memory:
+ virReportOOMError();
+error:
+ virLockSpaceResourceFree(res);
+ return NULL;
+}
+
+
+static void virLockSpaceResourceDataFree(void *opaque, const void *name
ATTRIBUTE_UNUSED)
+{
+ virLockSpaceResourcePtr res = opaque;
+ virLockSpaceResourceFree(res);
+}
+
+
+virLockSpacePtr virLockSpaceNew(const char *directory)
+{
+ virLockSpacePtr lockspace;
+
+ if (VIR_ALLOC(lockspace) < 0)
+ return NULL;
+
+ if (virMutexInit(&lockspace->lock) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("Unable to initialize lockspace mutex"));
+ VIR_FREE(lockspace);
+ return NULL;
+ }
+
+ if (!(lockspace->dir = strdup(directory)))
+ goto no_memory;
+
+ if (!(lockspace->resources = virHashCreate(10,
+ virLockSpaceResourceDataFree)))
+ goto error;
+
+ if (virFileExists(directory)) {
+ if (!virFileIsDir(directory)) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Lockspace location %s exists, but is not a
directory"),
+ directory);
+ goto error;
+ }
+ } else {
+ if (virFileMakePathWithMode(directory, 0700) < 0) {
+ virReportSystemError(errno,
+ _("Unable to create lockspace %s"),
+ directory);
+ goto error;
+ }
+ }
+
+ return lockspace;
+
+no_memory:
+ virReportOOMError();
+error:
+ virLockSpaceFree(lockspace);
+ return NULL;
+}
+
+
+void virLockSpaceFree(virLockSpacePtr lockspace)
+{
+ if (!lockspace)
+ return;
+
+ virHashFree(lockspace->resources);
+ VIR_FREE(lockspace->dir);
+ virMutexDestroy(&lockspace->lock);
+ VIR_FREE(lockspace);
+}
+
+
+int virLockSpaceCreateResource(virLockSpacePtr lockspace,
+ const char *resname)
+{
+ int ret = -1;
+ char *respath = NULL;
+ virLockSpaceResourcePtr res;
+
+ VIR_DEBUG("lockspace=%p resname=%s", lockspace, resname);
+
+ virMutexLock(&lockspace->lock);
+
+ if ((res = virHashLookup(lockspace->resources, resname))) {
+ virReportError(VIR_ERR_RESOURCE_BUSY,
+ _("Lockspace resource '%s' is locked"),
+ resname);
+ goto cleanup;
+ }
+
+ if (!(respath = virLockSpaceGetResourcePath(lockspace, resname)))
+ goto cleanup;
+
+ if (virFileTouch(respath, 0600) < 0)
+ goto cleanup;
+
+ ret = 0;
+
+cleanup:
+ virMutexUnlock(&lockspace->lock);
+ VIR_FREE(respath);
+ return ret;
+}
+
+
+int virLockSpaceDeleteResource(virLockSpacePtr lockspace,
+ const char *resname)
+{
+ int ret = -1;
+ char *respath = NULL;
+ virLockSpaceResourcePtr res;
+
+ VIR_DEBUG("lockspace=%p resname=%s", lockspace, resname);
+
+ virMutexLock(&lockspace->lock);
+
+ if ((res = virHashLookup(lockspace->resources, resname))) {
+ virReportError(VIR_ERR_RESOURCE_BUSY,
+ _("Lockspace resource '%s' is locked"),
+ resname);
+ goto cleanup;
+ }
+
+ if (!(respath = virLockSpaceGetResourcePath(lockspace, resname)))
+ goto cleanup;
+
+ if (unlink(respath) < 0 &&
+ errno != ENOENT) {
+ virReportSystemError(errno,
+ _("Unable to delete lockspace resource %s"),
+ respath);
+ goto cleanup;
+ }
+
+ ret = 0;
+
+cleanup:
+ virMutexUnlock(&lockspace->lock);
+ VIR_FREE(respath);
+ return ret;
+}
+
+
+int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
+ const char *resname,
+ unsigned int flags)
+{
+ int ret = -1;
+ virLockSpaceResourcePtr res;
+
+ VIR_DEBUG("lockspace=%p resname=%s flags=%x", lockspace, resname, flags);
+
+ virMutexLock(&lockspace->lock);
+
+ if ((res = virHashLookup(lockspace->resources, resname))) {
+ if ((res->flags & VIR_LOCK_SPACE_ACQUIRE_SHARED) &&
+ (flags & VIR_LOCK_SPACE_ACQUIRE_SHARED)) {
+ res->holdCount++;
+ goto done;
+ }
+ virReportError(VIR_ERR_RESOURCE_BUSY,
+ _("Lockspace resource '%s' is locked"),
+ resname);
+ goto cleanup;
+ }
+
+ if (!(res = virLockSpaceResourceNew(lockspace, resname, flags)))
+ goto cleanup;
+
+ if (virHashAddEntry(lockspace->resources, resname, res) < 0) {
+ virLockSpaceResourceFree(res);
+ goto cleanup;
+ }
+
+done:
+ ret = 0;
+
+cleanup:
+ virMutexUnlock(&lockspace->lock);
+ return ret;
+}
+
+
+int virLockSpaceReleaseResource(virLockSpacePtr lockspace,
+ const char *resname)
+{
+ int ret = -1;
+ virLockSpaceResourcePtr res;
+
+ VIR_DEBUG("lockspace=%p resname=%s", lockspace, resname);
+
+ virMutexLock(&lockspace->lock);
+
+ if (!(res = virHashLookup(lockspace->resources, resname))) {
+ virReportError(VIR_ERR_RESOURCE_BUSY,
+ _("Lockspace resource '%s' is not locked"),
+ resname);
+ goto cleanup;
+ }
+
+ if (res->holdCount == 1) {
+ if (virHashRemoveEntry(lockspace->resources, resname) < 0)
+ goto cleanup;
+ } else {
+ res->holdCount--;
+ }
+
+ ret = 0;
+
+cleanup:
+ virMutexUnlock(&lockspace->lock);
+ return ret;
+}
diff --git a/src/util/virlockspace.h b/src/util/virlockspace.h
new file mode 100644
index 0000000..6d91728
--- /dev/null
+++ b/src/util/virlockspace.h
@@ -0,0 +1,51 @@
+/*
+ * virlockspace.h: simple file based lockspaces
+ *
+ * Copyright (C) 2012 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/>.
+ *
+ */
+
+#ifndef __VIR_LOCK_SPACE_H__
+# define __VIR_LOCK_SPACE_H__
+
+#include "internal.h"
+
+typedef struct _virLockSpace virLockSpace;
+typedef virLockSpace *virLockSpacePtr;
+
+virLockSpacePtr virLockSpaceNew(const char *directory);
+
+void virLockSpaceFree(virLockSpacePtr lockspace);
+
+int virLockSpaceCreateResource(virLockSpacePtr lockspace,
+ const char *resname);
+int virLockSpaceDeleteResource(virLockSpacePtr lockspace,
+ const char *resname);
+
+typedef enum {
+ VIR_LOCK_SPACE_ACQUIRE_SHARED = (1 << 0),
+ VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE = (1 << 1),
+} virLockSpaceAcquireFlags;
+
+int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
+ const char *resname,
+ unsigned int flags);
+
+int virLockSpaceReleaseResource(virLockSpacePtr lockspace,
+ const char *resname);
+
+#endif /* __VIR_LOCK_SPACE_H__ */
diff --git a/src/util/virterror.c b/src/util/virterror.c
index a40cfe0..74f511a 100644
--- a/src/util/virterror.c
+++ b/src/util/virterror.c
@@ -112,7 +112,8 @@ VIR_ENUM_IMPL(virErrorDomain, VIR_ERR_DOMAIN_LAST,
"URI Utils", /* 45 */
"Authentication Utils",
"DBus Utils",
- "Parallels Cloud Server"
+ "Parallels Cloud Server",
+ "Lock Space",
)
@@ -1185,6 +1186,12 @@ virErrorMsg(virErrorNumber error, const char *info)
else
errmsg = _("block copy still active: %s");
break;
+ case VIR_ERR_RESOURCE_BUSY:
+ if (info == NULL)
+ errmsg = _("resource busy");
+ else
+ errmsg = _("resource busy %s");
+ break;
}
return errmsg;
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b931cea..8ca6441 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -91,7 +91,7 @@ test_programs = virshtest sockettest \
virhashtest virnetmessagetest virnetsockettest \
utiltest virnettlscontexttest shunloadtest \
virtimetest viruritest virkeyfiletest \
- virauthconfigtest
+ virauthconfigtest virlockspacetest
if WITH_DRIVER_MODULES
test_programs += virdrivermoduletest
@@ -503,6 +503,11 @@ virtimetest_SOURCES = \
virtimetest_CFLAGS = -Dabs_builddir="\"$(abs_builddir)\""
$(AM_CFLAGS)
virtimetest_LDADD = $(LDADDS)
+virlockspacetest_SOURCES = \
+ virlockspacetest.c testutils.h testutils.c
+virlockspacetest_CFLAGS = -Dabs_builddir="\"$(abs_builddir)\""
$(AM_CFLAGS)
+virlockspacetest_LDADD = $(LDADDS)
+
viruritest_SOURCES = \
viruritest.c testutils.h testutils.c
viruritest_CFLAGS = -Dabs_builddir="\"$(abs_builddir)\""
$(AM_CFLAGS)
diff --git a/tests/virlockspacetest.c b/tests/virlockspacetest.c
new file mode 100644
index 0000000..37175cb
--- /dev/null
+++ b/tests/virlockspacetest.c
@@ -0,0 +1,311 @@
+/*
+ * Copyright (C) 2011 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/>.
+ *
+ * Author: Daniel P. Berrange <berrange(a)redhat.com>
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <signal.h>
+
+#include "testutils.h"
+#include "util.h"
+#include "virterror_internal.h"
+#include "memory.h"
+#include "logging.h"
+
+#include "virlockspace.h"
+
+#define VIR_FROM_THIS VIR_FROM_RPC
+
+#define LOCKSPACE_DIR abs_builddir "/virlockspacedata"
+
+static int testLockSpaceCreate(const void *args ATTRIBUTE_UNUSED)
+{
+ virLockSpacePtr lockspace;
+ int ret = -1;
+
+ rmdir(LOCKSPACE_DIR);
+
+ lockspace = virLockSpaceNew(LOCKSPACE_DIR);
+
+ if (!virFileIsDir(LOCKSPACE_DIR))
+ goto cleanup;
+
+ ret = 0;
+
+cleanup:
+ virLockSpaceFree(lockspace);
+ rmdir(LOCKSPACE_DIR);
+ return ret;
+}
+
+
+static int testLockSpaceResourceLifecycle(const void *args ATTRIBUTE_UNUSED)
+{
+ virLockSpacePtr lockspace;
+ int ret = -1;
+
+ unlink(LOCKSPACE_DIR);
+
+ lockspace = virLockSpaceNew(LOCKSPACE_DIR);
+
+ if (!virFileIsDir(LOCKSPACE_DIR))
+ goto cleanup;
+
+ if (virLockSpaceCreateResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (!virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ if (virLockSpaceDeleteResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ ret = 0;
+
+cleanup:
+ virLockSpaceFree(lockspace);
+ rmdir(LOCKSPACE_DIR);
+ return ret;
+}
+
+
+static int testLockSpaceResourceLockExcl(const void *args ATTRIBUTE_UNUSED)
+{
+ virLockSpacePtr lockspace;
+ int ret = -1;
+
+ unlink(LOCKSPACE_DIR);
+
+ lockspace = virLockSpaceNew(LOCKSPACE_DIR);
+
+ if (!virFileIsDir(LOCKSPACE_DIR))
+ goto cleanup;
+
+ if (virLockSpaceCreateResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo", 0) < 0)
+ goto cleanup;
+
+ if (!virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo", 0) == 0)
+ goto cleanup;
+
+ if (virLockSpaceDeleteResource(lockspace, "foo") == 0)
+ goto cleanup;
+
+ if (virLockSpaceReleaseResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virLockSpaceDeleteResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ ret = 0;
+
+cleanup:
+ virLockSpaceFree(lockspace);
+ rmdir(LOCKSPACE_DIR);
+ return ret;
+}
+
+
+static int testLockSpaceResourceLockExclAuto(const void *args ATTRIBUTE_UNUSED)
+{
+ virLockSpacePtr lockspace;
+ int ret = -1;
+
+ unlink(LOCKSPACE_DIR);
+
+ lockspace = virLockSpaceNew(LOCKSPACE_DIR);
+
+ if (!virFileIsDir(LOCKSPACE_DIR))
+ goto cleanup;
+
+ if (virLockSpaceCreateResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo",
VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
+ goto cleanup;
+
+ if (!virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ if (virLockSpaceReleaseResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ ret = 0;
+
+cleanup:
+ virLockSpaceFree(lockspace);
+ rmdir(LOCKSPACE_DIR);
+ return ret;
+}
+
+
+static int testLockSpaceResourceLockShr(const void *args ATTRIBUTE_UNUSED)
+{
+ virLockSpacePtr lockspace;
+ int ret = -1;
+
+ unlink(LOCKSPACE_DIR);
+
+ lockspace = virLockSpaceNew(LOCKSPACE_DIR);
+
+ if (!virFileIsDir(LOCKSPACE_DIR))
+ goto cleanup;
+
+ if (virLockSpaceCreateResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo",
VIR_LOCK_SPACE_ACQUIRE_SHARED) < 0)
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo", 0) == 0)
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo",
VIR_LOCK_SPACE_ACQUIRE_SHARED) < 0)
+ goto cleanup;
+
+ if (virLockSpaceDeleteResource(lockspace, "foo") == 0)
+ goto cleanup;
+
+ if (virLockSpaceReleaseResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virLockSpaceDeleteResource(lockspace, "foo") == 0)
+ goto cleanup;
+
+ if (virLockSpaceReleaseResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virLockSpaceDeleteResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ ret = 0;
+
+cleanup:
+ virLockSpaceFree(lockspace);
+ rmdir(LOCKSPACE_DIR);
+ return ret;
+}
+
+
+static int testLockSpaceResourceLockShrAuto(const void *args ATTRIBUTE_UNUSED)
+{
+ virLockSpacePtr lockspace;
+ int ret = -1;
+
+ unlink(LOCKSPACE_DIR);
+
+ lockspace = virLockSpaceNew(LOCKSPACE_DIR);
+
+ if (!virFileIsDir(LOCKSPACE_DIR))
+ goto cleanup;
+
+ if (virLockSpaceCreateResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo",
+ VIR_LOCK_SPACE_ACQUIRE_SHARED |
+ VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
+ goto cleanup;
+
+ if (!virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo",
+ VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) == 0)
+ goto cleanup;
+
+ if (!virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ if (virLockSpaceAcquireResource(lockspace, "foo",
+ VIR_LOCK_SPACE_ACQUIRE_SHARED |
+ VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
+ goto cleanup;
+
+ if (!virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ if (virLockSpaceReleaseResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (!virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ if (virLockSpaceReleaseResource(lockspace, "foo") < 0)
+ goto cleanup;
+
+ if (virFileExists(LOCKSPACE_DIR "/foo"))
+ goto cleanup;
+
+ ret = 0;
+
+cleanup:
+ virLockSpaceFree(lockspace);
+ rmdir(LOCKSPACE_DIR);
+ return ret;
+}
+
+
+static int
+mymain(void)
+{
+ int ret = 0;
+
+ signal(SIGPIPE, SIG_IGN);
+
+ if (virtTestRun("Lockspace creation", 1, testLockSpaceCreate, NULL) <
0)
+ ret = -1;
+
+ if (virtTestRun("Lockspace res lifecycle", 1,
testLockSpaceResourceLifecycle, NULL) < 0)
+ ret = -1;
+
+ if (virtTestRun("Lockspace res lock excl", 1,
testLockSpaceResourceLockExcl, NULL) < 0)
+ ret = -1;
+
+ if (virtTestRun("Lockspace res lock shr", 1, testLockSpaceResourceLockShr,
NULL) < 0)
+ ret = -1;
+
+ if (virtTestRun("Lockspace res lock excl auto", 1,
testLockSpaceResourceLockExclAuto, NULL) < 0)
+ ret = -1;
+
+ if (virtTestRun("Lockspace res lock shr auto", 1,
testLockSpaceResourceLockShrAuto, NULL) < 0)
+ ret = -1;
+
+ return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+VIRT_TEST_MAIN(mymain)
--
1.7.11.2