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->hash_fn = jenkins_hash;
37 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len)
39 return tdb->hash_fn(ptr, len, tdb->hash_seed, tdb->hash_data);
42 uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off)
44 const struct tdb_used_record *r;
48 r = tdb_access_read(tdb, off, sizeof(*r), true);
49 if (TDB_PTR_IS_ERR(r)) {
54 klen = rec_key_length(r);
55 tdb_access_release(tdb, r);
57 key = tdb_access_read(tdb, off + sizeof(*r), klen, false);
58 if (TDB_PTR_IS_ERR(key)) {
62 hash = tdb_hash(tdb, key, klen);
63 tdb_access_release(tdb, key);
67 /* Get bits from a value. */
68 static uint32_t bits_from(uint64_t val, unsigned start, unsigned num)
71 return (val >> start) & ((1U << num) - 1);
74 /* We take bits from the top: that way we can lock whole sections of the hash
75 * by using lock ranges. */
76 static uint32_t use_bits(struct hash_info *h, unsigned num)
79 return bits_from(h->h, 64 - h->hash_used, num);
82 static tdb_bool_err key_matches(struct tdb_context *tdb,
83 const struct tdb_used_record *rec,
85 const struct tdb_data *key)
87 tdb_bool_err ret = false;
90 if (rec_key_length(rec) != key->dsize) {
91 add_stat(tdb, compare_wrong_keylen, 1);
95 rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false);
96 if (TDB_PTR_IS_ERR(rkey)) {
97 return TDB_PTR_ERR(rkey);
99 if (memcmp(rkey, key->dptr, key->dsize) == 0)
102 add_stat(tdb, compare_wrong_keycmp, 1);
103 tdb_access_release(tdb, rkey);
107 /* Does entry match? */
108 static tdb_bool_err match(struct tdb_context *tdb,
110 const struct tdb_data *key,
112 struct tdb_used_record *rec)
115 enum TDB_ERROR ecode;
117 add_stat(tdb, compares, 1);
118 /* Desired bucket must match. */
119 if (h->home_bucket != (val & TDB_OFF_HASH_GROUP_MASK)) {
120 add_stat(tdb, compare_wrong_bucket, 1);
124 /* Top bits of offset == next bits of hash. */
125 if (bits_from(val, TDB_OFF_HASH_EXTRA_BIT, TDB_OFF_UPPER_STEAL_EXTRA)
126 != bits_from(h->h, 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
127 TDB_OFF_UPPER_STEAL_EXTRA)) {
128 add_stat(tdb, compare_wrong_offsetbits, 1);
132 off = val & TDB_OFF_MASK;
133 ecode = tdb_read_convert(tdb, off, rec, sizeof(*rec));
134 if (ecode != TDB_SUCCESS) {
138 if ((h->h & ((1 << 11)-1)) != rec_hash(rec)) {
139 add_stat(tdb, compare_wrong_rechash, 1);
143 return key_matches(tdb, rec, off, key);
146 static tdb_off_t hbucket_off(tdb_off_t group_start, unsigned bucket)
149 + (bucket % (1 << TDB_HASH_GROUP_BITS)) * sizeof(tdb_off_t);
152 bool is_subhash(tdb_off_t val)
154 return (val >> TDB_OFF_UPPER_STEAL_SUBHASH_BIT) & 1;
157 /* FIXME: Guess the depth, don't over-lock! */
158 static tdb_off_t hlock_range(tdb_off_t group, tdb_off_t *size)
160 *size = 1ULL << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
161 return group << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
164 static tdb_off_t COLD find_in_chain(struct tdb_context *tdb,
168 struct tdb_used_record *rec,
169 struct traverse_info *tinfo)
172 enum TDB_ERROR ecode;
174 /* In case nothing is free, we set these to zero. */
175 h->home_bucket = h->found_bucket = 0;
177 for (off = chain; off; off = next) {
180 h->group_start = off;
181 ecode = tdb_read_convert(tdb, off, h->group, sizeof(h->group));
182 if (ecode != TDB_SUCCESS) {
186 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
189 /* Remember this empty bucket. */
190 h->home_bucket = h->found_bucket = i;
194 /* We can insert extra bits via add_to_hash
195 * empty bucket logic. */
196 recoff = h->group[i] & TDB_OFF_MASK;
197 ecode = tdb_read_convert(tdb, recoff, rec,
199 if (ecode != TDB_SUCCESS) {
203 ecode = key_matches(tdb, rec, recoff, &key);
208 h->home_bucket = h->found_bucket = i;
211 tinfo->levels[tinfo->num_levels]
213 tinfo->levels[tinfo->num_levels]
215 = 1 << TDB_HASH_GROUP_BITS;
216 tinfo->levels[tinfo->num_levels].entry
223 next = tdb_read_off(tdb, off
224 + offsetof(struct tdb_chain, next));
225 if (TDB_OFF_IS_ERR(next)) {
229 next += sizeof(struct tdb_used_record);
234 /* This is the core routine which searches the hashtable for an entry.
235 * On error, no locks are held and -ve is returned.
236 * Otherwise, hinfo is filled in (and the optional tinfo).
237 * If not found, the return value is 0.
238 * If found, the return value is the offset, and *rec is the record. */
239 tdb_off_t find_and_lock(struct tdb_context *tdb,
243 struct tdb_used_record *rec,
244 struct traverse_info *tinfo)
248 enum TDB_ERROR ecode;
250 h->h = tdb_hash(tdb, key.dptr, key.dsize);
252 group = use_bits(h, TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
253 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
255 h->hlock_start = hlock_range(group, &h->hlock_range);
256 ecode = tdb_lock_hashes(tdb, h->hlock_start, h->hlock_range, ltype,
258 if (ecode != TDB_SUCCESS) {
262 hashtable = offsetof(struct tdb_header, hashtable);
264 tinfo->toplevel_group = group;
265 tinfo->num_levels = 1;
266 tinfo->levels[0].entry = 0;
267 tinfo->levels[0].hashtable = hashtable
268 + (group << TDB_HASH_GROUP_BITS) * sizeof(tdb_off_t);
269 tinfo->levels[0].total_buckets = 1 << TDB_HASH_GROUP_BITS;
272 while (h->hash_used <= 64) {
273 /* Read in the hash group. */
274 h->group_start = hashtable
275 + group * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
277 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
279 if (ecode != TDB_SUCCESS) {
283 /* Pointer to another hash table? Go down... */
284 if (is_subhash(h->group[h->home_bucket])) {
285 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
286 + sizeof(struct tdb_used_record);
288 /* When we come back, use *next* bucket */
289 tinfo->levels[tinfo->num_levels-1].entry
290 += h->home_bucket + 1;
292 group = use_bits(h, TDB_SUBLEVEL_HASH_BITS
293 - TDB_HASH_GROUP_BITS);
294 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
296 tinfo->levels[tinfo->num_levels].hashtable
298 tinfo->levels[tinfo->num_levels].total_buckets
299 = 1 << TDB_SUBLEVEL_HASH_BITS;
300 tinfo->levels[tinfo->num_levels].entry
301 = group << TDB_HASH_GROUP_BITS;
307 /* It's in this group: search (until 0 or all searched) */
308 for (i = 0, h->found_bucket = h->home_bucket;
309 i < (1 << TDB_HASH_GROUP_BITS);
310 i++, h->found_bucket = ((h->found_bucket+1)
311 % (1 << TDB_HASH_GROUP_BITS))) {
313 if (is_subhash(h->group[h->found_bucket]))
316 if (!h->group[h->found_bucket])
319 berr = 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 enum TDB_ERROR replace_in_hash(struct tdb_context *tdb,
419 return tdb_write_off(tdb, hbucket_off(h->group_start, h->found_bucket),
420 encode_offset(new_off, h));
423 /* We slot in anywhere that's empty in the chain. */
424 static enum TDB_ERROR COLD add_to_chain(struct tdb_context *tdb,
429 enum TDB_ERROR ecode;
431 entry = tdb_find_zero_off(tdb, subhash, 1<<TDB_HASH_GROUP_BITS);
432 if (TDB_OFF_IS_ERR(entry)) {
436 if (entry == 1 << TDB_HASH_GROUP_BITS) {
439 next = tdb_read_off(tdb, subhash
440 + offsetof(struct tdb_chain, next));
441 if (TDB_OFF_IS_ERR(next)) {
446 next = alloc(tdb, 0, sizeof(struct tdb_chain), 0,
447 TDB_CHAIN_MAGIC, false);
448 if (TDB_OFF_IS_ERR(next))
450 ecode = zero_out(tdb,
451 next+sizeof(struct tdb_used_record),
452 sizeof(struct tdb_chain));
453 if (ecode != TDB_SUCCESS) {
456 ecode = tdb_write_off(tdb, subhash
457 + offsetof(struct tdb_chain,
460 if (ecode != TDB_SUCCESS) {
464 return add_to_chain(tdb, next, new_off);
467 return tdb_write_off(tdb, subhash + entry * sizeof(tdb_off_t),
471 /* Add into a newly created subhash. */
472 static enum TDB_ERROR add_to_subhash(struct tdb_context *tdb, tdb_off_t subhash,
473 unsigned hash_used, tdb_off_t val)
475 tdb_off_t off = (val & TDB_OFF_MASK), *group;
479 h.hash_used = hash_used;
481 if (hash_used + TDB_SUBLEVEL_HASH_BITS > 64)
482 return add_to_chain(tdb, subhash, off);
484 h.h = hash_record(tdb, off);
485 gnum = use_bits(&h, TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS);
486 h.group_start = subhash
487 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
488 h.home_bucket = use_bits(&h, TDB_HASH_GROUP_BITS);
490 group = tdb_access_write(tdb, h.group_start,
491 sizeof(*group) << TDB_HASH_GROUP_BITS, true);
492 if (TDB_PTR_IS_ERR(group)) {
493 return TDB_PTR_ERR(group);
495 force_into_group(group, h.home_bucket, encode_offset(off, &h));
496 return tdb_access_commit(tdb, group);
499 static enum TDB_ERROR expand_group(struct tdb_context *tdb, struct hash_info *h)
501 unsigned bucket, num_vals, i, magic;
504 tdb_off_t vals[1 << TDB_HASH_GROUP_BITS];
505 enum TDB_ERROR ecode;
507 /* Attach new empty subhash under fullest bucket. */
508 bucket = fullest_bucket(tdb, h->group, h->home_bucket);
510 if (h->hash_used == 64) {
511 add_stat(tdb, alloc_chain, 1);
512 subsize = sizeof(struct tdb_chain);
513 magic = TDB_CHAIN_MAGIC;
515 add_stat(tdb, alloc_subhash, 1);
516 subsize = (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS);
517 magic = TDB_HTABLE_MAGIC;
520 subhash = alloc(tdb, 0, subsize, 0, magic, false);
521 if (TDB_OFF_IS_ERR(subhash)) {
525 ecode = zero_out(tdb, subhash + sizeof(struct tdb_used_record),
527 if (ecode != TDB_SUCCESS) {
531 /* Remove any which are destined for bucket or are in wrong place. */
533 for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
534 unsigned home_bucket = h->group[i] & TDB_OFF_HASH_GROUP_MASK;
535 if (!h->group[i] || is_subhash(h->group[i]))
537 if (home_bucket == bucket || home_bucket != i) {
538 vals[num_vals++] = h->group[i];
542 /* FIXME: This assert is valid, but we do this during unit test :( */
543 /* assert(num_vals); */
545 /* Overwrite expanded bucket with subhash pointer. */
546 h->group[bucket] = subhash | (1ULL << TDB_OFF_UPPER_STEAL_SUBHASH_BIT);
548 /* Point to actual contents of record. */
549 subhash += sizeof(struct tdb_used_record);
551 /* Put values back. */
552 for (i = 0; i < num_vals; i++) {
553 unsigned this_bucket = vals[i] & TDB_OFF_HASH_GROUP_MASK;
555 if (this_bucket == bucket) {
556 ecode = add_to_subhash(tdb, subhash, h->hash_used,
558 if (ecode != TDB_SUCCESS)
561 /* There should be room to put this back. */
562 force_into_group(h->group, this_bucket, vals[i]);
568 enum TDB_ERROR delete_from_hash(struct tdb_context *tdb, struct hash_info *h)
570 unsigned int i, num_movers = 0;
571 tdb_off_t movers[1 << TDB_HASH_GROUP_BITS];
573 h->group[h->found_bucket] = 0;
574 for (i = 1; i < (1 << TDB_HASH_GROUP_BITS); i++) {
575 unsigned this_bucket;
577 this_bucket = (h->found_bucket+i) % (1 << TDB_HASH_GROUP_BITS);
578 /* Empty bucket? We're done. */
579 if (!h->group[this_bucket])
582 /* Ignore subhashes. */
583 if (is_subhash(h->group[this_bucket]))
586 /* If this one is not happy where it is, we'll move it. */
587 if ((h->group[this_bucket] & TDB_OFF_HASH_GROUP_MASK)
589 movers[num_movers++] = h->group[this_bucket];
590 h->group[this_bucket] = 0;
594 /* Put back the ones we erased. */
595 for (i = 0; i < num_movers; i++) {
596 force_into_group(h->group, movers[i] & TDB_OFF_HASH_GROUP_MASK,
600 /* Now we write back the hash group */
601 return tdb_write_convert(tdb, h->group_start,
602 h->group, sizeof(h->group));
605 enum TDB_ERROR add_to_hash(struct tdb_context *tdb, struct hash_info *h,
608 enum TDB_ERROR ecode;
610 /* We hit an empty bucket during search? That's where it goes. */
611 if (!h->group[h->found_bucket]) {
612 h->group[h->found_bucket] = encode_offset(new_off, h);
613 /* Write back the modified group. */
614 return tdb_write_convert(tdb, h->group_start,
615 h->group, sizeof(h->group));
618 if (h->hash_used > 64)
619 return add_to_chain(tdb, h->group_start, new_off);
621 /* We're full. Expand. */
622 ecode = expand_group(tdb, h);
623 if (ecode != TDB_SUCCESS) {
627 if (is_subhash(h->group[h->home_bucket])) {
628 /* We were expanded! */
632 /* Write back the modified group. */
633 ecode = tdb_write_convert(tdb, h->group_start, h->group,
635 if (ecode != TDB_SUCCESS) {
639 /* Move hashinfo down a level. */
640 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
641 + sizeof(struct tdb_used_record);
642 gnum = use_bits(h,TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
643 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
644 h->group_start = hashtable
645 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
646 ecode = tdb_read_convert(tdb, h->group_start, &h->group,
648 if (ecode != TDB_SUCCESS) {
653 /* Expanding the group must have made room if it didn't choose this
655 if (put_into_group(h->group, h->home_bucket, encode_offset(new_off,h))){
656 return tdb_write_convert(tdb, h->group_start,
657 h->group, sizeof(h->group));
660 /* This can happen if all hashes in group (and us) dropped into same
661 * group in subhash. */
662 return add_to_hash(tdb, h, new_off);
665 /* Traverse support: returns offset of record, or 0 or -ve error. */
666 static tdb_off_t iterate_hash(struct tdb_context *tdb,
667 struct traverse_info *tinfo)
669 tdb_off_t off, val, i;
670 struct traverse_level *tlevel;
672 tlevel = &tinfo->levels[tinfo->num_levels-1];
675 for (i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
676 tlevel->entry, tlevel->total_buckets);
677 i != tlevel->total_buckets;
678 i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
679 i+1, tlevel->total_buckets)) {
680 if (TDB_OFF_IS_ERR(i)) {
684 val = tdb_read_off(tdb, tlevel->hashtable+sizeof(tdb_off_t)*i);
685 if (TDB_OFF_IS_ERR(val)) {
689 off = val & TDB_OFF_MASK;
691 /* This makes the delete-all-in-traverse case work
692 * (and simplifies our logic a little). */
693 if (off == tinfo->prev)
698 if (!is_subhash(val)) {
704 /* When we come back, we want the next one */
708 tlevel->hashtable = off + sizeof(struct tdb_used_record);
710 /* Next level is a chain? */
711 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1))
712 tlevel->total_buckets = (1 << TDB_HASH_GROUP_BITS);
714 tlevel->total_buckets = (1 << TDB_SUBLEVEL_HASH_BITS);
719 if (tinfo->num_levels == 1)
722 /* Handle chained entries. */
723 if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1)) {
724 tlevel->hashtable = tdb_read_off(tdb, tlevel->hashtable
725 + offsetof(struct tdb_chain,
727 if (TDB_OFF_IS_ERR(tlevel->hashtable)) {
728 return tlevel->hashtable;
730 if (tlevel->hashtable) {
731 tlevel->hashtable += sizeof(struct tdb_used_record);
737 /* Go back up and keep searching. */
743 /* Return success if we find something, TDB_ERR_NOEXIST if none. */
744 enum TDB_ERROR next_in_hash(struct tdb_context *tdb,
745 struct traverse_info *tinfo,
746 TDB_DATA *kbuf, size_t *dlen)
748 const unsigned group_bits = TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS;
749 tdb_off_t hl_start, hl_range, off;
750 enum TDB_ERROR ecode;
752 while (tinfo->toplevel_group < (1 << group_bits)) {
753 hl_start = (tdb_off_t)tinfo->toplevel_group
754 << (64 - group_bits);
755 hl_range = 1ULL << group_bits;
756 ecode = tdb_lock_hashes(tdb, hl_start, hl_range, F_RDLCK,
758 if (ecode != TDB_SUCCESS) {
762 off = iterate_hash(tdb, tinfo);
764 struct tdb_used_record rec;
766 if (TDB_OFF_IS_ERR(off)) {
771 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
772 if (ecode != TDB_SUCCESS) {
775 if (rec_magic(&rec) != TDB_USED_MAGIC) {
776 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
779 " corrupt record at %llu",
784 kbuf->dsize = rec_key_length(&rec);
786 /* They want data as well? */
788 *dlen = rec_data_length(&rec);
789 kbuf->dptr = tdb_alloc_read(tdb,
794 kbuf->dptr = tdb_alloc_read(tdb,
798 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
799 if (TDB_PTR_IS_ERR(kbuf->dptr)) {
800 return TDB_PTR_ERR(kbuf->dptr);
805 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
807 tinfo->toplevel_group++;
808 tinfo->levels[0].hashtable
809 += (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
810 tinfo->levels[0].entry = 0;
812 return TDB_ERR_NOEXIST;
815 tdb_unlock_hashes(tdb, hl_start, hl_range, F_RDLCK);
820 enum TDB_ERROR first_in_hash(struct tdb_context *tdb,
821 struct traverse_info *tinfo,
822 TDB_DATA *kbuf, size_t *dlen)
825 tinfo->toplevel_group = 0;
826 tinfo->num_levels = 1;
827 tinfo->levels[0].hashtable = offsetof(struct tdb_header, hashtable);
828 tinfo->levels[0].entry = 0;
829 tinfo->levels[0].total_buckets = (1 << TDB_HASH_GROUP_BITS);
831 return next_in_hash(tdb, tinfo, kbuf, dlen);
834 /* Even if the entry isn't in this hash bucket, you'd have to lock this
835 * bucket to find it. */
836 static enum TDB_ERROR chainlock(struct tdb_context *tdb, const TDB_DATA *key,
837 int ltype, enum tdb_lock_flags waitflag,
840 enum TDB_ERROR ecode;
841 uint64_t h = tdb_hash(tdb, key->dptr, key->dsize);
842 tdb_off_t lockstart, locksize;
843 unsigned int group, gbits;
845 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
846 group = bits_from(h, 64 - gbits, gbits);
848 lockstart = hlock_range(group, &locksize);
850 ecode = tdb_lock_hashes(tdb, lockstart, locksize, ltype, waitflag);
851 tdb_trace_1rec(tdb, func, *key);
855 /* lock/unlock one hash chain. This is meant to be used to reduce
856 contention - it cannot guarantee how many records will be locked */
857 enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
859 return tdb->last_error = chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT,
863 void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
865 uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
866 tdb_off_t lockstart, locksize;
867 unsigned int group, gbits;
869 gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
870 group = bits_from(h, 64 - gbits, gbits);
872 lockstart = hlock_range(group, &locksize);
874 tdb_trace_1rec(tdb, "tdb_chainunlock", key);
875 tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK);
878 enum TDB_ERROR tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
880 return tdb->last_error = chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT,
881 "tdb_chainlock_read");
884 void tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
886 uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
887 tdb_off_t lockstart, locksize;
888 unsigned int group, gbits;
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_read", key);
896 tdb_unlock_hashes(tdb, lockstart, locksize, F_RDLCK);