From: Stefan Metzmacher Date: Sun, 28 Aug 2022 08:30:38 +0000 (+0200) Subject: s3:g_lock: reorder the logic in g_lock_lock_simple_fn() X-Git-Tag: talloc-2.4.0~855 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3a5174136d6706540ace5567d4e8193534dbd13a;p=thirdparty%2Fsamba.git s3:g_lock: reorder the logic in g_lock_lock_simple_fn() 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 Reviewed-by: Jeremy Allison --- diff --git a/source3/lib/g_lock.c b/source3/lib/g_lock.c index aab6dfa18d4..192e9305029 100644 --- a/source3/lib/g_lock.c +++ b/source3/lib/g_lock.c @@ -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; }