From: Rusty Russell Date: Tue, 8 Jun 2010 07:41:40 +0000 (+0930) Subject: libctdb: more bool conversion, and accompany lock by ctdb_db in API X-Git-Tag: ctdb-1.9.1~22 X-Git-Url: http://git.samba.org/?p=rusty%2Fctdb.git;a=commitdiff_plain;h=3f939956ddd693cba6ea5c655288f4f5ca95f768 libctdb: more bool conversion, and accompany lock by ctdb_db in API I missed some int->bool conversions previously, particularly the return of ctdb_writerecord(). By always handing functions ctdb_connection or ctdb_db, we keep it consistent with the rest of the API and can do extra lock consistency checks. Signed-off-by: Rusty Russell --- diff --git a/include/ctdb.h b/include/ctdb.h index 93e6c13f..e4aff867 100644 --- a/include/ctdb.h +++ b/include/ctdb.h @@ -204,7 +204,7 @@ struct ctdb_db; */ struct ctdb_request * ctdb_attachdb_send(struct ctdb_connection *ctdb, - const char *name, int persistent, uint32_t tdb_flags, + const char *name, bool persistent, uint32_t tdb_flags, ctdb_callback_t callback, void *cbdata); /** @@ -268,16 +268,19 @@ bool ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key, /** * ctdb_writerecord - write a locked record in a TDB + * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv. * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_recv * @data: the new data to place in the record. */ -int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data); +bool ctdb_writerecord(struct ctdb_db *ctdb_db, + struct ctdb_lock *lock, TDB_DATA data); /** * ctdb_release_lock - release a record lock on a TDB + * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv. * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_async */ -void ctdb_release_lock(struct ctdb_lock *lock); +void ctdb_release_lock(struct ctdb_db *ctdb_db, struct ctdb_lock *lock); /** * ctdb_message_fn_t - messaging callback for ctdb messages @@ -450,7 +453,7 @@ void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req); * Returns NULL on failure. */ struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb, - const char *name, int persistent, + const char *name, bool persistent, uint32_t tdb_flags); /** diff --git a/libctdb/ctdb.c b/libctdb/ctdb.c index afe7e1df..4a74bf7a 100644 --- a/libctdb/ctdb.c +++ b/libctdb/ctdb.c @@ -588,7 +588,7 @@ static void destroy_req_db(struct ctdb_connection *ctdb, struct ctdb_request * ctdb_attachdb_send(struct ctdb_connection *ctdb, - const char *name, int persistent, uint32_t tdb_flags, + const char *name, bool persistent, uint32_t tdb_flags, ctdb_callback_t callback, void *private_data) { struct ctdb_request *req; @@ -654,11 +654,15 @@ static void free_lock(struct ctdb_lock *lock) } -void ctdb_release_lock(struct ctdb_lock *lock) +void ctdb_release_lock(struct ctdb_db *ctdb_db, struct ctdb_lock *lock) { if (lock->held_magic != lock_magic(lock)) { DEBUG(lock->ctdb_db->ctdb, LOG_ALERT, "ctdb_release_lock invalid lock %p", lock); + } else if (lock->ctdb_db != ctdb_db) { + errno = EBADF; + DEBUG(ctdb_db->ctdb, LOG_ALERT, + "ctdb_release_lock: wrong ctdb_db."); } else { tdb_chainunlock(lock->ctdb_db->tdb, lock->key); DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG, @@ -798,22 +802,29 @@ ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key, return true; } -int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data) +bool ctdb_writerecord(struct ctdb_db *ctdb_db, + struct ctdb_lock *lock, TDB_DATA data) { + if (lock->ctdb_db != ctdb_db) { + errno = EBADF; + DEBUG(ctdb_db->ctdb, LOG_ALERT, + "ctdb_writerecord: Can not write, wrong ctdb_db."); + return false; + } + if (lock->held_magic != lock_magic(lock)) { errno = EBADF; - DEBUG(lock->ctdb_db->ctdb, LOG_ALERT, + DEBUG(ctdb_db->ctdb, LOG_ALERT, "ctdb_writerecord: Can not write. Lock has been released."); - return -1; + return false; } - if (lock->ctdb_db->persistent) { + if (ctdb_db->persistent) { errno = EINVAL; - DEBUG(lock->ctdb_db->ctdb, LOG_ALERT, + DEBUG(ctdb_db->ctdb, LOG_ALERT, "ctdb_writerecord: cannot write to persistent db"); - return -1; + return false; } - return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr, - data); + return ctdb_local_store(ctdb_db->tdb, lock->key, lock->hdr, data) == 0; } diff --git a/libctdb/sync.c b/libctdb/sync.c index 3ee33011..11cd6cad 100644 --- a/libctdb/sync.c +++ b/libctdb/sync.c @@ -77,7 +77,7 @@ bool ctdb_getrecmaster(struct ctdb_connection *ctdb, } struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb, - const char *name, int persistent, + const char *name, bool persistent, uint32_t tdb_flags) { struct ctdb_request *req; diff --git a/libctdb/tst.c b/libctdb/tst.c index 391e92c7..9f67b629 100644 --- a/libctdb/tst.c +++ b/libctdb/tst.c @@ -107,10 +107,11 @@ static void rrl_cb(struct ctdb_db *ctdb_db, data.dptr = tmp; data.dsize = strlen(tmp) + 1; - ctdb_writerecord(lock, data); + if (!ctdb_writerecord(ctdb_db, lock, data)) + printf("Error writing data!\n"); /* Release the lock as quickly as possible */ - ctdb_release_lock(lock); + ctdb_release_lock(ctdb_db, lock); printf("Wrote new record : %s\n", tmp); @@ -172,7 +173,8 @@ int main(int argc, char *argv[]) exit(10); } - ctdb_db_context = ctdb_attachdb(ctdb_connection, "test_test.tdb", 0, 0); + ctdb_db_context = ctdb_attachdb(ctdb_connection, "test_test.tdb", + false, 0); if (!ctdb_db_context) { printf("Failed to attach to database\n"); exit(10);