]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/private.h
tdb2: rework lock.c functions to return enum TDB_ERROR.
[ccan] / ccan / tdb2 / private.h
index d32c7546e092d648e4a3a156c54358aaaa30c134..4ecda6a5f529729518b051a53db711f582fde6db 100644 (file)
@@ -74,6 +74,16 @@ typedef uint64_t tdb_off_t;
 #define TDB_RECOVERY_INVALID_MAGIC (0x0ULL)
 
 #define TDB_OFF_ERR ((tdb_off_t)-1)
+#define TDB_OFF_IS_ERR(off) unlikely(off >= (tdb_off_t)TDB_ERR_LAST)
+
+/* Packing errors into pointers and v.v. */
+#define TDB_PTR_IS_ERR(ptr) \
+       unlikely((void *)(ptr) >= (void *)(long)TDB_ERR_LAST)
+#define TDB_PTR_ERR(p) ((enum TDB_ERROR)(long)(p))
+#define TDB_ERR_PTR(err) ((void *)(long)(err))
+
+/* Common case of returning true, false or -ve error. */
+typedef int tdb_bool_err;
 
 /* Prevent others from opening the file. */
 #define TDB_OPEN_LOCK 0
@@ -338,7 +348,7 @@ struct tdb_context {
        void *log_private;
 
        /* Hash function. */
-       tdb_hashfn_t khash;
+       uint64_t (*khash)(const void *key, size_t len, uint64_t seed, void *);
        void *hash_priv;
        uint64_t hash_seed;
 
@@ -382,6 +392,14 @@ struct tdb_methods {
 /* hash.c: */
 void tdb_hash_init(struct tdb_context *tdb);
 
+int first_in_hash(struct tdb_context *tdb,
+                 struct traverse_info *tinfo,
+                 TDB_DATA *kbuf, size_t *dlen);
+
+int next_in_hash(struct tdb_context *tdb,
+                struct traverse_info *tinfo,
+                TDB_DATA *kbuf, size_t *dlen);
+
 /* Hash random memory. */
 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len);
 
@@ -500,61 +518,54 @@ void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val);
 void tdb_lock_init(struct tdb_context *tdb);
 
 /* Lock/unlock a range of hashes. */
-int tdb_lock_hashes(struct tdb_context *tdb,
-                   tdb_off_t hash_lock, tdb_len_t hash_range,
-                   int ltype, enum tdb_lock_flags waitflag);
-int tdb_unlock_hashes(struct tdb_context *tdb,
-                     tdb_off_t hash_lock,
-                     tdb_len_t hash_range, int ltype);
+enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
+                              tdb_off_t hash_lock, tdb_len_t hash_range,
+                              int ltype, enum tdb_lock_flags waitflag);
+enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
+                                tdb_off_t hash_lock,
+                                tdb_len_t hash_range, int ltype);
 
 /* Lock/unlock a particular free bucket. */
-int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
-                        enum tdb_lock_flags waitflag);
+enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
+                                   enum tdb_lock_flags waitflag);
 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off);
 
 /* Serialize transaction start. */
-int tdb_transaction_lock(struct tdb_context *tdb, int ltype);
-int tdb_transaction_unlock(struct tdb_context *tdb, int ltype);
+enum TDB_ERROR tdb_transaction_lock(struct tdb_context *tdb, int ltype);
+void tdb_transaction_unlock(struct tdb_context *tdb, int ltype);
 
 /* Do we have any hash locks (ie. via tdb_chainlock) ? */
 bool tdb_has_hash_locks(struct tdb_context *tdb);
 
 /* Lock entire database. */
-int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
-                      enum tdb_lock_flags flags, bool upgradable);
-int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype);
-int tdb_allrecord_upgrade(struct tdb_context *tdb);
+enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
+                                 enum tdb_lock_flags flags, bool upgradable);
+void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype);
+enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb);
 
 /* Serialize db open. */
-int tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags);
+enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb,
+                            enum tdb_lock_flags flags);
 void tdb_unlock_open(struct tdb_context *tdb);
 bool tdb_has_open_lock(struct tdb_context *tdb);
 
 /* Serialize db expand. */
-int tdb_lock_expand(struct tdb_context *tdb, int ltype);
+enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype);
 void tdb_unlock_expand(struct tdb_context *tdb, int ltype);
 bool tdb_has_expansion_lock(struct tdb_context *tdb);
 
 /* If it needs recovery, grab all the locks and do it. */
-int tdb_lock_and_recover(struct tdb_context *tdb);
-
-/* traverse.c: */
-int first_in_hash(struct tdb_context *tdb, int ltype,
-                 struct traverse_info *tinfo,
-                 TDB_DATA *kbuf, size_t *dlen);
-int next_in_hash(struct tdb_context *tdb, int ltype,
-                struct traverse_info *tinfo,
-                TDB_DATA *kbuf, size_t *dlen);
+enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb);
 
 /* transaction.c: */
 int tdb_transaction_recover(struct tdb_context *tdb);
 bool tdb_needs_recovery(struct tdb_context *tdb);
 
 /* tdb.c: */
-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, ...);
 
 #ifdef TDB_TRACE
 void tdb_trace(struct tdb_context *tdb, const char *op);