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 (next == TDB_OFF_ERR)
231 next += sizeof(struct tdb_used_record);
236 /* This is the core routine which searches the hashtable for an entry.
237 * On error, no locks are held and TDB_OFF_ERR is returned.
238 * Otherwise, hinfo is filled in (and the optional tinfo).
239 * If not found, the return value is 0.
240 * If found, the return value is the offset, and *rec is the record. */
241 tdb_off_t find_and_lock(struct tdb_context *tdb,
245 struct tdb_used_record *rec,
246 struct traverse_info *tinfo)
250 enum TDB_ERROR ecode;
252 h->h = tdb_hash(tdb, key.dptr, key.dsize);
254 group = use_bits(h, TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
255 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
257 h->hlock_start = hlock_range(group, &h->hlock_range);
258 ecode = tdb_lock_hashes(tdb, h->hlock_start, h->hlock_range, ltype,
260 if (ecode != TDB_SUCCESS) {
265 hashtable = offsetof(struct tdb_header, hashtable);
267 tinfo->toplevel_group = group;
268 tinfo->num_levels = 1;
269 tinfo->levels[0].entry = 0;
270 tinfo->levels[0].hashtable = hashtable
271 + (group << TDB_HASH_GROUP_BITS) * sizeof(tdb_off_t);
272 tinfo->levels[0].total_buckets = 1 << TDB_HASH_GROUP_BITS;
275 while (h->hash_used <= 64) {
276 /* Read in the hash group. */
277 h->group_start = hashtable
278 + group * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
280 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
282 if (ecode != TDB_SUCCESS) {
287 /* Pointer to another hash table? Go down... */
288 if (is_subhash(h->group[h->home_bucket])) {
289 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
290 + sizeof(struct tdb_used_record);
292 /* When we come back, use *next* bucket */
293 tinfo->levels[tinfo->num_levels-1].entry
294 += h->home_bucket + 1;
296 group = use_bits(h, TDB_SUBLEVEL_HASH_BITS
297 - TDB_HASH_GROUP_BITS);
298 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
300 tinfo->levels[tinfo->num_levels].hashtable
302 tinfo->levels[tinfo->num_levels].total_buckets
303 = 1 << TDB_SUBLEVEL_HASH_BITS;
304 tinfo->levels[tinfo->num_levels].entry
305 = group << TDB_HASH_GROUP_BITS;
311 /* It's in this group: search (until 0 or all searched) */
312 for (i = 0, h->found_bucket = h->home_bucket;
313 i < (1 << TDB_HASH_GROUP_BITS);
314 i++, h->found_bucket = ((h->found_bucket+1)
315 % (1 << TDB_HASH_GROUP_BITS))) {
316 if (is_subhash(h->group[h->found_bucket]))
319 if (!h->group[h->found_bucket])
322 if (match(tdb, h, &key, h->group[h->found_bucket],
325 tinfo->levels[tinfo->num_levels-1].entry
328 return h->group[h->found_bucket] & TDB_OFF_MASK;
331 /* Didn't find it: h indicates where it would go. */
335 return find_in_chain(tdb, key, hashtable, h, rec, tinfo);
338 tdb_unlock_hashes(tdb, h->hlock_start, h->hlock_range, ltype);
342 /* I wrote a simple test, expanding a hash to 2GB, for the following
344 * 1) Expanding all the buckets at once,
345 * 2) Expanding the bucket we wanted to place the new entry into.
346 * 3) Expanding the most-populated bucket,
348 * I measured the worst/average/best density during this process.
353 * So we figure out the busiest bucket for the moment.
355 static unsigned fullest_bucket(struct tdb_context *tdb,
356 const tdb_off_t *group,
359 unsigned counts[1 << TDB_HASH_GROUP_BITS] = { 0 };
360 unsigned int i, best_bucket;
362 /* Count the new entry. */
363 counts[new_bucket]++;
364 best_bucket = new_bucket;
366 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
367 unsigned this_bucket;
369 if (is_subhash(group[i]))
371 this_bucket = group[i] & TDB_OFF_HASH_GROUP_MASK;
372 if (++counts[this_bucket] > counts[best_bucket])
373 best_bucket = this_bucket;
379 static bool put_into_group(tdb_off_t *group,
380 unsigned bucket, tdb_off_t encoded)
384 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
385 unsigned b = (bucket + i) % (1 << TDB_HASH_GROUP_BITS);
395 static void force_into_group(tdb_off_t *group,
396 unsigned bucket, tdb_off_t encoded)
398 if (!put_into_group(group, bucket, encoded))
402 static tdb_off_t encode_offset(tdb_off_t new_off, struct hash_info *h)
404 return h->home_bucket
406 | ((uint64_t)bits_from(h->h,
407 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
408 TDB_OFF_UPPER_STEAL_EXTRA)
409 << TDB_OFF_HASH_EXTRA_BIT);
412 /* Simply overwrite the hash entry we found before. */
413 int replace_in_hash(struct tdb_context *tdb,
417 enum TDB_ERROR ecode;
419 ecode = tdb_write_off(tdb, hbucket_off(h->group_start, h->found_bucket),
420 encode_offset(new_off, h));
421 if (ecode != TDB_SUCCESS) {
428 /* We slot in anywhere that's empty in the chain. */
429 static int COLD add_to_chain(struct tdb_context *tdb,
433 size_t entry = tdb_find_zero_off(tdb, subhash, 1<<TDB_HASH_GROUP_BITS);
434 enum TDB_ERROR ecode;
436 if (entry == 1 << TDB_HASH_GROUP_BITS) {
439 next = tdb_read_off(tdb, subhash
440 + offsetof(struct tdb_chain, next));
441 if (next == TDB_OFF_ERR)
445 next = alloc(tdb, 0, sizeof(struct tdb_chain), 0,
446 TDB_CHAIN_MAGIC, false);
447 if (next == TDB_OFF_ERR)
449 ecode = zero_out(tdb,
450 next+sizeof(struct tdb_used_record),
451 sizeof(struct tdb_chain));
452 if (ecode != TDB_SUCCESS) {
456 ecode = tdb_write_off(tdb, subhash
457 + offsetof(struct tdb_chain,
460 if (ecode != TDB_SUCCESS) {
465 return add_to_chain(tdb, next, new_off);
468 ecode = tdb_write_off(tdb, subhash + entry * sizeof(tdb_off_t),
470 if (ecode != TDB_SUCCESS) {
477 /* Add into a newly created subhash. */
478 static int add_to_subhash(struct tdb_context *tdb, tdb_off_t subhash,
479 unsigned hash_used, tdb_off_t val)
481 tdb_off_t off = (val & TDB_OFF_MASK), *group;
484 enum TDB_ERROR ecode;
486 h.hash_used = hash_used;
488 if (hash_used + TDB_SUBLEVEL_HASH_BITS > 64)
489 return add_to_chain(tdb, subhash, off);
491 h.h = hash_record(tdb, off);
492 gnum = use_bits(&h, TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS);
493 h.group_start = subhash
494 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
495 h.home_bucket = use_bits(&h, TDB_HASH_GROUP_BITS);
497 group = tdb_access_write(tdb, h.group_start,
498 sizeof(*group) << TDB_HASH_GROUP_BITS, true);
499 if (TDB_PTR_IS_ERR(group)) {
500 tdb->ecode = TDB_PTR_ERR(group);
503 force_into_group(group, h.home_bucket, encode_offset(off, &h));
504 ecode = tdb_access_commit(tdb, group);
505 if (ecode != TDB_SUCCESS) {
512 static int expand_group(struct tdb_context *tdb, struct hash_info *h)
514 unsigned bucket, num_vals, i, magic;
517 tdb_off_t vals[1 << TDB_HASH_GROUP_BITS];
518 enum TDB_ERROR ecode;
520 /* Attach new empty subhash under fullest bucket. */
521 bucket = fullest_bucket(tdb, h->group, h->home_bucket);
523 if (h->hash_used == 64) {
524 add_stat(tdb, alloc_chain, 1);
525 subsize = sizeof(struct tdb_chain);
526 magic = TDB_CHAIN_MAGIC;
528 add_stat(tdb, alloc_subhash, 1);
529 subsize = (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS);
530 magic = TDB_HTABLE_MAGIC;
533 subhash = alloc(tdb, 0, subsize, 0, magic, false);
534 if (subhash == TDB_OFF_ERR)
537 ecode = zero_out(tdb, subhash + sizeof(struct tdb_used_record),
539 if (ecode != TDB_SUCCESS) {
544 /* Remove any which are destined for bucket or are in wrong place. */
546 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
547 unsigned home_bucket = h->group[i] & TDB_OFF_HASH_GROUP_MASK;
548 if (!h->group[i] || is_subhash(h->group[i]))
550 if (home_bucket == bucket || home_bucket != i) {
551 vals[num_vals++] = h->group[i];
555 /* FIXME: This assert is valid, but we do this during unit test :( */
556 /* assert(num_vals); */
558 /* Overwrite expanded bucket with subhash pointer. */
559 h->group[bucket] = subhash | (1ULL << TDB_OFF_UPPER_STEAL_SUBHASH_BIT);
561 /* Point to actual contents of record. */
562 subhash += sizeof(struct tdb_used_record);
564 /* Put values back. */
565 for (i = 0; i < num_vals; i++) {
566 unsigned this_bucket = vals[i] & TDB_OFF_HASH_GROUP_MASK;
568 if (this_bucket == bucket) {
569 if (add_to_subhash(tdb, subhash, h->hash_used, vals[i]))
572 /* There should be room to put this back. */
573 force_into_group(h->group, this_bucket, vals[i]);
579 int delete_from_hash(struct tdb_context *tdb, struct hash_info *h)
581 unsigned int i, num_movers = 0;
582 tdb_off_t movers[1 << TDB_HASH_GROUP_BITS];
583 enum TDB_ERROR ecode;
585 h->group[h->found_bucket] = 0;
586 for (i = 1; i < (1 << TDB_HASH_GROUP_BITS); i++) {
587 unsigned this_bucket;
589 this_bucket = (h->found_bucket+i) % (1 << TDB_HASH_GROUP_BITS);
590 /* Empty bucket? We're done. */
591 if (!h->group[this_bucket])
594 /* Ignore subhashes. */
595 if (is_subhash(h->group[this_bucket]))
598 /* If this one is not happy where it is, we'll move it. */
599 if ((h->group[this_bucket] & TDB_OFF_HASH_GROUP_MASK)
601 movers[num_movers++] = h->group[this_bucket];
602 h->group[this_bucket] = 0;
606 /* Put back the ones we erased. */
607 for (i = 0; i < num_movers; i++) {
608 force_into_group(h->group, movers[i] & TDB_OFF_HASH_GROUP_MASK,
612 /* Now we write back the hash group */
613 ecode = tdb_write_convert(tdb, h->group_start,
614 h->group, sizeof(h->group));
615 if (ecode != TDB_SUCCESS) {
622 int add_to_hash(struct tdb_context *tdb, struct hash_info *h, tdb_off_t new_off)
624 enum TDB_ERROR ecode;
626 /* We hit an empty bucket during search? That's where it goes. */
627 if (!h->group[h->found_bucket]) {
628 h->group[h->found_bucket] = encode_offset(new_off, h);
629 /* Write back the modified group. */
630 ecode = tdb_write_convert(tdb, h->group_start,
631 h->group, sizeof(h->group));
632 if (ecode != TDB_SUCCESS) {
639 if (h->hash_used > 64)
640 return add_to_chain(tdb, h->group_start, new_off);
642 /* We're full. Expand. */
643 if (expand_group(tdb, h) == -1)
646 if (is_subhash(h->group[h->home_bucket])) {
647 /* We were expanded! */
651 /* Write back the modified group. */
652 ecode = tdb_write_convert(tdb, h->group_start, h->group,
654 if (ecode != TDB_SUCCESS) {
659 /* Move hashinfo down a level. */
660 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
661 + sizeof(struct tdb_used_record);
662 gnum = use_bits(h,TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
663 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
664 h->group_start = hashtable
665 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
666 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
668 if (ecode != TDB_SUCCESS) {
674 /* Expanding the group must have made room if it didn't choose this
676 if (put_into_group(h->group, h->home_bucket, encode_offset(new_off,h))){
677 ecode = tdb_write_convert(tdb, h->group_start,
678 h->group, sizeof(h->group));
679 if (ecode != TDB_SUCCESS) {
686 /* This can happen if all hashes in group (and us) dropped into same
687 * group in subhash. */
688 return add_to_hash(tdb, h, new_off);
691 /* Traverse support: returns offset of record, or 0 or TDB_OFF_ERR. */
692 static tdb_off_t iterate_hash(struct tdb_context *tdb,
693 struct traverse_info *tinfo)
697 struct traverse_level *tlevel;
699 tlevel = &tinfo->levels[tinfo->num_levels-1];
702 for (i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
703 tlevel->entry, tlevel->total_buckets);
704 i != tlevel->total_buckets;
705 i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
706 i+1, tlevel->total_buckets)) {
707 val = tdb_read_off(tdb, tlevel->hashtable+sizeof(tdb_off_t)*i);
708 if (unlikely(val == TDB_OFF_ERR))
711 off = val & TDB_OFF_MASK;
713 /* This makes the delete-all-in-traverse case work
714 * (and simplifies our logic a little). */
715 if (off == tinfo->prev)
720 if (!is_subhash(val)) {
726 /* When we come back, we want the next one */
730 tlevel->hashtable = off + sizeof(struct tdb_used_record);
732 /* Next level is a chain? */
733 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1))
734 tlevel->total_buckets = (1 << TDB_HASH_GROUP_BITS);
736 tlevel->total_buckets = (1 << TDB_SUBLEVEL_HASH_BITS);
741 if (tinfo->num_levels == 1)
744 /* Handle chained entries. */
745 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1)) {
746 tlevel->hashtable = tdb_read_off(tdb, tlevel->hashtable
747 + offsetof(struct tdb_chain,
749 if (tlevel->hashtable == TDB_OFF_ERR)
751 if (tlevel->hashtable) {
752 tlevel->hashtable += sizeof(struct tdb_used_record);
758 /* Go back up and keep searching. */
764 /* Return 1 if we find something, 0 if not, -1 on error. */
765 int next_in_hash(struct tdb_context *tdb,
766 struct traverse_info *tinfo,
767 TDB_DATA *kbuf, size_t *dlen)
769 const unsigned group_bits = TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS;
770 tdb_off_t hl_start, hl_range, off;
771 enum TDB_ERROR ecode;
773 while (tinfo->toplevel_group < (1 << group_bits)) {
774 hl_start = (tdb_off_t)tinfo->toplevel_group
775 << (64 - group_bits);
776 hl_range = 1ULL << group_bits;
777 ecode = tdb_lock_hashes(tdb, hl_start, hl_range, F_RDLCK,
779 if (ecode != TDB_SUCCESS) {
784 off = iterate_hash(tdb, tinfo);
786 struct tdb_used_record rec;
788 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
789 if (ecode != TDB_SUCCESS) {
791 tdb_unlock_hashes(tdb,
792 hl_start, hl_range, F_RDLCK);
795 if (rec_magic(&rec) != TDB_USED_MAGIC) {
796 tdb_logerr(tdb, TDB_ERR_CORRUPT,
799 " corrupt record at %llu",
804 kbuf->dsize = rec_key_length(&rec);
806 /* They want data as well? */
808 *dlen = rec_data_length(&rec);
809 kbuf->dptr = tdb_alloc_read(tdb,
814 kbuf->dptr = tdb_alloc_read(tdb,
818 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
819 if (TDB_PTR_IS_ERR(kbuf->dptr)) {
820 tdb->ecode = TDB_PTR_ERR(kbuf->dptr);
826 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
828 tinfo->toplevel_group++;
829 tinfo->levels[0].hashtable
830 += (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
831 tinfo->levels[0].entry = 0;
836 /* Return 1 if we find something, 0 if not, -1 on error. */
837 int first_in_hash(struct tdb_context *tdb,
838 struct traverse_info *tinfo,
839 TDB_DATA *kbuf, size_t *dlen)
842 tinfo->toplevel_group = 0;
843 tinfo->num_levels = 1;
844 tinfo->levels[0].hashtable = offsetof(struct tdb_header, hashtable);
845 tinfo->levels[0].entry = 0;
846 tinfo->levels[0].total_buckets = (1 << TDB_HASH_GROUP_BITS);
848 return next_in_hash(tdb, tinfo, kbuf, dlen);
851 /* Even if the entry isn't in this hash bucket, you'd have to lock this
852 * bucket to find it. */
853 static int chainlock(struct tdb_context *tdb, const TDB_DATA *key,
854 int ltype, enum tdb_lock_flags waitflag,
857 enum TDB_ERROR ecode;
858 uint64_t h = tdb_hash(tdb, key->dptr, key->dsize);
859 tdb_off_t lockstart, locksize;
860 unsigned int group, gbits;
862 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
863 group = bits_from(h, 64 - gbits, gbits);
865 lockstart = hlock_range(group, &locksize);
867 ecode = tdb_lock_hashes(tdb, lockstart, locksize, ltype, waitflag);
868 tdb_trace_1rec(tdb, func, *key);
869 if (ecode != TDB_SUCCESS) {
876 /* lock/unlock one hash chain. This is meant to be used to reduce
877 contention - it cannot guarantee how many records will be locked */
878 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
880 return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT, "tdb_chainlock");
883 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
885 uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
886 tdb_off_t lockstart, locksize;
887 unsigned int group, gbits;
888 enum TDB_ERROR ecode;
890 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
891 group = bits_from(h, 64 - gbits, gbits);
893 lockstart = hlock_range(group, &locksize);
895 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
896 ecode = tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK);
897 if (ecode != TDB_SUCCESS) {