tdb2: add TDB_RDONLY flag, allow setting/unsetting it.
[ccan] / ccan / tdb2 / open.c
index f7d6b3c5f3e5dbe356d9b842fcb1c30c36e93800..68f707b920ec8cf72bcf66b9788c1abd25bc26c0 100644 (file)
@@ -1,4 +1,22 @@
+ /*
+   Trivial Database 2: opening and closing TDBs
+   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/hash/hash.h>
 #include <assert.h>
 
 /* all lock info, to detect double-opens (fcntl file don't nest!) */
@@ -101,10 +119,10 @@ static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
        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.hash_test = tdb->hash_fn(&newdb.hdr.hash_test,
+                                          sizeof(newdb.hdr.hash_test),
+                                          newdb.hdr.hash_seed,
+                                          tdb->hash_data);
        newdb.hdr.recovery = 0;
        newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK;
        newdb.hdr.seqnum = 0;
@@ -173,7 +191,7 @@ static enum TDB_ERROR tdb_new_file(struct tdb_context *tdb)
        tdb->file = malloc(sizeof(*tdb->file));
        if (!tdb->file)
                return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
-                                 "tdb_open: could alloc tdb_file structure");
+                                 "tdb_open: cannot alloc tdb_file structure");
        tdb->file->num_lockrecs = 0;
        tdb->file->lockrecs = NULL;
        tdb->file->allrecord_lock.count = 0;
@@ -181,6 +199,142 @@ static enum TDB_ERROR tdb_new_file(struct tdb_context *tdb)
        return TDB_SUCCESS;
 }
 
+enum TDB_ERROR tdb_set_attribute(struct tdb_context *tdb,
+                                const union tdb_attribute *attr)
+{
+       switch (attr->base.attr) {
+       case TDB_ATTRIBUTE_LOG:
+               tdb->log_fn = attr->log.fn;
+               tdb->log_data = attr->log.data;
+               break;
+       case TDB_ATTRIBUTE_HASH:
+       case TDB_ATTRIBUTE_SEED:
+       case TDB_ATTRIBUTE_OPENHOOK:
+               return tdb->last_error
+                       = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                    TDB_LOG_USE_ERROR,
+                                    "tdb_set_attribute:"
+                                    " cannot set %s after opening",
+                                    attr->base.attr == TDB_ATTRIBUTE_HASH
+                                    ? "TDB_ATTRIBUTE_HASH"
+                                    : attr->base.attr == TDB_ATTRIBUTE_SEED
+                                    ? "TDB_ATTRIBUTE_SEED"
+                                    : "TDB_ATTRIBUTE_OPENHOOK");
+       case TDB_ATTRIBUTE_STATS:
+               return tdb->last_error
+                       = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                    TDB_LOG_USE_ERROR,
+                                    "tdb_set_attribute:"
+                                    " cannot set TDB_ATTRIBUTE_STATS");
+       case TDB_ATTRIBUTE_FLOCK:
+               tdb->lock_fn = attr->flock.lock;
+               tdb->unlock_fn = attr->flock.unlock;
+               tdb->lock_data = attr->flock.data;
+               break;
+       default:
+               return tdb->last_error
+                       = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                    TDB_LOG_USE_ERROR,
+                                    "tdb_set_attribute:"
+                                    " unknown attribute type %u",
+                                    attr->base.attr);
+       }
+       return TDB_SUCCESS;
+}
+
+static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
+                            void *unused)
+{
+       uint64_t ret;
+       /* hash64_stable assumes lower bits are more important; they are a
+        * slightly better hash.  We use the upper bits first, so swap them. */
+       ret = hash64_stable((const unsigned char *)key, length, seed);
+       return (ret >> 32) | (ret << 32);
+}
+
+enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
+                                union tdb_attribute *attr)
+{
+       switch (attr->base.attr) {
+       case TDB_ATTRIBUTE_LOG:
+               if (!tdb->log_fn)
+                       return tdb->last_error = TDB_ERR_NOEXIST;
+               attr->log.fn = tdb->log_fn;
+               attr->log.data = tdb->log_data;
+               break;
+       case TDB_ATTRIBUTE_HASH:
+               attr->hash.fn = tdb->hash_fn;
+               attr->hash.data = tdb->hash_data;
+               break;
+       case TDB_ATTRIBUTE_SEED:
+               attr->seed.seed = tdb->hash_seed;
+               break;
+       case TDB_ATTRIBUTE_OPENHOOK:
+               return tdb->last_error
+                       = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                    TDB_LOG_USE_ERROR,
+                                    "tdb_get_attribute:"
+                                    " cannot get TDB_ATTRIBUTE_OPENHOOK");
+       case TDB_ATTRIBUTE_STATS: {
+               size_t size = attr->stats.size;
+               if (size > tdb->stats.size)
+                       size = tdb->stats.size;
+               memcpy(&attr->stats, &tdb->stats, size);
+               break;
+       }
+       case TDB_ATTRIBUTE_FLOCK:
+               attr->flock.lock = tdb->lock_fn;
+               attr->flock.unlock = tdb->unlock_fn;
+               attr->flock.data = tdb->lock_data;
+               break;
+       default:
+               return tdb->last_error
+                       = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                    TDB_LOG_USE_ERROR,
+                                    "tdb_get_attribute:"
+                                    " unknown attribute type %u",
+                                    attr->base.attr);
+       }
+       attr->base.next = NULL;
+       return TDB_SUCCESS;
+}
+
+void tdb_unset_attribute(struct tdb_context *tdb,
+                        enum tdb_attribute_type type)
+{
+       switch (type) {
+       case TDB_ATTRIBUTE_LOG:
+               tdb->log_fn = NULL;
+               break;
+       case TDB_ATTRIBUTE_HASH:
+       case TDB_ATTRIBUTE_SEED:
+       case TDB_ATTRIBUTE_OPENHOOK:
+               tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
+                          "tdb_unset_attribute: cannot unset %s after opening",
+                          type == TDB_ATTRIBUTE_HASH
+                          ? "TDB_ATTRIBUTE_HASH"
+                          : type == TDB_ATTRIBUTE_SEED
+                          ? "TDB_ATTRIBUTE_SEED"
+                          : "TDB_ATTRIBUTE_OPENHOOK");
+               break;
+       case TDB_ATTRIBUTE_STATS:
+               tdb_logerr(tdb, TDB_ERR_EINVAL,
+                          TDB_LOG_USE_ERROR,
+                          "tdb_unset_attribute:"
+                          "cannot unset TDB_ATTRIBUTE_STATS");
+               break;
+       case TDB_ATTRIBUTE_FLOCK:
+               tdb->lock_fn = tdb_fcntl_lock;
+               tdb->unlock_fn = tdb_fcntl_unlock;
+               break;
+       default:
+               tdb_logerr(tdb, TDB_ERR_EINVAL,
+                          TDB_LOG_USE_ERROR,
+                          "tdb_unset_attribute: unknown attribute type %u",
+                          type);
+       }
+}
+
 struct tdb_context *tdb_open(const char *name, int tdb_flags,
                             int open_flags, mode_t mode,
                             union tdb_attribute *attr)
@@ -193,59 +347,63 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        ssize_t rlen;
        struct tdb_header hdr;
        struct tdb_attribute_seed *seed = NULL;
+       struct tdb_attribute_openhook *openhook = NULL;
        tdb_bool_err berr;
        enum TDB_ERROR ecode;
+       int openlock;
 
-       tdb = malloc(sizeof(*tdb));
+       tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0));
        if (!tdb) {
                /* Can't log this */
                errno = ENOMEM;
                return NULL;
        }
-       tdb->name = NULL;
+       /* Set name immediately for logging functions. */
+       if (name) {
+               tdb->name = strcpy((char *)(tdb + 1), name);
+       } else {
+               tdb->name = NULL;
+       }
        tdb->direct_access = 0;
        tdb->flags = tdb_flags;
-       tdb->logfn = NULL;
+       tdb->log_fn = NULL;
        tdb->transaction = NULL;
-       tdb->stats = NULL;
        tdb->access = NULL;
+       tdb->open_flags = open_flags;
        tdb->last_error = TDB_SUCCESS;
        tdb->file = NULL;
-       tdb_hash_init(tdb);
+       tdb->lock_fn = tdb_fcntl_lock;
+       tdb->unlock_fn = tdb_fcntl_unlock;
+       tdb->hash_fn = jenkins_hash;
+       memset(&tdb->stats, 0, sizeof(tdb->stats));
+       tdb->stats.base.attr = TDB_ATTRIBUTE_STATS;
+       tdb->stats.size = sizeof(tdb->stats);
        tdb_io_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;
+                       tdb->hash_fn = attr->hash.fn;
+                       tdb->hash_data = attr->hash.data;
                        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);
+               case TDB_ATTRIBUTE_OPENHOOK:
+                       openhook = &attr->openhook;
                        break;
                default:
-                       ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
-                                          TDB_LOG_USE_ERROR,
-                                          "tdb_open:"
-                                          " unknown attribute type %u",
-                                          attr->base.attr);
-                       goto fail;
+                       /* These are set as normal. */
+                       ecode = tdb_set_attribute(tdb, attr);
+                       if (ecode != TDB_SUCCESS)
+                               goto fail;
                }
                attr = attr->base.next;
        }
 
        if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
-                         | TDB_NOSYNC | TDB_SEQNUM)) {
+                         | TDB_NOSYNC | TDB_SEQNUM | TDB_ALLOW_NESTING
+                         | TDB_RDONLY)) {
                ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
                                   "tdb_open: unknown flags %u", tdb_flags);
                goto fail;
@@ -259,11 +417,17 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        }
 
        if ((open_flags & O_ACCMODE) == O_RDONLY) {
-               tdb->read_only = true;
-               tdb->mmap_flags = PROT_READ;
+               openlock = F_RDLCK;
+               tdb->flags |= TDB_RDONLY;
        } else {
-               tdb->read_only = false;
-               tdb->mmap_flags = PROT_READ | PROT_WRITE;
+               if (tdb_flags & TDB_RDONLY) {
+                       ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
+                                          TDB_LOG_USE_ERROR,
+                                          "tdb_open: can't use TDB_RDONLY"
+                                          " without O_RDONLY");
+                       goto fail;
+               }
+               openlock = F_WRLCK;
        }
 
        /* internal databases don't need any of the rest. */
@@ -278,16 +442,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                if (ecode != TDB_SUCCESS) {
                        goto fail;
                }
-               if (name) {
-                       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_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
                tdb->hash_seed = hdr.hash_seed;
                tdb_ftable_init(tdb);
@@ -318,12 +472,15 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                        tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                   "tdb_open: could not stat open %s: %s",
                                   name, strerror(errno));
+                       close(fd);
                        goto fail_errno;
                }
 
                ecode = tdb_new_file(tdb);
-               if (ecode != TDB_SUCCESS)
+               if (ecode != TDB_SUCCESS) {
+                       close(fd);
                        goto fail;
+               }
 
                tdb->file->next = files;
                tdb->file->fd = fd;
@@ -334,9 +491,21 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        }
 
        /* ensure there is only one process initialising at once */
-       ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
+       ecode = tdb_lock_open(tdb, openlock, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
        if (ecode != TDB_SUCCESS) {
-               goto fail;
+               saved_errno = errno;
+               goto fail_errno;
+       }
+
+       /* call their open hook if they gave us one. */
+       if (openhook) {
+               ecode = openhook->fn(tdb->file->fd, openhook->data);
+               if (ecode != TDB_SUCCESS) {
+                       tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
+                                  "tdb_open: open hook failed");
+                       goto fail;
+               }
+               open_flags |= O_CREAT;
        }
 
        /* If they used O_TRUNC, read will return 0. */
@@ -369,6 +538,12 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                                           name, (long long)hdr.version);
                        goto fail;
                }
+       } else if (tdb->flags & TDB_CONVERT) {
+               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
+                                  "tdb_open:"
+                                  " %s does not need TDB_CONVERT",
+                                  name);
+               goto fail;
        }
 
        tdb_convert(tdb, &hdr, sizeof(hdr));
@@ -384,27 +559,23 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                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;
-       }
-
        /* Clear any features we don't understand. */
        if ((open_flags & O_ACCMODE) != O_RDONLY) {
                hdr.features_used &= TDB_FEATURE_MASK;
-               if (tdb_write_convert(tdb, offsetof(struct tdb_header,
-                                                   features_used),
-                                     &hdr.features_used,
-                                     sizeof(hdr.features_used)) == -1)
+               ecode = tdb_write_convert(tdb, offsetof(struct tdb_header,
+                                                       features_used),
+                                         &hdr.features_used,
+                                         sizeof(hdr.features_used));
+               if (ecode != TDB_SUCCESS)
                        goto fail;
        }
 
-       tdb_unlock_open(tdb);
+       tdb_unlock_open(tdb, openlock);
 
        /* This make sure we have current map_size and mmap. */
-       tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
+       ecode = tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
+       if (unlikely(ecode != TDB_SUCCESS))
+               goto fail;
 
        /* Now it's fully formed, recover if necessary. */
        berr = tdb_needs_recovery(tdb);
@@ -454,9 +625,8 @@ fail_errno:
 #ifdef TDB_TRACE
        close(tdb->tracefd);
 #endif
-       free(cast_const(char *, tdb->name));
        if (tdb->file) {
-               tdb_unlock_all(tdb);
+               tdb_lock_cleanup(tdb);
                if (--tdb->file->refcnt == 0) {
                        assert(tdb->file->num_lockrecs == 0);
                        if (tdb->file->map_ptr) {
@@ -495,11 +665,10 @@ int tdb_close(struct tdb_context *tdb)
                else
                        tdb_munmap(tdb->file);
        }
-       free(cast_const(char *, tdb->name));
        if (tdb->file) {
                struct tdb_file **i;
 
-               tdb_unlock_all(tdb);
+               tdb_lock_cleanup(tdb);
                if (--tdb->file->refcnt == 0) {
                        ret = close(tdb->file->fd);