From cde8e290c9195cbc7a2388455df9e76a1f36135f Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Thu, 13 Feb 2014 16:48:35 +0100 Subject: [PATCH] tdb: simplify tdb_delete_hash() a bit Make the lock/unlock bracket more obvious by extracting locking (and finding) from the special cases to the top of the function. This also lets us take lock and find the record outside the special case branches (use dead records or not). There is a small semantic change implied: In the dead records case, the record to delete is looked up before the current dead records are potentially purged. Hence, if the record to delete is not found, the dead records are also not purge. This does not make a big difference though, because purging is only delayed until directly befor the next record to delete is in fact found. Signed-off-by: Michael Adam Reviewed-by: Jeremy Allison --- lib/tdb/common/tdb.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/tdb/common/tdb.c b/lib/tdb/common/tdb.c index 6256a05d04e..f24493ccd84 100644 --- a/lib/tdb/common/tdb.c +++ b/lib/tdb/common/tdb.c @@ -387,6 +387,11 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash) struct tdb_record rec; int ret; + rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK, &rec); + if (rec_ptr == 0) { + return -1; + } + if (tdb->max_dead_records != 0) { /* @@ -394,9 +399,6 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash) * tdb's with a very high create/delete rate like locking.tdb. */ - if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1) - return -1; - if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) { /* * Don't let the per-chain freelist grow too large, @@ -405,11 +407,6 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash) tdb_purge_dead(tdb, hash); } - if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) { - tdb_unlock(tdb, BUCKET(hash), F_WRLCK); - return -1; - } - /* * Just mark the record as dead. */ @@ -417,10 +414,6 @@ static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash) ret = tdb_rec_write(tdb, rec_ptr, &rec); } else { - if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK, - &rec))) - return -1; - ret = tdb_do_delete(tdb, rec_ptr, &rec); } -- 2.34.1