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 tdb_bool_err key_matches(struct tdb_context *tdb,
86 const struct tdb_used_record *rec,
88 const struct tdb_data *key)
90 tdb_bool_err ret = false;
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 return TDB_PTR_ERR(rkey);
102 if (memcmp(rkey, key->dptr, key->dsize) == 0)
105 add_stat(tdb, compare_wrong_keycmp, 1);
106 tdb_access_release(tdb, rkey);
110 /* Does entry match? */
111 static tdb_bool_err match(struct tdb_context *tdb,
113 const struct tdb_data *key,
115 struct tdb_used_record *rec)
118 enum TDB_ERROR ecode;
120 add_stat(tdb, compares, 1);
121 /* Desired bucket must match. */
122 if (h->home_bucket != (val & TDB_OFF_HASH_GROUP_MASK)) {
123 add_stat(tdb, compare_wrong_bucket, 1);
127 /* Top bits of offset == next bits of hash. */
128 if (bits_from(val, TDB_OFF_HASH_EXTRA_BIT, TDB_OFF_UPPER_STEAL_EXTRA)
129 != bits_from(h->h, 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
130 TDB_OFF_UPPER_STEAL_EXTRA)) {
131 add_stat(tdb, compare_wrong_offsetbits, 1);
135 off = val & TDB_OFF_MASK;
136 ecode = tdb_read_convert(tdb, off, rec, sizeof(*rec));
137 if (ecode != TDB_SUCCESS) {
141 if ((h->h & ((1 << 11)-1)) != rec_hash(rec)) {
142 add_stat(tdb, compare_wrong_rechash, 1);
146 return key_matches(tdb, rec, off, key);
149 static tdb_off_t hbucket_off(tdb_off_t group_start, unsigned bucket)
152 + (bucket % (1 << TDB_HASH_GROUP_BITS)) * sizeof(tdb_off_t);
155 bool is_subhash(tdb_off_t val)
157 return (val >> TDB_OFF_UPPER_STEAL_SUBHASH_BIT) & 1;
160 /* FIXME: Guess the depth, don't over-lock! */
161 static tdb_off_t hlock_range(tdb_off_t group, tdb_off_t *size)
163 *size = 1ULL << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
164 return group << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
167 static tdb_off_t COLD find_in_chain(struct tdb_context *tdb,
171 struct tdb_used_record *rec,
172 struct traverse_info *tinfo)
175 enum TDB_ERROR ecode;
177 /* In case nothing is free, we set these to zero. */
178 h->home_bucket = h->found_bucket = 0;
180 for (off = chain; off; off = next) {
183 h->group_start = off;
184 ecode = tdb_read_convert(tdb, off, h->group, sizeof(h->group));
185 if (ecode != TDB_SUCCESS) {
189 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
192 /* Remember this empty bucket. */
193 h->home_bucket = h->found_bucket = i;
197 /* We can insert extra bits via add_to_hash
198 * empty bucket logic. */
199 recoff = h->group[i] & TDB_OFF_MASK;
200 ecode = tdb_read_convert(tdb, recoff, rec,
202 if (ecode != TDB_SUCCESS) {
206 ecode = 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)) {
232 next += sizeof(struct tdb_used_record);
237 /* This is the core routine which searches the hashtable for an entry.
238 * On error, no locks are held and -ve is returned.
239 * Otherwise, hinfo is filled in (and the optional tinfo).
240 * If not found, the return value is 0.
241 * If found, the return value is the offset, and *rec is the record. */
242 tdb_off_t find_and_lock(struct tdb_context *tdb,
246 struct tdb_used_record *rec,
247 struct traverse_info *tinfo)
251 enum TDB_ERROR ecode;
253 h->h = tdb_hash(tdb, key.dptr, key.dsize);
255 group = use_bits(h, TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
256 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
258 h->hlock_start = hlock_range(group, &h->hlock_range);
259 ecode = tdb_lock_hashes(tdb, h->hlock_start, h->hlock_range, ltype,
261 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) {
286 /* Pointer to another hash table? Go down... */
287 if (is_subhash(h->group[h->home_bucket])) {
288 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
289 + sizeof(struct tdb_used_record);
291 /* When we come back, use *next* bucket */
292 tinfo->levels[tinfo->num_levels-1].entry
293 += h->home_bucket + 1;
295 group = use_bits(h, TDB_SUBLEVEL_HASH_BITS
296 - TDB_HASH_GROUP_BITS);
297 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
299 tinfo->levels[tinfo->num_levels].hashtable
301 tinfo->levels[tinfo->num_levels].total_buckets
302 = 1 << TDB_SUBLEVEL_HASH_BITS;
303 tinfo->levels[tinfo->num_levels].entry
304 = group << TDB_HASH_GROUP_BITS;
310 /* It's in this group: search (until 0 or all searched) */
311 for (i = 0, h->found_bucket = h->home_bucket;
312 i < (1 << TDB_HASH_GROUP_BITS);
313 i++, h->found_bucket = ((h->found_bucket+1)
314 % (1 << TDB_HASH_GROUP_BITS))) {
316 if (is_subhash(h->group[h->found_bucket]))
319 if (!h->group[h->found_bucket])
322 berr = match(tdb, h, &key, h->group[h->found_bucket],
330 tinfo->levels[tinfo->num_levels-1].entry
333 return h->group[h->found_bucket] & TDB_OFF_MASK;
336 /* Didn't find it: h indicates where it would go. */
340 return find_in_chain(tdb, key, hashtable, h, rec, tinfo);
343 tdb_unlock_hashes(tdb, h->hlock_start, h->hlock_range, ltype);
347 /* I wrote a simple test, expanding a hash to 2GB, for the following
349 * 1) Expanding all the buckets at once,
350 * 2) Expanding the bucket we wanted to place the new entry into.
351 * 3) Expanding the most-populated bucket,
353 * I measured the worst/average/best density during this process.
358 * So we figure out the busiest bucket for the moment.
360 static unsigned fullest_bucket(struct tdb_context *tdb,
361 const tdb_off_t *group,
364 unsigned counts[1 << TDB_HASH_GROUP_BITS] = { 0 };
365 unsigned int i, best_bucket;
367 /* Count the new entry. */
368 counts[new_bucket]++;
369 best_bucket = new_bucket;
371 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
372 unsigned this_bucket;
374 if (is_subhash(group[i]))
376 this_bucket = group[i] & TDB_OFF_HASH_GROUP_MASK;
377 if (++counts[this_bucket] > counts[best_bucket])
378 best_bucket = this_bucket;
384 static bool put_into_group(tdb_off_t *group,
385 unsigned bucket, tdb_off_t encoded)
389 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
390 unsigned b = (bucket + i) % (1 << TDB_HASH_GROUP_BITS);
400 static void force_into_group(tdb_off_t *group,
401 unsigned bucket, tdb_off_t encoded)
403 if (!put_into_group(group, bucket, encoded))
407 static tdb_off_t encode_offset(tdb_off_t new_off, struct hash_info *h)
409 return h->home_bucket
411 | ((uint64_t)bits_from(h->h,
412 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
413 TDB_OFF_UPPER_STEAL_EXTRA)
414 << TDB_OFF_HASH_EXTRA_BIT);
417 /* Simply overwrite the hash entry we found before. */
418 enum TDB_ERROR replace_in_hash(struct tdb_context *tdb,
422 return tdb_write_off(tdb, hbucket_off(h->group_start, h->found_bucket),
423 encode_offset(new_off, h));
426 /* We slot in anywhere that's empty in the chain. */
427 static enum TDB_ERROR COLD add_to_chain(struct tdb_context *tdb,
432 enum TDB_ERROR ecode;
434 entry = tdb_find_zero_off(tdb, subhash, 1<<TDB_HASH_GROUP_BITS);
435 if (TDB_OFF_IS_ERR(entry)) {
439 if (entry == 1 << TDB_HASH_GROUP_BITS) {
442 next = tdb_read_off(tdb, subhash
443 + offsetof(struct tdb_chain, next));
444 if (TDB_OFF_IS_ERR(next)) {
449 next = alloc(tdb, 0, sizeof(struct tdb_chain), 0,
450 TDB_CHAIN_MAGIC, false);
451 if (TDB_OFF_IS_ERR(next))
453 ecode = zero_out(tdb,
454 next+sizeof(struct tdb_used_record),
455 sizeof(struct tdb_chain));
456 if (ecode != TDB_SUCCESS) {
459 ecode = tdb_write_off(tdb, subhash
460 + offsetof(struct tdb_chain,
463 if (ecode != TDB_SUCCESS) {
467 return add_to_chain(tdb, next, new_off);
470 return tdb_write_off(tdb, subhash + entry * sizeof(tdb_off_t),
474 /* Add into a newly created subhash. */
475 static enum TDB_ERROR add_to_subhash(struct tdb_context *tdb, tdb_off_t subhash,
476 unsigned hash_used, tdb_off_t val)
478 tdb_off_t off = (val & TDB_OFF_MASK), *group;
482 h.hash_used = hash_used;
484 if (hash_used + TDB_SUBLEVEL_HASH_BITS > 64)
485 return add_to_chain(tdb, subhash, off);
487 h.h = hash_record(tdb, off);
488 gnum = use_bits(&h, TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS);
489 h.group_start = subhash
490 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
491 h.home_bucket = use_bits(&h, TDB_HASH_GROUP_BITS);
493 group = tdb_access_write(tdb, h.group_start,
494 sizeof(*group) << TDB_HASH_GROUP_BITS, true);
495 if (TDB_PTR_IS_ERR(group)) {
496 return TDB_PTR_ERR(group);
498 force_into_group(group, h.home_bucket, encode_offset(off, &h));
499 return tdb_access_commit(tdb, group);
502 static enum TDB_ERROR expand_group(struct tdb_context *tdb, struct hash_info *h)
504 unsigned bucket, num_vals, i, magic;
507 tdb_off_t vals[1 << TDB_HASH_GROUP_BITS];
508 enum TDB_ERROR ecode;
510 /* Attach new empty subhash under fullest bucket. */
511 bucket = fullest_bucket(tdb, h->group, h->home_bucket);
513 if (h->hash_used == 64) {
514 add_stat(tdb, alloc_chain, 1);
515 subsize = sizeof(struct tdb_chain);
516 magic = TDB_CHAIN_MAGIC;
518 add_stat(tdb, alloc_subhash, 1);
519 subsize = (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS);
520 magic = TDB_HTABLE_MAGIC;
523 subhash = alloc(tdb, 0, subsize, 0, magic, false);
524 if (TDB_OFF_IS_ERR(subhash)) {
528 ecode = zero_out(tdb, subhash + sizeof(struct tdb_used_record),
530 if (ecode != TDB_SUCCESS) {
534 /* Remove any which are destined for bucket or are in wrong place. */
536 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
537 unsigned home_bucket = h->group[i] & TDB_OFF_HASH_GROUP_MASK;
538 if (!h->group[i] || is_subhash(h->group[i]))
540 if (home_bucket == bucket || home_bucket != i) {
541 vals[num_vals++] = h->group[i];
545 /* FIXME: This assert is valid, but we do this during unit test :( */
546 /* assert(num_vals); */
548 /* Overwrite expanded bucket with subhash pointer. */
549 h->group[bucket] = subhash | (1ULL << TDB_OFF_UPPER_STEAL_SUBHASH_BIT);
551 /* Point to actual contents of record. */
552 subhash += sizeof(struct tdb_used_record);
554 /* Put values back. */
555 for (i = 0; i < num_vals; i++) {
556 unsigned this_bucket = vals[i] & TDB_OFF_HASH_GROUP_MASK;
558 if (this_bucket == bucket) {
559 ecode = add_to_subhash(tdb, subhash, h->hash_used,
561 if (ecode != TDB_SUCCESS)
564 /* There should be room to put this back. */
565 force_into_group(h->group, this_bucket, vals[i]);
571 enum TDB_ERROR delete_from_hash(struct tdb_context *tdb, struct hash_info *h)
573 unsigned int i, num_movers = 0;
574 tdb_off_t movers[1 << TDB_HASH_GROUP_BITS];
576 h->group[h->found_bucket] = 0;
577 for (i = 1; i < (1 << TDB_HASH_GROUP_BITS); i++) {
578 unsigned this_bucket;
580 this_bucket = (h->found_bucket+i) % (1 << TDB_HASH_GROUP_BITS);
581 /* Empty bucket? We're done. */
582 if (!h->group[this_bucket])
585 /* Ignore subhashes. */
586 if (is_subhash(h->group[this_bucket]))
589 /* If this one is not happy where it is, we'll move it. */
590 if ((h->group[this_bucket] & TDB_OFF_HASH_GROUP_MASK)
592 movers[num_movers++] = h->group[this_bucket];
593 h->group[this_bucket] = 0;
597 /* Put back the ones we erased. */
598 for (i = 0; i < num_movers; i++) {
599 force_into_group(h->group, movers[i] & TDB_OFF_HASH_GROUP_MASK,
603 /* Now we write back the hash group */
604 return tdb_write_convert(tdb, h->group_start,
605 h->group, sizeof(h->group));
608 enum TDB_ERROR add_to_hash(struct tdb_context *tdb, struct hash_info *h,
611 enum TDB_ERROR ecode;
613 /* We hit an empty bucket during search? That's where it goes. */
614 if (!h->group[h->found_bucket]) {
615 h->group[h->found_bucket] = encode_offset(new_off, h);
616 /* Write back the modified group. */
617 return tdb_write_convert(tdb, h->group_start,
618 h->group, sizeof(h->group));
621 if (h->hash_used > 64)
622 return add_to_chain(tdb, h->group_start, new_off);
624 /* We're full. Expand. */
625 ecode = expand_group(tdb, h);
626 if (ecode != TDB_SUCCESS) {
630 if (is_subhash(h->group[h->home_bucket])) {
631 /* We were expanded! */
635 /* Write back the modified group. */
636 ecode = tdb_write_convert(tdb, h->group_start, h->group,
638 if (ecode != TDB_SUCCESS) {
642 /* Move hashinfo down a level. */
643 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
644 + sizeof(struct tdb_used_record);
645 gnum = use_bits(h,TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
646 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
647 h->group_start = hashtable
648 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
649 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
651 if (ecode != TDB_SUCCESS) {
656 /* Expanding the group must have made room if it didn't choose this
658 if (put_into_group(h->group, h->home_bucket, encode_offset(new_off,h))){
659 return tdb_write_convert(tdb, h->group_start,
660 h->group, sizeof(h->group));
663 /* This can happen if all hashes in group (and us) dropped into same
664 * group in subhash. */
665 return add_to_hash(tdb, h, new_off);
668 /* Traverse support: returns offset of record, or 0 or -ve error. */
669 static tdb_off_t iterate_hash(struct tdb_context *tdb,
670 struct traverse_info *tinfo)
672 tdb_off_t off, val, i;
673 struct traverse_level *tlevel;
675 tlevel = &tinfo->levels[tinfo->num_levels-1];
678 for (i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
679 tlevel->entry, tlevel->total_buckets);
680 i != tlevel->total_buckets;
681 i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
682 i+1, tlevel->total_buckets)) {
683 if (TDB_OFF_IS_ERR(i)) {
687 val = tdb_read_off(tdb, tlevel->hashtable+sizeof(tdb_off_t)*i);
688 if (TDB_OFF_IS_ERR(val)) {
692 off = val & TDB_OFF_MASK;
694 /* This makes the delete-all-in-traverse case work
695 * (and simplifies our logic a little). */
696 if (off == tinfo->prev)
701 if (!is_subhash(val)) {
707 /* When we come back, we want the next one */
711 tlevel->hashtable = off + sizeof(struct tdb_used_record);
713 /* Next level is a chain? */
714 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1))
715 tlevel->total_buckets = (1 << TDB_HASH_GROUP_BITS);
717 tlevel->total_buckets = (1 << TDB_SUBLEVEL_HASH_BITS);
722 if (tinfo->num_levels == 1)
725 /* Handle chained entries. */
726 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1)) {
727 tlevel->hashtable = tdb_read_off(tdb, tlevel->hashtable
728 + offsetof(struct tdb_chain,
730 if (TDB_OFF_IS_ERR(tlevel->hashtable)) {
731 return tlevel->hashtable;
733 if (tlevel->hashtable) {
734 tlevel->hashtable += sizeof(struct tdb_used_record);
740 /* Go back up and keep searching. */
746 /* Return success if we find something, TDB_ERR_NOEXIST if none. */
747 enum TDB_ERROR next_in_hash(struct tdb_context *tdb,
748 struct traverse_info *tinfo,
749 TDB_DATA *kbuf, size_t *dlen)
751 const unsigned group_bits = TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS;
752 tdb_off_t hl_start, hl_range, off;
753 enum TDB_ERROR ecode;
755 while (tinfo->toplevel_group < (1 << group_bits)) {
756 hl_start = (tdb_off_t)tinfo->toplevel_group
757 << (64 - group_bits);
758 hl_range = 1ULL << group_bits;
759 ecode = tdb_lock_hashes(tdb, hl_start, hl_range, F_RDLCK,
761 if (ecode != TDB_SUCCESS) {
765 off = iterate_hash(tdb, tinfo);
767 struct tdb_used_record rec;
769 if (TDB_OFF_IS_ERR(off)) {
774 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
775 if (ecode != TDB_SUCCESS) {
778 if (rec_magic(&rec) != TDB_USED_MAGIC) {
779 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
782 " corrupt record at %llu",
787 kbuf->dsize = rec_key_length(&rec);
789 /* They want data as well? */
791 *dlen = rec_data_length(&rec);
792 kbuf->dptr = tdb_alloc_read(tdb,
797 kbuf->dptr = tdb_alloc_read(tdb,
801 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
802 if (TDB_PTR_IS_ERR(kbuf->dptr)) {
803 return TDB_PTR_ERR(kbuf->dptr);
808 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
810 tinfo->toplevel_group++;
811 tinfo->levels[0].hashtable
812 += (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
813 tinfo->levels[0].entry = 0;
815 return TDB_ERR_NOEXIST;
818 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
823 enum TDB_ERROR first_in_hash(struct tdb_context *tdb,
824 struct traverse_info *tinfo,
825 TDB_DATA *kbuf, size_t *dlen)
828 tinfo->toplevel_group = 0;
829 tinfo->num_levels = 1;
830 tinfo->levels[0].hashtable = offsetof(struct tdb_header, hashtable);
831 tinfo->levels[0].entry = 0;
832 tinfo->levels[0].total_buckets = (1 << TDB_HASH_GROUP_BITS);
834 return next_in_hash(tdb, tinfo, kbuf, dlen);
837 /* Even if the entry isn't in this hash bucket, you'd have to lock this
838 * bucket to find it. */
839 static enum TDB_ERROR chainlock(struct tdb_context *tdb, const TDB_DATA *key,
840 int ltype, enum tdb_lock_flags waitflag,
843 enum TDB_ERROR ecode;
844 uint64_t h = tdb_hash(tdb, key->dptr, key->dsize);
845 tdb_off_t lockstart, locksize;
846 unsigned int group, gbits;
848 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
849 group = bits_from(h, 64 - gbits, gbits);
851 lockstart = hlock_range(group, &locksize);
853 ecode = tdb_lock_hashes(tdb, lockstart, locksize, ltype, waitflag);
854 tdb_trace_1rec(tdb, func, *key);
858 /* lock/unlock one hash chain. This is meant to be used to reduce
859 contention - it cannot guarantee how many records will be locked */
860 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
862 tdb->ecode = chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT,
864 if (tdb->ecode == TDB_SUCCESS)
870 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
872 uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
873 tdb_off_t lockstart, locksize;
874 unsigned int group, gbits;
875 enum TDB_ERROR ecode;
877 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
878 group = bits_from(h, 64 - gbits, gbits);
880 lockstart = hlock_range(group, &locksize);
882 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
883 ecode = tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK);
884 if (ecode != TDB_SUCCESS) {