]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb.c
tdb2: get rid of zones
[ccan] / ccan / tdb2 / tdb.c
index 1a229dc2b94c1538e5093a8df55d74b8dd8d74f8..c0561fb9fe1f2511797aa5604d368721a3a57f61 100644 (file)
@@ -10,7 +10,7 @@ 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
+PRINTF_FMT(4, 5) static void
 null_log_fn(struct tdb_context *tdb,
            enum tdb_debug_level level, void *priv,
            const char *fmt, ...)
@@ -80,32 +80,24 @@ static uint64_t random_number(struct tdb_context *tdb)
 
 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_freelist flist;
 };
 
 /* 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),
@@ -115,26 +107,11 @@ static int tdb_new_database(struct tdb_context *tdb, struct tdb_header *hdr)
        /* 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_list = offsetof(struct new_database, flist);
+       memset(&newdb.flist, 0, sizeof(newdb.flist));
+       set_header(NULL, &newdb.flist.hdr, 0,
+                  sizeof(newdb.flist.buckets), sizeof(newdb.flist.buckets), 1);
 
        /* Magic food */
        memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
@@ -143,13 +120,12 @@ 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;
@@ -164,7 +140,7 @@ 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)) {
+       if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
                tdb->ecode = TDB_ERR_IO;
                return -1;
        }
@@ -181,6 +157,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        uint64_t hash_test;
        unsigned v;
        struct tdb_header hdr;
+       struct tdb_attribute_seed *seed = NULL;
 
        tdb = malloc(sizeof(*tdb));
        if (!tdb) {
@@ -199,7 +176,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        tdb->log_priv = NULL;
        tdb->transaction = NULL;
        tdb_hash_init(tdb);
-       /* last_zone will be set below. */
        tdb_io_init(tdb);
        tdb_lock_init(tdb);
 
@@ -213,6 +189,9 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                        tdb->khash = attr->hash.hash_fn;
                        tdb->hash_priv = attr->hash.hash_private;
                        break;
+               case TDB_ATTRIBUTE_SEED:
+                       seed = &attr->seed;
+                       break;
                default:
                        tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
                                 "tdb_open: unknown attribute type %u\n",
@@ -234,20 +213,23 @@ 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) {
+               if (tdb_new_database(tdb, seed, &hdr) != 0) {
                        tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
                                 "tdb_open: tdb_new_database failed!");
                        goto fail;
                }
                tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
                tdb->hash_seed = hdr.hash_seed;
-               tdb_zone_init(tdb);
+               tdb_flist_init(tdb);
                return tdb;
        }
 
@@ -272,7 +254,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
 
        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 (!(open_flags & O_CREAT)
+                   || tdb_new_database(tdb, seed, &hdr) == -1) {
                        if (errno == 0) {
                                errno = EIO; /* ie bad format or something */
                        }
@@ -330,8 +313,7 @@ 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)
+       if (tdb_flist_init(tdb) == -1)
                goto fail;
 
        tdb->next = tdbs;
@@ -374,8 +356,7 @@ 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, keylen, datalen, keylen + dataroom, h))
                return -1;
 
        return tdb_write_convert(tdb, off, rec, sizeof(*rec));
@@ -386,7 +367,6 @@ 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;
@@ -398,7 +378,7 @@ static int replace_data(struct tdb_context *tdb,
 
        /* We didn't like the existing one: remove it. */
        if (old_off) {
-               add_free_record(tdb, old_zone, old_off,
+               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 +409,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;
 
@@ -470,8 +450,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 +470,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;
 
@@ -540,8 +519,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 +537,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 +560,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 +574,7 @@ 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,
+       if (add_free_record(tdb, off,
                            sizeof(struct tdb_used_record)
                            + rec_key_length(&rec)
                            + rec_data_length(&rec)
@@ -656,3 +634,21 @@ enum TDB_ERROR tdb_error(struct tdb_context *tdb)
 {
        return tdb->ecode;
 }
+
+const char *tdb_errorstr(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";
+}