+/* This is the core routine which searches the hashtable for an entry.
+ * On error, no locks are held and TDB_OFF_ERR is returned.
+ * Otherwise, *num_locks locks of type ltype from *start_lock are held.
+ * The bucket where the entry is (or would be) is in *bucket.
+ * If not found, the return value is 0.
+ * If found, the return value is the offset, and *rec is the record. */
+static tdb_off_t find_and_lock(struct tdb_context *tdb,
+ struct tdb_data key,
+ uint64_t h,
+ int ltype,
+ tdb_off_t *start_lock,
+ tdb_len_t *num_locks,
+ tdb_off_t *bucket,
+ struct tdb_used_record *rec)
+{
+ tdb_off_t off;
+
+ /* FIXME: can we avoid locks for some fast paths? */
+ *start_lock = tdb_lock_list(tdb, h, ltype, TDB_LOCK_WAIT);
+ if (*start_lock == TDB_OFF_ERR)
+ return TDB_OFF_ERR;
+
+ /* Fast path. */
+ off = entry_matches(tdb, *start_lock, h, &key, rec);
+ if (likely(off != TDB_OFF_ERR)) {
+ *bucket = *start_lock;
+ *num_locks = 1;
+ return off;
+ }
+
+ /* Slow path, need to grab more locks and search. */
+
+ /* Warning: this may drop the lock on *bucket! */
+ *num_locks = relock_hash_to_zero(tdb, *start_lock, ltype);
+ if (*num_locks == TDB_OFF_ERR)
+ return TDB_OFF_ERR;
+
+ for (*bucket = *start_lock;
+ *bucket < *start_lock + *num_locks;
+ (*bucket)++) {
+ off = entry_matches(tdb, *bucket, h, &key, rec);
+ /* Empty entry or we found it? */
+ if (off == 0 || off != TDB_OFF_ERR)
+ return off;
+ }
+
+ /* We didn't find a zero entry? Something went badly wrong... */
+ unlock_lists(tdb, *start_lock, *start_lock + *num_locks, ltype);
+ tdb->ecode = TDB_ERR_CORRUPT;
+ tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
+ "find_and_lock: expected to find an empty hash bucket!\n");
+ return TDB_OFF_ERR;
+}
+