tdb2: unify tdb1_store into tdb_store
[ccan] / ccan / tdb2 / tdb.c
index f6ca01657518af29c1111700c530abda29ba7d43..a7aa4572382b0f8b10ef03c4adc08599fd7e7293 100644 (file)
+ /*
+   Trivial Database 2: fetch, store and misc routines.
+   Copyright (C) Rusty Russell 2010
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
 #include "private.h"
-#include <ccan/tdb2/tdb2.h>
-#include <assert.h>
+#include <ccan/asprintf/asprintf.h>
 #include <stdarg.h>
 
-/* The null return. */
-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;
-
-static bool tdb_already_open(dev_t device, ino_t ino)
-{
-       struct tdb_context *i;
-       
-       for (i = tdbs; i; i = i->next) {
-               if (i->device == device && i->inode == ino) {
-                       return true;
-               }
-       }
-
-       return false;
-}
-
-static bool read_all(int fd, void *buf, size_t len)
-{
-       while (len) {
-               ssize_t ret;
-               ret = read(fd, buf, len);
-               if (ret < 0)
-                       return false;
-               if (ret == 0) {
-                       /* ETOOSHORT? */
-                       errno = EWOULDBLOCK;
-                       return false;
-               }
-               buf = (char *)buf + ret;
-               len -= ret;
-       }
-       return true;
-}
-
-static uint64_t random_number(struct tdb_context *tdb)
-{
-       int fd;
-       uint64_t ret = 0;
-       struct timeval now;
-
-       fd = open("/dev/urandom", O_RDONLY);
-       if (fd >= 0) {
-               if (read_all(fd, &ret, sizeof(ret))) {
-                       close(fd);
-                       return ret;
-               }
-               close(fd);
-       }
-       /* FIXME: Untested!  Based on Wikipedia protocol description! */
-       fd = open("/dev/egd-pool", O_RDWR);
-       if (fd >= 0) {
-               /* Command is 1, next byte is size we want to read. */
-               char cmd[2] = { 1, sizeof(uint64_t) };
-               if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
-                       char reply[1 + sizeof(uint64_t)];
-                       int r = read(fd, reply, sizeof(reply));
-                       if (r > 1) {
-                               /* Copy at least some bytes. */
-                               memcpy(&ret, reply+1, r - 1);
-                               if (reply[0] == sizeof(uint64_t)
-                                   && r == sizeof(reply)) {
-                                       close(fd);
-                                       return ret;
-                               }
-                       }
-               }
-               close(fd);
-       }
-
-       /* Fallback: pid and time. */
-       gettimeofday(&now, NULL);
-       ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
-       tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
-                  "tdb_open: random from getpid and time");
-       return ret;
-}
-
-struct new_database {
-       struct tdb_header hdr;
-       struct tdb_freetable ftable;
-};
-
-/* initialise a new database */
-static enum TDB_ERROR 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 magic_len;
-       ssize_t rlen;
-       enum TDB_ERROR ecode;
-
-       /* Fill in the header */
-       newdb.hdr.version = TDB_VERSION;
-       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 empty. */
-       newdb.hdr.free_table = offsetof(struct new_database, ftable);
-       memset(&newdb.ftable, 0, sizeof(newdb.ftable));
-       ecode = 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);
-       if (ecode != TDB_SUCCESS) {
-               return ecode;
-       }
-
-       /* Magic food */
-       memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
-       strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
-
-       /* 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, sizeof(newdb) - magic_len);
-
-       *hdr = newdb.hdr;
-
-       if (tdb->flags & TDB_INTERNAL) {
-               tdb->map_size = sizeof(newdb);
-               tdb->map_ptr = malloc(tdb->map_size);
-               if (!tdb->map_ptr) {
-                       return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
-                                         "tdb_new_database:"
-                                         " failed to allocate");
-               }
-               memcpy(tdb->map_ptr, &newdb, tdb->map_size);
-               return TDB_SUCCESS;
-       }
-       if (lseek(tdb->fd, 0, SEEK_SET) == -1) {
-               return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                 "tdb_new_database:"
-                                 " failed to seek: %s", strerror(errno));
-       }
-
-       if (ftruncate(tdb->fd, 0) == -1) {
-               return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                 "tdb_new_database:"
-                                 " failed to truncate: %s", strerror(errno));
-       }
-
-       rlen = write(tdb->fd, &newdb, sizeof(newdb));
-       if (rlen != sizeof(newdb)) {
-               if (rlen >= 0)
-                       errno = ENOSPC;
-               return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                 "tdb_new_database: %zi writing header: %s",
-                                 rlen, strerror(errno));
-       }
-       return TDB_SUCCESS;
-}
-
-struct tdb_context *tdb_open(const char *name, int tdb_flags,
-                            int open_flags, mode_t mode,
-                            union tdb_attribute *attr)
-{
-       struct tdb_context *tdb;
-       struct stat st;
-       int saved_errno = 0;
-       uint64_t hash_test;
-       unsigned v;
-       ssize_t rlen;
-       struct tdb_header hdr;
-       struct tdb_attribute_seed *seed = NULL;
-       tdb_bool_err berr;
-       enum TDB_ERROR ecode;
-
-       tdb = malloc(sizeof(*tdb));
-       if (!tdb) {
-               /* Can't log this */
-               errno = ENOMEM;
-               return NULL;
-       }
-       tdb->name = NULL;
-       tdb->map_ptr = NULL;
-       tdb->direct_access = 0;
-       tdb->fd = -1;
-       tdb->map_size = sizeof(struct tdb_header);
-       tdb->flags = tdb_flags;
-       tdb->logfn = NULL;
-       tdb->transaction = NULL;
-       tdb->stats = NULL;
-       tdb->access = NULL;
-       tdb_hash_init(tdb);
-       tdb_io_init(tdb);
-       tdb_lock_init(tdb);
-
-       while (attr) {
-               switch (attr->base.attr) {
-               case TDB_ATTRIBUTE_LOG:
-                       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:
-                       ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
-                                          TDB_LOG_USE_ERROR,
-                                          "tdb_open:"
-                                          " unknown attribute type %u",
-                                          attr->base.attr);
-                       goto fail;
-               }
-               attr = attr->base.next;
-       }
-
-       if ((open_flags & O_ACCMODE) == O_WRONLY) {
-               ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
-                                  "tdb_open: can't open tdb %s write-only",
-                                  name);
-               goto fail;
-       }
-
-       if ((open_flags & O_ACCMODE) == O_RDONLY) {
-               tdb->read_only = true;
-               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);
-               ecode = tdb_new_database(tdb, seed, &hdr);
-               if (ecode != TDB_SUCCESS) {
-                       goto fail;
-               }
-               tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
-               tdb->hash_seed = hdr.hash_seed;
-               tdb_ftable_init(tdb);
-               return tdb;
-       }
-
-       if ((tdb->fd = open(name, open_flags, mode)) == -1) {
-               /* errno set by open(2) */
-               saved_errno = errno;
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                  "tdb_open: could not open file %s: %s",
-                                  name, strerror(errno));
-               goto fail;
-       }
-
-       /* on exec, don't inherit the fd */
-       v = fcntl(tdb->fd, F_GETFD, 0);
-        fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
-
-       /* ensure there is only one process initialising at once */
-       ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
-       if (ecode != TDB_SUCCESS) {
-               goto fail;
-       }
-
-       /* If they used O_TRUNC, read will return 0. */
-       rlen = read(tdb->fd, &hdr, sizeof(hdr));
-       if (rlen == 0 && (open_flags & O_CREAT)) {
-               ecode = tdb_new_database(tdb, seed, &hdr);
-               if (ecode != TDB_SUCCESS) {
-                       goto fail;
-               }
-       } else if (rlen < 0) {
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                  "tdb_open: error %s reading %s",
-                                  strerror(errno), name);
-               goto fail;
-       } else if (rlen < sizeof(hdr)
-                  || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                  "tdb_open: %s is not a tdb file", name);
-               goto fail;
-       }
-
-       if (hdr.version != TDB_VERSION) {
-               if (hdr.version == bswap_64(TDB_VERSION))
-                       tdb->flags |= TDB_CONVERT;
-               else {
-                       /* wrong version */
-                       ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                          "tdb_open:"
-                                          " %s is unknown version 0x%llx",
-                                          name, (long long)hdr.version);
-                       goto fail;
-               }
-       }
-
-       tdb_convert(tdb, &hdr, sizeof(hdr));
-       tdb->hash_seed = hdr.hash_seed;
-       hash_test = TDB_HASH_MAGIC;
-       hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
-       if (hdr.hash_test != hash_test) {
-               /* wrong hash variant */
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                  "tdb_open:"
-                                  " %s uses a different hash function",
-                                  name);
-               goto fail;
-       }
-
-       if (fstat(tdb->fd, &st) == -1) {
-               saved_errno = errno;
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_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 */
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_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) {
-               ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
-                                  "tdb_open: failed to allocate name");
-               goto fail;
-       }
-
-       tdb->device = st.st_dev;
-       tdb->inode = st.st_ino;
-       tdb_unlock_open(tdb);
-
-       /* This make sure we have current map_size and mmap. */
-       tdb->methods->oob(tdb, tdb->map_size + 1, true);
-
-       /* Now it's fully formed, recover if necessary. */
-       berr = tdb_needs_recovery(tdb);
-       if (unlikely(berr != false)) {
-               if (berr < 0) {
-                       ecode = berr;
-                       goto fail;
-               }
-               ecode = tdb_lock_and_recover(tdb);
-               if (ecode != TDB_SUCCESS) {
-                       goto fail;
-               }
-       }
-
-       ecode = tdb_ftable_init(tdb);
-       if (ecode != TDB_SUCCESS) {
-               goto fail;
-       }
-
-       tdb->next = tdbs;
-       tdbs = tdb;
-       return tdb;
-
- fail:
-       /* Map ecode to some logical errno. */
-       if (!saved_errno) {
-               switch (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;
-               default:
-                       saved_errno = EINVAL;
-                       break;
-               }
-       }
-
-#ifdef TDB_TRACE
-       close(tdb->tracefd);
-#endif
-       if (tdb->map_ptr) {
-               if (tdb->flags & TDB_INTERNAL) {
-                       free(tdb->map_ptr);
-               } else
-                       tdb_munmap(tdb);
-       }
-       free(tdb->lockrecs);
-       free((char *)tdb->name);
-       if (tdb->fd != -1)
-               if (close(tdb->fd) != 0)
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                  "tdb_open: failed to close tdb->fd"
-                                  " on error: %s", strerror(errno));
-       free(tdb);
-       errno = saved_errno;
-       return NULL;
-}
-
 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
                                     tdb_off_t off,
                                     tdb_len_t keylen,
