2 Trivial Database 2: hash handling
3 Copyright (C) Rusty Russell 2010
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include <ccan/hash/hash.h>
22 static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
26 /* hash64_stable assumes lower bits are more important; they are a
27 * slightly better hash. We use the upper bits first, so swap them. */
28 ret = hash64_stable((const unsigned char *)key, length, seed);
29 return (ret >> 32) | (ret << 32);
32 void tdb_hash_init(struct tdb_context *tdb)
34 tdb->khash = jenkins_hash;
35 tdb->hash_priv = NULL;
38 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len)
40 return tdb->khash(ptr, len, tdb->hash_seed, tdb->hash_priv);
43 uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off)
45 const struct tdb_used_record *r;
49 r = tdb_access_read(tdb, off, sizeof(*r), true);
50 if (TDB_PTR_IS_ERR(r)) {
51 tdb->ecode = TDB_PTR_ERR(r);
56 klen = rec_key_length(r);
57 tdb_access_release(tdb, r);
59 key = tdb_access_read(tdb, off + sizeof(*r), klen, false);
60 if (TDB_PTR_IS_ERR(key)) {
61 tdb->ecode = TDB_PTR_ERR(key);
65 hash = tdb_hash(tdb, key, klen);
66 tdb_access_release(tdb, key);
70 /* Get bits from a value. */
71 static uint32_t bits_from(uint64_t val, unsigned start, unsigned num)
74 return (val >> start) & ((1U << num) - 1);
77 /* We take bits from the top: that way we can lock whole sections of the hash
78 * by using lock ranges. */
79 static uint32_t use_bits(struct hash_info *h, unsigned num)
82 return bits_from(h->h, 64 - h->hash_used, num);
85 static bool key_matches(struct tdb_context *tdb,
86 const struct tdb_used_record *rec,
88 const struct tdb_data *key)
93 if (rec_key_length(rec) != key->dsize) {
94 add_stat(tdb, compare_wrong_keylen, 1);
98 rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false);
99 if (TDB_PTR_IS_ERR(rkey)) {
100 tdb->ecode = TDB_PTR_ERR(rkey);
103 if (memcmp(rkey, key->dptr, key->dsize) == 0)
106 add_stat(tdb, compare_wrong_keycmp, 1);
107 tdb_access_release(tdb, rkey);
111 /* Does entry match? */
112 static bool match(struct tdb_context *tdb,
114 const struct tdb_data *key,
116 struct tdb_used_record *rec)
119 enum TDB_ERROR ecode;
121 add_stat(tdb, compares, 1);
122 /* Desired bucket must match. */
123 if (h->home_bucket != (val & TDB_OFF_HASH_GROUP_MASK)) {
124 add_stat(tdb, compare_wrong_bucket, 1);
128 /* Top bits of offset == next bits of hash. */
129 if (bits_from(val, TDB_OFF_HASH_EXTRA_BIT, TDB_OFF_UPPER_STEAL_EXTRA)
130 != bits_from(h->h, 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
131 TDB_OFF_UPPER_STEAL_EXTRA)) {
132 add_stat(tdb, compare_wrong_offsetbits, 1);
136 off = val & TDB_OFF_MASK;
137 ecode = tdb_read_convert(tdb, off, rec, sizeof(*rec));
138 if (ecode != TDB_SUCCESS) {
143 if ((h->h & ((1 << 11)-1)) != rec_hash(rec)) {
144 add_stat(tdb, compare_wrong_rechash, 1);
148 return key_matches(tdb, rec, off, key);
151 static tdb_off_t hbucket_off(tdb_off_t group_start, unsigned bucket)
154 + (bucket % (1 << TDB_HASH_GROUP_BITS)) * sizeof(tdb_off_t);
157 bool is_subhash(tdb_off_t val)
159 return (val >> TDB_OFF_UPPER_STEAL_SUBHASH_BIT) & 1;
162 /* FIXME: Guess the depth, don't over-lock! */
163 static tdb_off_t hlock_range(tdb_off_t group, tdb_off_t *size)
165 *size = 1ULL << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
166 return group << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
169 static tdb_off_t COLD find_in_chain(struct tdb_context *tdb,
173 struct tdb_used_record *rec,
174 struct traverse_info *tinfo)
177 enum TDB_ERROR ecode;
179 /* In case nothing is free, we set these to zero. */
180 h->home_bucket = h->found_bucket = 0;
182 for (off = chain; off; off = next) {
185 h->group_start = off;
186 ecode = tdb_read_convert(tdb, off, h->group, sizeof(h->group));
187 if (ecode != TDB_SUCCESS) {
192 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
195 /* Remember this empty bucket. */
196 h->home_bucket = h->found_bucket = i;
200 /* We can insert extra bits via add_to_hash
201 * empty bucket logic. */
202 recoff = h->group[i] & TDB_OFF_MASK;
203 ecode = tdb_read_convert(tdb, recoff, rec,
205 if (ecode != TDB_SUCCESS) {
210 if (key_matches(tdb, rec, recoff, &key)) {
211 h->home_bucket = h->found_bucket = i;
214 tinfo->levels[tinfo->num_levels]
216 tinfo->levels[tinfo->num_levels]
218 = 1 << TDB_HASH_GROUP_BITS;
219 tinfo->levels[tinfo->num_levels].entry
226 next = tdb_read_off(tdb, off
227 + offsetof(struct tdb_chain, next));
228 if (TDB_OFF_IS_ERR(next)) {
233 next += sizeof(struct tdb_used_record);
238 /* This is the core routine which searches the hashtable for an entry.
239 * On error, no locks are held and TDB_OFF_ERR is returned.
240 * Otherwise, hinfo is filled in (and the optional tinfo).
241 * If not found, the return value is 0.
242 * If found, the return value is the offset, and *rec is the record. */
243 tdb_off_t find_and_lock(struct tdb_context *tdb,
247 struct tdb_used_record *rec,
248 struct traverse_info *tinfo)
252 enum TDB_ERROR ecode;
254 h->h = tdb_hash(tdb, key.dptr, key.dsize);
256 group = use_bits(h, TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
257 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
259 h->hlock_start = hlock_range(group, &h->hlock_range);
260 ecode = tdb_lock_hashes(tdb, h->hlock_start, h->hlock_range, ltype,
262 if (ecode != TDB_SUCCESS) {
267 hashtable = offsetof(struct tdb_header, hashtable);
269 tinfo->toplevel_group = group;
270 tinfo->num_levels = 1;
271 tinfo->levels[0].entry = 0;
272 tinfo->levels[0].hashtable = hashtable
273 + (group << TDB_HASH_GROUP_BITS) * sizeof(tdb_off_t);
274 tinfo->levels[0].total_buckets = 1 << TDB_HASH_GROUP_BITS;
277 while (h->hash_used <= 64) {
278 /* Read in the hash group. */
279 h->group_start = hashtable
280 + group * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
282 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
284 if (ecode != TDB_SUCCESS) {
289 /* Pointer to another hash table? Go down... */
290 if (is_subhash(h->group[h->home_bucket])) {
291 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
292 + sizeof(struct tdb_used_record);
294 /* When we come back, use *next* bucket */
295 tinfo->levels[tinfo->num_levels-1].entry
296 += h->home_bucket + 1;
298 group = use_bits(h, TDB_SUBLEVEL_HASH_BITS
299 - TDB_HASH_GROUP_BITS);
300 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
302 tinfo->levels[tinfo->num_levels].hashtable
304 tinfo->levels[tinfo->num_levels].total_buckets
305 = 1 << TDB_SUBLEVEL_HASH_BITS;
306 tinfo->levels[tinfo->num_levels].entry
307 = group << TDB_HASH_GROUP_BITS;
313 /* It's in this group: search (until 0 or all searched) */
314 for (i = 0, h->found_bucket = h->home_bucket;
315 i < (1 << TDB_HASH_GROUP_BITS);
316 i++, h->found_bucket = ((h->found_bucket+1)
317 % (1 << TDB_HASH_GROUP_BITS))) {
318 if (is_subhash(h->group[h->found_bucket]))
321 if (!h->group[h->found_bucket])
324 if (match(tdb, h, &key, h->group[h->found_bucket],
327 tinfo->levels[tinfo->num_levels-1].entry
330 return h->group[h->found_bucket] & TDB_OFF_MASK;
333 /* Didn't find it: h indicates where it would go. */
337 return find_in_chain(tdb, key, hashtable, h, rec, tinfo);
340 tdb_unlock_hashes(tdb, h->hlock_start, h->hlock_range, ltype);
344 /* I wrote a simple test, expanding a hash to 2GB, for the following
346 * 1) Expanding all the buckets at once,
347 * 2) Expanding the bucket we wanted to place the new entry into.
348 * 3) Expanding the most-populated bucket,
350 * I measured the worst/average/best density during this process.
355 * So we figure out the busiest bucket for the moment.
357 static unsigned fullest_bucket(struct tdb_context *tdb,
358 const tdb_off_t *group,
361 unsigned counts[1 << TDB_HASH_GROUP_BITS] = { 0 };
362 unsigned int i, best_bucket;
364 /* Count the new entry. */
365 counts[new_bucket]++;
366 best_bucket = new_bucket;
368 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
369 unsigned this_bucket;
371 if (is_subhash(group[i]))
373 this_bucket = group[i] & TDB_OFF_HASH_GROUP_MASK;
374 if (++counts[this_bucket] > counts[best_bucket])
375 best_bucket = this_bucket;
381 static bool put_into_group(tdb_off_t *group,
382 unsigned bucket, tdb_off_t encoded)
386 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
387 unsigned b = (bucket + i) % (1 << TDB_HASH_GROUP_BITS);
397 static void force_into_group(tdb_off_t *group,
398 unsigned bucket, tdb_off_t encoded)
400 if (!put_into_group(group, bucket, encoded))
404 static tdb_off_t encode_offset(tdb_off_t new_off, struct hash_info *h)
406 return h->home_bucket
408 | ((uint64_t)bits_from(h->h,
409 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
410 TDB_OFF_UPPER_STEAL_EXTRA)
411 << TDB_OFF_HASH_EXTRA_BIT);
414 /* Simply overwrite the hash entry we found before. */
415 int replace_in_hash(struct tdb_context *tdb,
419 enum TDB_ERROR ecode;
421 ecode = tdb_write_off(tdb, hbucket_off(h->group_start, h->found_bucket),
422 encode_offset(new_off, h));
423 if (ecode != TDB_SUCCESS) {
430 /* We slot in anywhere that's empty in the chain. */
431 static int COLD add_to_chain(struct tdb_context *tdb,
436 enum TDB_ERROR ecode;
438 entry = tdb_find_zero_off(tdb, subhash, 1<<TDB_HASH_GROUP_BITS);
439 if (TDB_OFF_IS_ERR(entry)) {
444 if (entry == 1 << TDB_HASH_GROUP_BITS) {
447 next = tdb_read_off(tdb, subhash
448 + offsetof(struct tdb_chain, next));
449 if (TDB_OFF_IS_ERR(next)) {
455 next = alloc(tdb, 0, sizeof(struct tdb_chain), 0,
456 TDB_CHAIN_MAGIC, false);
457 if (next == TDB_OFF_ERR)
459 ecode = zero_out(tdb,
460 next+sizeof(struct tdb_used_record),
461 sizeof(struct tdb_chain));
462 if (ecode != TDB_SUCCESS) {
466 ecode = tdb_write_off(tdb, subhash
467 + offsetof(struct tdb_chain,
470 if (ecode != TDB_SUCCESS) {
475 return add_to_chain(tdb, next, new_off);
478 ecode = tdb_write_off(tdb, subhash + entry * sizeof(tdb_off_t),
480 if (ecode != TDB_SUCCESS) {
487 /* Add into a newly created subhash. */
488 static int add_to_subhash(struct tdb_context *tdb, tdb_off_t subhash,
489 unsigned hash_used, tdb_off_t val)
491 tdb_off_t off = (val & TDB_OFF_MASK), *group;
494 enum TDB_ERROR ecode;
496 h.hash_used = hash_used;
498 if (hash_used + TDB_SUBLEVEL_HASH_BITS > 64)
499 return add_to_chain(tdb, subhash, off);
501 h.h = hash_record(tdb, off);
502 gnum = use_bits(&h, TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS);
503 h.group_start = subhash
504 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
505 h.home_bucket = use_bits(&h, TDB_HASH_GROUP_BITS);
507 group = tdb_access_write(tdb, h.group_start,
508 sizeof(*group) << TDB_HASH_GROUP_BITS, true);
509 if (TDB_PTR_IS_ERR(group)) {
510 tdb->ecode = TDB_PTR_ERR(group);
513 force_into_group(group, h.home_bucket, encode_offset(off, &h));
514 ecode = tdb_access_commit(tdb, group);
515 if (ecode != TDB_SUCCESS) {
522 static int expand_group(struct tdb_context *tdb, struct hash_info *h)
524 unsigned bucket, num_vals, i, magic;
527 tdb_off_t vals[1 << TDB_HASH_GROUP_BITS];
528 enum TDB_ERROR ecode;
530 /* Attach new empty subhash under fullest bucket. */
531 bucket = fullest_bucket(tdb, h->group, h->home_bucket);
533 if (h->hash_used == 64) {
534 add_stat(tdb, alloc_chain, 1);
535 subsize = sizeof(struct tdb_chain);
536 magic = TDB_CHAIN_MAGIC;
538 add_stat(tdb, alloc_subhash, 1);
539 subsize = (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS);
540 magic = TDB_HTABLE_MAGIC;
543 subhash = alloc(tdb, 0, subsize, 0, magic, false);
544 if (subhash == TDB_OFF_ERR)
547 ecode = zero_out(tdb, subhash + sizeof(struct tdb_used_record),
549 if (ecode != TDB_SUCCESS) {
554 /* Remove any which are destined for bucket or are in wrong place. */
556 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
557 unsigned home_bucket = h->group[i] & TDB_OFF_HASH_GROUP_MASK;
558 if (!h->group[i] || is_subhash(h->group[i]))
560 if (home_bucket == bucket || home_bucket != i) {
561 vals[num_vals++] = h->group[i];
565 /* FIXME: This assert is valid, but we do this during unit test :( */
566 /* assert(num_vals); */
568 /* Overwrite expanded bucket with subhash pointer. */
569 h->group[bucket] = subhash | (1ULL << TDB_OFF_UPPER_STEAL_SUBHASH_BIT);
571 /* Point to actual contents of record. */
572 subhash += sizeof(struct tdb_used_record);
574 /* Put values back. */
575 for (i = 0; i < num_vals; i++) {
576 unsigned this_bucket = vals[i] & TDB_OFF_HASH_GROUP_MASK;
578 if (this_bucket == bucket) {
579 if (add_to_subhash(tdb, subhash, h->hash_used, vals[i]))
582 /* There should be room to put this back. */
583 force_into_group(h->group, this_bucket, vals[i]);
589 int delete_from_hash(struct tdb_context *tdb, struct hash_info *h)
591 unsigned int i, num_movers = 0;
592 tdb_off_t movers[1 << TDB_HASH_GROUP_BITS];
593 enum TDB_ERROR ecode;
595 h->group[h->found_bucket] = 0;
596 for (i = 1; i < (1 << TDB_HASH_GROUP_BITS); i++) {
597 unsigned this_bucket;
599 this_bucket = (h->found_bucket+i) % (1 << TDB_HASH_GROUP_BITS);
600 /* Empty bucket? We're done. */
601 if (!h->group[this_bucket])
604 /* Ignore subhashes. */
605 if (is_subhash(h->group[this_bucket]))
608 /* If this one is not happy where it is, we'll move it. */
609 if ((h->group[this_bucket] & TDB_OFF_HASH_GROUP_MASK)
611 movers[num_movers++] = h->group[this_bucket];
612 h->group[this_bucket] = 0;
616 /* Put back the ones we erased. */
617 for (i = 0; i < num_movers; i++) {
618 force_into_group(h->group, movers[i] & TDB_OFF_HASH_GROUP_MASK,
622 /* Now we write back the hash group */
623 ecode = tdb_write_convert(tdb, h->group_start,
624 h->group, sizeof(h->group));
625 if (ecode != TDB_SUCCESS) {
632 int add_to_hash(struct tdb_context *tdb, struct hash_info *h, tdb_off_t new_off)
634 enum TDB_ERROR ecode;
636 /* We hit an empty bucket during search? That's where it goes. */
637 if (!h->group[h->found_bucket]) {
638 h->group[h->found_bucket] = encode_offset(new_off, h);
639 /* Write back the modified group. */
640 ecode = tdb_write_convert(tdb, h->group_start,
641 h->group, sizeof(h->group));
642 if (ecode != TDB_SUCCESS) {
649 if (h->hash_used > 64)
650 return add_to_chain(tdb, h->group_start, new_off);
652 /* We're full. Expand. */
653 if (expand_group(tdb, h) == -1)
656 if (is_subhash(h->group[h->home_bucket])) {
657 /* We were expanded! */
661 /* Write back the modified group. */
662 ecode = tdb_write_convert(tdb, h->group_start, h->group,
664 if (ecode != TDB_SUCCESS) {
669 /* Move hashinfo down a level. */
670 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
671 + sizeof(struct tdb_used_record);
672 gnum = use_bits(h,TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
673 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
674 h->group_start = hashtable
675 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
676 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
678 if (ecode != TDB_SUCCESS) {
684 /* Expanding the group must have made room if it didn't choose this
686 if (put_into_group(h->group, h->home_bucket, encode_offset(new_off,h))){
687 ecode = tdb_write_convert(tdb, h->group_start,
688 h->group, sizeof(h->group));
689 if (ecode != TDB_SUCCESS) {
696 /* This can happen if all hashes in group (and us) dropped into same
697 * group in subhash. */
698 return add_to_hash(tdb, h, new_off);
701 /* Traverse support: returns offset of record, or 0 or TDB_OFF_ERR. */
702 static tdb_off_t iterate_hash(struct tdb_context *tdb,
703 struct traverse_info *tinfo)
705 tdb_off_t off, val, i;
706 struct traverse_level *tlevel;
708 tlevel = &tinfo->levels[tinfo->num_levels-1];
711 for (i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
712 tlevel->entry, tlevel->total_buckets);
713 i != tlevel->total_buckets;
714 i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
715 i+1, tlevel->total_buckets)) {
716 if (TDB_OFF_IS_ERR(i)) {
721 val = tdb_read_off(tdb, tlevel->hashtable+sizeof(tdb_off_t)*i);
722 if (TDB_OFF_IS_ERR(val)) {
727 off = val & TDB_OFF_MASK;
729 /* This makes the delete-all-in-traverse case work
730 * (and simplifies our logic a little). */
731 if (off == tinfo->prev)
736 if (!is_subhash(val)) {
742 /* When we come back, we want the next one */
746 tlevel->hashtable = off + sizeof(struct tdb_used_record);
748 /* Next level is a chain? */
749 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1))
750 tlevel->total_buckets = (1 << TDB_HASH_GROUP_BITS);
752 tlevel->total_buckets = (1 << TDB_SUBLEVEL_HASH_BITS);
757 if (tinfo->num_levels == 1)
760 /* Handle chained entries. */
761 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1)) {
762 tlevel->hashtable = tdb_read_off(tdb, tlevel->hashtable
763 + offsetof(struct tdb_chain,
765 if (TDB_OFF_IS_ERR(tlevel->hashtable)) {
766 tdb->ecode = tlevel->hashtable;
769 if (tlevel->hashtable) {
770 tlevel->hashtable += sizeof(struct tdb_used_record);
776 /* Go back up and keep searching. */
782 /* Return 1 if we find something, 0 if not, -1 on error. */
783 int next_in_hash(struct tdb_context *tdb,
784 struct traverse_info *tinfo,
785 TDB_DATA *kbuf, size_t *dlen)
787 const unsigned group_bits = TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS;
788 tdb_off_t hl_start, hl_range, off;
789 enum TDB_ERROR ecode;
791 while (tinfo->toplevel_group < (1 << group_bits)) {
792 hl_start = (tdb_off_t)tinfo->toplevel_group
793 << (64 - group_bits);
794 hl_range = 1ULL << group_bits;
795 ecode = tdb_lock_hashes(tdb, hl_start, hl_range, F_RDLCK,
797 if (ecode != TDB_SUCCESS) {
802 off = iterate_hash(tdb, tinfo);
804 struct tdb_used_record rec;
806 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
807 if (ecode != TDB_SUCCESS) {
809 tdb_unlock_hashes(tdb,
810 hl_start, hl_range, F_RDLCK);
813 if (rec_magic(&rec) != TDB_USED_MAGIC) {
814 tdb_logerr(tdb, TDB_ERR_CORRUPT,
817 " corrupt record at %llu",
822 kbuf->dsize = rec_key_length(&rec);
824 /* They want data as well? */
826 *dlen = rec_data_length(&rec);
827 kbuf->dptr = tdb_alloc_read(tdb,
832 kbuf->dptr = tdb_alloc_read(tdb,
836 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
837 if (TDB_PTR_IS_ERR(kbuf->dptr)) {
838 tdb->ecode = TDB_PTR_ERR(kbuf->dptr);
844 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
846 tinfo->toplevel_group++;
847 tinfo->levels[0].hashtable
848 += (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
849 tinfo->levels[0].entry = 0;
854 /* Return 1 if we find something, 0 if not, -1 on error. */
855 int first_in_hash(struct tdb_context *tdb,
856 struct traverse_info *tinfo,
857 TDB_DATA *kbuf, size_t *dlen)
860 tinfo->toplevel_group = 0;
861 tinfo->num_levels = 1;
862 tinfo->levels[0].hashtable = offsetof(struct tdb_header, hashtable);
863 tinfo->levels[0].entry = 0;
864 tinfo->levels[0].total_buckets = (1 << TDB_HASH_GROUP_BITS);
866 return next_in_hash(tdb, tinfo, kbuf, dlen);
869 /* Even if the entry isn't in this hash bucket, you'd have to lock this
870 * bucket to find it. */
871 static int chainlock(struct tdb_context *tdb, const TDB_DATA *key,
872 int ltype, enum tdb_lock_flags waitflag,
875 enum TDB_ERROR ecode;
876 uint64_t h = tdb_hash(tdb, key->dptr, key->dsize);
877 tdb_off_t lockstart, locksize;
878 unsigned int group, gbits;
880 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
881 group = bits_from(h, 64 - gbits, gbits);
883 lockstart = hlock_range(group, &locksize);
885 ecode = tdb_lock_hashes(tdb, lockstart, locksize, ltype, waitflag);
886 tdb_trace_1rec(tdb, func, *key);
887 if (ecode != TDB_SUCCESS) {
894 /* lock/unlock one hash chain. This is meant to be used to reduce
895 contention - it cannot guarantee how many records will be locked */
896 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
898 return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT, "tdb_chainlock");
901 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
903 uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
904 tdb_off_t lockstart, locksize;
905 unsigned int group, gbits;
906 enum TDB_ERROR ecode;
908 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
909 group = bits_from(h, 64 - gbits, gbits);
911 lockstart = hlock_range(group, &locksize);
913 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
914 ecode = tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK);
915 if (ecode != TDB_SUCCESS) {