]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb.c
tdb2: rework transaction.c internal functions to return enum TDB_ERROR.
[ccan] / ccan / tdb2 / tdb.c
index 38124582d125625766922e4dab802ca22de2ce82..600d6b765d7bbb7f84d9a4d6f7a374c5a2fadb9c 100644 (file)
@@ -117,9 +117,13 @@ static int tdb_new_database(struct tdb_context *tdb,
        /* 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);
+       tdb->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 (tdb->ecode != TDB_SUCCESS) {
+               return -1;
+       }
 
        /* Magic food */
        memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
@@ -173,6 +177,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        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) {
@@ -265,9 +271,8 @@ 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, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
-               /* errno set by tdb_brlock */
-               saved_errno = errno;
+       tdb->ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
+       if (tdb->ecode != TDB_SUCCESS) {
                goto fail;
        }
 
@@ -346,12 +351,23 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        tdb->methods->oob(tdb, tdb->map_size + 1, true);
 
        /* Now it's fully formed, recover if necessary. */
-       if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
-               goto fail;
+       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) {
+                       tdb->ecode = ecode;
+                       goto fail;
+               }
        }
 
-       if (tdb_ftable_init(tdb) == -1)
+       tdb->ecode = tdb_ftable_init(tdb);
+       if (tdb->ecode != TDB_SUCCESS) {
                goto fail;
+       }
 
        tdb->next = tdbs;
        tdbs = tdb;
@@ -409,12 +425,21 @@ static int update_rec_hdr(struct tdb_context *tdb,
                          uint64_t h)
 {
        uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
+       enum TDB_ERROR ecode;
 
-       if (set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
-                      keylen + dataroom, h))
+       ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
+                          keylen + dataroom, h);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                return -1;
+       }
 
-       return tdb_write_convert(tdb, off, rec, sizeof(*rec));
+       ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
+               return -1;
+       }
+       return 0;
 }
 
 /* Returns -1 on error, 0 on OK */
@@ -425,33 +450,45 @@ static int replace_data(struct tdb_context *tdb,
                        bool growing)
 {
        tdb_off_t new_off;
+       enum TDB_ERROR ecode;
 
        /* Allocate a new record. */
        new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
                        growing);
-       if (unlikely(new_off == TDB_OFF_ERR))
+       if (TDB_OFF_IS_ERR(new_off)) {
+               tdb->ecode = new_off;
                return -1;
+       }
 
        /* We didn't like the existing one: remove it. */
        if (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)
-                       return -1;
+               ecode = add_free_record(tdb, old_off,
+                                       sizeof(struct tdb_used_record)
+                                       + key.dsize + old_room);
+               if (ecode == TDB_SUCCESS)
+                       ecode = replace_in_hash(tdb, h, new_off);
        } else {
-               if (add_to_hash(tdb, h, new_off) == -1)
-                       return -1;
+               ecode = add_to_hash(tdb, h, new_off);
+       }
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
+               return -1;
        }
 
        new_off += sizeof(struct tdb_used_record);
-       if (tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize) == -1)
+       ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                return -1;
+       }
 
        new_off += key.dsize;
-       if (tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
+       ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                return -1;
+       }
 
        /* FIXME: tdb_increment_seqnum(tdb); */
        return 0;
@@ -465,10 +502,13 @@ int tdb_store(struct tdb_context *tdb,
        tdb_len_t old_room = 0;
        struct tdb_used_record rec;
        int ret;
+       enum TDB_ERROR ecode;
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
-       if (unlikely(off == TDB_OFF_ERR))
+       if (TDB_OFF_IS_ERR(off)) {
+               tdb->ecode = off;
                return -1;
+       }
 
        /* Now we have lock on this hash bucket. */
        if (flag == TDB_INSERT) {
@@ -486,10 +526,15 @@ int tdb_store(struct tdb_context *tdb,
                                                   key.dsize, dbuf.dsize,
                                                   &rec, h.h))
                                        goto fail;
-                               if (tdb->methods->twrite(tdb, off + sizeof(rec)
-                                                        + key.dsize,
-                                                        dbuf.dptr, dbuf.dsize))
+                               ecode = tdb->methods->twrite(tdb,
+                                                            off + sizeof(rec)
+                                                            + key.dsize,
+                                                            dbuf.dptr,
+                                                            dbuf.dsize);
+                               if (ecode != TDB_SUCCESS) {
+                                       tdb->ecode = ecode;
                                        goto fail;
+                               }
                                tdb_unlock_hashes(tdb, h.hlock_start,
                                                  h.hlock_range, F_WRLCK);
                                return 0;
@@ -524,11 +569,14 @@ int tdb_append(struct tdb_context *tdb,
        tdb_len_t old_room = 0, old_dlen;
        unsigned char *newdata;
        struct tdb_data new_dbuf;
+       enum TDB_ERROR ecode;
        int ret;
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
-       if (unlikely(off == TDB_OFF_ERR))
+       if (TDB_OFF_IS_ERR(off)) {
+               tdb->ecode = off;
                return -1;
+       }
 
        if (off) {
                old_dlen = rec_data_length(&rec);
@@ -541,9 +589,12 @@ int tdb_append(struct tdb_context *tdb,
                                goto fail;
 
                        off += sizeof(rec) + key.dsize + old_dlen;
-                       if (tdb->methods->twrite(tdb, off, dbuf.dptr,
-                                                dbuf.dsize) == -1)
+                       ecode = tdb->methods->twrite(tdb, off, dbuf.dptr,
+                                                    dbuf.dsize);
+                       if (ecode != TDB_SUCCESS) {
+                               tdb->ecode = ecode;
                                goto fail;
+                       }
 
                        /* FIXME: tdb_increment_seqnum(tdb); */
                        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
@@ -559,8 +610,10 @@ int tdb_append(struct tdb_context *tdb,
                                   (size_t)(key.dsize+old_dlen+dbuf.dsize));
                        goto fail;
                }
-               if (tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
-                                       newdata, old_dlen) != 0) {
+               ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
+                                           newdata, old_dlen);
+               if (ecode != TDB_SUCCESS) {
+                       tdb->ecode = ecode;
                        free(newdata);
                        goto fail;
                }
@@ -592,8 +645,10 @@ struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
        struct tdb_data ret;
 
        off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
-       if (unlikely(off == TDB_OFF_ERR))
+       if (TDB_OFF_IS_ERR(off)) {
+               tdb->ecode = off;
                return tdb_null;
+       }
 
        if (!off) {
                tdb->ecode = TDB_ERR_NOEXIST;
@@ -602,6 +657,10 @@ struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
                ret.dsize = rec_data_length(&rec);
                ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
                                          ret.dsize);
+               if (TDB_PTR_IS_ERR(ret.dptr)) {
+                       tdb->ecode = TDB_PTR_ERR(ret.dptr);
+                       ret = tdb_null;
+               }
        }
 
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
@@ -613,10 +672,13 @@ int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
        tdb_off_t off;
        struct tdb_used_record rec;
        struct hash_info h;
+       enum TDB_ERROR ecode;
 
        off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
-       if (unlikely(off == TDB_OFF_ERR))
+       if (TDB_OFF_IS_ERR(off)) {
+               tdb->ecode = off;
                return -1;
+       }
 
        if (!off) {
                tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
@@ -624,17 +686,23 @@ int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
                return -1;
        }
 
-       if (delete_from_hash(tdb, &h) == -1)
+       ecode = delete_from_hash(tdb, &h);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                goto unlock_err;
+       }
 
        /* Free the deleted entry. */
        add_stat(tdb, frees, 1);
-       if (add_free_record(tdb, off,
-                           sizeof(struct tdb_used_record)
-                           + rec_key_length(&rec)
-                           + rec_data_length(&rec)
-                           + rec_extra_padding(&rec)) != 0)
+       ecode = add_free_record(tdb, off,
+                               sizeof(struct tdb_used_record)
+                               + rec_key_length(&rec)
+                               + rec_data_length(&rec)
+                               + rec_extra_padding(&rec));
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
                goto unlock_err;
+       }
 
        tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
        return 0;
@@ -706,10 +774,10 @@ const char *tdb_errorstr(const struct tdb_context *tdb)
        return "Invalid error code";
 }
 
-void COLD tdb_logerr(struct tdb_context *tdb,
-                    enum TDB_ERROR ecode,
-                    enum tdb_log_level level,
-                    const char *fmt, ...)
+enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
+                              enum TDB_ERROR ecode,
+                              enum tdb_log_level level,
+                              const char *fmt, ...)
 {
        char *message;
        va_list ap;
@@ -720,7 +788,7 @@ void COLD tdb_logerr(struct tdb_context *tdb,
        tdb->ecode = ecode;
 
        if (!tdb->logfn)
-               return;
+               return ecode;
 
        /* FIXME: Doesn't assume asprintf. */
        va_start(ap, fmt);
@@ -732,7 +800,7 @@ void COLD tdb_logerr(struct tdb_context *tdb,
                tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
                           "out of memory formatting message:");
                tdb->logfn(tdb, level, tdb->log_private, fmt);
-               return;
+               return ecode;
        }
        va_start(ap, fmt);
        len = vsprintf(message, fmt, ap);
@@ -740,4 +808,5 @@ void COLD tdb_logerr(struct tdb_context *tdb,
        tdb->logfn(tdb, level, tdb->log_private, message);
        free(message);
        errno = saved_errno;
+       return ecode;
 }