@@ -462,10 +55,11 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb,
 
        /* We didn't like the existing one: remove it. */
        if (old_off) {
-               add_stat(tdb, frees, 1);
+               tdb->stats.frees++;
                ecode = add_free_record(tdb, old_off,
                                        sizeof(struct tdb_used_record)
-                                       + key.dsize + old_room);
+                                       + key.dsize + old_room,
+                                       TDB_LOCK_WAIT, true);
                if (ecode == TDB_SUCCESS)
                        ecode = replace_in_hash(tdb, h, new_off);
        } else {
@@ -476,21 +70,41 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb,
        }
 
        new_off += sizeof(struct tdb_used_record);
-       ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
+       ecode = tdb->tdb2.io->twrite(tdb, new_off, key.dptr, key.dsize);
        if (ecode != TDB_SUCCESS) {
                return ecode;
        }
 
        new_off += key.dsize;
-       ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
+       ecode = tdb->tdb2.io->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
        if (ecode != TDB_SUCCESS) {
                return ecode;
        }
 
-       /* FIXME: tdb_increment_seqnum(tdb); */
+       if (tdb->flags & TDB_SEQNUM)
+               tdb_inc_seqnum(tdb);
+
        return TDB_SUCCESS;
 }
 
+static enum TDB_ERROR update_data(struct tdb_context *tdb,
+                                 tdb_off_t off,
+                                 struct tdb_data dbuf,
+                                 tdb_len_t extra)
+{
+       enum TDB_ERROR ecode;
+
+       ecode = tdb->tdb2.io->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
+       if (ecode == TDB_SUCCESS && extra) {
+               /* Put a zero in; future versions may append other data. */
+               ecode = tdb->tdb2.io->twrite(tdb, off + dbuf.dsize, "", 1);
+       }
+       if (tdb->flags & TDB_SEQNUM)
+               tdb_inc_seqnum(tdb);
+
+       return ecode;
+}
+
 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
                         struct tdb_data key, struct tdb_data dbuf, int flag)
 {
@@ -500,9 +114,15 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb,
        struct tdb_used_record rec;
        enum TDB_ERROR ecode;
 
+       if (tdb->flags & TDB_VERSION1) {
+               if (tdb1_store(tdb, key, dbuf, flag) == -1)
+                       return tdb->last_error;
+               return TDB_SUCCESS;
+       }
+
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (TDB_OFF_IS_ERR(off)) {
-               return off;
+               return tdb->last_error = off;
        }
 
        /* Now we have lock on this hash bucket. */
@@ -523,17 +143,16 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb,
                                if (ecode != TDB_SUCCESS) {
                                        goto out;
                                }
-                               ecode = tdb->methods->twrite(tdb,
-                                                            off + sizeof(rec)
-                                                            + key.dsize,
-                                                            dbuf.dptr,
-                                                            dbuf.dsize);
+                               ecode = update_data(tdb,
+                                                   off + sizeof(rec)
+                                                   + key.dsize, dbuf,
+                                                   old_room - dbuf.dsize);
                                if (ecode != TDB_SUCCESS) {
                                        goto out;
                                }
                                tdb_unlock_hashes(tdb, h.hlock_start,
                                                  h.hlock_range, F_WRLCK);
-                               return TDB_SUCCESS;
+                               return tdb->last_error = TDB_SUCCESS;
                        }
                } else {
                        if (flag == TDB_MODIFY) {
@@ -550,7 +169,7 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb,
        ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
 out:
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
-       return ecode;
+       return tdb->last_error = ecode;
 }
 
 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
@@ -566,7 +185,7 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb,
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (TDB_OFF_IS_ERR(off)) {
-               return off;
+               return tdb->last_error = off;
        }
 
        if (off) {
@@ -583,8 +202,8 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb,
                        }
 
                        off += sizeof(rec) + key.dsize + old_dlen;
-                       ecode = tdb->methods->twrite(tdb, off, dbuf.dptr,
-                                                    dbuf.dsize);
+                       ecode = update_data(tdb, off, dbuf,
+                                           rec_extra_padding(&rec));
                        goto out;
                }
 
