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 update_header(struct tdb_context *tdb)
26 struct tdb_header_volatile pad, *v;
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 v = tdb_get(tdb, offsetof(struct tdb_header, v), &pad, sizeof(*v));
38 /* On failure, imply we updated header so they retry. */
41 tdb->header_uptodate = true;
42 if (likely(memcmp(&tdb->header.v, v, sizeof(*v)) == 0)) {
49 static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
52 return hash64_stable((const unsigned char *)key, length, seed);
55 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len)
57 return tdb->khash(ptr, len, tdb->header.hash_seed, tdb->hash_priv);
60 static bool tdb_already_open(dev_t device, ino_t ino)
62 struct tdb_context *i;
64 for (i = tdbs; i; i = i->next) {
65 if (i->device == device && i->inode == ino) {
73 static uint64_t random_number(struct tdb_context *tdb)
79 fd = open("/dev/urandom", O_RDONLY);
81 if (tdb_read_all(fd, &ret, sizeof(ret))) {
82 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
83 "tdb_open: random from /dev/urandom\n");
89 /* FIXME: Untested! Based on Wikipedia protocol description! */
90 fd = open("/dev/egd-pool", O_RDWR);
92 /* Command is 1, next byte is size we want to read. */
93 char cmd[2] = { 1, sizeof(uint64_t) };
94 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
95 char reply[1 + sizeof(uint64_t)];
96 int r = read(fd, reply, sizeof(reply));
98 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
99 "tdb_open: %u random bytes from"
100 " /dev/egd-pool\n", r-1);
101 /* Copy at least some bytes. */
102 memcpy(&ret, reply+1, r - 1);
103 if (reply[0] == sizeof(uint64_t)
104 && r == sizeof(reply)) {
113 /* Fallback: pid and time. */
114 gettimeofday(&now, NULL);
115 ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
116 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
117 "tdb_open: random from getpid and time\n");
121 struct new_database {
122 struct tdb_header hdr;
123 struct tdb_used_record hrec;
124 tdb_off_t hash[1ULL << INITIAL_HASH_BITS];
125 struct tdb_used_record frec;
126 tdb_off_t free[INITIAL_FREE_BUCKETS + 1]; /* One overflow bucket */
129 /* initialise a new database */
130 static int tdb_new_database(struct tdb_context *tdb)
132 /* We make it up in memory, then write it out if not internal */
133 struct new_database newdb;
134 unsigned int magic_off = offsetof(struct tdb_header, magic_food);
136 /* Fill in the header */
137 newdb.hdr.version = TDB_VERSION;
138 newdb.hdr.hash_seed = random_number(tdb);
139 newdb.hdr.hash_test = TDB_HASH_MAGIC;
140 newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
141 sizeof(newdb.hdr.hash_test),
145 newdb.hdr.v.generation = 0;
147 /* The initial zone must cover the initial database size! */
148 BUILD_ASSERT((1ULL << INITIAL_ZONE_BITS) >= sizeof(newdb));
150 /* Free array has 1 zone, 10 buckets. All buckets empty. */
151 newdb.hdr.v.num_zones = 1;
152 newdb.hdr.v.zone_bits = INITIAL_ZONE_BITS;
153 newdb.hdr.v.free_buckets = INITIAL_FREE_BUCKETS;
154 newdb.hdr.v.free_off = offsetof(struct new_database, free);
155 set_header(tdb, &newdb.frec, 0,
156 sizeof(newdb.free), sizeof(newdb.free), 0);
157 memset(newdb.free, 0, sizeof(newdb.free));
159 /* Initial hashes are empty. */
160 newdb.hdr.v.hash_bits = INITIAL_HASH_BITS;
161 newdb.hdr.v.hash_off = offsetof(struct new_database, hash);
162 set_header(tdb, &newdb.hrec, 0,
163 sizeof(newdb.hash), sizeof(newdb.hash), 0);
164 memset(newdb.hash, 0, sizeof(newdb.hash));
167 memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
168 strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
170 /* This creates an endian-converted database, as if read from disk */
172 (char *)&newdb.hdr + magic_off,
173 sizeof(newdb) - magic_off);
175 tdb->header = newdb.hdr;
177 if (tdb->flags & TDB_INTERNAL) {
178 tdb->map_size = sizeof(newdb);
179 tdb->map_ptr = malloc(tdb->map_size);
181 tdb->ecode = TDB_ERR_OOM;
184 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
187 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
190 if (ftruncate(tdb->fd, 0) == -1)
193 if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
194 tdb->ecode = TDB_ERR_IO;
200 struct tdb_context *tdb_open(const char *name, int tdb_flags,
201 int open_flags, mode_t mode,
202 union tdb_attribute *attr)
204 struct tdb_context *tdb;
210 tdb = malloc(sizeof(*tdb));
219 /* map_size will be set below. */
220 tdb->ecode = TDB_SUCCESS;
221 /* header will be read in below. */
222 tdb->header_uptodate = false;
223 tdb->flags = tdb_flags;
224 tdb->log = null_log_fn;
225 tdb->log_priv = NULL;
226 tdb->khash = jenkins_hash;
227 tdb->hash_priv = NULL;
228 tdb->transaction = NULL;
229 /* last_zone will be set below. */
235 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
236 "tdb_open: attributes not yet supported\n");
241 if ((open_flags & O_ACCMODE) == O_WRONLY) {
242 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
243 "tdb_open: can't open tdb %s write-only\n", name);
248 if ((open_flags & O_ACCMODE) == O_RDONLY) {
249 tdb->read_only = true;
250 /* read only databases don't do locking */
251 tdb->flags |= TDB_NOLOCK;
253 tdb->read_only = false;
255 /* internal databases don't need any of the rest. */
256 if (tdb->flags & TDB_INTERNAL) {
257 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
258 if (tdb_new_database(tdb) != 0) {
259 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
260 "tdb_open: tdb_new_database failed!");
263 TEST_IT(tdb->flags & TDB_CONVERT);
264 tdb_convert(tdb, &tdb->header, sizeof(tdb->header));
268 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
269 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
270 "tdb_open: could not open file %s: %s\n",
271 name, strerror(errno));
272 goto fail; /* errno set by open(2) */
275 /* on exec, don't inherit the fd */
276 v = fcntl(tdb->fd, F_GETFD, 0);
277 fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
279 /* ensure there is only one process initialising at once */
280 if (tdb_lock_open(tdb) == -1) {
281 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
282 "tdb_open: failed to get open lock on %s: %s\n",
283 name, strerror(errno));
284 goto fail; /* errno set by tdb_brlock */
287 if (!tdb_pread_all(tdb->fd, &tdb->header, sizeof(tdb->header), 0)
288 || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
289 if (!(open_flags & O_CREAT) || tdb_new_database(tdb) == -1) {
291 errno = EIO; /* ie bad format or something */
295 } else if (tdb->header.version != TDB_VERSION) {
296 if (tdb->header.version == bswap_64(TDB_VERSION))
297 tdb->flags |= TDB_CONVERT;
300 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
301 "tdb_open: %s is unknown version 0x%llx\n",
302 name, (long long)tdb->header.version);
308 tdb_convert(tdb, &tdb->header, sizeof(tdb->header));
309 hash_test = TDB_HASH_MAGIC;
310 hash_test = tdb->khash(&hash_test, sizeof(hash_test),
311 tdb->header.hash_seed, tdb->hash_priv);
312 if (tdb->header.hash_test != hash_test) {
313 /* wrong hash variant */
314 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
315 "tdb_open: %s uses a different hash function\n",
321 if (fstat(tdb->fd, &st) == -1)
324 /* Is it already in the open list? If so, fail. */
325 if (tdb_already_open(st.st_dev, st.st_ino)) {
327 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
328 "tdb_open: %s (%d,%d) is already open in this process\n",
329 name, (int)st.st_dev, (int)st.st_ino);
334 tdb->name = strdup(name);
340 tdb->map_size = st.st_size;
341 tdb->device = st.st_dev;
342 tdb->inode = st.st_ino;
344 tdb_unlock_open(tdb);
361 if (tdb->flags & TDB_INTERNAL) {
366 free((char *)tdb->name);
368 if (close(tdb->fd) != 0)
369 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
370 "tdb_open: failed to close tdb->fd"
377 static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
379 return memcmp(data.dptr, key.dptr, data.dsize) == 0;
382 static void unlock_lists(struct tdb_context *tdb,
383 uint64_t start, uint64_t end, int ltype)
386 tdb_unlock_list(tdb, start, ltype);
387 start = (start + 1) & ((1ULL << tdb->header.v.hash_bits) - 1);
388 } while (start != end);
391 /* FIXME: Return header copy? */
392 /* Returns -1 or offset of entry (0 if not found).
393 * Locks hash entried from *start to *end (where the entry was found). */
394 static tdb_off_t find_bucket_and_lock(struct tdb_context *tdb,
395 const struct tdb_data *key,
405 /* hash_bits might be out of date... */
407 *start = *end = hash & ((1ULL << tdb->header.v.hash_bits) - 1);
408 hextra = hash >> tdb->header.v.hash_bits;
410 /* FIXME: can we avoid locks for some fast paths? */
411 if (tdb_lock_list(tdb, *end, ltype, TDB_LOCK_WAIT) == -1)
414 /* We only need to check this for first lock. */
415 if (unlikely(update_header(tdb))) {
416 tdb_unlock_list(tdb, *end, ltype);
420 while ((off = tdb_read_off(tdb, tdb->header.v.hash_off
421 + *end * sizeof(tdb_off_t)))
423 struct tdb_used_record pad, *r;
424 uint64_t keylen, next;
426 /* Didn't find it? */
430 #if 0 /* FIXME: Check other bits. */
431 unsigned int bits, bitmask, hoffextra;
432 /* Bottom three bits show how many extra hash bits. */
433 bits = (off & ((1 << TDB_EXTRA_HASHBITS_NUM) - 1)) + 1;
434 bitmask = (1 << bits)-1;
435 hoffextra = ((off >> TDB_EXTRA_HASHBITS_NUM) & bitmask);
436 if ((hextra & bitmask) != hoffextra)
440 r = tdb_get(tdb, off, &pad, sizeof(*r));
444 if (rec_magic(r) != TDB_MAGIC) {
445 tdb->ecode = TDB_ERR_CORRUPT;
446 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
447 "find_bucket_and_lock: bad magic 0x%llx"
448 " at offset %llu!\n",
449 (long long)rec_magic(r), (long long)off);
453 /* FIXME: check extra bits in header! */
454 keylen = rec_key_length(r);
455 if (keylen != key->dsize)
458 switch (tdb_parse_data(tdb, *key, off + sizeof(*r), key->dsize,
459 tdb_key_compare, NULL)) {
462 *room = rec_data_length(r) + rec_extra_padding(r);
463 return off >> TDB_EXTRA_HASHBITS_NUM;
471 /* Lock next bucket. */
472 /* FIXME: We can deadlock if this wraps! */
473 next = (*end + 1) & ((1ULL << tdb->header.v.hash_bits) - 1);
474 if (next == *start) {
475 tdb->ecode = TDB_ERR_CORRUPT;
476 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
477 "find_bucket_and_lock: full hash table!\n");
480 if (tdb_lock_list(tdb, next, ltype, TDB_LOCK_WAIT) == -1)
486 TEST_IT(*end < *start);
487 unlock_lists(tdb, *start, *end, ltype);
491 static int update_rec_hdr(struct tdb_context *tdb,
498 struct tdb_used_record rec;
500 if (set_header(tdb, &rec, keylen, datalen, room - datalen, h))
503 return tdb_write_convert(tdb, off, &rec, sizeof(rec));
506 /* If we fail, others will try after us. */
507 static void enlarge_hash(struct tdb_context *tdb)
510 uint64_t h, num = 1ULL << tdb->header.v.hash_bits;
511 struct tdb_used_record pad, *r;
513 /* FIXME: We should do this without holding locks throughout. */
514 if (tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false) == -1)
517 if (unlikely(update_header(tdb))) {
518 /* Someone else enlarged for us? Nothing to do. */
519 if ((1ULL << tdb->header.v.hash_bits) != num)
523 newoff = alloc(tdb, 0, num * 2, 0, false);
524 if (unlikely(newoff == TDB_OFF_ERR))
526 if (unlikely(newoff == 0)) {
527 if (tdb_expand(tdb, 0, num * 2, false) == -1)
529 newoff = alloc(tdb, 0, num * 2, 0, false);
530 if (newoff == TDB_OFF_ERR || newoff == 0)
534 /* FIXME: If the space before is empty, we know this is in its ideal
535 * location. We can steal a bit from the pointer to avoid rehash. */
536 for (i = tdb_find_nonzero_off(tdb, tdb->header.v.hash_off, num);
538 i += tdb_find_nonzero_off(tdb, tdb->header.v.hash_off
539 + i*sizeof(tdb_off_t), num - i)) {
541 off = tdb_read_off(tdb, tdb->header.v.hash_off
542 + i*sizeof(tdb_off_t));
543 if (unlikely(off == TDB_OFF_ERR))
545 if (unlikely(!off)) {
546 tdb->ecode = TDB_ERR_CORRUPT;
547 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
548 "find_bucket_and_lock: zero hash bucket!\n");
551 h = hash_record(tdb, off);
552 /* FIXME: Encode extra hash bits! */
553 if (tdb_write_off(tdb, newoff
554 + (h & ((num * 2) - 1)) * sizeof(uint64_t),
559 /* Free up old hash. */
560 r = tdb_get(tdb, tdb->header.v.hash_off, &pad, sizeof(*r));
563 add_free_record(tdb, tdb->header.v.hash_off,
564 rec_data_length(r) + rec_extra_padding(r));
566 /* Now we write the modified header. */
567 tdb->header.v.generation++;
568 tdb->header.v.hash_bits++;
569 tdb->header.v.hash_off = newoff;
570 tdb_write_convert(tdb, offsetof(struct tdb_header, v),
571 &tdb->header.v, sizeof(tdb->header.v));
573 tdb_allrecord_unlock(tdb, F_WRLCK);
576 int tdb_store(struct tdb_context *tdb,
577 struct tdb_data key, struct tdb_data dbuf, int flag)
579 tdb_off_t new_off, off, start, end, room;
581 bool growing = false;
583 h = tdb_hash(tdb, key.dptr, key.dsize);
584 off = find_bucket_and_lock(tdb, &key, h, &start, &end, &room, F_WRLCK);
585 if (off == TDB_OFF_ERR)
588 /* Now we have lock on this hash bucket. */
589 if (flag == TDB_INSERT) {
591 tdb->ecode = TDB_ERR_EXISTS;
596 if (room >= key.dsize + dbuf.dsize) {
598 if (update_rec_hdr(tdb, off,
599 key.dsize, dbuf.dsize,
604 /* FIXME: See if right record is free? */
605 /* Hint to allocator that we've realloced. */
608 if (flag == TDB_MODIFY) {
609 /* if the record doesn't exist and we
610 are in TDB_MODIFY mode then we should fail
612 tdb->ecode = TDB_ERR_NOEXIST;
618 /* Allocate a new record. */
619 new_off = alloc(tdb, key.dsize, dbuf.dsize, h, growing);
621 unlock_lists(tdb, start, end, F_WRLCK);
622 /* Expand, then try again... */
623 if (tdb_expand(tdb, key.dsize, dbuf.dsize, growing) == -1)
625 return tdb_store(tdb, key, dbuf, flag);
628 /* We didn't like the existing one: remove it. */
630 add_free_record(tdb, off, sizeof(struct tdb_used_record)
635 off = tdb->header.v.hash_off + end * sizeof(tdb_off_t);
636 /* FIXME: Encode extra hash bits! */
637 if (tdb_write_off(tdb, off, new_off) == -1)
640 off = new_off + sizeof(struct tdb_used_record);
641 if (tdb->methods->write(tdb, off, key.dptr, key.dsize) == -1)
644 if (tdb->methods->write(tdb, off, dbuf.dptr, dbuf.dsize) == -1)
647 /* FIXME: tdb_increment_seqnum(tdb); */
648 unlock_lists(tdb, start, end, F_WRLCK);
650 /* By simple trial and error, this roughly approximates a 60%
652 if (unlikely(end - start > 4 * tdb->header.v.hash_bits - 32))
658 unlock_lists(tdb, start, end, F_WRLCK);
662 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
664 tdb_off_t off, start, end, room;
666 struct tdb_used_record pad, *r;
669 h = tdb_hash(tdb, key.dptr, key.dsize);
670 off = find_bucket_and_lock(tdb, &key, h, &start, &end, &room, F_RDLCK);
671 if (off == TDB_OFF_ERR)
675 unlock_lists(tdb, start, end, F_RDLCK);
676 tdb->ecode = TDB_SUCCESS;
680 r = tdb_get(tdb, off, &pad, sizeof(*r));
682 unlock_lists(tdb, start, end, F_RDLCK);
686 ret.dsize = rec_data_length(r);
687 ret.dptr = tdb_alloc_read(tdb, off + sizeof(*r) + key.dsize,
689 unlock_lists(tdb, start, end, F_RDLCK);
693 static int hash_add(struct tdb_context *tdb, uint64_t h, tdb_off_t off)
695 tdb_off_t i, hoff, len, num;
697 i = (h & ((1ULL << tdb->header.v.hash_bits) - 1));
698 hoff = tdb->header.v.hash_off + i * sizeof(tdb_off_t);
699 len = (1ULL << tdb->header.v.hash_bits) - i;
701 /* Look for next space. */
702 num = tdb_find_zero_off(tdb, hoff, len);
703 if (unlikely(num == len)) {
704 hoff = tdb->header.v.hash_off;
705 len = (1ULL << tdb->header.v.hash_bits);
706 num = tdb_find_zero_off(tdb, hoff, len);
710 /* FIXME: Encode extra hash bits! */
711 return tdb_write_off(tdb, hoff + num * sizeof(tdb_off_t), off);
714 static int unlink_used_record(struct tdb_context *tdb, tdb_off_t chain,
715 uint64_t *extra_locks)
717 tdb_off_t num, len, i, hoff;
719 /* FIXME: Maybe lock more in search? Maybe don't lock if scan
722 len = (1ULL << tdb->header.v.hash_bits) - (chain + 1);
723 hoff = tdb->header.v.hash_off + (chain + 1) * sizeof(tdb_off_t);
724 num = tdb_find_zero_off(tdb, hoff, len);
726 /* We want to lock the zero entry, too. In the wrap case,
727 * this locks one extra. That's harmless. */
730 for (i = chain + 1; i < chain + 1 + num; i++) {
731 if (tdb_lock_list(tdb, i, F_WRLCK, TDB_LOCK_WAIT) == -1) {
733 unlock_lists(tdb, chain + 1, i-1, F_WRLCK);
738 /* The wrap case: we need those locks out of order! */
739 if (unlikely(num == len + 1)) {
740 *extra_locks = tdb_find_zero_off(tdb, tdb->header.v.hash_off,
741 1ULL << tdb->header.v.hash_bits);
743 for (i = 0; i < *extra_locks; i++) {
744 if (tdb_lock_list(tdb, i, F_WRLCK, TDB_LOCK_NOWAIT)) {
745 /* Failed. Caller must lock in order. */
747 unlock_lists(tdb, 0, i-1, F_WRLCK);
748 unlock_lists(tdb, chain + 1, chain + num,
756 /* Now we have the locks, be certain that offset is still 0! */
757 hoff = tdb->header.v.hash_off
758 + (((chain + num) * sizeof(tdb_off_t))
759 & ((1ULL << tdb->header.v.hash_bits) - 1));
761 if (unlikely(tdb_read_off(tdb, hoff) != 0)) {
762 unlock_lists(tdb, chain + 1, chain + num, F_WRLCK);
766 /* OK, all locked. Unlink first one. */
767 hoff = tdb->header.v.hash_off + chain * sizeof(tdb_off_t);
768 if (tdb_write_off(tdb, hoff, 0) == -1)
771 /* Rehash the rest. */
772 for (i = 1; i < num; i++) {
776 hoff = tdb->header.v.hash_off
777 + (((chain + i) * sizeof(tdb_off_t))
778 & ((1ULL << tdb->header.v.hash_bits) - 1));
779 off = tdb_read_off(tdb, hoff);
780 if (unlikely(off == TDB_OFF_ERR))
783 /* Maybe use a bit to indicate it is in ideal place? */
784 h = hash_record(tdb, off);
785 /* Is it happy where it is? */
786 if ((h & ((1ULL << tdb->header.v.hash_bits)-1)) == (chain + i))
790 if (tdb_write_off(tdb, hoff, 0) == -1)
794 if (hash_add(tdb, h, off) == -1)
797 unlock_lists(tdb, chain + 1, chain + num, F_WRLCK);
801 unlock_lists(tdb, chain + 1, chain + num, F_WRLCK);
805 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
807 tdb_off_t off, start, end, room, extra_locks = 0;
811 h = tdb_hash(tdb, key.dptr, key.dsize);
812 off = find_bucket_and_lock(tdb, &key, h, &start, &end, &room, F_WRLCK);
813 if (off == TDB_OFF_ERR)
817 unlock_lists(tdb, start, end, F_WRLCK);
818 tdb->ecode = TDB_ERR_NOEXIST;
822 ret = unlink_used_record(tdb, end, &extra_locks);
823 if (unlikely(ret == 1)) {
826 unlock_lists(tdb, start, end, F_WRLCK);
828 /* We need extra locks at the start. */
829 for (i = 0; i < extra_locks; i++) {
830 if (tdb_lock_list(tdb, i, F_WRLCK, TDB_LOCK_WAIT)) {
832 unlock_lists(tdb, 0, i-1, F_WRLCK);
836 /* Try again now we're holding more locks. */
837 ret = tdb_delete(tdb, key);
838 unlock_lists(tdb, 0, i, F_WRLCK);
841 unlock_lists(tdb, start, end, F_WRLCK);
845 int tdb_close(struct tdb_context *tdb)
847 struct tdb_context **i;
851 if (tdb->transaction) {
852 tdb_transaction_cancel(tdb);
855 tdb_trace(tdb, "tdb_close");
858 if (tdb->flags & TDB_INTERNAL)
863 free((char *)tdb->name);
865 ret = close(tdb->fd);
870 /* Remove from contexts list */
871 for (i = &tdbs; *i; i = &(*i)->next) {
886 enum TDB_ERROR tdb_error(struct tdb_context *tdb)