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)) {
55 klen = rec_key_length(r);
56 tdb_access_release(tdb, r);
58 key = tdb_access_read(tdb, off + sizeof(*r), klen, false);
59 if (TDB_PTR_IS_ERR(key)) {
63 hash = tdb_hash(tdb, key, klen);
64 tdb_access_release(tdb, key);
68 /* Get bits from a value. */
69 static uint32_t bits_from(uint64_t val, unsigned start, unsigned num)
72 return (val >> start) & ((1U << num) - 1);
75 /* We take bits from the top: that way we can lock whole sections of the hash
76 * by using lock ranges. */
77 static uint32_t use_bits(struct hash_info *h, unsigned num)
80 return bits_from(h->h, 64 - h->hash_used, num);
83 static tdb_bool_err key_matches(struct tdb_context *tdb,
84 const struct tdb_used_record *rec,
86 const struct tdb_data *key)
88 tdb_bool_err ret = false;
91 if (rec_key_length(rec) != key->dsize) {
92 add_stat(tdb, compare_wrong_keylen, 1);
96 rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false);
97 if (TDB_PTR_IS_ERR(rkey)) {
98 return TDB_PTR_ERR(rkey);
100 if (memcmp(rkey, key->dptr, key->dsize) == 0)
103 add_stat(tdb, compare_wrong_keycmp, 1);
104 tdb_access_release(tdb, rkey);
108 /* Does entry match? */
109 static tdb_bool_err match(struct tdb_context *tdb,
111 const struct tdb_data *key,
113 struct tdb_used_record *rec)
116 enum TDB_ERROR ecode;
118 add_stat(tdb, compares, 1);
119 /* Desired bucket must match. */
120 if (h->home_bucket != (val & TDB_OFF_HASH_GROUP_MASK)) {
121 add_stat(tdb, compare_wrong_bucket, 1);
125 /* Top bits of offset == next bits of hash. */
126 if (bits_from(val, TDB_OFF_HASH_EXTRA_BIT, TDB_OFF_UPPER_STEAL_EXTRA)
127 != bits_from(h->h, 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
128 TDB_OFF_UPPER_STEAL_EXTRA)) {
129 add_stat(tdb, compare_wrong_offsetbits, 1);
133 off = val & TDB_OFF_MASK;
134 ecode = tdb_read_convert(tdb, off, rec, sizeof(*rec));
135 if (ecode != TDB_SUCCESS) {
139 if ((h->h & ((1 << 11)-1)) != rec_hash(rec)) {
140 add_stat(tdb, compare_wrong_rechash, 1);
144 return key_matches(tdb, rec, off, key);
147 static tdb_off_t hbucket_off(tdb_off_t group_start, unsigned bucket)
150 + (bucket % (1 << TDB_HASH_GROUP_BITS)) * sizeof(tdb_off_t);
153 bool is_subhash(tdb_off_t val)
155 return (val >> TDB_OFF_UPPER_STEAL_SUBHASH_BIT) & 1;
158 /* FIXME: Guess the depth, don't over-lock! */
159 static tdb_off_t hlock_range(tdb_off_t group, tdb_off_t *size)
161 *size = 1ULL << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
162 return group << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
165 static tdb_off_t COLD find_in_chain(struct tdb_context *tdb,
169 struct tdb_used_record *rec,
170 struct traverse_info *tinfo)
173 enum TDB_ERROR ecode;
175 /* In case nothing is free, we set these to zero. */
176 h->home_bucket = h->found_bucket = 0;
178 for (off = chain; off; off = next) {
181 h->group_start = off;
182 ecode = tdb_read_convert(tdb, off, h->group, sizeof(h->group));
183 if (ecode != TDB_SUCCESS) {
187 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
190 /* Remember this empty bucket. */
191 h->home_bucket = h->found_bucket = i;
195 /* We can insert extra bits via add_to_hash
196 * empty bucket logic. */
197 recoff = h->group[i] & TDB_OFF_MASK;
198 ecode = tdb_read_convert(tdb, recoff, rec,
200 if (ecode != TDB_SUCCESS) {
204 ecode = key_matches(tdb, rec, recoff, &key);
209 h->home_bucket = h->found_bucket = i;
212 tinfo->levels[tinfo->num_levels]
214 tinfo->levels[tinfo->num_levels]
216 = 1 << TDB_HASH_GROUP_BITS;
217 tinfo->levels[tinfo->num_levels].entry
224 next = tdb_read_off(tdb, off
225 + offsetof(struct tdb_chain, next));
226 if (TDB_OFF_IS_ERR(next)) {
230 next += sizeof(struct tdb_used_record);
235 /* This is the core routine which searches the hashtable for an entry.
236 * On error, no locks are held and -ve is returned.
237 * Otherwise, hinfo is filled in (and the optional tinfo).
238 * If not found, the return value is 0.
239 * If found, the return value is the offset, and *rec is the record. */
240 tdb_off_t find_and_lock(struct tdb_context *tdb,
244 struct tdb_used_record *rec,
245 struct traverse_info *tinfo)
249 enum TDB_ERROR ecode;
251 h->h = tdb_hash(tdb, key.dptr, key.dsize);
253 group = use_bits(h, TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
254 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
256 h->hlock_start = hlock_range(group, &h->hlock_range);
257 ecode = tdb_lock_hashes(tdb, h->hlock_start, h->hlock_range, ltype,
259 if (ecode != TDB_SUCCESS) {
263 hashtable = offsetof(struct tdb_header, hashtable);
265 tinfo->toplevel_group = group;
266 tinfo->num_levels = 1;
267 tinfo->levels[0].entry = 0;
268 tinfo->levels[0].hashtable = hashtable
269 + (group << TDB_HASH_GROUP_BITS) * sizeof(tdb_off_t);
270 tinfo->levels[0].total_buckets = 1 << TDB_HASH_GROUP_BITS;
273 while (h->hash_used <= 64) {
274 /* Read in the hash group. */
275 h->group_start = hashtable
276 + group * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
278 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
280 if (ecode != TDB_SUCCESS) {
284 /* Pointer to another hash table? Go down... */
285 if (is_subhash(h->group[h->home_bucket])) {
286 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
287 + sizeof(struct tdb_used_record);
289 /* When we come back, use *next* bucket */
290 tinfo->levels[tinfo->num_levels-1].entry
291 += h->home_bucket + 1;
293 group = use_bits(h, TDB_SUBLEVEL_HASH_BITS
294 - TDB_HASH_GROUP_BITS);
295 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
297 tinfo->levels[tinfo->num_levels].hashtable
299 tinfo->levels[tinfo->num_levels].total_buckets
300 = 1 << TDB_SUBLEVEL_HASH_BITS;
301 tinfo->levels[tinfo->num_levels].entry
302 = group << TDB_HASH_GROUP_BITS;
308 /* It's in this group: search (until 0 or all searched) */
309 for (i = 0, h->found_bucket = h->home_bucket;
310 i < (1 << TDB_HASH_GROUP_BITS);
311 i++, h->found_bucket = ((h->found_bucket+1)
312 % (1 << TDB_HASH_GROUP_BITS))) {
314 if (is_subhash(h->group[h->found_bucket]))
317 if (!h->group[h->found_bucket])
320 berr = match(tdb, h, &key, h->group[h->found_bucket],
328 tinfo->levels[tinfo->num_levels-1].entry
331 return h->group[h->found_bucket] & TDB_OFF_MASK;
334 /* Didn't find it: h indicates where it would go. */
338 return find_in_chain(tdb, key, hashtable, h, rec, tinfo);
341 tdb_unlock_hashes(tdb, h->hlock_start, h->hlock_range, ltype);
345 /* I wrote a simple test, expanding a hash to 2GB, for the following
347 * 1) Expanding all the buckets at once,
348 * 2) Expanding the bucket we wanted to place the new entry into.
349 * 3) Expanding the most-populated bucket,
351 * I measured the worst/average/best density during this process.
356 * So we figure out the busiest bucket for the moment.
358 static unsigned fullest_bucket(struct tdb_context *tdb,
359 const tdb_off_t *group,
362 unsigned counts[1 << TDB_HASH_GROUP_BITS] = { 0 };
363 unsigned int i, best_bucket;
365 /* Count the new entry. */
366 counts[new_bucket]++;
367 best_bucket = new_bucket;
369 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
370 unsigned this_bucket;
372 if (is_subhash(group[i]))
374 this_bucket = group[i] & TDB_OFF_HASH_GROUP_MASK;
375 if (++counts[this_bucket] > counts[best_bucket])
376 best_bucket = this_bucket;
382 static bool put_into_group(tdb_off_t *group,
383 unsigned bucket, tdb_off_t encoded)
387 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
388 unsigned b = (bucket + i) % (1 << TDB_HASH_GROUP_BITS);
398 static void force_into_group(tdb_off_t *group,
399 unsigned bucket, tdb_off_t encoded)
401 if (!put_into_group(group, bucket, encoded))
405 static tdb_off_t encode_offset(tdb_off_t new_off, struct hash_info *h)
407 return h->home_bucket
409 | ((uint64_t)bits_from(h->h,
410 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
411 TDB_OFF_UPPER_STEAL_EXTRA)
412 << TDB_OFF_HASH_EXTRA_BIT);
415 /* Simply overwrite the hash entry we found before. */
416 enum TDB_ERROR replace_in_hash(struct tdb_context *tdb,
420 return tdb_write_off(tdb, hbucket_off(h->group_start, h->found_bucket),
421 encode_offset(new_off, h));
424 /* We slot in anywhere that's empty in the chain. */
425 static enum TDB_ERROR COLD add_to_chain(struct tdb_context *tdb,
430 enum TDB_ERROR ecode;
432 entry = tdb_find_zero_off(tdb, subhash, 1<<TDB_HASH_GROUP_BITS);
433 if (TDB_OFF_IS_ERR(entry)) {
437 if (entry == 1 << TDB_HASH_GROUP_BITS) {
440 next = tdb_read_off(tdb, subhash
441 + offsetof(struct tdb_chain, next));
442 if (TDB_OFF_IS_ERR(next)) {
447 next = alloc(tdb, 0, sizeof(struct tdb_chain), 0,
448 TDB_CHAIN_MAGIC, false);
449 if (TDB_OFF_IS_ERR(next))
451 ecode = zero_out(tdb,
452 next+sizeof(struct tdb_used_record),
453 sizeof(struct tdb_chain));
454 if (ecode != TDB_SUCCESS) {
457 ecode = tdb_write_off(tdb, subhash
458 + offsetof(struct tdb_chain,
461 if (ecode != TDB_SUCCESS) {
465 return add_to_chain(tdb, next, new_off);
468 return tdb_write_off(tdb, subhash + entry * sizeof(tdb_off_t),
472 /* Add into a newly created subhash. */
473 static enum TDB_ERROR add_to_subhash(struct tdb_context *tdb, tdb_off_t subhash,
474 unsigned hash_used, tdb_off_t val)
476 tdb_off_t off = (val & TDB_OFF_MASK), *group;
480 h.hash_used = hash_used;
482 if (hash_used + TDB_SUBLEVEL_HASH_BITS > 64)
483 return add_to_chain(tdb, subhash, off);
485 h.h = hash_record(tdb, off);
486 gnum = use_bits(&h, TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS);
487 h.group_start = subhash
488 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
489 h.home_bucket = use_bits(&h, TDB_HASH_GROUP_BITS);
491 group = tdb_access_write(tdb, h.group_start,
492 sizeof(*group) << TDB_HASH_GROUP_BITS, true);
493 if (TDB_PTR_IS_ERR(group)) {
494 return TDB_PTR_ERR(group);
496 force_into_group(group, h.home_bucket, encode_offset(off, &h));
497 return tdb_access_commit(tdb, group);
500 static enum TDB_ERROR expand_group(struct tdb_context *tdb, struct hash_info *h)
502 unsigned bucket, num_vals, i, magic;
505 tdb_off_t vals[1 << TDB_HASH_GROUP_BITS];
506 enum TDB_ERROR ecode;
508 /* Attach new empty subhash under fullest bucket. */
509 bucket = fullest_bucket(tdb, h->group, h->home_bucket);
511 if (h->hash_used == 64) {
512 add_stat(tdb, alloc_chain, 1);
513 subsize = sizeof(struct tdb_chain);
514 magic = TDB_CHAIN_MAGIC;
516 add_stat(tdb, alloc_subhash, 1);
517 subsize = (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS);
518 magic = TDB_HTABLE_MAGIC;
521 subhash = alloc(tdb, 0, subsize, 0, magic, false);
522 if (TDB_OFF_IS_ERR(subhash)) {
526 ecode = zero_out(tdb, subhash + sizeof(struct tdb_used_record),
528 if (ecode != TDB_SUCCESS) {
532 /* Remove any which are destined for bucket or are in wrong place. */
534 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
535 unsigned home_bucket = h->group[i] & TDB_OFF_HASH_GROUP_MASK;
536 if (!h->group[i] || is_subhash(h->group[i]))
538 if (home_bucket == bucket || home_bucket != i) {
539 vals[num_vals++] = h->group[i];
543 /* FIXME: This assert is valid, but we do this during unit test :( */
544 /* assert(num_vals); */
546 /* Overwrite expanded bucket with subhash pointer. */
547 h->group[bucket] = subhash | (1ULL << TDB_OFF_UPPER_STEAL_SUBHASH_BIT);
549 /* Point to actual contents of record. */
550 subhash += sizeof(struct tdb_used_record);
552 /* Put values back. */
553 for (i = 0; i < num_vals; i++) {
554 unsigned this_bucket = vals[i] & TDB_OFF_HASH_GROUP_MASK;
556 if (this_bucket == bucket) {
557 ecode = add_to_subhash(tdb, subhash, h->hash_used,
559 if (ecode != TDB_SUCCESS)
562 /* There should be room to put this back. */
563 force_into_group(h->group, this_bucket, vals[i]);
569 enum TDB_ERROR delete_from_hash(struct tdb_context *tdb, struct hash_info *h)
571 unsigned int i, num_movers = 0;
572 tdb_off_t movers[1 << TDB_HASH_GROUP_BITS];
574 h->group[h->found_bucket] = 0;
575 for (i = 1; i < (1 << TDB_HASH_GROUP_BITS); i++) {
576 unsigned this_bucket;
578 this_bucket = (h->found_bucket+i) % (1 << TDB_HASH_GROUP_BITS);
579 /* Empty bucket? We're done. */
580 if (!h->group[this_bucket])
583 /* Ignore subhashes. */
584 if (is_subhash(h->group[this_bucket]))
587 /* If this one is not happy where it is, we'll move it. */
588 if ((h->group[this_bucket] & TDB_OFF_HASH_GROUP_MASK)
590 movers[num_movers++] = h->group[this_bucket];
591 h->group[this_bucket] = 0;
595 /* Put back the ones we erased. */
596 for (i = 0; i < num_movers; i++) {
597 force_into_group(h->group, movers[i] & TDB_OFF_HASH_GROUP_MASK,
601 /* Now we write back the hash group */
602 return tdb_write_convert(tdb, h->group_start,
603 h->group, sizeof(h->group));
606 enum TDB_ERROR add_to_hash(struct tdb_context *tdb, struct hash_info *h,
609 enum TDB_ERROR ecode;
611 /* We hit an empty bucket during search? That's where it goes. */
612 if (!h->group[h->found_bucket]) {
613 h->group[h->found_bucket] = encode_offset(new_off, h);
614 /* Write back the modified group. */
615 return tdb_write_convert(tdb, h->group_start,
616 h->group, sizeof(h->group));
619 if (h->hash_used > 64)
620 return add_to_chain(tdb, h->group_start, new_off);
622 /* We're full. Expand. */
623 ecode = expand_group(tdb, h);
624 if (ecode != TDB_SUCCESS) {
628 if (is_subhash(h->group[h->home_bucket])) {
629 /* We were expanded! */
633 /* Write back the modified group. */
634 ecode = tdb_write_convert(tdb, h->group_start, h->group,
636 if (ecode != TDB_SUCCESS) {
640 /* Move hashinfo down a level. */
641 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
642 + sizeof(struct tdb_used_record);
643 gnum = use_bits(h,TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
644 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
645 h->group_start = hashtable
646 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
647 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
649 if (ecode != TDB_SUCCESS) {
654 /* Expanding the group must have made room if it didn't choose this
656 if (put_into_group(h->group, h->home_bucket, encode_offset(new_off,h))){
657 return tdb_write_convert(tdb, h->group_start,
658 h->group, sizeof(h->group));
661 /* This can happen if all hashes in group (and us) dropped into same
662 * group in subhash. */
663 return add_to_hash(tdb, h, new_off);
666 /* Traverse support: returns offset of record, or 0 or -ve error. */
667 static tdb_off_t iterate_hash(struct tdb_context *tdb,
668 struct traverse_info *tinfo)
670 tdb_off_t off, val, i;
671 struct traverse_level *tlevel;
673 tlevel = &tinfo->levels[tinfo->num_levels-1];
676 for (i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
677 tlevel->entry, tlevel->total_buckets);
678 i != tlevel->total_buckets;
679 i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
680 i+1, tlevel->total_buckets)) {
681 if (TDB_OFF_IS_ERR(i)) {
685 val = tdb_read_off(tdb, tlevel->hashtable+sizeof(tdb_off_t)*i);
686 if (TDB_OFF_IS_ERR(val)) {
690 off = val & TDB_OFF_MASK;
692 /* This makes the delete-all-in-traverse case work
693 * (and simplifies our logic a little). */
694 if (off == tinfo->prev)
699 if (!is_subhash(val)) {
705 /* When we come back, we want the next one */
709 tlevel->hashtable = off + sizeof(struct tdb_used_record);
711 /* Next level is a chain? */
712 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1))
713 tlevel->total_buckets = (1 << TDB_HASH_GROUP_BITS);
715 tlevel->total_buckets = (1 << TDB_SUBLEVEL_HASH_BITS);
720 if (tinfo->num_levels == 1)
723 /* Handle chained entries. */
724 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1)) {
725 tlevel->hashtable = tdb_read_off(tdb, tlevel->hashtable
726 + offsetof(struct tdb_chain,
728 if (TDB_OFF_IS_ERR(tlevel->hashtable)) {
729 return tlevel->hashtable;
731 if (tlevel->hashtable) {
732 tlevel->hashtable += sizeof(struct tdb_used_record);
738 /* Go back up and keep searching. */
744 /* Return success if we find something, TDB_ERR_NOEXIST if none. */
745 enum TDB_ERROR next_in_hash(struct tdb_context *tdb,
746 struct traverse_info *tinfo,
747 TDB_DATA *kbuf, size_t *dlen)
749 const unsigned group_bits = TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS;
750 tdb_off_t hl_start, hl_range, off;
751 enum TDB_ERROR ecode;
753 while (tinfo->toplevel_group < (1 << group_bits)) {
754 hl_start = (tdb_off_t)tinfo->toplevel_group
755 << (64 - group_bits);
756 hl_range = 1ULL << group_bits;
757 ecode = tdb_lock_hashes(tdb, hl_start, hl_range, F_RDLCK,
759 if (ecode != TDB_SUCCESS) {
763 off = iterate_hash(tdb, tinfo);
765 struct tdb_used_record rec;
767 if (TDB_OFF_IS_ERR(off)) {
772 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
773 if (ecode != TDB_SUCCESS) {
776 if (rec_magic(&rec) != TDB_USED_MAGIC) {
777 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
780 " corrupt record at %llu",
785 kbuf->dsize = rec_key_length(&rec);
787 /* They want data as well? */
789 *dlen = rec_data_length(&rec);
790 kbuf->dptr = tdb_alloc_read(tdb,
795 kbuf->dptr = tdb_alloc_read(tdb,
799 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
800 if (TDB_PTR_IS_ERR(kbuf->dptr)) {
801 return TDB_PTR_ERR(kbuf->dptr);
806 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
808 tinfo->toplevel_group++;
809 tinfo->levels[0].hashtable
810 += (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
811 tinfo->levels[0].entry = 0;
813 return TDB_ERR_NOEXIST;
816 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
821 enum TDB_ERROR first_in_hash(struct tdb_context *tdb,
822 struct traverse_info *tinfo,
823 TDB_DATA *kbuf, size_t *dlen)
826 tinfo->toplevel_group = 0;
827 tinfo->num_levels = 1;
828 tinfo->levels[0].hashtable = offsetof(struct tdb_header, hashtable);
829 tinfo->levels[0].entry = 0;
830 tinfo->levels[0].total_buckets = (1 << TDB_HASH_GROUP_BITS);
832 return next_in_hash(tdb, tinfo, kbuf, dlen);
835 /* Even if the entry isn't in this hash bucket, you'd have to lock this
836 * bucket to find it. */
837 static enum TDB_ERROR chainlock(struct tdb_context *tdb, const TDB_DATA *key,
838 int ltype, enum tdb_lock_flags waitflag,
841 enum TDB_ERROR ecode;
842 uint64_t h = tdb_hash(tdb, key->dptr, key->dsize);
843 tdb_off_t lockstart, locksize;
844 unsigned int group, gbits;
846 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
847 group = bits_from(h, 64 - gbits, gbits);
849 lockstart = hlock_range(group, &locksize);
851 ecode = tdb_lock_hashes(tdb, lockstart, locksize, ltype, waitflag);
852 tdb_trace_1rec(tdb, func, *key);
856 /* lock/unlock one hash chain. This is meant to be used to reduce
857 contention - it cannot guarantee how many records will be locked */
858 enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
860 return tdb->last_error = chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT,
864 enum TDB_ERROR tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
866 uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
867 tdb_off_t lockstart, locksize;
868 unsigned int group, gbits;
870 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
871 group = bits_from(h, 64 - gbits, gbits);
873 lockstart = hlock_range(group, &locksize);
875 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
876 return tdb->last_error = tdb_unlock_hashes(tdb, lockstart, locksize,