@@ -598,7 +217,7 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb,
                                                    + dbuf.dsize));
                        goto out;
                }
-               ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
+               ecode = tdb->tdb2.io->tread(tdb, off + sizeof(rec) + key.dsize,
                                            newdata, old_dlen);
                if (ecode != TDB_SUCCESS) {
                        goto out_free_newdata;
@@ -618,7 +237,7 @@ out_free_newdata:
        free(newdata);
 out:
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
-       return ecode;
+       return tdb->last_error = ecode;
 }
 
 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
@@ -631,7 +250,7 @@ enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
 
        off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
        if (TDB_OFF_IS_ERR(off)) {
-               return off;
+               return tdb->last_error = off;
        }
 
        if (!off) {
@@ -647,7 +266,24 @@ enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
        }
 
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
-       return ecode;
+       return tdb->last_error = ecode;
+}
+
+bool tdb_exists(struct tdb_context *tdb, TDB_DATA key)
+{
+       tdb_off_t off;
+       struct tdb_used_record rec;
+       struct hash_info h;
+
+       off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
+       if (TDB_OFF_IS_ERR(off)) {
+               tdb->last_error = off;
+               return false;
+       }
+       tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
+
+       tdb->last_error = TDB_SUCCESS;
+       return off ? true : false;
 }
 
 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
@@ -659,7 +295,7 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
        if (TDB_OFF_IS_ERR(off)) {
-               return off;
+               return tdb->last_error = off;
        }
 
        if (!off) {
@@ -673,56 +309,138 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
        }
 
        /* Free the deleted entry. */
-       add_stat(tdb, frees, 1);
+       tdb->stats.frees++;
        ecode = add_free_record(tdb, off,
                                sizeof(struct tdb_used_record)
                                + rec_key_length(&rec)
                                + rec_data_length(&rec)
-                               + rec_extra_padding(&rec));
+                               + rec_extra_padding(&rec),
+                               TDB_LOCK_WAIT, true);
+
+       if (tdb->flags & TDB_SEQNUM)
+               tdb_inc_seqnum(tdb);
 
 unlock:
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
-       return ecode;
+       return tdb->last_error = ecode;
 }
 
-int tdb_close(struct tdb_context *tdb)
+unsigned int tdb_get_flags(struct tdb_context *tdb)
 {
-       struct tdb_context **i;
-       int ret = 0;
+       return tdb->flags;
+}
 
-       tdb_trace(tdb, "tdb_close");
+static bool inside_transaction(const struct tdb_context *tdb)
+{
+       if (tdb->flags & TDB_VERSION1)
+               return tdb->tdb1.transaction != NULL;
+       else
+               return tdb->tdb2.transaction != NULL;
+}
 
-       if (tdb->transaction) {
-               tdb_transaction_cancel(tdb);
+static bool readonly_changable(struct tdb_context *tdb, const char *caller)
+{
+       if (inside_transaction(tdb)) {
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "%s: can't change"
+                                            " TDB_RDONLY inside transaction",
+                                            caller);
+               return false;
+       }
+
+       if (tdb->file->allrecord_lock.count != 0
+           || tdb->file->num_lockrecs != 0) {
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "%s: can't change"
+                                            " TDB_RDONLY holding locks",
+                                            caller);
+               return false;
        }
+       return true;
+}
 
-       if (tdb->map_ptr) {
-               if (tdb->flags & TDB_INTERNAL)
-                       free(tdb->map_ptr);
-               else
-                       tdb_munmap(tdb);
-       }
-       free((char *)tdb->name);
-       if (tdb->fd != -1) {
-               ret = close(tdb->fd);
-               tdb->fd = -1;
+void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
+{
+       if (tdb->flags & TDB_INTERNAL) {
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "tdb_add_flag: internal db");
+               return;
+       }
+       switch (flag) {
+       case TDB_NOLOCK:
+               tdb->flags |= TDB_NOLOCK;
+               break;
+       case TDB_NOMMAP:
+               tdb->flags |= TDB_NOMMAP;
+               tdb_munmap(tdb->file);
+               break;
+       case TDB_NOSYNC:
+               tdb->flags |= TDB_NOSYNC;
+               break;
+       case TDB_SEQNUM:
+               tdb->flags |= TDB_SEQNUM;
+               break;
+       case TDB_ALLOW_NESTING:
+               tdb->flags |= TDB_ALLOW_NESTING;
+               break;
+       case TDB_RDONLY:
+               if (readonly_changable(tdb, "tdb_add_flag"))
+                       tdb->flags |= TDB_RDONLY;
+               break;
+       default:
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "tdb_add_flag: Unknown flag %u",
+                                            flag);
        }
-       free(tdb->lockrecs);
+}
 
-       /* Remove from contexts list */
-       for (i = &tdbs; *i; i = &(*i)->next) {
-               if (*i == tdb) {
-                       *i = tdb->next;
+void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
+{
+       if (tdb->flags & TDB_INTERNAL) {
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "tdb_remove_flag: internal db");
+               return;
+       }
+       switch (flag) {
+       case TDB_NOLOCK:
+               tdb->flags &= ~TDB_NOLOCK;
+               break;
+       case TDB_NOMMAP:
+               tdb->flags &= ~TDB_NOMMAP;
+               tdb_mmap(tdb);
+               break;
+       case TDB_NOSYNC:
+               tdb->flags &= ~TDB_NOSYNC;
+               break;
+       case TDB_SEQNUM:
+               tdb->flags &= ~TDB_SEQNUM;
+               break;
+       case TDB_ALLOW_NESTING:
+               tdb->flags &= ~TDB_ALLOW_NESTING;
+               break;
+       case TDB_RDONLY:
+               if ((tdb->open_flags & O_ACCMODE) == O_RDONLY) {
+                       tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                                    TDB_LOG_USE_ERROR,
+                                                    "tdb_remove_flag: can't"
+                                                    " remove TDB_RDONLY on tdb"
+                                                    " opened with O_RDONLY");
                        break;
                }
+               if (readonly_changable(tdb, "tdb_remove_flag"))
+                       tdb->flags &= ~TDB_RDONLY;
+               break;
+       default:
+               tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                            TDB_LOG_USE_ERROR,
+                                            "tdb_remove_flag: Unknown flag %u",
+                                            flag);
        }
