4 Copyright (C) Rusty Russell 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include <sys/socket.h>
27 #include "libctdb_private.h"
29 #include "local_tdb.h"
31 #include <dlinklist.h>
32 #include <ctdb_protocol.h>
34 /* Remove type-safety macros. */
35 #undef ctdb_attachdb_send
36 #undef ctdb_readrecordlock_async
40 struct ctdb_lock *next, *prev;
42 struct ctdb_db *ctdb_db;
45 /* This will always be true by the time user sees this. */
47 struct ctdb_ltdb_header *hdr;
49 /* For convenience, we stash original callback here. */
50 ctdb_rrl_callback_t callback;
53 static void remove_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
55 DLIST_REMOVE(ctdb->locks, lock);
58 /* FIXME: for thread safety, need tid info too. */
59 static bool holding_lock(struct ctdb_connection *ctdb)
61 /* For the moment, you can't ever hold more than 1 lock. */
62 return (ctdb->locks != NULL);
65 static void add_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
67 DLIST_ADD(ctdb->locks, lock);
70 /* FIXME: Could be in shared util code with rest of ctdb */
71 static void close_noerr(int fd)
78 /* FIXME: Could be in shared util code with rest of ctdb */
79 static void free_noerr(void *p)
86 /* FIXME: Could be in shared util code with rest of ctdb */
87 static void set_nonblocking(int fd)
90 v = fcntl(fd, F_GETFL, 0);
91 fcntl(fd, F_SETFL, v | O_NONBLOCK);
94 /* FIXME: Could be in shared util code with rest of ctdb */
95 static void set_close_on_exec(int fd)
98 v = fcntl(fd, F_GETFD, 0);
99 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
102 static void set_pnn(struct ctdb_connection *ctdb,
103 struct ctdb_request *req,
106 if (!ctdb_getpnn_recv(ctdb, req, &ctdb->pnn)) {
107 DEBUG(ctdb, LOG_CRIT,
108 "ctdb_connect(async): failed to get pnn");
111 ctdb_request_free(ctdb, req);
114 struct ctdb_connection *ctdb_connect(const char *addr,
115 ctdb_log_fn_t log_fn, void *log_priv)
117 struct ctdb_connection *ctdb;
118 struct sockaddr_un sun;
120 ctdb = malloc(sizeof(*ctdb));
122 /* With no format string, we hope it doesn't use ap! */
124 memset(&ap, 0, sizeof(ap));
126 log_fn(log_priv, LOG_ERR, "ctdb_connect: no memory", ap);
132 ctdb->message_handlers = NULL;
134 ctdb->broken = false;
136 ctdb->log_priv = log_priv;
139 memset(&sun, 0, sizeof(sun));
140 sun.sun_family = AF_UNIX;
143 strncpy(sun.sun_path, addr, sizeof(sun.sun_path));
144 ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
148 set_nonblocking(ctdb->fd);
149 set_close_on_exec(ctdb->fd);
151 if (connect(ctdb->fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
154 /* Immediately queue a request to get our pnn. */
155 if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
161 close_noerr(ctdb->fd);
168 int ctdb_get_fd(struct ctdb_connection *ctdb)
173 int ctdb_which_events(struct ctdb_connection *ctdb)
182 struct ctdb_request *new_ctdb_request(size_t len,
183 ctdb_callback_t cb, void *cbdata)
185 struct ctdb_request *req = malloc(sizeof(*req));
188 req->io = new_io_elem(len);
193 req->hdr.hdr = io_elem_data(req->io, NULL);
196 req->priv_data = cbdata;
198 req->extra_destructor = NULL;
202 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req)
204 if (req->extra_destructor) {
205 req->extra_destructor(ctdb, req);
208 free_io_elem(req->reply);
210 free_io_elem(req->io);
214 /* Sanity-checking wrapper for reply. */
215 static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
216 struct ctdb_request *req,
220 struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
222 /* Library user error if this isn't a reply to a call. */
223 if (req->hdr.hdr->operation != CTDB_REQ_CALL) {
226 "This was not a ctdbd call request: operation %u",
227 req->hdr.hdr->operation);
231 if (req->hdr.call->callid != callid) {
234 "This was not a ctdbd %u call request: %u",
235 callid, req->hdr.call->callid);
239 /* ctdbd or our error if this isn't a reply call. */
240 if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
242 DEBUG(ctdb, LOG_CRIT,
243 "Invalid ctdbd call reply: len %zu, operation %u",
244 len, inhdr->hdr.operation);
251 /* Sanity-checking wrapper for reply. */
252 struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
253 struct ctdb_request *req,
254 enum ctdb_controls control)
257 struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
259 /* Library user error if this isn't a reply to a call. */
260 if (len < sizeof(*inhdr)) {
262 DEBUG(ctdb, LOG_CRIT,
263 "Short ctdbd control reply: %zu bytes", len);
266 if (req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
269 "This was not a ctdbd control request: operation %u",
270 req->hdr.hdr->operation);
274 /* ... or if it was a different control from what we expected. */
275 if (req->hdr.control->opcode != control) {
278 "This was not an opcode %u ctdbd control request: %u",
279 control, req->hdr.control->opcode);
283 /* ctdbd or our error if this isn't a reply call. */
284 if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
286 DEBUG(ctdb, LOG_CRIT,
287 "Invalid ctdbd control reply: operation %u",
288 inhdr->hdr.operation);
295 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
297 struct ctdb_req_header *hdr;
299 struct ctdb_request *i;
301 hdr = io_elem_data(in, &len);
302 /* FIXME: use len to check packet! */
304 if (hdr->operation == CTDB_REQ_MESSAGE) {
305 deliver_message(ctdb, hdr);
309 for (i = ctdb->doneq; i; i = i->next) {
310 if (i->hdr.hdr->reqid == hdr->reqid) {
311 DLIST_REMOVE(ctdb->doneq, i);
313 i->callback(ctdb, i, i->priv_data);
317 DEBUG(ctdb, LOG_WARNING,
318 "Unexpected ctdbd request reply: operation %u reqid %u",
319 hdr->operation, hdr->reqid);
323 /* Remove "harmless" errors. */
324 static ssize_t real_error(ssize_t ret)
326 if (ret < 0 && (errno == EINTR || errno == EWOULDBLOCK))
331 bool ctdb_service(struct ctdb_connection *ctdb, int revents)
337 if (holding_lock(ctdb)) {
338 DEBUG(ctdb, LOG_WARNING, "Do not block while holding lock!");
341 if (revents & POLLOUT) {
343 if (real_error(write_io_elem(ctdb->fd,
344 ctdb->outq->io)) < 0) {
346 "ctdb_service: error writing to ctdbd");
350 if (io_elem_finished(ctdb->outq->io)) {
351 struct ctdb_request *done = ctdb->outq;
352 DLIST_REMOVE(ctdb->outq, done);
353 /* We add at the head: any dead ones
355 DLIST_ADD(ctdb->doneq, done);
360 while (revents & POLLIN) {
364 ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
367 "ctdb_service: allocating readbuf");
373 ret = read_io_elem(ctdb->fd, ctdb->in);
374 if (real_error(ret) < 0 || ret == 0) {
375 /* They closed fd? */
379 "ctdb_service: error reading from ctdbd");
382 } else if (ret < 0) {
383 /* No progress, stop loop. */
385 } else if (io_elem_finished(ctdb->in)) {
386 handle_incoming(ctdb, ctdb->in);
394 /* This is inefficient. We could pull in idtree.c. */
395 static bool reqid_used(const struct ctdb_connection *ctdb, uint32_t reqid)
397 struct ctdb_request *i;
399 for (i = ctdb->outq; i; i = i->next) {
400 if (i->hdr.hdr->reqid == reqid) {
404 for (i = ctdb->doneq; i; i = i->next) {
405 if (i->hdr.hdr->reqid == reqid) {
412 uint32_t new_reqid(struct ctdb_connection *ctdb)
414 while (reqid_used(ctdb, ctdb->next_id)) {
417 return ctdb->next_id++;
420 struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
423 const void *extra_data,
425 ctdb_callback_t callback,
428 struct ctdb_request *req;
429 struct ctdb_req_control *pkt;
431 req = new_ctdb_request(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
435 io_elem_init_req_header(req->io,
436 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
438 pkt = req->hdr.control;
440 pkt->opcode = opcode;
444 pkt->datalen = extra;
445 memcpy(pkt->data, extra_data, extra);
446 DLIST_ADD(ctdb->outq, req);
450 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
451 struct ctdb_request *req,
454 ctdb_request_free(ctdb, req);
457 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
459 DEBUG(ctdb, LOG_DEBUG, "ctdb_cancel: %p (id %u)",
460 req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
462 /* FIXME: If it's not sent, we could just free it right now. */
463 req->callback = ctdb_cancel_callback;
467 struct ctdb_connection *ctdb;
471 struct tdb_context *tdb;
473 /* The lock we are holding, if any (we can only have one!) */
474 struct ctdb_lock *lock;
476 ctdb_callback_t callback;
480 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
481 struct ctdb_request *req,
484 struct ctdb_db *db = _db;
486 /* Do callback on original request. */
487 db->callback(ctdb, req->extra, db->private_data);
490 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
491 struct ctdb_request *req)
493 struct ctdb_request *dbpath_req = req->extra;
494 struct ctdb_reply_control *reply;
495 struct ctdb_db *db = req->priv_data;
496 uint32_t tdb_flags = db->tdb_flags;
498 /* Never sent the dbpath request? We've failed. */
500 /* FIXME: Save errno? */
505 reply = unpack_reply_control(ctdb, dbpath_req, CTDB_CONTROL_GETDBPATH);
509 if (reply->status != 0) {
510 DEBUG(db->ctdb, LOG_ERR,
511 "ctdb_attachdb_recv: reply status %i", reply->status);
515 tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
516 tdb_flags |= TDB_DISALLOW_NESTING;
518 /* FIXME: Setup logging to go through our logging. */
519 db->tdb = tdb_open((char *)reply->data, 0, tdb_flags, O_RDWR, 0);
520 if (db->tdb == NULL) {
521 DEBUG(db->ctdb, LOG_ERR,
522 "ctdb_attachdb_recv: failed to tdb_open %s",
523 (char *)reply->data);
527 /* Finally, separate the db from the request (see destroy_req_db). */
528 req->priv_data = NULL;
529 DEBUG(db->ctdb, LOG_DEBUG,
530 "ctdb_attachdb_recv: db %p, tdb %s", db, (char *)reply->data);
534 static void attachdb_done(struct ctdb_connection *ctdb,
535 struct ctdb_request *req,
538 struct ctdb_db *db = _db;
539 struct ctdb_request *req2;
540 struct ctdb_reply_control *reply;
541 enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
543 if (db->persistent) {
544 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
547 reply = unpack_reply_control(ctdb, req, control);
548 if (!reply || reply->status != 0) {
551 "ctdb_attachdb_send(async): DB_ATTACH status %i",
554 /* We failed. Hand request to user and have them discover it
555 * via ctdb_attachdb_recv. */
556 db->callback(ctdb, req, db->private_data);
559 db->id = *(uint32_t *)reply->data;
561 /* Now we do another call, to get the dbpath. */
562 req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
564 &db->id, sizeof(db->id),
565 attachdb_getdbpath_done, db);
567 DEBUG(db->ctdb, LOG_ERR,
568 "ctdb_attachdb_send(async): failed to allocate");
569 db->callback(ctdb, req, db->private_data);
574 DEBUG(db->ctdb, LOG_DEBUG,
575 "ctdb_attachdb_send(async): created getdbpath request");
578 static void destroy_req_db(struct ctdb_connection *ctdb,
579 struct ctdb_request *req)
581 /* Incomplete db is in priv_data. */
582 free(req->priv_data);
583 /* second request is chained off this one. */
585 ctdb_request_free(ctdb, req->extra);
589 struct ctdb_request *
590 ctdb_attachdb_send(struct ctdb_connection *ctdb,
591 const char *name, int persistent, uint32_t tdb_flags,
592 ctdb_callback_t callback, void *private_data)
594 struct ctdb_request *req;
598 /* FIXME: Search if db already open. */
599 db = malloc(sizeof(*db));
605 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
607 opcode = CTDB_CONTROL_DB_ATTACH;
610 req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
611 strlen(name) + 1, attachdb_done, db);
613 DEBUG(db->ctdb, LOG_ERR,
614 "ctdb_attachdb_send: failed allocating DB_ATTACH");
620 db->tdb_flags = tdb_flags;
621 db->persistent = persistent;
622 db->callback = callback;
623 db->private_data = private_data;
625 req->extra_destructor = destroy_req_db;
626 /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
629 /* Flags get overloaded into srvid. */
630 req->hdr.control->srvid = tdb_flags;
631 DEBUG(db->ctdb, LOG_DEBUG,
632 "ctdb_attachdb_send: DB_ATTACH request %p", req);
636 void ctdb_release_lock(struct ctdb_lock *lock)
639 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
640 DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
641 "ctdb_release_lock %p", lock);
643 remove_lock(lock->ctdb_db->ctdb, lock);
649 /* We keep the lock if local node is the dmaster. */
650 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
652 struct ctdb_ltdb_header *hdr;
654 if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
655 DEBUG(lock->ctdb_db->ctdb, LOG_WARNING,
656 "ctdb_readrecordlock_async: failed to chainlock");
660 hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
661 if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
662 DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
663 "ctdb_readrecordlock_async: got local lock");
666 add_lock(lock->ctdb_db->ctdb, lock);
670 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
675 /* If they shutdown before we hand them the lock, we free it here. */
676 static void destroy_lock(struct ctdb_connection *ctdb,
677 struct ctdb_request *req)
679 ctdb_release_lock(req->extra);
682 static void readrecordlock_retry(struct ctdb_connection *ctdb,
683 struct ctdb_request *req, void *private)
685 struct ctdb_lock *lock = req->extra;
686 struct ctdb_reply_call *reply;
689 /* OK, we've received reply to noop migration */
690 reply = unpack_reply_call(ctdb, req, CTDB_NULL_FUNC);
691 if (!reply || reply->status != 0) {
694 "ctdb_readrecordlock_async(async):"
695 " NULL_FUNC returned %i", reply->status);
697 lock->callback(lock->ctdb_db, NULL, tdb_null, private);
698 ctdb_request_free(ctdb, req); /* Also frees lock. */
702 /* Can we get lock now? */
703 if (try_readrecordlock(lock, &data)) {
704 /* Now it's their responsibility to free lock & request! */
705 req->extra_destructor = NULL;
706 lock->callback(lock->ctdb_db, lock, data, private);
710 /* Retransmit the same request again (we lost race). */
711 io_elem_reset(req->io);
712 DLIST_ADD(ctdb->outq, req);
716 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
717 ctdb_rrl_callback_t callback, void *cbdata)
719 struct ctdb_request *req;
720 struct ctdb_lock *lock;
723 if (holding_lock(ctdb_db->ctdb)) {
724 DEBUG(ctdb_db->ctdb, LOG_ERR,
725 "ctdb_readrecordlock_async: already holding lock");
730 lock = malloc(sizeof(*lock) + key.dsize);
732 DEBUG(ctdb_db->ctdb, LOG_ERR,
733 "ctdb_readrecordlock_async: lock allocation failed");
736 lock->key.dptr = (void *)(lock + 1);
737 memcpy(lock->key.dptr, key.dptr, key.dsize);
738 lock->key.dsize = key.dsize;
739 lock->ctdb_db = ctdb_db;
744 if (try_readrecordlock(lock, &data)) {
745 callback(ctdb_db, lock, data, cbdata);
749 /* Slow path: create request. */
750 req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
751 + key.dsize, readrecordlock_retry, cbdata);
753 DEBUG(ctdb_db->ctdb, LOG_ERR,
754 "ctdb_readrecordlock_async: allocation failed");
755 ctdb_release_lock(lock);
759 req->extra_destructor = destroy_lock;
760 /* We store the original callback in the lock, and use our own. */
761 lock->callback = callback;
763 io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
764 new_reqid(ctdb_db->ctdb));
766 req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
767 req->hdr.call->db_id = ctdb_db->id;
768 req->hdr.call->callid = CTDB_NULL_FUNC;
769 req->hdr.call->hopcount = 0;
770 req->hdr.call->keylen = key.dsize;
771 req->hdr.call->calldatalen = 0;
772 memcpy(req->hdr.call->data, key.dptr, key.dsize);
773 DLIST_ADD(ctdb_db->ctdb->outq, req);
777 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data)
779 if (lock->ctdb_db->persistent) {
781 DEBUG(lock->ctdb_db->ctdb, LOG_ERR,
782 "ctdb_writerecord: cannot write to persistent db");
786 return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr,