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
39 /* FIXME: Could be in shared util code with rest of ctdb */
40 static void close_noerr(int fd)
47 /* FIXME: Could be in shared util code with rest of ctdb */
48 static void free_noerr(void *p)
55 /* FIXME: Could be in shared util code with rest of ctdb */
56 static void set_nonblocking(int fd)
59 v = fcntl(fd, F_GETFL, 0);
60 fcntl(fd, F_SETFL, v | O_NONBLOCK);
63 /* FIXME: Could be in shared util code with rest of ctdb */
64 static void set_close_on_exec(int fd)
67 v = fcntl(fd, F_GETFD, 0);
68 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
71 static void set_pnn(struct ctdb_connection *ctdb,
72 struct ctdb_request *req,
75 if (ctdb_getpnn_recv(ctdb, req, &ctdb->pnn) != 0) {
76 /* FIXME: Report error. */
79 ctdb_request_free(ctdb, req);
82 struct ctdb_connection *ctdb_connect(const char *addr,
83 ctdb_log_fn_t log_fn, void *log_priv)
85 struct ctdb_connection *ctdb;
86 struct sockaddr_un sun;
88 ctdb = malloc(sizeof(*ctdb));
94 ctdb->message_handlers = NULL;
98 ctdb->log_priv = log_priv;
100 memset(&sun, 0, sizeof(sun));
101 sun.sun_family = AF_UNIX;
104 strncpy(sun.sun_path, addr, sizeof(sun.sun_path));
105 ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
109 set_nonblocking(ctdb->fd);
110 set_close_on_exec(ctdb->fd);
112 if (connect(ctdb->fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
115 /* Immediately queue a request to get our pnn. */
116 if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
122 close_noerr(ctdb->fd);
129 int ctdb_get_fd(struct ctdb_connection *ctdb)
134 int ctdb_which_events(struct ctdb_connection *ctdb)
143 struct ctdb_request *new_ctdb_request(size_t len,
144 ctdb_callback_t cb, void *cbdata)
146 struct ctdb_request *req = malloc(sizeof(*req));
149 req->io = new_io_elem(len);
154 req->hdr.hdr = io_elem_data(req->io, NULL);
157 req->priv_data = cbdata;
159 req->extra_destructor = NULL;
163 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req)
165 if (req->extra_destructor) {
166 req->extra_destructor(ctdb, req);
169 free_io_elem(req->reply);
171 free_io_elem(req->io);
175 /* Sanity-checking wrapper for reply.
177 static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
178 struct ctdb_request *req,
182 struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
184 /* ctdbd or our error if this isn't a reply call. */
185 if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
190 /* Library user error if this isn't a reply to a call. */
191 if (req->hdr.hdr->operation != CTDB_REQ_CALL
192 || req->hdr.call->callid != callid) {
200 /* Sanity-checking wrapper for reply.
202 struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
203 struct ctdb_request *req,
204 enum ctdb_controls control)
207 struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
209 /* Library user error if this isn't a reply to a call. */
210 if (len < sizeof(*inhdr)
211 || req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
216 /* ... or if it was a different control from what we expected. */
217 if (req->hdr.control->opcode != control) {
222 /* ctdbd or our error if this isn't a reply call. */
223 if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
231 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
233 struct ctdb_req_header *hdr;
235 struct ctdb_request *i;
237 hdr = io_elem_data(in, &len);
238 /* FIXME: use len to check packet! */
240 if (hdr->operation == CTDB_REQ_MESSAGE) {
241 deliver_message(ctdb, hdr);
245 for (i = ctdb->doneq; i; i = i->next) {
246 if (i->hdr.hdr->reqid == hdr->reqid) {
247 DLIST_REMOVE(ctdb->doneq, i);
249 i->callback(ctdb, i, i->priv_data);
253 /* FIXME: report this error. */
257 /* Remove "harmless" errors. */
258 static ssize_t real_error(ssize_t ret)
260 if (ret < 0 && (errno == EINTR || errno == EWOULDBLOCK))
265 int ctdb_service(struct ctdb_connection *ctdb, int revents)
271 if (revents & POLLOUT) {
273 if (real_error(write_io_elem(ctdb->fd,
274 ctdb->outq->io)) < 0) {
278 if (io_elem_finished(ctdb->outq->io)) {
279 struct ctdb_request *done = ctdb->outq;
280 DLIST_REMOVE(ctdb->outq, done);
281 /* We add at the head: any dead ones
283 DLIST_ADD(ctdb->doneq, done);
288 while (revents & POLLIN) {
292 ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
299 ret = read_io_elem(ctdb->fd, ctdb->in);
300 if (real_error(ret) < 0 || ret == 0) {
301 /* They closed fd? */
306 } else if (ret < 0) {
307 /* No progress, stop loop. */
309 } else if (io_elem_finished(ctdb->in)) {
310 handle_incoming(ctdb, ctdb->in);
318 /* This is inefficient. We could pull in idtree.c. */
319 static bool reqid_used(const struct ctdb_connection *ctdb, uint32_t reqid)
321 struct ctdb_request *i;
323 for (i = ctdb->outq; i; i = i->next) {
324 if (i->hdr.hdr->reqid == reqid) {
328 for (i = ctdb->doneq; i; i = i->next) {
329 if (i->hdr.hdr->reqid == reqid) {
336 uint32_t new_reqid(struct ctdb_connection *ctdb)
338 while (reqid_used(ctdb, ctdb->next_id)) {
341 return ctdb->next_id++;
344 struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
347 const void *extra_data,
349 ctdb_callback_t callback,
352 struct ctdb_request *req;
353 struct ctdb_req_control *pkt;
355 req = new_ctdb_request(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
359 io_elem_init_req_header(req->io,
360 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
362 pkt = req->hdr.control;
364 pkt->opcode = opcode;
368 pkt->datalen = extra;
369 memcpy(pkt->data, extra_data, extra);
370 DLIST_ADD(ctdb->outq, req);
374 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
375 struct ctdb_request *req,
378 ctdb_request_free(ctdb, req);
381 int ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
383 /* FIXME: If it's not sent, we could just free it right now. */
384 req->callback = ctdb_cancel_callback;
389 struct ctdb_connection *ctdb;
393 struct tdb_context *tdb;
395 ctdb_callback_t callback;
399 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
400 struct ctdb_request *req,
403 struct ctdb_db *db = _db;
405 /* Do callback on original request. */
406 db->callback(ctdb, req->extra, db->private_data);
409 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
410 struct ctdb_request *req)
412 struct ctdb_request *dbpath_req = req->extra;
413 struct ctdb_reply_control *reply;
414 struct ctdb_db *db = req->priv_data;
415 uint32_t tdb_flags = db->tdb_flags;
417 /* Never sent the dbpath request? We've failed. */
419 /* FIXME: Save errno? */
424 reply = unpack_reply_control(ctdb, dbpath_req, CTDB_CONTROL_GETDBPATH);
425 if (!reply || reply->status != 0) {
429 tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
430 tdb_flags |= TDB_DISALLOW_NESTING;
432 db->tdb = tdb_open((char *)reply->data, 0, tdb_flags, O_RDWR, 0);
433 if (db->tdb == NULL) {
437 /* Finally, separate the db from the request (see destroy_req_db). */
438 req->priv_data = NULL;
442 static void attachdb_done(struct ctdb_connection *ctdb,
443 struct ctdb_request *req,
446 struct ctdb_db *db = _db;
447 struct ctdb_request *req2;
448 struct ctdb_reply_control *reply;
449 enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
451 if (db->persistent) {
452 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
455 reply = unpack_reply_control(ctdb, req, control);
456 if (!reply || reply->status != 0) {
457 /* We failed. Hand request to user and have them discover it
458 * via ctdb_attachdb_recv. */
459 db->callback(ctdb, req, db->private_data);
462 db->id = *(uint32_t *)reply->data;
464 /* Now we do another call, to get the dbpath. */
465 req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
467 &db->id, sizeof(db->id),
468 attachdb_getdbpath_done, db);
470 db->callback(ctdb, req, db->private_data);
477 static void destroy_req_db(struct ctdb_connection *ctdb,
478 struct ctdb_request *req)
480 /* Incomplete db is in priv_data. */
481 free(req->priv_data);
482 /* second request is chained off this one. */
484 ctdb_request_free(ctdb, req->extra);
488 struct ctdb_request *
489 ctdb_attachdb_send(struct ctdb_connection *ctdb,
490 const char *name, int persistent, uint32_t tdb_flags,
491 ctdb_callback_t callback, void *private_data)
493 struct ctdb_request *req;
497 /* FIXME: Search if db already open. */
498 db = malloc(sizeof(*db));
504 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
506 opcode = CTDB_CONTROL_DB_ATTACH;
509 req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
510 strlen(name) + 1, attachdb_done, db);
517 db->tdb_flags = tdb_flags;
518 db->persistent = persistent;
519 db->callback = callback;
520 db->private_data = private_data;
522 req->extra_destructor = destroy_req_db;
523 /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
526 /* Flags get overloaded into srvid. */
527 req->hdr.control->srvid = tdb_flags;
532 struct ctdb_db *ctdb_db;
535 /* This will always be true by the time user sees this. */
537 struct ctdb_ltdb_header *hdr;
539 /* For convenience, we stash original callback here. */
540 ctdb_rrl_callback_t callback;
543 void ctdb_release_lock(struct ctdb_lock *lock)
546 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
551 static void ctdb_free_lock(struct ctdb_lock *lock)
554 /* FIXME: report error. Callback never released the lock */
555 ctdb_release_lock(lock);
562 /* We keep the lock if local node is the dmaster. */
563 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
565 struct ctdb_ltdb_header *hdr;
567 if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
571 hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
572 if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
578 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
583 /* If they shutdown before we hand them the lock, we free it here. */
584 static void destroy_lock(struct ctdb_connection *ctdb,
585 struct ctdb_request *req)
587 ctdb_release_lock(req->extra);
588 ctdb_free_lock(req->extra);
591 static void readrecordlock_retry(struct ctdb_connection *ctdb,
592 struct ctdb_request *req, void *private)
594 struct ctdb_lock *lock = req->extra;
595 struct ctdb_reply_call *reply;
598 /* OK, we've received reply to noop migration */
599 reply = unpack_reply_call(ctdb, req, CTDB_NULL_FUNC);
600 if (!reply || reply->status != 0) {
601 lock->callback(lock->ctdb_db, NULL, tdb_null, private);
602 ctdb_request_free(ctdb, req); /* Also frees lock. */
603 ctdb_free_lock(lock);
607 /* Can we get lock now? */
608 if (try_readrecordlock(lock, &data)) {
609 /* Now it's their responsibility to free lock & request! */
610 req->extra_destructor = NULL;
611 lock->callback(lock->ctdb_db, lock, data, private);
612 ctdb_free_lock(lock);
616 /* Retransmit the same request again (we lost race). */
617 io_elem_reset(req->io);
618 DLIST_ADD(ctdb->outq, req);
622 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
623 ctdb_rrl_callback_t callback, void *cbdata)
625 struct ctdb_request *req;
626 struct ctdb_lock *lock;
630 lock = malloc(sizeof(*lock) + key.dsize);
634 lock->key.dptr = (void *)(lock + 1);
635 memcpy(lock->key.dptr, key.dptr, key.dsize);
636 lock->key.dsize = key.dsize;
637 lock->ctdb_db = ctdb_db;
642 if (try_readrecordlock(lock, &data)) {
643 callback(ctdb_db, lock, data, cbdata);
644 ctdb_free_lock(lock);
648 /* Slow path: create request. */
649 req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
650 + key.dsize, readrecordlock_retry, cbdata);
652 ctdb_release_lock(lock);
653 ctdb_free_lock(lock);
657 req->extra_destructor = destroy_lock;
658 /* We store the original callback in the lock, and use our own. */
659 lock->callback = callback;
661 io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
662 new_reqid(ctdb_db->ctdb));
664 req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
665 req->hdr.call->db_id = ctdb_db->id;
666 req->hdr.call->callid = CTDB_NULL_FUNC;
667 req->hdr.call->hopcount = 0;
668 req->hdr.call->keylen = key.dsize;
669 req->hdr.call->calldatalen = 0;
670 memcpy(req->hdr.call->data, key.dptr, key.dsize);
671 DLIST_ADD(ctdb_db->ctdb->outq, req);
675 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data)
677 if (lock->ctdb_db->persistent) {
678 /* FIXME: Report error. */
683 /* FIXME: Report error. */
687 return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr,