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_send
38 /* FIXME: Could be in shared util code with rest of ctdb */
39 static void close_noerr(int fd)
46 /* FIXME: Could be in shared util code with rest of ctdb */
47 static void free_noerr(void *p)
54 /* FIXME: Could be in shared util code with rest of ctdb */
55 static void set_nonblocking(int fd)
58 v = fcntl(fd, F_GETFL, 0);
59 fcntl(fd, F_SETFL, v | O_NONBLOCK);
62 /* FIXME: Could be in shared util code with rest of ctdb */
63 static void set_close_on_exec(int fd)
66 v = fcntl(fd, F_GETFD, 0);
67 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
70 static void set_pnn(struct ctdb_connection *ctdb,
71 struct ctdb_request *req,
74 if (ctdb_getpnn_recv(req, &ctdb->pnn) != 0) {
75 /* FIXME: Report error. */
80 struct ctdb_connection *ctdb_connect(const char *addr)
82 struct ctdb_connection *ctdb;
83 struct sockaddr_un sun;
85 ctdb = malloc(sizeof(*ctdb));
91 ctdb->message_handlers = NULL;
95 memset(&sun, 0, sizeof(sun));
96 sun.sun_family = AF_UNIX;
99 strncpy(sun.sun_path, addr, sizeof(sun.sun_path));
100 ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
104 set_nonblocking(ctdb->fd);
105 set_close_on_exec(ctdb->fd);
107 if (connect(ctdb->fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
110 /* Immediately queue a request to get our pnn. */
111 if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
117 close_noerr(ctdb->fd);
124 int ctdb_get_fd(struct ctdb_connection *ctdb)
129 int ctdb_which_events(struct ctdb_connection *ctdb)
138 struct ctdb_request *new_ctdb_request(size_t len,
139 ctdb_callback_t cb, void *cbdata)
141 struct ctdb_request *req = malloc(sizeof(*req));
144 req->io = new_io_elem(len);
149 req->hdr.hdr = io_elem_data(req->io, NULL);
152 req->priv_data = cbdata;
154 req->extra_destructor = NULL;
158 void ctdb_request_free(struct ctdb_request *req)
160 if (req->extra_destructor) {
161 req->extra_destructor(req);
164 free_io_elem(req->reply);
166 free_io_elem(req->io);
170 /* Sanity-checking wrapper for reply.
172 static struct ctdb_reply_call *unpack_reply_call(struct ctdb_request *req,
176 struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
178 /* ctdbd or our error if this isn't a reply call. */
179 if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
184 /* Library user error if this isn't a reply to a call. */
185 if (req->hdr.hdr->operation != CTDB_REQ_CALL
186 || req->hdr.call->callid != callid) {
194 /* Sanity-checking wrapper for reply.
196 struct ctdb_reply_control *unpack_reply_control(struct ctdb_request *req,
197 enum ctdb_controls control)
200 struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
202 /* Library user error if this isn't a reply to a call. */
203 if (len < sizeof(*inhdr)
204 || req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
209 /* ... or if it was a different control from what we expected. */
210 if (req->hdr.control->opcode != control) {
215 /* ctdbd or our error if this isn't a reply call. */
216 if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
224 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
226 struct ctdb_req_header *hdr;
228 struct ctdb_request *i;
230 hdr = io_elem_data(in, &len);
231 /* FIXME: use len to check packet! */
233 if (hdr->operation == CTDB_REQ_MESSAGE) {
234 deliver_message(ctdb, hdr);
238 for (i = ctdb->doneq; i; i = i->next) {
239 if (i->hdr.hdr->reqid == hdr->reqid) {
240 DLIST_REMOVE(ctdb->doneq, i);
242 i->callback(ctdb, i, i->priv_data);
243 ctdb_request_free(i);
247 /* FIXME: report this error. */
251 /* Remove "harmless" errors. */
252 static ssize_t real_error(ssize_t ret)
254 if (ret < 0 && (errno == EINTR || errno == EWOULDBLOCK))
259 int ctdb_service(struct ctdb_connection *ctdb, int revents)
265 if (revents & POLLOUT) {
267 if (real_error(write_io_elem(ctdb->fd,
268 ctdb->outq->io)) < 0) {
272 if (io_elem_finished(ctdb->outq->io)) {
273 struct ctdb_request *done = ctdb->outq;
274 DLIST_REMOVE(ctdb->outq, done);
275 /* We add at the head: any dead ones
277 DLIST_ADD(ctdb->doneq, done);
282 while (revents & POLLIN) {
286 ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
293 ret = read_io_elem(ctdb->fd, ctdb->in);
294 if (real_error(ret) < 0 || ret == 0) {
295 /* They closed fd? */
300 } else if (ret < 0) {
301 /* No progress, stop loop. */
303 } else if (io_elem_finished(ctdb->in)) {
304 handle_incoming(ctdb, ctdb->in);
312 /* This is inefficient. We could pull in idtree.c. */
313 static bool reqid_used(const struct ctdb_connection *ctdb, uint32_t reqid)
315 struct ctdb_request *i;
317 for (i = ctdb->outq; i; i = i->next) {
318 if (i->hdr.hdr->reqid == reqid) {
322 for (i = ctdb->doneq; i; i = i->next) {
323 if (i->hdr.hdr->reqid == reqid) {
330 uint32_t new_reqid(struct ctdb_connection *ctdb)
332 while (reqid_used(ctdb, ctdb->next_id)) {
335 return ctdb->next_id++;
338 struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
341 const void *extra_data,
343 ctdb_callback_t callback,
346 struct ctdb_request *req;
347 struct ctdb_req_control *pkt;
349 req = new_ctdb_request(sizeof(*pkt) + extra, callback, cbdata);
353 io_elem_init_req_header(req->io,
354 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
356 pkt = req->hdr.control;
357 pkt->opcode = opcode;
361 pkt->datalen = extra;
362 memcpy(pkt->data, extra_data, extra);
363 DLIST_ADD(ctdb->outq, req);
367 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
368 struct ctdb_request *req,
371 ctdb_request_free(req);
374 int ctdb_cancel(struct ctdb_request *req)
376 /* FIXME: If it's not sent, we could just free it right now. */
377 req->callback = ctdb_cancel_callback;
382 struct ctdb_connection *ctdb;
386 struct tdb_context *tdb;
388 ctdb_callback_t callback;
392 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
393 struct ctdb_request *req,
396 struct ctdb_db *db = _db;
398 /* Do callback on original request. */
399 db->callback(ctdb, req->extra, db->private_data);
402 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_request *req)
404 struct ctdb_request *dbpath_req = req->extra;
405 struct ctdb_reply_control *reply;
406 struct ctdb_db *db = req->priv_data;
407 uint32_t tdb_flags = db->tdb_flags;
409 /* Never sent the dbpath request? We've failed. */
411 /* FIXME: Save errno? */
416 reply = unpack_reply_control(dbpath_req, CTDB_CONTROL_GETDBPATH);
417 if (!reply || reply->status != 0) {
421 tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
422 tdb_flags |= TDB_DISALLOW_NESTING;
424 db->tdb = tdb_open((char *)reply->data, 0, tdb_flags, O_RDWR, 0);
425 if (db->tdb == NULL) {
429 /* Finally, separate the db from the request (see destroy_req_db). */
430 req->priv_data = NULL;
434 static void attachdb_done(struct ctdb_connection *ctdb,
435 struct ctdb_request *req,
438 struct ctdb_db *db = _db;
439 struct ctdb_request *req2;
440 struct ctdb_reply_control *reply;
441 enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
443 if (db->persistent) {
444 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
447 reply = unpack_reply_control(req, control);
448 if (!reply || reply->status != 0) {
449 /* We failed. Hand request to user and have them discover it
450 * via ctdb_attachdb_recv. */
451 db->callback(ctdb, req, db);
454 db->id = *(uint32_t *)reply->data;
456 /* Now we do another call, to get the dbpath. */
457 req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
459 &db->id, sizeof(db->id),
460 attachdb_getdbpath_done, db);
462 db->callback(ctdb, req, db);
469 static void destroy_req_db(struct ctdb_request *req)
471 /* Incomplete db is in priv_data. */
472 free(req->priv_data);
473 /* second request is chained off this one. */
475 ctdb_request_free(req->extra);
479 struct ctdb_request *
480 ctdb_attachdb_send(struct ctdb_connection *ctdb,
481 const char *name, int persistent, uint32_t tdb_flags,
482 ctdb_callback_t callback, void *private_data)
484 struct ctdb_request *req;
488 /* FIXME: Search if db already open. */
489 db = malloc(sizeof(*db));
495 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
497 opcode = CTDB_CONTROL_DB_ATTACH;
500 req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
501 strlen(name) + 1, attachdb_done, db);
508 db->tdb_flags = tdb_flags;
509 db->persistent = persistent;
510 db->callback = callback;
511 db->private_data = private_data;
513 req->extra_destructor = destroy_req_db;
514 /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
517 /* Flags get overloaded into srvid. */
518 req->hdr.control->srvid = tdb_flags;
523 struct ctdb_db *ctdb_db;
526 /* This will always be true by the time user sees this. */
528 struct ctdb_ltdb_header *hdr;
531 /* For convenience, we stash original callback here. */
532 ctdb_callback_t callback;
535 void ctdb_release_lock(struct ctdb_lock *lock)
538 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
540 free(lock->hdr); /* Also frees data */
544 /* We keep the lock if local node is the dmaster. */
545 static bool try_readrecordlock(struct ctdb_lock *lock)
547 struct ctdb_ltdb_header *hdr;
549 if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
553 hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, &lock->data);
554 if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
560 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
565 /* If they cancel *before* we hand them the lock from
566 * ctdb_readrecordlock_recv, we free it here. */
567 static void destroy_lock(struct ctdb_request *req)
569 ctdb_release_lock(req->extra);
572 struct ctdb_lock *ctdb_readrecordlock_recv(struct ctdb_db *ctdb_db,
573 struct ctdb_request *req,
576 struct ctdb_lock *lock = req->extra;
579 /* Something went wrong. */
583 /* Now it's their responsibility to free! */
584 req->extra_destructor = NULL;
589 static void readrecordlock_retry(struct ctdb_connection *ctdb,
590 struct ctdb_request *req, void *private)
592 struct ctdb_lock *lock = req->extra;
593 struct ctdb_reply_call *reply;
595 /* OK, we've received reply to noop migration */
596 reply = unpack_reply_call(req, CTDB_NULL_FUNC);
597 if (!reply || reply->status != 0) {
598 lock->callback(ctdb, req, private);
602 /* Can we get lock now? */
603 if (try_readrecordlock(lock)) {
604 lock->callback(ctdb, req, private);
608 /* Retransmit the same request again (we lost race). */
609 io_elem_reset(req->io);
610 DLIST_ADD(ctdb->outq, req);
615 ctdb_readrecordlock_send(struct ctdb_db *ctdb_db, TDB_DATA key,
616 struct ctdb_request **reqp,
617 ctdb_callback_t callback, void *cbdata)
619 struct ctdb_request *req;
620 struct ctdb_lock *lock;
623 lock = malloc(sizeof(*lock) + key.dsize);
627 lock->key.dptr = (void *)(lock + 1);
628 memcpy(lock->key.dptr, key.dptr, key.dsize);
629 lock->key.dsize = key.dsize;
630 lock->ctdb_db = ctdb_db;
634 /* Get ready in case we need to send a migrate request. */
635 req = new_ctdb_request(sizeof(*req->hdr.call)
636 + key.dsize, callback, cbdata);
638 ctdb_release_lock(lock);
642 req->extra_destructor = destroy_lock;
644 if (try_readrecordlock(lock)) {
646 callback(ctdb_db->ctdb, req, cbdata);
650 /* We store the original callback in the lock, and use our own. */
651 lock->callback = callback;
652 req->callback = readrecordlock_retry;
654 io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
655 new_reqid(ctdb_db->ctdb));
657 req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
658 req->hdr.call->db_id = ctdb_db->id;
659 req->hdr.call->callid = CTDB_NULL_FUNC;
660 req->hdr.call->hopcount = 0;
661 req->hdr.call->keylen = key.dsize;
662 req->hdr.call->calldatalen = 0;
663 memcpy(req->hdr.call->data, key.dptr, key.dsize);
664 DLIST_ADD(ctdb_db->ctdb->outq, req);
669 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data)
671 if (lock->ctdb_db->persistent) {
672 /* FIXME: Report error. */
676 return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr,