2 #include <ccan/tdb2/tdb2.h>
3 #include <ccan/hash/hash.h>
4 #include <ccan/build_assert/build_assert.h>
5 #include <ccan/likely/likely.h>
9 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
11 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
12 static struct tdb_context *tdbs = NULL;
14 PRINTF_ATTRIBUTE(4, 5) static void
15 null_log_fn(struct tdb_context *tdb,
16 enum tdb_debug_level level, void *priv,
21 /* We do a lot of work assuming our copy of the header volatile area
22 * is uptodate, and usually it is. However, once we grab a lock, we have to
24 bool header_changed(struct tdb_context *tdb)
28 if (!(tdb->flags & TDB_NOLOCK) && tdb->header_uptodate) {
29 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
30 "warning: header uptodate already\n");
33 /* We could get a partial update if we're not holding any locks. */
34 assert((tdb->flags & TDB_NOLOCK) || tdb_has_locks(tdb));
36 tdb->header_uptodate = true;
37 gen = tdb_read_off(tdb, offsetof(struct tdb_header, v.generation));
38 if (unlikely(gen != tdb->header.v.generation)) {
39 tdb_read_convert(tdb, offsetof(struct tdb_header, v),
40 &tdb->header.v, sizeof(tdb->header.v));
46 int write_header(struct tdb_context *tdb)
48 assert(tdb_read_off(tdb, offsetof(struct tdb_header, v.generation))
49 == tdb->header.v.generation);
50 tdb->header.v.generation++;
51 return tdb_write_convert(tdb, offsetof(struct tdb_header, v),
52 &tdb->header.v, sizeof(tdb->header.v));
55 static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
58 return hash64_stable((const unsigned char *)key, length, seed);
61 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len)
63 return tdb->khash(ptr, len, tdb->header.hash_seed, tdb->hash_priv);
66 static bool tdb_already_open(dev_t device, ino_t ino)
68 struct tdb_context *i;
70 for (i = tdbs; i; i = i->next) {
71 if (i->device == device && i->inode == ino) {
79 static uint64_t random_number(struct tdb_context *tdb)
85 fd = open("/dev/urandom", O_RDONLY);
87 if (tdb_read_all(fd, &ret, sizeof(ret))) {
88 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
89 "tdb_open: random from /dev/urandom\n");
95 /* FIXME: Untested! Based on Wikipedia protocol description! */
96 fd = open("/dev/egd-pool", O_RDWR);
98 /* Command is 1, next byte is size we want to read. */
99 char cmd[2] = { 1, sizeof(uint64_t) };
100 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
101 char reply[1 + sizeof(uint64_t)];
102 int r = read(fd, reply, sizeof(reply));
104 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
105 "tdb_open: %u random bytes from"
106 " /dev/egd-pool\n", r-1);
107 /* Copy at least some bytes. */
108 memcpy(&ret, reply+1, r - 1);
109 if (reply[0] == sizeof(uint64_t)
110 && r == sizeof(reply)) {
119 /* Fallback: pid and time. */
120 gettimeofday(&now, NULL);
121 ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
122 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
123 "tdb_open: random from getpid and time\n");
127 struct new_database {
128 struct tdb_header hdr;
129 struct tdb_used_record hrec;
130 tdb_off_t hash[1ULL << INITIAL_HASH_BITS];
131 struct tdb_used_record frec;
132 tdb_off_t free[INITIAL_FREE_BUCKETS + 1]; /* One overflow bucket */
135 /* initialise a new database */
136 static int tdb_new_database(struct tdb_context *tdb)
138 /* We make it up in memory, then write it out if not internal */
139 struct new_database newdb;
140 unsigned int magic_off = offsetof(struct tdb_header, magic_food);
142 /* Fill in the header */
143 newdb.hdr.version = TDB_VERSION;
144 newdb.hdr.hash_seed = random_number(tdb);
145 newdb.hdr.hash_test = TDB_HASH_MAGIC;
146 newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
147 sizeof(newdb.hdr.hash_test),
151 newdb.hdr.v.generation = 0;
153 /* The initial zone must cover the initial database size! */
154 BUILD_ASSERT((1ULL << INITIAL_ZONE_BITS) >= sizeof(newdb));
156 /* Free array has 1 zone, 10 buckets. All buckets empty. */
157 newdb.hdr.v.num_zones = 1;
158 newdb.hdr.v.zone_bits = INITIAL_ZONE_BITS;
159 newdb.hdr.v.free_buckets = INITIAL_FREE_BUCKETS;
160 newdb.hdr.v.free_off = offsetof(struct new_database, free);
161 set_header(tdb, &newdb.frec, 0,
162 sizeof(newdb.free), sizeof(newdb.free), 0);
163 memset(newdb.free, 0, sizeof(newdb.free));
165 /* Initial hashes are empty. */
166 newdb.hdr.v.hash_bits = INITIAL_HASH_BITS;
167 newdb.hdr.v.hash_off = offsetof(struct new_database, hash);
168 set_header(tdb, &newdb.hrec, 0,
169 sizeof(newdb.hash), sizeof(newdb.hash), 0);
170 memset(newdb.hash, 0, sizeof(newdb.hash));
173 memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
174 strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
176 /* This creates an endian-converted database, as if read from disk */
178 (char *)&newdb.hdr + magic_off,
179 sizeof(newdb) - magic_off);
181 tdb->header = newdb.hdr;
183 if (tdb->flags & TDB_INTERNAL) {
184 tdb->map_size = sizeof(newdb);
185 tdb->map_ptr = malloc(tdb->map_size);
187 tdb->ecode = TDB_ERR_OOM;
190 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
193 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
196 if (ftruncate(tdb->fd, 0) == -1)
199 if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
200 tdb->ecode = TDB_ERR_IO;
206 struct tdb_context *tdb_open(const char *name, int tdb_flags,
207 int open_flags, mode_t mode,
208 union tdb_attribute *attr)
210 struct tdb_context *tdb;
216 tdb = malloc(sizeof(*tdb));
225 /* map_size will be set below. */
226 tdb->ecode = TDB_SUCCESS;
227 /* header will be read in below. */
228 tdb->header_uptodate = false;
229 tdb->flags = tdb_flags;
230 tdb->log = null_log_fn;
231 tdb->log_priv = NULL;
232 tdb->khash = jenkins_hash;
233 tdb->hash_priv = NULL;
234 tdb->transaction = NULL;
235 /* last_zone will be set below. */
241 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
242 "tdb_open: attributes not yet supported\n");
247 if ((open_flags & O_ACCMODE) == O_WRONLY) {
248 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
249 "tdb_open: can't open tdb %s write-only\n", name);
254 if ((open_flags & O_ACCMODE) == O_RDONLY) {
255 tdb->read_only = true;
256 /* read only databases don't do locking */
257 tdb->flags |= TDB_NOLOCK;
259 tdb->read_only = false;
261 /* internal databases don't need any of the rest. */
262 if (tdb->flags & TDB_INTERNAL) {
263 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
264 if (tdb_new_database(tdb) != 0) {
265 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
266 "tdb_open: tdb_new_database failed!");
269 TEST_IT(tdb->flags & TDB_CONVERT);
270 tdb_convert(tdb, &tdb->header, sizeof(tdb->header));
274 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
275 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
276 "tdb_open: could not open file %s: %s\n",
277 name, strerror(errno));
278 goto fail; /* errno set by open(2) */
281 /* on exec, don't inherit the fd */
282 v = fcntl(tdb->fd, F_GETFD, 0);
283 fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
285 /* ensure there is only one process initialising at once */
286 if (tdb_lock_open(tdb) == -1) {
287 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
288 "tdb_open: failed to get open lock on %s: %s\n",
289 name, strerror(errno));
290 goto fail; /* errno set by tdb_brlock */
293 if (!tdb_pread_all(tdb->fd, &tdb->header, sizeof(tdb->header), 0)
294 || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
295 if (!(open_flags & O_CREAT) || tdb_new_database(tdb) == -1) {
297 errno = EIO; /* ie bad format or something */
301 } else if (tdb->header.version != TDB_VERSION) {
302 if (tdb->header.version == bswap_64(TDB_VERSION))
303 tdb->flags |= TDB_CONVERT;
306 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
307 "tdb_open: %s is unknown version 0x%llx\n",
308 name, (long long)tdb->header.version);
314 tdb_convert(tdb, &tdb->header, sizeof(tdb->header));
315 hash_test = TDB_HASH_MAGIC;
316 hash_test = tdb->khash(&hash_test, sizeof(hash_test),
317 tdb->header.hash_seed, tdb->hash_priv);
318 if (tdb->header.hash_test != hash_test) {
319 /* wrong hash variant */
320 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
321 "tdb_open: %s uses a different hash function\n",
327 if (fstat(tdb->fd, &st) == -1)
330 /* Is it already in the open list? If so, fail. */
331 if (tdb_already_open(st.st_dev, st.st_ino)) {
333 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
334 "tdb_open: %s (%d,%d) is already open in this process\n",
335 name, (int)st.st_dev, (int)st.st_ino);
340 tdb->name = strdup(name);
346 tdb->map_size = st.st_size;
347 tdb->device = st.st_dev;
348 tdb->inode = st.st_ino;
350 tdb_unlock_open(tdb);
367 if (tdb->flags & TDB_INTERNAL) {
372 free((char *)tdb->name);
374 if (close(tdb->fd) != 0)
375 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
376 "tdb_open: failed to close tdb->fd"
383 static tdb_off_t hash_off(struct tdb_context *tdb, uint64_t list)
385 return tdb->header.v.hash_off
386 + ((list & ((1ULL << tdb->header.v.hash_bits) - 1))
387 * sizeof(tdb_off_t));
390 /* Returns 0 if the entry is a zero (definitely not a match).
391 * Returns a valid entry offset if it's a match. Fills in rec.
392 * Otherwise returns TDB_OFF_ERR: keep searching. */
393 static tdb_off_t entry_matches(struct tdb_context *tdb,
396 const struct tdb_data *key,
397 struct tdb_used_record *rec)
401 const unsigned char *rkey;
403 off = tdb_read_off(tdb, tdb->header.v.hash_off
404 + list * sizeof(tdb_off_t));
405 if (off == 0 || off == TDB_OFF_ERR)
408 #if 0 /* FIXME: Check other bits. */
409 unsigned int bits, bitmask, hoffextra;
410 /* Bottom three bits show how many extra hash bits. */
411 bits = (off & ((1 << TDB_EXTRA_HASHBITS_NUM) - 1)) + 1;
412 bitmask = (1 << bits)-1;
413 hoffextra = ((off >> TDB_EXTRA_HASHBITS_NUM) & bitmask);
414 uint64_t hextra = hash >> tdb->header.v.hash_bits;
415 if ((hextra & bitmask) != hoffextra)
420 if (tdb_read_convert(tdb, off, rec, sizeof(*rec)) == -1)
423 /* FIXME: check extra bits in header! */
424 keylen = rec_key_length(rec);
425 if (keylen != key->dsize)
428 rkey = tdb_access_read(tdb, off + sizeof(*rec), keylen);
431 if (memcmp(rkey, key->dptr, keylen) != 0)
433 tdb_access_release(tdb, rkey);
437 /* FIXME: Optimize? */
438 static void unlock_range(struct tdb_context *tdb,
439 tdb_off_t list, tdb_len_t num,
444 for (i = list; i < list + num; i++)
445 tdb_unlock_list(tdb, i, ltype);
448 /* FIXME: Optimize? */
449 static int lock_range(struct tdb_context *tdb,
450 tdb_off_t list, tdb_len_t num,
455 for (i = list; i < list + num; i++) {
456 if (tdb_lock_list(tdb, i, ltype, TDB_LOCK_WAIT) != 0) {
457 unlock_range(tdb, list, i - list, ltype);
464 /* We lock hashes up to the next empty offset. We already hold the
465 * lock on the start bucket, but we may need to release and re-grab
466 * it. If we fail, we hold no locks at all! */
467 static tdb_len_t relock_hash_to_zero(struct tdb_context *tdb,
468 tdb_off_t start, int ltype)
470 tdb_len_t num, len, pre_locks;
473 num = 1ULL << tdb->header.v.hash_bits;
474 len = tdb_find_zero_off(tdb, hash_off(tdb, start), num - start);
475 if (unlikely(len == num - start)) {
476 /* We hit the end of the hash range. Drop lock: we have
477 to lock start of hash first. */
478 tdb_unlock_list(tdb, start, ltype);
479 /* Grab something, so header is stable. */
480 if (tdb_lock_list(tdb, 0, ltype, TDB_LOCK_WAIT))
482 len = tdb_find_zero_off(tdb, hash_off(tdb, 0), num);
483 if (lock_range(tdb, 1, len, ltype) == -1) {
484 tdb_unlock_list(tdb, 0, ltype);
490 /* We already have lock on start. */
494 if (unlikely(lock_range(tdb, start, len, ltype) == -1)) {
496 unlock_range(tdb, 0, pre_locks, ltype);
498 tdb_unlock_list(tdb, start, ltype);
502 /* Now, did we lose the race, and it's not zero any more? */
503 if (unlikely(tdb_read_off(tdb, hash_off(tdb, pre_locks + len)) != 0)) {
504 unlock_range(tdb, 0, pre_locks, ltype);
505 /* Leave the start locked, as expected. */
506 unlock_range(tdb, start + 1, len - 1, ltype);
510 return pre_locks + len;
513 /* FIXME: modify, don't rewrite! */
514 static int update_rec_hdr(struct tdb_context *tdb,
518 struct tdb_used_record *rec,
521 uint64_t room = rec_data_length(rec) + rec_extra_padding(rec);
523 if (set_header(tdb, rec, keylen, datalen, room - datalen, h))
526 return tdb_write_convert(tdb, off, rec, sizeof(*rec));
529 /* If we fail, others will try after us. */
530 static void enlarge_hash(struct tdb_context *tdb)
533 uint64_t h, num = 1ULL << tdb->header.v.hash_bits;
534 struct tdb_used_record pad, *r;
536 /* FIXME: We should do this without holding locks throughout. */
537 if (tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false) == -1)
540 /* Someone else enlarged for us? Nothing to do. */
541 if ((1ULL << tdb->header.v.hash_bits) != num)
544 newoff = alloc(tdb, 0, num * 2, 0, false);
545 if (unlikely(newoff == TDB_OFF_ERR))
547 if (unlikely(newoff == 0)) {
548 if (tdb_expand(tdb, 0, num * 2, false) == -1)
550 newoff = alloc(tdb, 0, num * 2, 0, false);
551 if (newoff == TDB_OFF_ERR || newoff == 0)
555 /* FIXME: If the space before is empty, we know this is in its ideal
556 * location. We can steal a bit from the pointer to avoid rehash. */
557 for (i = tdb_find_nonzero_off(tdb, tdb->header.v.hash_off, num);
559 i += tdb_find_nonzero_off(tdb, tdb->header.v.hash_off
560 + i*sizeof(tdb_off_t), num - i)) {
562 off = tdb_read_off(tdb, tdb->header.v.hash_off
563 + i*sizeof(tdb_off_t));
564 if (unlikely(off == TDB_OFF_ERR))
566 if (unlikely(!off)) {
567 tdb->ecode = TDB_ERR_CORRUPT;
568 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
569 "find_bucket_and_lock: zero hash bucket!\n");
572 h = hash_record(tdb, off);
573 /* FIXME: Encode extra hash bits! */
574 if (tdb_write_off(tdb, newoff
575 + (h & ((num * 2) - 1)) * sizeof(uint64_t),
580 /* Free up old hash. */
581 r = tdb_get(tdb, tdb->header.v.hash_off, &pad, sizeof(*r));
584 add_free_record(tdb, tdb->header.v.hash_off,
585 rec_data_length(r) + rec_extra_padding(r));
587 /* Now we write the modified header. */
588 tdb->header.v.generation++;
589 tdb->header.v.hash_bits++;
590 tdb->header.v.hash_off = newoff;
591 tdb_write_convert(tdb, offsetof(struct tdb_header, v),
592 &tdb->header.v, sizeof(tdb->header.v));
594 tdb_allrecord_unlock(tdb, F_WRLCK);
597 int tdb_store(struct tdb_context *tdb,
598 struct tdb_data key, struct tdb_data dbuf, int flag)
600 tdb_off_t new_off, off, old_bucket, start, num_locks = 1;
601 struct tdb_used_record rec;
603 bool growing = false;
605 h = tdb_hash(tdb, key.dptr, key.dsize);
607 /* FIXME: can we avoid locks for some fast paths? */
608 start = tdb_lock_list(tdb, h, F_WRLCK, TDB_LOCK_WAIT);
609 if (start == TDB_OFF_ERR)
614 off = entry_matches(tdb, start, h, &key, &rec);
615 if (unlikely(off == TDB_OFF_ERR)) {
616 /* Slow path, need to grab more locks and search. */
619 /* Warning: this may drop the lock! Does that on error. */
620 num_locks = relock_hash_to_zero(tdb, start, F_WRLCK);
621 if (num_locks == TDB_OFF_ERR)
624 for (i = start; i < start + num_locks; i++) {
625 off = entry_matches(tdb, i, h, &key, &rec);
626 /* Empty entry or we found it? */
627 if (off == 0 || off != TDB_OFF_ERR) {
632 if (i == start + num_locks)
636 /* Now we have lock on this hash bucket. */
637 if (flag == TDB_INSERT) {
639 tdb->ecode = TDB_ERR_EXISTS;
644 if (rec_data_length(&rec) + rec_extra_padding(&rec)
647 if (update_rec_hdr(tdb, off,
648 key.dsize, dbuf.dsize,
653 /* FIXME: See if right record is free? */
654 /* Hint to allocator that we've realloced. */
657 if (flag == TDB_MODIFY) {
658 /* if the record doesn't exist and we
659 are in TDB_MODIFY mode then we should fail
661 tdb->ecode = TDB_ERR_NOEXIST;
667 /* Allocate a new record. */
668 new_off = alloc(tdb, key.dsize, dbuf.dsize, h, growing);
670 unlock_range(tdb, start, num_locks, F_WRLCK);
671 /* Expand, then try again... */
672 if (tdb_expand(tdb, key.dsize, dbuf.dsize, growing) == -1)
674 return tdb_store(tdb, key, dbuf, flag);
677 /* We didn't like the existing one: remove it. */
679 add_free_record(tdb, off, sizeof(struct tdb_used_record)
680 + rec_key_length(&rec)
681 + rec_data_length(&rec)
682 + rec_extra_padding(&rec));
686 /* FIXME: Encode extra hash bits! */
687 if (tdb_write_off(tdb, hash_off(tdb, old_bucket), new_off) == -1)
690 off = new_off + sizeof(struct tdb_used_record);
691 if (tdb->methods->write(tdb, off, key.dptr, key.dsize) == -1)
694 if (tdb->methods->write(tdb, off, dbuf.dptr, dbuf.dsize) == -1)
697 /* FIXME: tdb_increment_seqnum(tdb); */
698 unlock_range(tdb, start, num_locks, F_WRLCK);
700 /* FIXME: by simple simulation, this approximated 60% full.
701 * Check in real case! */
702 if (unlikely(num_locks > 4 * tdb->header.v.hash_bits - 31))
708 unlock_range(tdb, start, num_locks, F_WRLCK);
712 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
714 tdb_off_t off, start, num_locks = 1;
715 struct tdb_used_record rec;
719 h = tdb_hash(tdb, key.dptr, key.dsize);
721 /* FIXME: can we avoid locks for some fast paths? */
722 start = tdb_lock_list(tdb, h, F_RDLCK, TDB_LOCK_WAIT);
723 if (start == TDB_OFF_ERR)
727 off = entry_matches(tdb, start, h, &key, &rec);
728 if (unlikely(off == TDB_OFF_ERR)) {
729 /* Slow path, need to grab more locks and search. */
732 /* Warning: this may drop the lock! Does that on error. */
733 num_locks = relock_hash_to_zero(tdb, start, F_RDLCK);
734 if (num_locks == TDB_OFF_ERR)
737 for (i = start; i < start + num_locks; i++) {
738 off = entry_matches(tdb, i, h, &key, &rec);
739 /* Empty entry or we found it? */
740 if (off == 0 || off != TDB_OFF_ERR)
743 if (i == start + num_locks)
748 unlock_range(tdb, start, num_locks, F_RDLCK);
749 tdb->ecode = TDB_ERR_NOEXIST;
753 ret.dsize = rec_data_length(&rec);
754 ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
756 unlock_range(tdb, start, num_locks, F_RDLCK);
760 static int hash_add(struct tdb_context *tdb, uint64_t h, tdb_off_t off)
762 tdb_off_t i, hoff, len, num;
764 /* Look for next space. */
765 i = (h & ((1ULL << tdb->header.v.hash_bits) - 1));
766 len = (1ULL << tdb->header.v.hash_bits) - i;
767 num = tdb_find_zero_off(tdb, hash_off(tdb, i), len);
769 if (unlikely(num == len)) {
770 /* We wrapped. Look through start of hash table. */
771 hoff = hash_off(tdb, 0);
772 len = (1ULL << tdb->header.v.hash_bits);
773 num = tdb_find_zero_off(tdb, hoff, len);
775 tdb->ecode = TDB_ERR_CORRUPT;
776 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
777 "hash_add: full hash table!\n");
781 /* FIXME: Encode extra hash bits! */
782 return tdb_write_off(tdb, hash_off(tdb, i + num), off);
785 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
787 tdb_off_t i, old_bucket, off, start, num_locks = 1;
788 struct tdb_used_record rec;
791 h = tdb_hash(tdb, key.dptr, key.dsize);
793 /* FIXME: can we avoid locks for some fast paths? */
794 start = tdb_lock_list(tdb, h, F_WRLCK, TDB_LOCK_WAIT);
795 if (start == TDB_OFF_ERR)
800 off = entry_matches(tdb, start, h, &key, &rec);
801 if (off && off != TDB_OFF_ERR) {
802 /* We can only really fastpath delete if next bucket
803 * is 0. Note that we haven't locked it, but our lock
804 * on this bucket stops anyone overflowing into it
806 if (tdb_read_off(tdb, hash_off(tdb, h+1)) == 0)
812 if (unlikely(off == TDB_OFF_ERR)) {
813 /* Slow path, need to grab more locks and search. */
816 /* Warning: this may drop the lock! Does that on error. */
817 num_locks = relock_hash_to_zero(tdb, start, F_WRLCK);
818 if (num_locks == TDB_OFF_ERR)
821 for (i = start; i < start + num_locks; i++) {
822 off = entry_matches(tdb, i, h, &key, &rec);
823 /* Empty entry or we found it? */
824 if (off == 0 || off != TDB_OFF_ERR) {
829 if (i == start + num_locks)
834 unlock_range(tdb, start, num_locks, F_WRLCK);
835 tdb->ecode = TDB_ERR_NOEXIST;
840 /* This actually unlinks it. */
841 if (tdb_write_off(tdb, hash_off(tdb, old_bucket), 0) == -1)
844 /* Rehash anything following. */
845 for (i = hash_off(tdb, old_bucket+1);
846 i != hash_off(tdb, h + num_locks);
847 i += sizeof(tdb_off_t)) {
851 off2 = tdb_read_off(tdb, i);
852 if (unlikely(off2 == TDB_OFF_ERR))
855 /* Maybe use a bit to indicate it is in ideal place? */
856 h2 = hash_record(tdb, off2);
857 /* Is it happy where it is? */
858 if (hash_off(tdb, h2) == i)
862 if (tdb_write_off(tdb, i, 0) == -1)
866 if (hash_add(tdb, h2, off2) == -1)
870 /* Free the deleted entry. */
871 if (add_free_record(tdb, off,
872 sizeof(struct tdb_used_record)
873 + rec_key_length(&rec)
874 + rec_data_length(&rec)
875 + rec_extra_padding(&rec)) != 0)
878 unlock_range(tdb, start, num_locks, F_WRLCK);
882 unlock_range(tdb, start, num_locks, F_WRLCK);
886 int tdb_close(struct tdb_context *tdb)
888 struct tdb_context **i;
892 if (tdb->transaction) {
893 tdb_transaction_cancel(tdb);
896 tdb_trace(tdb, "tdb_close");
899 if (tdb->flags & TDB_INTERNAL)
904 free((char *)tdb->name);
906 ret = close(tdb->fd);
911 /* Remove from contexts list */
912 for (i = &tdbs; *i; i = &(*i)->next) {
927 enum TDB_ERROR tdb_error(struct tdb_context *tdb)