#include <dlinklist.h>
#include <ctdb_protocol.h>
+/* Remove type-safety macros. */
+#undef ctdb_attachdb_send
+#undef ctdb_readrecordlock_async
+#undef ctdb_connect
+
+struct ctdb_lock {
+ struct ctdb_lock *next, *prev;
+
+ struct ctdb_db *ctdb_db;
+ TDB_DATA key;
+
+ /* This will always be set by the time user sees this. */
+ unsigned long held_magic;
+ struct ctdb_ltdb_header *hdr;
+
+ /* For convenience, we stash original callback here. */
+ ctdb_rrl_callback_t callback;
+};
+
+struct ctdb_db {
+ struct ctdb_connection *ctdb;
+ bool persistent;
+ uint32_t tdb_flags;
+ uint32_t id;
+ struct tdb_context *tdb;
+
+ ctdb_callback_t callback;
+ void *private_data;
+};
+
+static void remove_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
+{
+ DLIST_REMOVE(ctdb->locks, lock);
+}
+
+/* FIXME: for thread safety, need tid info too. */
+static bool holding_lock(struct ctdb_connection *ctdb)
+{
+ /* For the moment, you can't ever hold more than 1 lock. */
+ return (ctdb->locks != NULL);
+}
+
+static void add_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
+{
+ DLIST_ADD(ctdb->locks, lock);
+}
+
+static void cleanup_locks(struct ctdb_connection *ctdb, struct ctdb_db *db)
+{
+ struct ctdb_lock *i, *next;
+
+ for (i = ctdb->locks; i; i = next) {
+ /* Grab next pointer, as release_lock will free i */
+ next = i->next;
+ if (i->ctdb_db == db) {
+ ctdb_release_lock(db, i);
+ }
+ }
+}
+
/* FIXME: Could be in shared util code with rest of ctdb */
static void close_noerr(int fd)
{
fcntl(fd, F_SETFD, v | FD_CLOEXEC);
}
-static void set_pnn(int32_t status, uint32_t pnn, void *private_data)
+static void set_pnn(struct ctdb_connection *ctdb,
+ struct ctdb_request *req,
+ void *unused)
{
- if (status != 0) {
- /* FIXME: Report error. */
- ((struct ctdb_connection *)private_data)->broken = true;
- } else {
- ((struct ctdb_connection *)private_data)->pnn = pnn;
+ if (!ctdb_getpnn_recv(ctdb, req, &ctdb->pnn)) {
+ DEBUG(ctdb, LOG_CRIT,
+ "ctdb_connect(async): failed to get pnn");
+ ctdb->broken = true;
}
+ ctdb_request_free(ctdb, req);
}
-struct ctdb_connection *ctdb_connect(const char *addr)
+struct ctdb_connection *ctdb_connect(const char *addr,
+ ctdb_log_fn_t log_fn, void *log_priv)
{
struct ctdb_connection *ctdb;
struct sockaddr_un sun;
ctdb = malloc(sizeof(*ctdb));
- if (!ctdb)
+ if (!ctdb) {
+ /* With no format string, we hope it doesn't use ap! */
+ va_list ap;
+ memset(&ap, 0, sizeof(ap));
+ errno = ENOMEM;
+ log_fn(log_priv, LOG_ERR, "ctdb_connect: no memory", ap);
goto fail;
+ }
ctdb->outq = NULL;
ctdb->doneq = NULL;
- ctdb->immediateq = NULL;
ctdb->in = NULL;
ctdb->message_handlers = NULL;
+ ctdb->next_id = 0;
+ ctdb->broken = false;
+ ctdb->log = log_fn;
+ ctdb->log_priv = log_priv;
+ ctdb->locks = NULL;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
goto close_fail;
/* Immediately queue a request to get our pnn. */
- if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, ctdb))
+ if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
goto close_fail;
return ctdb;
return NULL;
}
+void ctdb_disconnect(struct ctdb_connection *ctdb)
+{
+ struct ctdb_request *i;
+
+ DEBUG(ctdb, LOG_DEBUG, "ctdb_disconnect");
+
+ while ((i = ctdb->outq) != NULL) {
+ DLIST_REMOVE(ctdb->outq, i);
+ ctdb_request_free(ctdb, i);
+ }
+
+ while ((i = ctdb->doneq) != NULL) {
+ DLIST_REMOVE(ctdb->doneq, i);
+ ctdb_request_free(ctdb, i);
+ }
+
+ if (ctdb->in)
+ free_io_elem(ctdb->in);
+
+ remove_message_handlers(ctdb);
+
+ close(ctdb->fd);
+ /* Just in case they try to reuse */
+ ctdb->fd = -1;
+ free(ctdb);
+}
+
int ctdb_get_fd(struct ctdb_connection *ctdb)
{
return ctdb->fd;
return events;
}
-struct ctdb_request *new_ctdb_request(size_t len)
+struct ctdb_request *new_ctdb_request(size_t len,
+ ctdb_callback_t cb, void *cbdata)
{
struct ctdb_request *req = malloc(sizeof(*req));
if (!req)
return NULL;
}
req->hdr.hdr = io_elem_data(req->io, NULL);
- req->cancelled = false;
+ req->reply = NULL;
+ req->callback = cb;
+ req->priv_data = cbdata;
+ req->extra = NULL;
+ req->extra_destructor = NULL;
return req;
}
-static struct ctdb_request *new_immediate_request(void)
+void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req)
{
- struct ctdb_request *req = malloc(sizeof(*req));
- if (!req)
- return NULL;
- req->cancelled = false;
- req->io = NULL;
- req->hdr.hdr = NULL;
- return req;
-}
-
-static void free_ctdb_request(struct ctdb_request *req)
-{
- /* immediate requests don't have IO */
- if (req->io) {
- free_io_elem(req->io);
+ if (req->next || req->prev) {
+ DEBUG(ctdb, LOG_ALERT,
+ "ctdb_request_free: request not complete! ctdb_cancel? %p (id %u)",
+ req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
+ ctdb_cancel(ctdb, req);
+ return;
+ }
+ if (req->extra_destructor) {
+ req->extra_destructor(ctdb, req);
}
+ if (req->reply) {
+ free_io_elem(req->reply);
+ }
+ free_io_elem(req->io);
free(req);
}
-static void handle_call_reply(struct ctdb_connection *ctdb,
- struct ctdb_req_header *hdr,
- struct ctdb_request *i)
+/* Sanity-checking wrapper for reply. */
+static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
+ struct ctdb_request *req,
+ uint32_t callid)
{
- struct ctdb_req_call *call = i->hdr.call;
- struct ctdb_reply_call *reply = (struct ctdb_reply_call *)hdr;
+ size_t len;
+ struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
+
+ /* Library user error if this isn't a reply to a call. */
+ if (req->hdr.hdr->operation != CTDB_REQ_CALL) {
+ errno = EINVAL;
+ DEBUG(ctdb, LOG_ALERT,
+ "This was not a ctdbd call request: operation %u",
+ req->hdr.hdr->operation);
+ return NULL;
+ }
- switch (call->callid) {
- case CTDB_NULL_FUNC:
- /* FIXME: We should let it steal the request, rathe than copy */
- i->callback.nullfunc(reply->status, reply, i->priv_data);
- break;
+ if (req->hdr.call->callid != callid) {
+ errno = EINVAL;
+ DEBUG(ctdb, LOG_ALERT,
+ "This was not a ctdbd %u call request: %u",
+ callid, req->hdr.call->callid);
+ return NULL;
}
+
+ /* ctdbd or our error if this isn't a reply call. */
+ if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
+ errno = EIO;
+ DEBUG(ctdb, LOG_CRIT,
+ "Invalid ctdbd call reply: len %zu, operation %u",
+ len, inhdr->hdr.operation);
+ return NULL;
+ }
+
+ return inhdr;
}
-static void handle_control_reply(struct ctdb_connection *ctdb,
- struct ctdb_req_header *hdr,
- struct ctdb_request *i)
+/* Sanity-checking wrapper for reply. */
+struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
+ struct ctdb_request *req,
+ enum ctdb_controls control)
{
- struct ctdb_req_control *control = i->hdr.control;
- struct ctdb_reply_control *reply = (struct ctdb_reply_control *)hdr;
+ size_t len;
+ struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
+
+ /* Library user error if this isn't a reply to a call. */
+ if (len < sizeof(*inhdr)) {
+ errno = EINVAL;
+ DEBUG(ctdb, LOG_ALERT,
+ "Short ctdbd control reply: %zu bytes", len);
+ return NULL;
+ }
+ if (req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
+ errno = EINVAL;
+ DEBUG(ctdb, LOG_ALERT,
+ "This was not a ctdbd control request: operation %u",
+ req->hdr.hdr->operation);
+ return NULL;
+ }
- switch (control->opcode) {
- case CTDB_CONTROL_GET_RECMASTER:
- i->callback.getrecmaster(0, reply->status, i->priv_data);
- break;
- case CTDB_CONTROL_GET_PNN:
- i->callback.getpnn(0, reply->status, i->priv_data);
- break;
- case CTDB_CONTROL_REGISTER_SRVID:
- i->callback.register_srvid(reply->status, i->priv_data);
- break;
- case CTDB_CONTROL_DB_ATTACH_PERSISTENT:
- case CTDB_CONTROL_DB_ATTACH:
- i->callback.attachdb(reply->status, *(uint32_t *)reply->data,
- i->priv_data);
- break;
- case CTDB_CONTROL_GETDBPATH:
- i->callback.getdbpath(reply->status, (char *)reply->data,
- i->priv_data);
- break;
+ /* ... or if it was a different control from what we expected. */
+ if (req->hdr.control->opcode != control) {
+ errno = EINVAL;
+ DEBUG(ctdb, LOG_ALERT,
+ "This was not an opcode %u ctdbd control request: %u",
+ control, req->hdr.control->opcode);
+ return NULL;
+ }
+
+ /* ctdbd or our error if this isn't a reply call. */
+ if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
+ errno = EIO;
+ DEBUG(ctdb, LOG_CRIT,
+ "Invalid ctdbd control reply: operation %u",
+ inhdr->hdr.operation);
+ return NULL;
}
+
+ return inhdr;
}
-static void handle_incoming(struct ctdb_connection *ctdb,
- struct ctdb_req_header *hdr,
- size_t len /* FIXME: use len to check packet! */)
+static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
{
+ struct ctdb_req_header *hdr;
+ size_t len;
struct ctdb_request *i;
+ hdr = io_elem_data(in, &len);
+ /* FIXME: use len to check packet! */
+
if (hdr->operation == CTDB_REQ_MESSAGE) {
deliver_message(ctdb, hdr);
return;
}
- if (hdr->operation != CTDB_REPLY_CALL
- && hdr->operation != CTDB_REPLY_CONTROL) {
- /* FIXME: report this error. */
- return;
- }
-
for (i = ctdb->doneq; i; i = i->next) {
if (i->hdr.hdr->reqid == hdr->reqid) {
- if (!i->cancelled) {
- if (hdr->operation == CTDB_REPLY_CALL)
- handle_call_reply(ctdb, hdr, i);
- else
- handle_control_reply(ctdb, hdr, i);
- }
DLIST_REMOVE(ctdb->doneq, i);
- free_ctdb_request(i);
+ i->reply = in;
+ i->callback(ctdb, i, i->priv_data);
return;
}
}
- /* FIXME: report this error. */
+ DEBUG(ctdb, LOG_WARNING,
+ "Unexpected ctdbd request reply: operation %u reqid %u",
+ hdr->operation, hdr->reqid);
+ free_io_elem(in);
}
/* Remove "harmless" errors. */
return ret;
}
-int ctdb_service(struct ctdb_connection *ctdb, int revents)
+bool ctdb_service(struct ctdb_connection *ctdb, int revents)
{
if (ctdb->broken) {
- return -1;
+ return false;
+ }
+
+ if (holding_lock(ctdb)) {
+ DEBUG(ctdb, LOG_ALERT, "Do not block while holding lock!");
}
if (revents & POLLOUT) {
while (ctdb->outq) {
if (real_error(write_io_elem(ctdb->fd,
ctdb->outq->io)) < 0) {
+ DEBUG(ctdb, LOG_ERR,
+ "ctdb_service: error writing to ctdbd");
ctdb->broken = true;
- return -1;
+ return false;
}
if (io_elem_finished(ctdb->outq->io)) {
struct ctdb_request *done = ctdb->outq;
DLIST_REMOVE(ctdb->outq, done);
- if (done->cancelled) {
- free_ctdb_request(done);
- } else {
- DLIST_ADD_END(ctdb->doneq, done,
- struct ctdb_request);
- }
+ /* We add at the head: any dead ones
+ * sit and end. */
+ DLIST_ADD(ctdb->doneq, done);
}
}
}
if (!ctdb->in) {
ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
if (!ctdb->in) {
+ DEBUG(ctdb, LOG_ERR,
+ "ctdb_service: allocating readbuf");
ctdb->broken = true;
- return -1;
+ return false;
}
}
/* They closed fd? */
if (ret == 0)
errno = EBADF;
+ DEBUG(ctdb, LOG_ERR,
+ "ctdb_service: error reading from ctdbd");
ctdb->broken = true;
- return -1;
+ return false;
} else if (ret < 0) {
/* No progress, stop loop. */
revents = 0;
} else if (io_elem_finished(ctdb->in)) {
- struct ctdb_req_header *hdr;
- size_t len;
-
- hdr = io_elem_data(ctdb->in, &len);
- handle_incoming(ctdb, hdr, len);
- free_io_elem(ctdb->in);
+ handle_incoming(ctdb, ctdb->in);
ctdb->in = NULL;
}
}
- while (ctdb->immediateq) {
- struct ctdb_request *imm = ctdb->immediateq;
- /* This has to handle fake->cancelled internally. */
- imm->callback.immediate(imm, imm->priv_data);
- DLIST_REMOVE(ctdb->immediateq, imm);
- free_ctdb_request(imm);
- }
-
- return 0;
+ return true;
}
/* This is inefficient. We could pull in idtree.c. */
uint32_t opcode,
uint32_t destnode,
const void *extra_data,
- size_t extra)
+ size_t extra,
+ ctdb_callback_t callback,
+ void *cbdata)
{
struct ctdb_request *req;
struct ctdb_req_control *pkt;
- req = new_ctdb_request(sizeof(*pkt) + extra);
+ req = new_ctdb_request(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
if (!req)
return NULL;
CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
pkt = req->hdr.control;
+ pkt->pad = 0;
pkt->opcode = opcode;
pkt->srvid = 0;
pkt->client_id = 0;
pkt->flags = 0;
pkt->datalen = extra;
memcpy(pkt->data, extra_data, extra);
- DLIST_ADD_END(ctdb->outq, req, struct ctdb_request);
+ DLIST_ADD(ctdb->outq, req);
return req;
}
-int ctdb_cancel(struct ctdb_request *req)
+void ctdb_cancel_callback(struct ctdb_connection *ctdb,
+ struct ctdb_request *req,
+ void *unused)
{
- /* FIXME: If it's not sent, we could just free it right now. */
- req->cancelled = true;
- return 0;
+ ctdb_request_free(ctdb, req);
}
-struct ctdb_db {
- struct ctdb_connection *ctdb;
- bool persistent;
- uint32_t tdb_flags;
- uint32_t id;
- struct tdb_context *tdb;
+void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
+{
+ if (!req->next && !req->prev) {
+ DEBUG(ctdb, LOG_ALERT,
+ "ctdb_cancel: request completed! ctdb_request_free? %p (id %u)",
+ req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
+ ctdb_request_free(ctdb, req);
+ return;
+ }
- ctdb_attachdb_cb callback;
- void *private_data;
-};
+ DEBUG(ctdb, LOG_DEBUG, "ctdb_cancel: %p (id %u)",
+ req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
+
+ /* FIXME: If it's not sent, we could just free it right now. */
+ req->callback = ctdb_cancel_callback;
+}
-static void attachdb_getdbpath_done(int status, const char *path,
+void ctdb_detachdb(struct ctdb_connection *ctdb, struct ctdb_db *db)
+{
+ cleanup_locks(ctdb, db);
+ tdb_close(db->tdb);
+ free(db);
+}
+
+static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
+ struct ctdb_request *req,
void *_db)
{
struct ctdb_db *db = _db;
+
+ /* Do callback on original request. */
+ db->callback(ctdb, req->extra, db->private_data);
+}
+
+struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
+ struct ctdb_request *req)
+{
+ struct ctdb_request *dbpath_req = req->extra;
+ struct ctdb_reply_control *reply;
+ struct ctdb_db *db = req->priv_data;
uint32_t tdb_flags = db->tdb_flags;
+ struct tdb_logging_context log;
- if (status != 0) {
- db->callback(status, NULL, db->private_data);
- free(db);
- return;
+ /* Never sent the dbpath request? We've failed. */
+ if (!dbpath_req) {
+ /* FIXME: Save errno? */
+ errno = EINVAL;
+ return NULL;
+ }
+
+ reply = unpack_reply_control(ctdb, dbpath_req, CTDB_CONTROL_GETDBPATH);
+ if (!reply) {
+ return NULL;
+ }
+ if (reply->status != 0) {
+ DEBUG(db->ctdb, LOG_ERR,
+ "ctdb_attachdb_recv: reply status %i", reply->status);
+ return NULL;
}
tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
tdb_flags |= TDB_DISALLOW_NESTING;
- db->tdb = tdb_open(path, 0, tdb_flags, O_RDWR, 0);
+ log.log_fn = ctdb_tdb_log_bridge;
+ log.log_private = ctdb;
+ db->tdb = tdb_open_ex((char *)reply->data, 0, tdb_flags, O_RDWR, 0,
+ &log, NULL);
if (db->tdb == NULL) {
- db->callback(-1, NULL, db->private_data);
- free(db);
- return;
+ DEBUG(db->ctdb, LOG_ERR,
+ "ctdb_attachdb_recv: failed to tdb_open %s",
+ (char *)reply->data);
+ return NULL;
}
- /* Finally, we tell the client that we opened the db. */
- db->callback(status, db, db->private_data);
+ /* Finally, separate the db from the request (see destroy_req_db). */
+ req->priv_data = NULL;
+ DEBUG(db->ctdb, LOG_DEBUG,
+ "ctdb_attachdb_recv: db %p, tdb %s", db, (char *)reply->data);
+ return db;
}
-static void attachdb_done(int status, uint32_t id, struct ctdb_db *db)
+static void attachdb_done(struct ctdb_connection *ctdb,
+ struct ctdb_request *req,
+ void *_db)
{
- struct ctdb_request *req;
+ struct ctdb_db *db = _db;
+ struct ctdb_request *req2;
+ struct ctdb_reply_control *reply;
+ enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
- if (status != 0) {
- db->callback(status, NULL, db->private_data);
- free(db);
+ if (db->persistent) {
+ control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
+ }
+
+ reply = unpack_reply_control(ctdb, req, control);
+ if (!reply || reply->status != 0) {
+ if (reply) {
+ DEBUG(ctdb, LOG_ERR,
+ "ctdb_attachdb_send(async): DB_ATTACH status %i",
+ reply->status);
+ }
+ /* We failed. Hand request to user and have them discover it
+ * via ctdb_attachdb_recv. */
+ db->callback(ctdb, req, db->private_data);
return;
}
- db->id = id;
+ db->id = *(uint32_t *)reply->data;
/* Now we do another call, to get the dbpath. */
- req = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
- CTDB_CURRENT_NODE, &id, sizeof(id));
- if (!req) {
- db->callback(-1, NULL, db->private_data);
- free(db);
+ req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
+ CTDB_CURRENT_NODE,
+ &db->id, sizeof(db->id),
+ attachdb_getdbpath_done, db);
+ if (!req2) {
+ DEBUG(db->ctdb, LOG_ERR,
+ "ctdb_attachdb_send(async): failed to allocate");
+ db->callback(ctdb, req, db->private_data);
return;
}
- req->callback.getdbpath = attachdb_getdbpath_done;
- req->priv_data = db;
+ req->extra = req2;
+ req2->extra = req;
+ DEBUG(db->ctdb, LOG_DEBUG,
+ "ctdb_attachdb_send(async): created getdbpath request");
+}
+
+static void destroy_req_db(struct ctdb_connection *ctdb,
+ struct ctdb_request *req)
+{
+ /* Incomplete db is in priv_data. */
+ free(req->priv_data);
+ /* second request is chained off this one. */
+ if (req->extra) {
+ ctdb_request_free(ctdb, req->extra);
+ }
}
struct ctdb_request *
ctdb_attachdb_send(struct ctdb_connection *ctdb,
- const char *name, int persistent, uint32_t tdb_flags,
- ctdb_attachdb_cb callback,
- void *private_data)
+ const char *name, bool persistent, uint32_t tdb_flags,
+ ctdb_callback_t callback, void *private_data)
{
struct ctdb_request *req;
struct ctdb_db *db;
uint32_t opcode;
/* FIXME: Search if db already open. */
-
db = malloc(sizeof(*db));
if (!db) {
return NULL;
}
req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
- strlen(name) + 1);
+ strlen(name) + 1, attachdb_done, db);
if (!req) {
+ DEBUG(ctdb, LOG_ERR,
+ "ctdb_attachdb_send: failed allocating DB_ATTACH");
free(db);
return NULL;
}
db->callback = callback;
db->private_data = private_data;
- req->callback.attachdb = attachdb_done;
- req->priv_data = db;
+ req->extra_destructor = destroy_req_db;
+ /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
+ req->extra = NULL;
/* Flags get overloaded into srvid. */
req->hdr.control->srvid = tdb_flags;
+ DEBUG(db->ctdb, LOG_DEBUG,
+ "ctdb_attachdb_send: DB_ATTACH request %p", req);
return req;
}
-struct ctdb_lock {
- struct ctdb_db *ctdb_db;
- TDB_DATA key;
- struct ctdb_ltdb_header *hdr;
- TDB_DATA data;
- bool held;
- /* For convenience, we stash this here. */
- ctdb_readrecordlock_cb callback;
- void *private_data;
-};
+static unsigned long lock_magic(struct ctdb_lock *lock)
+{
+ /* A non-zero magic specific to this structure. */
+ return ((unsigned long)lock->key.dptr
+ ^ (((unsigned long)lock->key.dptr) << 16)
+ ^ 0xBADC0FFEEBADC0DEULL)
+ | 1;
+}
-void ctdb_release_lock(struct ctdb_lock *lock)
+/* This is only called on locks before they're held. */
+static void free_lock(struct ctdb_lock *lock)
{
- if (lock->held) {
- tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
+ if (lock->held_magic) {
+ DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
+ "free_lock invalid lock %p", lock);
}
- free(lock->key.dptr);
- free(lock->hdr); /* Also frees lock->data */
+ free(lock->hdr);
free(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,
+ "ctdb_release_lock %p", lock);
+ remove_lock(lock->ctdb_db->ctdb, lock);
+ }
+ lock->held_magic = 0;
+ free_lock(lock);
+}
+
+
/* We keep the lock if local node is the dmaster. */
-static bool try_readrecordlock(struct ctdb_lock *lock)
+static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
{
+ struct ctdb_ltdb_header *hdr;
+
if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
- return false;
+ DEBUG(lock->ctdb_db->ctdb, LOG_WARNING,
+ "ctdb_readrecordlock_async: failed to chainlock");
+ return NULL;
}
- lock->hdr = ctdb_local_fetch(lock->ctdb_db->tdb,
- lock->key, &lock->data);
- if (lock->hdr && lock->hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
- lock->held = true;
+ hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
+ if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
+ DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
+ "ctdb_readrecordlock_async: got local lock");
+ lock->held_magic = lock_magic(lock);
+ lock->hdr = hdr;
+ add_lock(lock->ctdb_db->ctdb, lock);
return true;
}
tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
- free(lock->hdr);
- return false;
+ free(hdr);
+ return NULL;
}
-static void readrecordlock_done(int, struct ctdb_reply_call *, void *);
-
-static struct ctdb_request *new_readrecordlock_request(struct ctdb_lock *lock)
+/* If they shutdown before we hand them the lock, we free it here. */
+static void destroy_lock(struct ctdb_connection *ctdb,
+ struct ctdb_request *req)
{
- struct ctdb_request *req;
- struct ctdb_req_call *pkt;
-
- req = new_ctdb_request(sizeof(*pkt) + lock->key.dsize);
- if (!req)
- return NULL;
- req->callback.nullfunc = readrecordlock_done;
- req->priv_data = lock;
-
- io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
- new_reqid(lock->ctdb_db->ctdb));
-
- pkt = req->hdr.call;
- pkt->flags = CTDB_IMMEDIATE_MIGRATION;
- pkt->db_id = lock->ctdb_db->id;
- pkt->callid = CTDB_NULL_FUNC;
- pkt->hopcount = 0;
- pkt->keylen = lock->key.dsize;
- pkt->calldatalen = 0;
- memcpy(pkt->data, lock->key.dptr, lock->key.dsize);
- DLIST_ADD_END(lock->ctdb_db->ctdb->outq, req, struct ctdb_request);
- return req;
+ free_lock(req->extra);
}
-/* OK, let's try again... */
-static void readrecordlock_done(int status, struct ctdb_reply_call *reply,
- void *_lock)
+static void readrecordlock_retry(struct ctdb_connection *ctdb,
+ struct ctdb_request *req, void *private)
{
- struct ctdb_lock *lock = _lock;
+ struct ctdb_lock *lock = req->extra;
+ struct ctdb_reply_call *reply;
+ TDB_DATA data;
- if (status != 0) {
- lock->callback(status, NULL, tdb_null, lock->private_data);
- ctdb_release_lock(lock);
+ /* OK, we've received reply to noop migration */
+ reply = unpack_reply_call(ctdb, req, CTDB_NULL_FUNC);
+ if (!reply || reply->status != 0) {
+ if (reply) {
+ DEBUG(ctdb, LOG_ERR,
+ "ctdb_readrecordlock_async(async):"
+ " NULL_FUNC returned %i", reply->status);
+ }
+ lock->callback(lock->ctdb_db, NULL, tdb_null, private);
+ ctdb_request_free(ctdb, req); /* Also frees lock. */
return;
}
- if (try_readrecordlock(lock)) {
- lock->callback(0, lock, lock->data, lock->private_data);
+ /* Can we get lock now? */
+ if (try_readrecordlock(lock, &data)) {
+ /* Now it's their responsibility to free lock & request! */
+ req->extra_destructor = NULL;
+ lock->callback(lock->ctdb_db, lock, data, private);
+ ctdb_request_free(ctdb, req);
return;
}
- if (!new_readrecordlock_request(lock)) {
- lock->callback(-1, NULL, tdb_null, lock->private_data);
- ctdb_release_lock(lock);
- }
-}
-
-static void lock_complete(struct ctdb_request *req, void *_lock)
-{
- struct ctdb_lock *lock = _lock;
-
- if (!req->cancelled) {
- lock->callback(0, lock, lock->data, lock->private_data);
- } else {
- ctdb_release_lock(lock);
- }
+ /* Retransmit the same request again (we lost race). */
+ io_elem_reset(req->io);
+ DLIST_ADD(ctdb->outq, req);
}
-struct ctdb_request *
-ctdb_readrecordlock_send(struct ctdb_db *ctdb_db,
- TDB_DATA key,
- ctdb_readrecordlock_cb callback,
- void *private_data)
+bool
+ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
+ ctdb_rrl_callback_t callback, void *cbdata)
{
struct ctdb_request *req;
struct ctdb_lock *lock;
+ TDB_DATA data;
- lock = malloc(sizeof(*lock));
- if (!lock)
- return NULL;
- lock->key.dptr = malloc(key.dsize);
- if (!lock->key.dptr) {
- free_noerr(lock);
- return NULL;
+ if (holding_lock(ctdb_db->ctdb)) {
+ DEBUG(ctdb_db->ctdb, LOG_ALERT,
+ "ctdb_readrecordlock_async: already holding lock");
+ return false;
}
+
+ /* Setup lock */
+ lock = malloc(sizeof(*lock) + key.dsize);
+ if (!lock) {
+ DEBUG(ctdb_db->ctdb, LOG_ERR,
+ "ctdb_readrecordlock_async: lock allocation failed");
+ return false;
+ }
+ lock->key.dptr = (void *)(lock + 1);
memcpy(lock->key.dptr, key.dptr, key.dsize);
lock->key.dsize = key.dsize;
lock->ctdb_db = ctdb_db;
- lock->callback = callback;
- lock->private_data = private_data;
lock->hdr = NULL;
- lock->held = false;
-
- if (try_readrecordlock(lock)) {
- /* We pretend to be async, so we just queue this. */
- req = new_immediate_request();
- if (!req) {
- ctdb_release_lock(lock);
- return NULL;
- }
- req->callback.immediate = lock_complete;
- req->priv_data = lock;
- DLIST_ADD_END(lock->ctdb_db->ctdb->immediateq,
- req, struct ctdb_request);
- return req;
+ lock->held_magic = 0;
+
+ /* Fast path. */
+ if (try_readrecordlock(lock, &data)) {
+ callback(ctdb_db, lock, data, cbdata);
+ return true;
}
- req = new_readrecordlock_request(lock);
+ /* Slow path: create request. */
+ req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
+ + key.dsize, readrecordlock_retry, cbdata);
if (!req) {
- ctdb_release_lock(lock);
+ DEBUG(ctdb_db->ctdb, LOG_ERR,
+ "ctdb_readrecordlock_async: allocation failed");
+ free_lock(lock);
return NULL;
}
- return req;
+ req->extra = lock;
+ req->extra_destructor = destroy_lock;
+ /* We store the original callback in the lock, and use our own. */
+ lock->callback = callback;
+
+ io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
+ new_reqid(ctdb_db->ctdb));
+
+ req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
+ req->hdr.call->db_id = ctdb_db->id;
+ req->hdr.call->callid = CTDB_NULL_FUNC;
+ req->hdr.call->hopcount = 0;
+ req->hdr.call->keylen = key.dsize;
+ req->hdr.call->calldatalen = 0;
+ memcpy(req->hdr.call->data, key.dptr, key.dsize);
+ DLIST_ADD(ctdb_db->ctdb->outq, req);
+ 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->persistent) {
- /* FIXME: Report error. */
- return -1;
+ 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(ctdb_db->ctdb, LOG_ALERT,
+ "ctdb_writerecord: Can not write. Lock has been released.");
+ return false;
+ }
+
+ if (ctdb_db->persistent) {
+ errno = EINVAL;
+ DEBUG(ctdb_db->ctdb, LOG_ALERT,
+ "ctdb_writerecord: cannot write to persistent db");
+ return false;
}
- return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr, data);
+ switch (ctdb_local_store(ctdb_db->tdb, lock->key, lock->hdr, data)) {
+ case 0:
+ DEBUG(ctdb_db->ctdb, LOG_DEBUG,
+ "ctdb_writerecord: optimized away noop write.");
+ /* fall thru */
+ case 1:
+ return true;
+
+ default:
+ switch (errno) {
+ case ENOMEM:
+ DEBUG(ctdb_db->ctdb, LOG_CRIT,
+ "ctdb_writerecord: out of memory.");
+ break;
+ case EINVAL:
+ DEBUG(ctdb_db->ctdb, LOG_ALERT,
+ "ctdb_writerecord: record changed under lock?");
+ break;
+ default: /* TDB already logged. */
+ break;
+ }
+ return false;
+ }
}