-
-#ifdef TDB_TRACE
-       close(tdb->tracefd);
-#endif
-       free(tdb);
-
-       return ret;
 }
 
 const char *tdb_errorstr(enum TDB_ERROR ecode)
@@ -742,6 +460,11 @@ const char *tdb_errorstr(enum TDB_ERROR ecode)
        return "Invalid error code";
 }
 
+enum TDB_ERROR tdb_error(struct tdb_context *tdb)
+{
+       return tdb->last_error;
+}
+
 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
                               enum TDB_ERROR ecode,
                               enum tdb_log_level level,
@@ -753,26 +476,79 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
        /* tdb_open paths care about errno, so save it. */
        int saved_errno = errno;
 
-       if (!tdb->logfn)
+       if (!tdb->log_fn)
                return ecode;
 
-       /* FIXME: Doesn't assume asprintf. */
        va_start(ap, fmt);
-       len = vsnprintf(NULL, 0, fmt, ap);
+       len = vasprintf(&message, fmt, ap);
        va_end(ap);
 
-       message = malloc(len + 1);
-       if (!message) {
-               tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
-                          "out of memory formatting message:");
-               tdb->logfn(tdb, level, tdb->log_private, fmt);
-               return ecode;
+       if (len < 0) {
+               tdb->log_fn(tdb, TDB_LOG_ERROR, TDB_ERR_OOM,
+                           "out of memory formatting message:", tdb->log_data);
+               tdb->log_fn(tdb, level, ecode, fmt, tdb->log_data);
+       } else {
+               tdb->log_fn(tdb, level, ecode, message, tdb->log_data);
+               free(message);
        }
-       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;
        return ecode;
 }
+
+enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
+                                TDB_DATA key,
+                                enum TDB_ERROR (*parse)(TDB_DATA k,
+                                                        TDB_DATA d,
+                                                        void *data),
+                                void *data)
+{
+       tdb_off_t off;
+       struct tdb_used_record rec;
+       struct hash_info h;
+       enum TDB_ERROR ecode;
+
+       off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
+       if (TDB_OFF_IS_ERR(off)) {
+               return tdb->last_error = off;
+       }
+
+       if (!off) {
+               ecode = TDB_ERR_NOEXIST;
+       } else {
+               const void *dptr;
+               dptr = tdb_access_read(tdb, off + sizeof(rec) + key.dsize,
+                                      rec_data_length(&rec), false);
+               if (TDB_PTR_IS_ERR(dptr)) {
+                       ecode = TDB_PTR_ERR(dptr);
+               } else {
+                       TDB_DATA d = tdb_mkdata(dptr, rec_data_length(&rec));
+
+                       ecode = parse(key, d, data);
+                       tdb_access_release(tdb, dptr);
+               }
+       }
+
+       tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
+       return tdb->last_error = ecode;
+}
+
+const char *tdb_name(const struct tdb_context *tdb)
+{
+       return tdb->name;
+}
+
+int64_t tdb_get_seqnum(struct tdb_context *tdb)
+{
+       tdb_off_t off = tdb_read_off(tdb, offsetof(struct tdb_header, seqnum));
+       if (TDB_OFF_IS_ERR(off))
+               tdb->last_error = off;
+       else
+               tdb->last_error = TDB_SUCCESS;
+       return off;
+}
+       
+
+int tdb_fd(const struct tdb_context *tdb)
+{
+       return tdb->file->fd;
+}