virlockspace: Allow caller to specify start and length offset in virLockSpaceAcquireResource

So far the virLockSpaceAcquireResource() locks the first byte in
the underlying file. But caller might want to lock other range.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Michal Privoznik 2018-08-14 12:25:36 +02:00
parent b916a25e8a
commit afd5a27575
4 changed files with 41 additions and 10 deletions

View File

@ -50,6 +50,8 @@ virLockSpaceProtocolDispatchAcquireResource(virNetServerPtr server ATTRIBUTE_UNU
virNetServerClientGetPrivateData(client);
virLockSpacePtr lockspace;
unsigned int newFlags;
off_t start = 0;
off_t len = 1;
virMutexLock(&priv->lock);
@ -84,6 +86,7 @@ virLockSpaceProtocolDispatchAcquireResource(virNetServerPtr server ATTRIBUTE_UNU
if (virLockSpaceAcquireResource(lockspace,
args->name,
priv->ownerPid,
start, len,
newFlags) < 0)
goto cleanup;

View File

@ -115,8 +115,10 @@ static void virLockSpaceResourceFree(virLockSpaceResourcePtr res)
static virLockSpaceResourcePtr
virLockSpaceResourceNew(virLockSpacePtr lockspace,
const char *resname,
unsigned int flags,
pid_t owner)
pid_t owner,
off_t start,
off_t len,
unsigned int flags)
{
virLockSpaceResourcePtr res;
bool shared = !!(flags & VIR_LOCK_SPACE_ACQUIRE_SHARED);
@ -157,7 +159,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
goto error;
}
if (virFileLock(res->fd, shared, 0, 1, false) < 0) {
if (virFileLock(res->fd, shared, start, len, false) < 0) {
if (errno == EACCES || errno == EAGAIN) {
virReportError(VIR_ERR_RESOURCE_BUSY,
_("Lockspace resource '%s' is locked"),
@ -204,7 +206,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
goto error;
}
if (virFileLock(res->fd, shared, 0, 1, false) < 0) {
if (virFileLock(res->fd, shared, start, len, false) < 0) {
if (errno == EACCES || errno == EAGAIN) {
virReportError(VIR_ERR_RESOURCE_BUSY,
_("Lockspace resource '%s' is locked"),
@ -612,6 +614,8 @@ int virLockSpaceDeleteResource(virLockSpacePtr lockspace,
int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
const char *resname,
pid_t owner,
off_t start,
off_t len,
unsigned int flags)
{
int ret = -1;
@ -641,7 +645,8 @@ int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
goto cleanup;
}
if (!(res = virLockSpaceResourceNew(lockspace, resname, flags, owner)))
if (!(res = virLockSpaceResourceNew(lockspace, resname,
owner, start, len, flags)))
goto cleanup;
if (virHashAddEntry(lockspace->resources, resname, res) < 0) {

View File

@ -22,6 +22,8 @@
#ifndef __VIR_LOCK_SPACE_H__
# define __VIR_LOCK_SPACE_H__
# include <sys/types.h>
# include "internal.h"
# include "virjson.h"
@ -50,6 +52,8 @@ typedef enum {
int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
const char *resname,
pid_t owner,
off_t start,
off_t len,
unsigned int flags);
int virLockSpaceReleaseResource(virLockSpacePtr lockspace,

View File

@ -99,6 +99,8 @@ static int testLockSpaceResourceLockExcl(const void *args ATTRIBUTE_UNUSED)
{
virLockSpacePtr lockspace;
int ret = -1;
const off_t start = 0;
const off_t len = 1;
rmdir(LOCKSPACE_DIR);
@ -111,13 +113,13 @@ static int testLockSpaceResourceLockExcl(const void *args ATTRIBUTE_UNUSED)
if (virLockSpaceCreateResource(lockspace, "foo") < 0)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(), 0) < 0)
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(), start, len, 0) < 0)
goto cleanup;
if (!virFileExists(LOCKSPACE_DIR "/foo"))
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(), 0) == 0)
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(), start, len, 0) == 0)
goto cleanup;
if (virLockSpaceDeleteResource(lockspace, "foo") == 0)
@ -145,6 +147,8 @@ static int testLockSpaceResourceLockExclAuto(const void *args ATTRIBUTE_UNUSED)
{
virLockSpacePtr lockspace;
int ret = -1;
const off_t start = 0;
const off_t len = 1;
rmdir(LOCKSPACE_DIR);
@ -158,6 +162,7 @@ static int testLockSpaceResourceLockExclAuto(const void *args ATTRIBUTE_UNUSED)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
start, len,
VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
goto cleanup;
@ -183,6 +188,8 @@ static int testLockSpaceResourceLockShr(const void *args ATTRIBUTE_UNUSED)
{
virLockSpacePtr lockspace;
int ret = -1;
const off_t start = 0;
const off_t len = 1;
rmdir(LOCKSPACE_DIR);
@ -196,13 +203,16 @@ static int testLockSpaceResourceLockShr(const void *args ATTRIBUTE_UNUSED)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
start, len,
VIR_LOCK_SPACE_ACQUIRE_SHARED) < 0)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(), 0) == 0)
if (virLockSpaceAcquireResource(lockspace, "foo",
geteuid(), start, len, 0) == 0)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
start, len,
VIR_LOCK_SPACE_ACQUIRE_SHARED) < 0)
goto cleanup;
@ -237,6 +247,8 @@ static int testLockSpaceResourceLockShrAuto(const void *args ATTRIBUTE_UNUSED)
{
virLockSpacePtr lockspace;
int ret = -1;
const off_t start = 0;
const off_t len = 1;
rmdir(LOCKSPACE_DIR);
@ -250,6 +262,7 @@ static int testLockSpaceResourceLockShrAuto(const void *args ATTRIBUTE_UNUSED)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
start, len,
VIR_LOCK_SPACE_ACQUIRE_SHARED |
VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
goto cleanup;
@ -258,6 +271,7 @@ static int testLockSpaceResourceLockShrAuto(const void *args ATTRIBUTE_UNUSED)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
start, len,
VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) == 0)
goto cleanup;
@ -265,6 +279,7 @@ static int testLockSpaceResourceLockShrAuto(const void *args ATTRIBUTE_UNUSED)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, "foo", geteuid(),
start, len,
VIR_LOCK_SPACE_ACQUIRE_SHARED |
VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE) < 0)
goto cleanup;
@ -297,6 +312,8 @@ static int testLockSpaceResourceLockPath(const void *args ATTRIBUTE_UNUSED)
{
virLockSpacePtr lockspace;
int ret = -1;
const off_t start = 0;
const off_t len = 1;
rmdir(LOCKSPACE_DIR);
@ -309,13 +326,15 @@ static int testLockSpaceResourceLockPath(const void *args ATTRIBUTE_UNUSED)
if (virLockSpaceCreateResource(lockspace, LOCKSPACE_DIR "/foo") < 0)
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, LOCKSPACE_DIR "/foo", geteuid(), 0) < 0)
if (virLockSpaceAcquireResource(lockspace, LOCKSPACE_DIR "/foo",
geteuid(), start, len, 0) < 0)
goto cleanup;
if (!virFileExists(LOCKSPACE_DIR "/foo"))
goto cleanup;
if (virLockSpaceAcquireResource(lockspace, LOCKSPACE_DIR "/foo", geteuid(), 0) == 0)
if (virLockSpaceAcquireResource(lockspace, LOCKSPACE_DIR "/foo",
geteuid(), start, len, 0) == 0)
goto cleanup;
if (virLockSpaceDeleteResource(lockspace, LOCKSPACE_DIR "/foo") == 0)