[libvirt] [PATCH v2 2/7] virlockspace: Introduce VIR_LOCK_SPACE_ACQUIRE_WAIT

Michal Privoznik posted 7 patches 6 years, 3 months ago
There is a newer version of this series
[libvirt] [PATCH v2 2/7] virlockspace: Introduce VIR_LOCK_SPACE_ACQUIRE_WAIT
Posted by Michal Privoznik 6 years, 3 months ago
This flag modifies the way the lock is acquired. It waits for the
lock to be set instead of usual set-or-fail logic that happens
without this flag.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/util/virlockspace.c | 14 ++++++++++----
 src/util/virlockspace.h |  1 +
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
index 60bfef4c5f..d75a6d4a6e 100644
--- a/src/util/virlockspace.c
+++ b/src/util/virlockspace.c
@@ -122,6 +122,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
 {
     virLockSpaceResourcePtr res;
     bool shared = !!(flags & VIR_LOCK_SPACE_ACQUIRE_SHARED);
+    bool waitForLock = !!(flags & VIR_LOCK_SPACE_ACQUIRE_WAIT);
 
     if (VIR_ALLOC(res) < 0)
         return NULL;
@@ -159,7 +160,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
                 goto error;
             }
 
-            if (virFileLock(res->fd, shared, start, len, false) < 0) {
+            if (virFileLock(res->fd, shared, start, len, waitForLock) < 0) {
                 if (errno == EACCES || errno == EAGAIN) {
                     virReportError(VIR_ERR_RESOURCE_BUSY,
                                    _("Lockspace resource '%s' is locked"),
@@ -206,7 +207,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
             goto error;
         }
 
-        if (virFileLock(res->fd, shared, start, len, false) < 0) {
+        if (virFileLock(res->fd, shared, start, len, waitForLock) < 0) {
             if (errno == EACCES || errno == EAGAIN) {
                 virReportError(VIR_ERR_RESOURCE_BUSY,
                                _("Lockspace resource '%s' is locked"),
@@ -625,11 +626,16 @@ int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
               lockspace, resname, flags, (unsigned long long)owner);
 
     virCheckFlags(VIR_LOCK_SPACE_ACQUIRE_SHARED |
-                  VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE, -1);
+                  VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE |
+                  VIR_LOCK_SPACE_ACQUIRE_WAIT, -1);
+
+    VIR_EXCLUSIVE_FLAGS_RET(VIR_LOCK_SPACE_ACQUIRE_WAIT,
+                            VIR_LOCK_SPACE_ACQUIRE_SHARED, -1);
 
     virMutexLock(&lockspace->lock);
 
-    if ((res = virHashLookup(lockspace->resources, resname))) {
+    if (!(flags & VIR_LOCK_SPACE_ACQUIRE_WAIT) &&
+        (res = virHashLookup(lockspace->resources, resname))) {
         if ((res->flags & VIR_LOCK_SPACE_ACQUIRE_SHARED) &&
             (flags & VIR_LOCK_SPACE_ACQUIRE_SHARED)) {
 
diff --git a/src/util/virlockspace.h b/src/util/virlockspace.h
index 24f2c89be6..bfaed34d5b 100644
--- a/src/util/virlockspace.h
+++ b/src/util/virlockspace.h
@@ -47,6 +47,7 @@ int virLockSpaceDeleteResource(virLockSpacePtr lockspace,
 typedef enum {
     VIR_LOCK_SPACE_ACQUIRE_SHARED     = (1 << 0),
     VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE = (1 << 1),
+    VIR_LOCK_SPACE_ACQUIRE_WAIT       = (1 << 2),
 } virLockSpaceAcquireFlags;
 
 int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
-- 
2.16.4

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 2/7] virlockspace: Introduce VIR_LOCK_SPACE_ACQUIRE_WAIT
Posted by John Ferlan 6 years, 3 months ago

On 08/14/2018 07:19 AM, Michal Privoznik wrote:
> This flag modifies the way the lock is acquired. It waits for the
> lock to be set instead of usual set-or-fail logic that happens
> without this flag.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/util/virlockspace.c | 14 ++++++++++----
>  src/util/virlockspace.h |  1 +
>  2 files changed, 11 insertions(+), 4 deletions(-)
> 

Reviewed-by: John Ferlan <jferlan@redhat.com>

John

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 2/7] virlockspace: Introduce VIR_LOCK_SPACE_ACQUIRE_WAIT
Posted by Daniel P. Berrangé 6 years, 3 months ago
On Tue, Aug 14, 2018 at 01:19:38PM +0200, Michal Privoznik wrote:
> This flag modifies the way the lock is acquired. It waits for the
> lock to be set instead of usual set-or-fail logic that happens
> without this flag.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/util/virlockspace.c | 14 ++++++++++----
>  src/util/virlockspace.h |  1 +
>  2 files changed, 11 insertions(+), 4 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list