]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/hash.c
tdb2: fix tdb_chainlock
[ccan] / ccan / tdb2 / hash.c
1  /* 
2    Trivial Database 2: hash handling
3    Copyright (C) Rusty Russell 2010
4    
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.
9
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.
14
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/>.
17 */
18 #include "private.h"
19 #include <assert.h>
20 #include <ccan/hash/hash.h>
21
22 static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
23                              void *arg)
24 {
25         uint64_t ret;
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);
30 }
31
32 void tdb_hash_init(struct tdb_context *tdb)
33 {
34         tdb->khash = jenkins_hash;
35         tdb->hash_priv = NULL;
36 }
37
38 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len)
39 {
40         return tdb->khash(ptr, len, tdb->hash_seed, tdb->hash_priv);
41 }
42
43 uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off)
44 {
45         struct tdb_used_record pad, *r;
46         const void *key;
47         uint64_t klen, hash;
48
49         r = tdb_get(tdb, off, &pad, sizeof(pad));
50         if (!r)
51                 /* FIXME */
52                 return 0;
53
54         klen = rec_key_length(r);
55         key = tdb_access_read(tdb, off + sizeof(pad), klen, false);
56         if (!key)
57                 return 0;
58
59         hash = tdb_hash(tdb, key, klen);
60         tdb_access_release(tdb, key);
61         return hash;
62 }
63
64 /* Get bits from a value. */
65 static uint32_t bits(uint64_t val, unsigned start, unsigned num)
66 {
67         assert(num <= 32);
68         return (val >> start) & ((1U << num) - 1);
69 }
70
71 /* We take bits from the top: that way we can lock whole sections of the hash
72  * by using lock ranges. */
73 static uint32_t use_bits(struct hash_info *h, unsigned num)
74 {
75         h->hash_used += num;
76         return bits(h->h, 64 - h->hash_used, num);
77 }
78
79 /* Does entry match? */
80 static bool match(struct tdb_context *tdb,
81                   struct hash_info *h,
82                   const struct tdb_data *key,
83                   tdb_off_t val,
84                   struct tdb_used_record *rec)
85 {
86         bool ret;
87         const unsigned char *rkey;
88         tdb_off_t off;
89
90         /* FIXME: Handle hash value truncated. */
91         if (bits(val, TDB_OFF_HASH_TRUNCATED_BIT, 1))
92                 abort();
93
94         /* Desired bucket must match. */
95         if (h->home_bucket != (val & TDB_OFF_HASH_GROUP_MASK))
96                 return false;
97
98         /* Top bits of offset == next bits of hash. */
99         if (bits(val, TDB_OFF_HASH_EXTRA_BIT, TDB_OFF_UPPER_STEAL_EXTRA)
100             != bits(h->h, 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
101                     TDB_OFF_UPPER_STEAL_EXTRA))
102                 return false;
103
104         off = val & TDB_OFF_MASK;
105         if (tdb_read_convert(tdb, off, rec, sizeof(*rec)) == -1)
106                 return false;
107
108         /* FIXME: check extra bits in header? */
109         if (rec_key_length(rec) != key->dsize)
110                 return false;
111
112         rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false);
113         if (!rkey)
114                 return false;
115         ret = (memcmp(rkey, key->dptr, key->dsize) == 0);
116         tdb_access_release(tdb, rkey);
117         return ret;
118 }
119
120 static tdb_off_t hbucket_off(tdb_off_t group_start, unsigned bucket)
121 {
122         return group_start
123                 + (bucket % (1 << TDB_HASH_GROUP_BITS)) * sizeof(tdb_off_t);
124 }
125
126 /* Truncated hashes can't be all 1: that's how we spot a sub-hash */
127 bool is_subhash(tdb_off_t val)
128 {
129         return val >> (64-TDB_OFF_UPPER_STEAL) == (1<<TDB_OFF_UPPER_STEAL) - 1;
130 }
131
132 /* FIXME: Guess the depth, don't over-lock! */
133 static tdb_off_t hlock_range(tdb_off_t group, tdb_off_t *size)
134 {
135         *size = 1ULL << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
136         return group << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
137 }
138
139 /* This is the core routine which searches the hashtable for an entry.
140  * On error, no locks are held and TDB_OFF_ERR is returned.
141  * Otherwise, hinfo is filled in (and the optional tinfo).
142  * If not found, the return value is 0.
143  * If found, the return value is the offset, and *rec is the record. */
144 tdb_off_t find_and_lock(struct tdb_context *tdb,
145                         struct tdb_data key,
146                         int ltype,
147                         struct hash_info *h,
148                         struct tdb_used_record *rec,
149                         struct traverse_info *tinfo)
150 {
151         uint32_t i, group;
152         tdb_off_t hashtable;
153
154         h->h = tdb_hash(tdb, key.dptr, key.dsize);
155         h->hash_used = 0;
156         group = use_bits(h, TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
157         h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
158
159         h->hlock_start = hlock_range(group, &h->hlock_range);
160         if (tdb_lock_hashes(tdb, h->hlock_start, h->hlock_range, ltype,
161                             TDB_LOCK_WAIT))
162                 return TDB_OFF_ERR;
163
164         hashtable = offsetof(struct tdb_header, hashtable);
165         if (tinfo) {
166                 tinfo->toplevel_group = group;
167                 tinfo->num_levels = 1;
168                 tinfo->levels[0].entry = 0;
169                 tinfo->levels[0].hashtable = hashtable 
170                         + (group << TDB_HASH_GROUP_BITS) * sizeof(tdb_off_t);
171                 tinfo->levels[0].total_buckets = 1 << TDB_HASH_GROUP_BITS;
172         }
173
174         while (likely(h->hash_used < 64)) {
175                 /* Read in the hash group. */
176                 h->group_start = hashtable
177                         + group * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
178
179                 if (tdb_read_convert(tdb, h->group_start, &h->group,
180                                      sizeof(h->group)) == -1)
181                         goto fail;
182
183                 /* Pointer to another hash table?  Go down... */
184                 if (is_subhash(h->group[h->home_bucket])) {
185                         hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
186                                 + sizeof(struct tdb_used_record);
187                         if (tinfo) {
188                                 /* When we come back, use *next* bucket */
189                                 tinfo->levels[tinfo->num_levels-1].entry
190                                         += h->home_bucket + 1;
191                         }
192                         group = use_bits(h, TDB_SUBLEVEL_HASH_BITS
193                                          - TDB_HASH_GROUP_BITS);
194                         h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
195                         if (tinfo) {
196                                 tinfo->levels[tinfo->num_levels].hashtable
197                                         = hashtable;
198                                 tinfo->levels[tinfo->num_levels].total_buckets
199                                         = 1 << TDB_SUBLEVEL_HASH_BITS;
200                                 tinfo->levels[tinfo->num_levels].entry
201                                         = group << TDB_HASH_GROUP_BITS;
202                                 tinfo->num_levels++;
203                         }
204                         continue;
205                 }
206
207                 /* It's in this group: search (until 0 or all searched) */
208                 for (i = 0, h->found_bucket = h->home_bucket;
209                      i < (1 << TDB_HASH_GROUP_BITS);
210                      i++, h->found_bucket = ((h->found_bucket+1)
211                                              % (1 << TDB_HASH_GROUP_BITS))) {
212                         if (is_subhash(h->group[h->found_bucket]))
213                                 continue;
214
215                         if (!h->group[h->found_bucket])
216                                 break;
217
218                         if (match(tdb, h, &key, h->group[h->found_bucket],
219                                   rec)) {
220                                 if (tinfo) {
221                                         tinfo->levels[tinfo->num_levels-1].entry
222                                                 += h->found_bucket;
223                                 }
224                                 return h->group[h->found_bucket] & TDB_OFF_MASK;
225                         }
226                 }
227                 /* Didn't find it: h indicates where it would go. */
228                 return 0;
229         }
230
231         /* FIXME: We hit the bottom.  Chain! */
232         abort();
233
234 fail:
235         tdb_unlock_hashes(tdb, h->hlock_start, h->hlock_range, ltype);
236         return TDB_OFF_ERR;
237 }
238
239 /* I wrote a simple test, expanding a hash to 2GB, for the following
240  * cases:
241  * 1) Expanding all the buckets at once,
242  * 2) Expanding the most-populated bucket,
243  * 3) Expanding the bucket we wanted to place the new entry ito.
244  *
245  * I measured the worst/average/best density during this process.
246  * 1) 3%/16%/30%
247  * 2) 4%/20%/38%
248  * 3) 6%/22%/41%
249  *
250  * So we figure out the busiest bucket for the moment.
251  */
252 static unsigned fullest_bucket(struct tdb_context *tdb,
253                                const tdb_off_t *group,
254                                unsigned new_bucket)
255 {
256         unsigned counts[1 << TDB_HASH_GROUP_BITS] = { 0 };
257         unsigned int i, best_bucket;
258
259         /* Count the new entry. */
260         counts[new_bucket]++;
261         best_bucket = new_bucket;
262
263         for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
264                 unsigned this_bucket;
265
266                 if (is_subhash(group[i]))
267                         continue;
268                 this_bucket = group[i] & TDB_OFF_HASH_GROUP_MASK;
269                 if (++counts[this_bucket] > counts[best_bucket])
270                         best_bucket = this_bucket;
271         }
272
273         return best_bucket;
274 }
275
276 static bool put_into_group(tdb_off_t *group,
277                            unsigned bucket, tdb_off_t encoded)
278 {
279         unsigned int i;
280
281         for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
282                 unsigned b = (bucket + i) % (1 << TDB_HASH_GROUP_BITS);
283
284                 if (group[b] == 0) {
285                         group[b] = encoded;
286                         return true;
287                 }
288         }
289         return false;
290 }
291
292 static void force_into_group(tdb_off_t *group,
293                              unsigned bucket, tdb_off_t encoded)
294 {
295         if (!put_into_group(group, bucket, encoded))
296                 abort();
297 }
298
299 static tdb_off_t encode_offset(tdb_off_t new_off, struct hash_info *h)
300 {
301         return h->home_bucket
302                 | new_off
303                 | ((uint64_t)bits(h->h,
304                                   64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
305                                   TDB_OFF_UPPER_STEAL_EXTRA)
306                    << TDB_OFF_HASH_EXTRA_BIT);
307 }
308
309 /* Simply overwrite the hash entry we found before. */ 
310 int replace_in_hash(struct tdb_context *tdb,
311                     struct hash_info *h,
312                     tdb_off_t new_off)
313 {
314         return tdb_write_off(tdb, hbucket_off(h->group_start, h->found_bucket),
315                              encode_offset(new_off, h));
316 }
317
318 /* Add into a newly created subhash. */
319 static int add_to_subhash(struct tdb_context *tdb, tdb_off_t subhash,
320                           unsigned hash_used, tdb_off_t val)
321 {
322         tdb_off_t off = (val & TDB_OFF_MASK), *group;
323         struct hash_info h;
324         unsigned int gnum;
325
326         h.hash_used = hash_used;
327
328         /* FIXME chain if hash_used == 64 */
329         if (hash_used + TDB_SUBLEVEL_HASH_BITS > 64)
330                 abort();
331
332         /* FIXME: Do truncated hash bits if we can! */
333         h.h = hash_record(tdb, off);
334         gnum = use_bits(&h, TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS);
335         h.group_start = subhash + sizeof(struct tdb_used_record)
336                 + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
337         h.home_bucket = use_bits(&h, TDB_HASH_GROUP_BITS);
338
339         group = tdb_access_write(tdb, h.group_start,
340                                  sizeof(*group) << TDB_HASH_GROUP_BITS, true);
341         if (!group)
342                 return -1;
343         force_into_group(group, h.home_bucket, encode_offset(off, &h));
344         return tdb_access_commit(tdb, group);
345 }
346
347 static int expand_group(struct tdb_context *tdb, struct hash_info *h)
348 {
349         unsigned bucket, num_vals, i;
350         tdb_off_t subhash;
351         tdb_off_t vals[1 << TDB_HASH_GROUP_BITS];
352
353         /* Attach new empty subhash under fullest bucket. */
354         bucket = fullest_bucket(tdb, h->group, h->home_bucket);
355
356         subhash = alloc(tdb, 0, sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS,
357                         0, false);
358         if (subhash == TDB_OFF_ERR)
359                 return -1;
360
361         if (zero_out(tdb, subhash + sizeof(struct tdb_used_record),
362                      sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) == -1)
363                 return -1;
364
365         /* Remove any which are destined for bucket or are in wrong place. */
366         num_vals = 0;
367         for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
368                 unsigned home_bucket = h->group[i] & TDB_OFF_HASH_GROUP_MASK;
369                 if (!h->group[i] || is_subhash(h->group[i]))
370                         continue;
371                 if (home_bucket == bucket || home_bucket != i) {
372                         vals[num_vals++] = h->group[i];
373                         h->group[i] = 0;
374                 }
375         }
376         /* FIXME: This assert is valid, but we do this during unit test :( */
377         /* assert(num_vals); */
378
379         /* Overwrite expanded bucket with subhash pointer. */
380         h->group[bucket] = subhash | ~((1ULL << (64 - TDB_OFF_UPPER_STEAL))-1);
381
382         /* Put values back. */
383         for (i = 0; i < num_vals; i++) {
384                 unsigned this_bucket = vals[i] & TDB_OFF_HASH_GROUP_MASK;
385
386                 if (this_bucket == bucket) {
387                         if (add_to_subhash(tdb, subhash, h->hash_used, vals[i]))
388                                 return -1;
389                 } else {
390                         /* There should be room to put this back. */
391                         force_into_group(h->group, this_bucket, vals[i]);
392                 }
393         }
394         return 0;
395 }
396
397 int delete_from_hash(struct tdb_context *tdb, struct hash_info *h)
398 {
399         unsigned int i, num_movers = 0;
400         tdb_off_t movers[1 << TDB_HASH_GROUP_BITS];
401
402         h->group[h->found_bucket] = 0;
403         for (i = 1; i < (1 << TDB_HASH_GROUP_BITS); i++) {
404                 unsigned this_bucket;
405
406                 this_bucket = (h->found_bucket+i) % (1 << TDB_HASH_GROUP_BITS);
407                 /* Empty bucket?  We're done. */
408                 if (!h->group[this_bucket])
409                         break;
410
411                 /* Ignore subhashes. */
412                 if (is_subhash(h->group[this_bucket]))
413                         continue;
414
415                 /* If this one is not happy where it is, we'll move it. */
416                 if ((h->group[this_bucket] & TDB_OFF_HASH_GROUP_MASK)
417                     != this_bucket) {
418                         movers[num_movers++] = h->group[this_bucket];
419                         h->group[this_bucket] = 0;
420                 }
421         }
422
423         /* Put back the ones we erased. */
424         for (i = 0; i < num_movers; i++) {
425                 force_into_group(h->group, movers[i] & TDB_OFF_HASH_GROUP_MASK,
426                                  movers[i]);
427         }
428
429         /* Now we write back the hash group */
430         return tdb_write_convert(tdb, h->group_start,
431                                  h->group, sizeof(h->group));
432 }
433
434 int add_to_hash(struct tdb_context *tdb, struct hash_info *h, tdb_off_t new_off)
435 {
436         /* FIXME: chain! */
437         if (h->hash_used >= 64)
438                 abort();
439
440         /* We hit an empty bucket during search?  That's where it goes. */
441         if (!h->group[h->found_bucket]) {
442                 h->group[h->found_bucket] = encode_offset(new_off, h);
443                 /* Write back the modified group. */
444                 return tdb_write_convert(tdb, h->group_start,
445                                          h->group, sizeof(h->group));
446         }
447
448         /* We're full.  Expand. */
449         if (expand_group(tdb, h) == -1)
450                 return -1;
451
452         if (is_subhash(h->group[h->home_bucket])) {
453                 /* We were expanded! */
454                 tdb_off_t hashtable;
455                 unsigned int gnum;
456
457                 /* Write back the modified group. */
458                 if (tdb_write_convert(tdb, h->group_start, h->group,
459                                       sizeof(h->group)))
460                         return -1;
461
462                 /* Move hashinfo down a level. */
463                 hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
464                         + sizeof(struct tdb_used_record);
465                 gnum = use_bits(h,TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
466                 h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
467                 h->group_start = hashtable
468                         + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
469                 if (tdb_read_convert(tdb, h->group_start, &h->group,
470                                      sizeof(h->group)) == -1)
471                         return -1;
472         }
473
474         /* Expanding the group must have made room if it didn't choose this
475          * bucket. */
476         if (put_into_group(h->group, h->home_bucket, encode_offset(new_off, h)))
477                 return tdb_write_convert(tdb, h->group_start,
478                                          h->group, sizeof(h->group));
479
480         /* This can happen if all hashes in group (and us) dropped into same
481          * group in subhash. */
482         return add_to_hash(tdb, h, new_off);
483 }
484
485 /* Traverse support: returns offset of record, or 0 or TDB_OFF_ERR. */
486 static tdb_off_t iterate_hash(struct tdb_context *tdb,
487                               struct traverse_info *tinfo)
488 {
489         tdb_off_t off, val;
490         unsigned int i;
491         struct traverse_level *tlevel;
492
493         tlevel = &tinfo->levels[tinfo->num_levels-1];
494
495 again:
496         for (i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
497                                       tlevel->entry, tlevel->total_buckets);
498              i != tlevel->total_buckets;
499              i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
500                                       i+1, tlevel->total_buckets)) {
501                 val = tdb_read_off(tdb, tlevel->hashtable+sizeof(tdb_off_t)*i);
502                 if (unlikely(val == TDB_OFF_ERR))
503                         return TDB_OFF_ERR;
504
505                 off = val & TDB_OFF_MASK;
506
507                 /* This makes the delete-all-in-traverse case work
508                  * (and simplifies our logic a little). */
509                 if (off == tinfo->prev)
510                         continue;
511
512                 tlevel->entry = i;
513
514                 if (!is_subhash(val)) {
515                         /* Found one. */
516                         tinfo->prev = off;
517                         return off;
518                 }
519
520                 /* When we come back, we want the next one */
521                 tlevel->entry++;
522                 tinfo->num_levels++;
523                 tlevel++;
524                 tlevel->hashtable = off + sizeof(struct tdb_used_record);
525                 tlevel->entry = 0;
526                 tlevel->total_buckets = (1 << TDB_SUBLEVEL_HASH_BITS);
527                 goto again;
528         }
529
530         /* Nothing there? */
531         if (tinfo->num_levels == 1)
532                 return 0;
533
534         /* Go back up and keep searching. */
535         tinfo->num_levels--;
536         tlevel--;
537         goto again;
538 }
539
540 /* Return 1 if we find something, 0 if not, -1 on error. */
541 int next_in_hash(struct tdb_context *tdb, int ltype,
542                  struct traverse_info *tinfo,
543                  TDB_DATA *kbuf, size_t *dlen)
544 {
545         const unsigned group_bits = TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS;
546         tdb_off_t hlock_start, hlock_range, off;
547
548         while (tinfo->toplevel_group < (1 << group_bits)) {
549                 hlock_start = (tdb_off_t)tinfo->toplevel_group
550                         << (64 - group_bits);
551                 hlock_range = 1ULL << group_bits;
552                 if (tdb_lock_hashes(tdb, hlock_start, hlock_range, ltype,
553                                     TDB_LOCK_WAIT) != 0)
554                         return -1;
555
556                 off = iterate_hash(tdb, tinfo);
557                 if (off) {
558                         struct tdb_used_record rec;
559
560                         if (tdb_read_convert(tdb, off, &rec, sizeof(rec))) {
561                                 tdb_unlock_hashes(tdb,
562                                                   hlock_start, hlock_range,
563                                                   ltype);
564                                 return -1;
565                         }
566                         if (rec_magic(&rec) != TDB_MAGIC) {
567                                 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
568                                          "next_in_hash:"
569                                          " corrupt record at %llu\n",
570                                          (long long)off);
571                                 return -1;
572                         }
573
574                         kbuf->dsize = rec_key_length(&rec);
575
576                         /* They want data as well? */
577                         if (dlen) {
578                                 *dlen = rec_data_length(&rec);
579                                 kbuf->dptr = tdb_alloc_read(tdb, 
580                                                             off + sizeof(rec),
581                                                             kbuf->dsize
582                                                             + *dlen);
583                         } else {
584                                 kbuf->dptr = tdb_alloc_read(tdb, 
585                                                             off + sizeof(rec),
586                                                             kbuf->dsize);
587                         }
588                         tdb_unlock_hashes(tdb, hlock_start, hlock_range, ltype);
589                         return kbuf->dptr ? 1 : -1;
590                 }
591
592                 tdb_unlock_hashes(tdb, hlock_start, hlock_range, ltype);
593
594                 tinfo->toplevel_group++;
595                 tinfo->levels[0].hashtable
596                         += (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
597                 tinfo->levels[0].entry = 0;
598         }
599         return 0;
600 }
601
602 /* Return 1 if we find something, 0 if not, -1 on error. */
603 int first_in_hash(struct tdb_context *tdb, int ltype,
604                   struct traverse_info *tinfo,
605                   TDB_DATA *kbuf, size_t *dlen)
606 {
607         tinfo->prev = 0;
608         tinfo->toplevel_group = 0;
609         tinfo->num_levels = 1;
610         tinfo->levels[0].hashtable = offsetof(struct tdb_header, hashtable);
611         tinfo->levels[0].entry = 0;
612         tinfo->levels[0].total_buckets = (1 << TDB_HASH_GROUP_BITS);
613
614         return next_in_hash(tdb, ltype, tinfo, kbuf, dlen);
615 }
616
617 /* Even if the entry isn't in this hash bucket, you'd have to lock this
618  * bucket to find it. */
619 static int chainlock(struct tdb_context *tdb, const TDB_DATA *key,
620                      int ltype, enum tdb_lock_flags waitflag,
621                      const char *func)
622 {
623         int ret;
624         uint64_t h = tdb_hash(tdb, key->dptr, key->dsize);
625         tdb_off_t lockstart, locksize;
626         unsigned int group, gbits;
627
628         gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
629         group = bits(h, 64 - gbits, gbits);
630
631         lockstart = hlock_range(group, &locksize);
632
633         ret = tdb_lock_hashes(tdb, lockstart, locksize, ltype, waitflag);
634         tdb_trace_1rec(tdb, func, *key);
635         return ret;
636 }
637
638 /* lock/unlock one hash chain. This is meant to be used to reduce
639    contention - it cannot guarantee how many records will be locked */
640 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
641 {
642         return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT, "tdb_chainlock");
643 }
644
645 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
646 {
647         uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
648         tdb_off_t lockstart, locksize;
649         unsigned int group, gbits;
650
651         gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
652         group = bits(h, 64 - gbits, gbits);
653
654         lockstart = hlock_range(group, &locksize);
655
656         tdb_trace_1rec(tdb, "tdb_chainunlock", key);
657         return tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK);
658 }