]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3:g_lock: reorder the logic in g_lock_lock_simple_fn()
authorStefan Metzmacher <metze@samba.org>
Sun, 28 Aug 2022 08:30:38 +0000 (10:30 +0200)
committerJeremy Allison <jra@samba.org>
Tue, 20 Sep 2022 00:34:35 +0000 (00:34 +0000)
First we fully check if we'll get the lock
and then store the lock.

This is not change in behavior, but it will simplify further changes.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/lib/g_lock.c

index aab6dfa18d42ebdad6b643e3abeddc10cee0e9c0..192e9305029551d06ae9294803fbf3fe72bf8f4a 100644 (file)
@@ -836,17 +836,9 @@ static void g_lock_lock_simple_fn(
        struct g_lock_lock_simple_state *state = private_data;
        struct server_id_buf buf;
        struct g_lock lck = { .exclusive.pid = 0 };
+       struct server_id *new_shared = NULL;
        bool ok;
 
-       /*
-        * We're trying to get a lock and if we are
-        * successful in doing that, we should not
-        * wakeup any other waiters, all they would
-        * find is that we're holding a lock they
-        * are conflicting with.
-        */
-       dbwrap_watched_watch_skip_alerting(rec);
-
        ok = g_lock_parse(value.dptr, value.dsize, &lck);
        if (!ok) {
                DBG_DEBUG("g_lock_parse failed\n");
@@ -860,24 +852,33 @@ static void g_lock_lock_simple_fn(
                goto not_granted;
        }
 
-       lck.unique_lock_epoch = generate_unique_u64(lck.unique_lock_epoch);
-
        if (state->type == G_LOCK_WRITE) {
                if (lck.num_shared != 0) {
                        DBG_DEBUG("num_shared=%zu\n", lck.num_shared);
                        goto not_granted;
                }
                lck.exclusive = state->me;
-               state->status = g_lock_store(rec, &lck, NULL, NULL, 0);
-               return;
-       }
-
-       if (state->type == G_LOCK_READ) {
+       } else if (state->type == G_LOCK_READ) {
                g_lock_cleanup_shared(&lck);
-               state->status = g_lock_store(rec, &lck, &state->me, NULL, 0);
-               return;
+               new_shared = &state->me;
+       } else {
+               smb_panic(__location__);
        }
 
+       lck.unique_lock_epoch = generate_unique_u64(lck.unique_lock_epoch);
+
+       /*
+        * We're storing a lock and if we are
+        * successful in doing that, we should not
+        * wakeup any other waiters, all they would
+        * find is that we're holding a lock they
+        * are conflicting with.
+        */
+       dbwrap_watched_watch_skip_alerting(rec);
+
+       state->status = g_lock_store(rec, &lck, new_shared, NULL, 0);
+       return;
+
 not_granted:
        state->status = NT_STATUS_LOCK_NOT_GRANTED;
 }