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