[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v3 09/28] 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>
---
 src/locking/lock_daemon_dispatch.c |  3 +++
 src/util/virlockspace.c            | 15 ++++++++++-----
 src/util/virlockspace.h            |  4 ++++
 tests/virlockspacetest.c           | 29 ++++++++++++++++++++++++-----
 4 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/src/locking/lock_daemon_dispatch.c b/src/locking/lock_daemon_dispatch.c
index 1b479db55d..10248ec0b5 100644
--- a/src/locking/lock_daemon_dispatch.c
+++ b/src/locking/lock_daemon_dispatch.c
@@ -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;
 
diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
index 3364c843aa..60bfef4c5f 100644
--- a/src/util/virlockspace.c
+++ b/src/util/virlockspace.c
@@ -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) {
diff --git a/src/util/virlockspace.h b/src/util/virlockspace.h
index 041cf20396..24f2c89be6 100644
--- a/src/util/virlockspace.h
+++ b/src/util/virlockspace.h
@@ -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,
diff --git a/tests/virlockspacetest.c b/tests/virlockspacetest.c
index 75ad98a02c..2409809353 100644
--- a/tests/virlockspacetest.c
+++ b/tests/virlockspacetest.c
@@ -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)
-- 
2.16.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]