tdb2: update documentation
[ccan] / ccan / tdb2 / tdb.c
index 1a229dc2b94c1538e5093a8df55d74b8dd8d74f8..dfa68451e51a878d4be75f5431121ff1f012b9d4 100644 (file)
@@ -1,8 +1,7 @@
 #include "private.h"
 #include <ccan/tdb2/tdb2.h>
-#include <ccan/build_assert/build_assert.h>
-#include <ccan/likely/likely.h>
 #include <assert.h>
+#include <stdarg.h>
 
 /* The null return. */
 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
@@ -10,13 +9,6 @@ struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
 static struct tdb_context *tdbs = NULL;
 
-PRINTF_ATTRIBUTE(4, 5) static void
-null_log_fn(struct tdb_context *tdb,
-           enum tdb_debug_level level, void *priv,
-           const char *fmt, ...)
-{
-}
-
 static bool tdb_already_open(dev_t device, ino_t ino)
 {
        struct tdb_context *i;
@@ -39,8 +31,8 @@ static uint64_t random_number(struct tdb_context *tdb)
        fd = open("/dev/urandom", O_RDONLY);
        if (fd >= 0) {
                if (tdb_read_all(fd, &ret, sizeof(ret))) {
-                       tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
-                                "tdb_open: random from /dev/urandom\n");
+                       tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
+                                "tdb_open: random from /dev/urandom");
                        close(fd);
                        return ret;
                }
@@ -55,9 +47,9 @@ static uint64_t random_number(struct tdb_context *tdb)
                        char reply[1 + sizeof(uint64_t)];
                        int r = read(fd, reply, sizeof(reply));
                        if (r > 1) {
-                               tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
-                                        "tdb_open: %u random bytes from"
-                                        " /dev/egd-pool\n", r-1);
+                               tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
+                                          "tdb_open: %u random bytes from"
+                                          " /dev/egd-pool", r-1);
                                /* Copy at least some bytes. */
                                memcpy(&ret, reply+1, r - 1);
                                if (reply[0] == sizeof(uint64_t)
@@ -73,68 +65,47 @@ static uint64_t random_number(struct tdb_context *tdb)
        /* Fallback: pid and time. */
        gettimeofday(&now, NULL);
        ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
-       tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
-                "tdb_open: random from getpid and time\n");
+       tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
+                  "tdb_open: random from getpid and time");
        return ret;
 }
 
 struct new_database {
        struct tdb_header hdr;
-       /* Initial free zone. */
-       struct free_zone_header zhdr;
-       tdb_off_t free[BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS) + 1];
-       struct tdb_free_record frec;
-       /* Rest up to 1 << INITIAL_ZONE_BITS is empty. */
-       char space[(1 << INITIAL_ZONE_BITS)
-                  - sizeof(struct free_zone_header)
-                  - sizeof(tdb_off_t) * (BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS)+1)
-                  - sizeof(struct tdb_free_record)];
-       uint8_t tailer;
-       /* Don't count final padding! */
+       struct tdb_freetable ftable;
 };
 
 /* initialise a new database */
-static int tdb_new_database(struct tdb_context *tdb, struct tdb_header *hdr)
+static int tdb_new_database(struct tdb_context *tdb,
+                           struct tdb_attribute_seed *seed,
+                           struct tdb_header *hdr)
 {
        /* We make it up in memory, then write it out if not internal */
        struct new_database newdb;
-       unsigned int bucket, magic_len, dbsize;
-
-       /* Don't want any extra padding! */
-       dbsize = offsetof(struct new_database, tailer) + sizeof(newdb.tailer);
+       unsigned int magic_len;
 
        /* Fill in the header */
        newdb.hdr.version = TDB_VERSION;
-       newdb.hdr.hash_seed = random_number(tdb);
+       if (seed)
+               newdb.hdr.hash_seed = seed->seed;
+       else
+               newdb.hdr.hash_seed = random_number(tdb);
        newdb.hdr.hash_test = TDB_HASH_MAGIC;
        newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
                                         sizeof(newdb.hdr.hash_test),
                                         newdb.hdr.hash_seed,
                                         tdb->hash_priv);
+       newdb.hdr.recovery = 0;
        memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
        /* Initial hashes are empty. */
        memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
 
-       /* Free is mostly empty... */
-       newdb.zhdr.zone_bits = INITIAL_ZONE_BITS;
-       memset(newdb.free, 0, sizeof(newdb.free));
-
-       /* Create the single free entry. */
-       newdb.frec.magic_and_meta = TDB_FREE_MAGIC | INITIAL_ZONE_BITS;
-       newdb.frec.data_len = (sizeof(newdb.frec)
-                                - sizeof(struct tdb_used_record)
-                                + sizeof(newdb.space));
-
-       /* Add it to the correct bucket. */
-       bucket = size_to_bucket(INITIAL_ZONE_BITS, newdb.frec.data_len);
-       newdb.free[bucket] = offsetof(struct new_database, frec);
-       newdb.frec.next = newdb.frec.prev = 0;
-
-       /* Clear free space to keep valgrind happy, and avoid leaking stack. */
-       memset(newdb.space, 0, sizeof(newdb.space));
-
-       /* Tailer contains maximum number of free_zone bits. */
-       newdb.tailer = INITIAL_ZONE_BITS;
+       /* Free is empty. */
+       newdb.hdr.free_table = offsetof(struct new_database, ftable);
+       memset(&newdb.ftable, 0, sizeof(newdb.ftable));
+       set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
+                  sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
+                  sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr), 0);
 
        /* Magic food */
        memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
@@ -143,16 +114,16 @@ static int tdb_new_database(struct tdb_context *tdb, struct tdb_header *hdr)
        /* This creates an endian-converted database, as if read from disk */
        magic_len = sizeof(newdb.hdr.magic_food);
        tdb_convert(tdb,
-                   (char *)&newdb.hdr + magic_len,
-                   offsetof(struct new_database, space) - magic_len);
+                   (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
 
        *hdr = newdb.hdr;
 
        if (tdb->flags & TDB_INTERNAL) {
-               tdb->map_size = dbsize;
+               tdb->map_size = sizeof(newdb);
                tdb->map_ptr = malloc(tdb->map_size);
                if (!tdb->map_ptr) {
-                       tdb->ecode = TDB_ERR_OOM;
+                       tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
+                                  "tdb_new_database: failed to allocate");
                        return -1;
                }
                memcpy(tdb->map_ptr, &newdb, tdb->map_size);
@@ -164,8 +135,10 @@ static int tdb_new_database(struct tdb_context *tdb, struct tdb_header *hdr)
        if (ftruncate(tdb->fd, 0) == -1)
                return -1;
 
-       if (!tdb_pwrite_all(tdb->fd, &newdb, dbsize, 0)) {
-               tdb->ecode = TDB_ERR_IO;
+       if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
+               tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
+                          "tdb_new_database: failed to write: %s",
+                          strerror(errno));
                return -1;
        }
        return 0;
@@ -177,16 +150,17 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
 {
        struct tdb_context *tdb;
        struct stat st;
-       int save_errno;
+       int saved_errno = 0;
        uint64_t hash_test;
        unsigned v;
        struct tdb_header hdr;
+       struct tdb_attribute_seed *seed = NULL;
 
        tdb = malloc(sizeof(*tdb));
        if (!tdb) {
                /* Can't log this */
                errno = ENOMEM;
-               goto fail;
+               return NULL;
        }
        tdb->name = NULL;
        tdb->map_ptr = NULL;
@@ -195,38 +169,45 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        tdb->map_size = sizeof(struct tdb_header);
        tdb->ecode = TDB_SUCCESS;
        tdb->flags = tdb_flags;
-       tdb->log = null_log_fn;
-       tdb->log_priv = NULL;
+       tdb->logfn = NULL;
        tdb->transaction = NULL;
+       tdb->stats = NULL;
+       tdb->access = NULL;
        tdb_hash_init(tdb);
-       /* last_zone will be set below. */
        tdb_io_init(tdb);
        tdb_lock_init(tdb);
 
        while (attr) {
                switch (attr->base.attr) {
                case TDB_ATTRIBUTE_LOG:
-                       tdb->log = attr->log.log_fn;
-                       tdb->log_priv = attr->log.log_private;
+                       tdb->logfn = attr->log.log_fn;
+                       tdb->log_private = attr->log.log_private;
                        break;
                case TDB_ATTRIBUTE_HASH:
                        tdb->khash = attr->hash.hash_fn;
                        tdb->hash_priv = attr->hash.hash_private;
                        break;
+               case TDB_ATTRIBUTE_SEED:
+                       seed = &attr->seed;
+                       break;
+               case TDB_ATTRIBUTE_STATS:
+                       tdb->stats = &attr->stats;
+                       /* They have stats we don't know about?  Tell them. */
+                       if (tdb->stats->size > sizeof(attr->stats))
+                               tdb->stats->size = sizeof(attr->stats);
+                       break;
                default:
-                       tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                                "tdb_open: unknown attribute type %u\n",
-                                attr->base.attr);
-                       errno = EINVAL;
+                       tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
+                                  "tdb_open: unknown attribute type %u",
+                                  attr->base.attr);
                        goto fail;
                }
                attr = attr->base.next;
        }
 
        if ((open_flags & O_ACCMODE) == O_WRONLY) {
-               tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                        "tdb_open: can't open tdb %s write-only\n", name);
-               errno = EINVAL;
+               tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
+                          "tdb_open: can't open tdb %s write-only", name);
                goto fail;
        }
 
@@ -234,28 +215,31 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                tdb->read_only = true;
                /* read only databases don't do locking */
                tdb->flags |= TDB_NOLOCK;
-       } else
+               tdb->mmap_flags = PROT_READ;
+       } else {
                tdb->read_only = false;
+               tdb->mmap_flags = PROT_READ | PROT_WRITE;
+       }
 
        /* internal databases don't need any of the rest. */
        if (tdb->flags & TDB_INTERNAL) {
                tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
-               if (tdb_new_database(tdb, &hdr) != 0) {
-                       tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                                "tdb_open: tdb_new_database failed!");
+               if (tdb_new_database(tdb, seed, &hdr) != 0) {
                        goto fail;
                }
                tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
                tdb->hash_seed = hdr.hash_seed;
-               tdb_zone_init(tdb);
+               tdb_ftable_init(tdb);
                return tdb;
        }
 
        if ((tdb->fd = open(name, open_flags, mode)) == -1) {
-               tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
-                        "tdb_open: could not open file %s: %s\n",
-                        name, strerror(errno));
-               goto fail;      /* errno set by open(2) */
+               /* errno set by open(2) */
+               saved_errno = errno;
+               tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
+                          "tdb_open: could not open file %s: %s",
+                          name, strerror(errno));
+               goto fail;
        }
 
        /* on exec, don't inherit the fd */
@@ -263,19 +247,20 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
 
        /* ensure there is only one process initialising at once */
-       if (tdb_lock_open(tdb) == -1) {
-               tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                        "tdb_open: failed to get open lock on %s: %s\n",
-                        name, strerror(errno));
-               goto fail;      /* errno set by tdb_brlock */
+       if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
+               /* errno set by tdb_brlock */
+               saved_errno = errno;
+               goto fail;
        }
 
        if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
            || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
-               if (!(open_flags & O_CREAT) || tdb_new_database(tdb, &hdr) == -1) {
-                       if (errno == 0) {
-                               errno = EIO; /* ie bad format or something */
-                       }
+               if (!(open_flags & O_CREAT)) {
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
+                                  "tdb_open: %s is not a tdb file", name);
+                       goto fail;
+               }
+               if (tdb_new_database(tdb, seed, &hdr) == -1) {
                        goto fail;
                }
        } else if (hdr.version != TDB_VERSION) {
@@ -283,10 +268,9 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                        tdb->flags |= TDB_CONVERT;
                else {
                        /* wrong version */
-                       tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                                "tdb_open: %s is unknown version 0x%llx\n",
-                                name, (long long)hdr.version);
-                       errno = EIO;
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
+                                  "tdb_open: %s is unknown version 0x%llx",
+                                  name, (long long)hdr.version);
                        goto fail;
                }
        }
@@ -297,29 +281,34 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
        if (hdr.hash_test != hash_test) {
                /* wrong hash variant */
-               tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                        "tdb_open: %s uses a different hash function\n",
-                        name);
-               errno = EIO;
+               tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
+                          "tdb_open: %s uses a different hash function",
+                          name);
                goto fail;
        }
 
-       if (fstat(tdb->fd, &st) == -1)
+       if (fstat(tdb->fd, &st) == -1) {
+               saved_errno = errno;
+               tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
+                          "tdb_open: could not stat open %s: %s",
+                          name, strerror(errno));
                goto fail;
+       }
 
        /* Is it already in the open list?  If so, fail. */
        if (tdb_already_open(st.st_dev, st.st_ino)) {
                /* FIXME */
-               tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                        "tdb_open: %s (%d,%d) is already open in this process\n",
-                        name, (int)st.st_dev, (int)st.st_ino);
-               errno = EBUSY;
+               tdb_logerr(tdb, TDB_ERR_NESTING, TDB_DEBUG_ERROR,
+                          "tdb_open: %s (%d,%d) is already open in this"
+                          " process",
+                          name, (int)st.st_dev, (int)st.st_ino);
                goto fail;
        }
 
        tdb->name = strdup(name);
        if (!tdb->name) {
-               errno = ENOMEM;
+               tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
+                          "tdb_open: failed to allocate name");
                goto fail;
        }
 
@@ -330,8 +319,12 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        /* This make sure we have current map_size and mmap. */
        tdb->methods->oob(tdb, tdb->map_size + 1, true);
 
-       /* Now we can pick a random free zone to start from. */
-       if (tdb_zone_init(tdb) == -1)
+       /* Now it's fully formed, recover if necessary. */
+       if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
+               goto fail;
+       }
+
+       if (tdb_ftable_init(tdb) == -1)
                goto fail;
 
        tdb->next = tdbs;
@@ -339,10 +332,30 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        return tdb;
 
  fail:
-       save_errno = errno;
-
-       if (!tdb)
-               return NULL;
+       /* Map ecode to some logical errno. */
+       if (!saved_errno) {
+               switch (tdb->ecode) {
+               case TDB_ERR_CORRUPT:
+               case TDB_ERR_IO:
+                       saved_errno = EIO;
+                       break;
+               case TDB_ERR_LOCK:
+                       saved_errno = EWOULDBLOCK;
+                       break;
+               case TDB_ERR_OOM:
+                       saved_errno = ENOMEM;
+                       break;
+               case TDB_ERR_EINVAL:
+                       saved_errno = EINVAL;
+                       break;
+               case TDB_ERR_NESTING:
+                       saved_errno = EBUSY;
+                       break;
+               default:
+                       saved_errno = EINVAL;
+                       break;
+               }
+       }
 
 #ifdef TDB_TRACE
        close(tdb->tracefd);
@@ -356,15 +369,14 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        free((char *)tdb->name);
        if (tdb->fd != -1)
                if (close(tdb->fd) != 0)
-                       tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                                "tdb_open: failed to close tdb->fd"
-                                " on error!\n");
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
+                                  "tdb_open: failed to close tdb->fd"
+                                  " on error!");
        free(tdb);
-       errno = save_errno;
+       errno = saved_errno;
        return NULL;
 }
 
-/* FIXME: modify, don't rewrite! */
 static int update_rec_hdr(struct tdb_context *tdb,
                          tdb_off_t off,
                          tdb_len_t keylen,
@@ -374,8 +386,8 @@ static int update_rec_hdr(struct tdb_context *tdb,
 {
        uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
 
-       if (set_header(tdb, rec, keylen, datalen, keylen + dataroom, h,
-                      rec_zone_bits(rec)))
+       if (set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
+                      keylen + dataroom, h))
                return -1;
 
        return tdb_write_convert(tdb, off, rec, sizeof(*rec));
@@ -386,19 +398,20 @@ static int replace_data(struct tdb_context *tdb,
                        struct hash_info *h,
                        struct tdb_data key, struct tdb_data dbuf,
                        tdb_off_t old_off, tdb_len_t old_room,
-                       unsigned old_zone,
                        bool growing)
 {
        tdb_off_t new_off;
 
        /* Allocate a new record. */
-       new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, growing);
+       new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
+                       growing);
        if (unlikely(new_off == TDB_OFF_ERR))
                return -1;
 
        /* We didn't like the existing one: remove it. */
        if (old_off) {
-               add_free_record(tdb, old_zone, old_off,
+               add_stat(tdb, frees, 1);
+               add_free_record(tdb, old_off,
                                sizeof(struct tdb_used_record)
                                + key.dsize + old_room);
                if (replace_in_hash(tdb, h, new_off) == -1)
@@ -429,7 +442,7 @@ int tdb_store(struct tdb_context *tdb,
        struct tdb_used_record rec;
        int ret;
 
-       off = find_and_lock(tdb, key, F_WRLCK, &h, &rec);
+       off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (unlikely(off == TDB_OFF_ERR))
                return -1;
 
@@ -457,7 +470,6 @@ int tdb_store(struct tdb_context *tdb,
                                                  h.hlock_range, F_WRLCK);
                                return 0;
                        }
-                       /* FIXME: See if right record is free? */
                } else {
                        if (flag == TDB_MODIFY) {
                                /* if the record doesn't exist and we
@@ -470,8 +482,7 @@ int tdb_store(struct tdb_context *tdb,
        }
 
        /* If we didn't use the old record, this implies we're growing. */
-       ret = replace_data(tdb, &h, key, dbuf, off, old_room,
-                          rec_zone_bits(&rec), off != 0);
+       ret = replace_data(tdb, &h, key, dbuf, off, old_room, off != 0);
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
        return ret;
 
@@ -491,7 +502,7 @@ int tdb_append(struct tdb_context *tdb,
        struct tdb_data new_dbuf;
        int ret;
 
-       off = find_and_lock(tdb, key, F_WRLCK, &h, &rec);
+       off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (unlikely(off == TDB_OFF_ERR))
                return -1;
 
@@ -515,15 +526,13 @@ int tdb_append(struct tdb_context *tdb,
                                          F_WRLCK);
                        return 0;
                }
-               /* FIXME: Check right record free? */
 
                /* Slow path. */
                newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
                if (!newdata) {
-                       tdb->ecode = TDB_ERR_OOM;
-                       tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
-                                "tdb_append: cannot allocate %llu bytes!\n",
-                                (long long)key.dsize + old_dlen + dbuf.dsize);
+                       tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
+                                  "tdb_append: failed to allocate %zu bytes",
+                                  (size_t)(key.dsize+old_dlen+dbuf.dsize));
                        goto fail;
                }
                if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
@@ -540,8 +549,7 @@ int tdb_append(struct tdb_context *tdb,
        }
 
        /* If they're using tdb_append(), it implies they're growing record. */
-       ret = replace_data(tdb, &h, key, new_dbuf, off,
-                          old_room, rec_zone_bits(&rec), true);
+       ret = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
        free(newdata);
 
@@ -559,7 +567,7 @@ struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
        struct hash_info h;
        struct tdb_data ret;
 
-       off = find_and_lock(tdb, key, F_RDLCK, &h, &rec);
+       off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
        if (unlikely(off == TDB_OFF_ERR))
                return tdb_null;
 
@@ -582,7 +590,7 @@ int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
        struct tdb_used_record rec;
        struct hash_info h;
 
-       off = find_and_lock(tdb, key, F_WRLCK, &h, &rec);
+       off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (unlikely(off == TDB_OFF_ERR))
                return -1;
 
@@ -596,7 +604,8 @@ int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
                goto unlock_err;
 
        /* Free the deleted entry. */
-       if (add_free_record(tdb, rec_zone_bits(&rec), off,
+       add_stat(tdb, frees, 1);
+       if (add_free_record(tdb, off,
                            sizeof(struct tdb_used_record)
                            + rec_key_length(&rec)
                            + rec_data_length(&rec)
@@ -616,12 +625,11 @@ int tdb_close(struct tdb_context *tdb)
        struct tdb_context **i;
        int ret = 0;
 
-       /* FIXME:
+       tdb_trace(tdb, "tdb_close");
+
        if (tdb->transaction) {
                tdb_transaction_cancel(tdb);
        }
-       */
-       tdb_trace(tdb, "tdb_close");
 
        if (tdb->map_ptr) {
                if (tdb->flags & TDB_INTERNAL)
@@ -652,7 +660,60 @@ int tdb_close(struct tdb_context *tdb)
        return ret;
 }
 
-enum TDB_ERROR tdb_error(struct tdb_context *tdb)
+enum TDB_ERROR tdb_error(const struct tdb_context *tdb)
 {
        return tdb->ecode;
 }
+
+const char *tdb_errorstr(const struct tdb_context *tdb)
+{
+       /* Gcc warns if you miss a case in the switch, so use that. */
+       switch (tdb->ecode) {
+       case TDB_SUCCESS: return "Success";
+       case TDB_ERR_CORRUPT: return "Corrupt database";
+       case TDB_ERR_IO: return "IO Error";
+       case TDB_ERR_LOCK: return "Locking error";
+       case TDB_ERR_OOM: return "Out of memory";
+       case TDB_ERR_EXISTS: return "Record exists";
+       case TDB_ERR_NESTING: return "Transaction already started";
+       case TDB_ERR_EINVAL: return "Invalid parameter";
+       case TDB_ERR_NOEXIST: return "Record does not exist";
+       case TDB_ERR_RDONLY: return "write not permitted";
+       }
+       return "Invalid error code";
+}
+
+void COLD tdb_logerr(struct tdb_context *tdb,
+                    enum TDB_ERROR ecode,
+                    enum tdb_debug_level level,
+                    const char *fmt, ...)
+{
+       char *message;
+       va_list ap;
+       size_t len;
+       /* tdb_open paths care about errno, so save it. */
+       int saved_errno = errno;
+
+       tdb->ecode = ecode;
+
+       if (!tdb->logfn)
+               return;
+
+       /* FIXME: Doesn't assume asprintf. */
+       va_start(ap, fmt);
+       len = vsnprintf(NULL, 0, fmt, ap);
+       va_end(ap);
+
+       message = malloc(len + 1);
+       if (!message) {
+               tdb->logfn(tdb, level, tdb->log_private,
+                          "out of memory formatting message");
+               return;
+       }
+       va_start(ap, fmt);
+       len = vsprintf(message, fmt, ap);
+       va_end(ap);
+       tdb->logfn(tdb, level, tdb->log_private, message);
+       free(message);
+       errno = saved_errno;
+}