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
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. */
78 ctdb_request_free(req);
81 struct ctdb_connection *ctdb_connect(const char *addr)
83 struct ctdb_connection *ctdb;
84 struct sockaddr_un sun;
86 ctdb = malloc(sizeof(*ctdb));
92 ctdb->message_handlers = NULL;
96 memset(&sun, 0, sizeof(sun));
97 sun.sun_family = AF_UNIX;
100 strncpy(sun.sun_path, addr, sizeof(sun.sun_path));
101 ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
105 set_nonblocking(ctdb->fd);
106 set_close_on_exec(ctdb->fd);
108 if (connect(ctdb->fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
111 /* Immediately queue a request to get our pnn. */
112 if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
118 close_noerr(ctdb->fd);
125 int ctdb_get_fd(struct ctdb_connection *ctdb)
130 int ctdb_which_events(struct ctdb_connection *ctdb)
139 struct ctdb_request *new_ctdb_request(size_t len,
140 ctdb_callback_t cb, void *cbdata)
142 struct ctdb_request *req = malloc(sizeof(*req));
145 req->io = new_io_elem(len);
150 req->hdr.hdr = io_elem_data(req->io, NULL);
153 req->priv_data = cbdata;
155 req->extra_destructor = NULL;
159 void ctdb_request_free(struct ctdb_request *req)
161 if (req->extra_destructor) {
162 req->extra_destructor(req);
165 free_io_elem(req->reply);
167 free_io_elem(req->io);
171 /* Sanity-checking wrapper for reply.
173 static struct ctdb_reply_call *unpack_reply_call(struct ctdb_request *req,
177 struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
179 /* ctdbd or our error if this isn't a reply call. */
180 if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
185 /* Library user error if this isn't a reply to a call. */
186 if (req->hdr.hdr->operation != CTDB_REQ_CALL
187 || req->hdr.call->callid != callid) {
195 /* Sanity-checking wrapper for reply.
197 struct ctdb_reply_control *unpack_reply_control(struct ctdb_request *req,
198 enum ctdb_controls control)
201 struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
203 /* Library user error if this isn't a reply to a call. */
204 if (len < sizeof(*inhdr)
205 || req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
210 /* ... or if it was a different control from what we expected. */
211 if (req->hdr.control->opcode != control) {
216 /* ctdbd or our error if this isn't a reply call. */
217 if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
225 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
227 struct ctdb_req_header *hdr;
229 struct ctdb_request *i;
231 hdr = io_elem_data(in, &len);
232 /* FIXME: use len to check packet! */
234 if (hdr->operation == CTDB_REQ_MESSAGE) {
235 deliver_message(ctdb, hdr);
239 for (i = ctdb->doneq; i; i = i->next) {
240 if (i->hdr.hdr->reqid == hdr->reqid) {
241 DLIST_REMOVE(ctdb->doneq, i);
243 i->callback(ctdb, i, i->priv_data);
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(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
353 io_elem_init_req_header(req->io,
354 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
356 pkt = req->hdr.control;
358 pkt->opcode = opcode;
362 pkt->datalen = extra;
363 memcpy(pkt->data, extra_data, extra);
364 DLIST_ADD(ctdb->outq, req);
368 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
369 struct ctdb_request *req,
372 ctdb_request_free(req);
375 int ctdb_cancel(struct ctdb_request *req)
377 /* FIXME: If it's not sent, we could just free it right now. */
378 req->callback = ctdb_cancel_callback;
383 struct ctdb_connection *ctdb;
387 struct tdb_context *tdb;
389 ctdb_callback_t callback;
393 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
394 struct ctdb_request *req,
397 struct ctdb_db *db = _db;
399 /* Do callback on original request. */
400 db->callback(ctdb, req->extra, db->private_data);
403 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_request *req)
405 struct ctdb_request *dbpath_req = req->extra;
406 struct ctdb_reply_control *reply;
407 struct ctdb_db *db = req->priv_data;
408 uint32_t tdb_flags = db->tdb_flags;
410 /* Never sent the dbpath request? We've failed. */
412 /* FIXME: Save errno? */
417 reply = unpack_reply_control(dbpath_req, CTDB_CONTROL_GETDBPATH);
418 if (!reply || reply->status != 0) {
422 tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
423 tdb_flags |= TDB_DISALLOW_NESTING;
425 db->tdb = tdb_open((char *)reply->data, 0, tdb_flags, O_RDWR, 0);
426 if (db->tdb == NULL) {
430 /* Finally, separate the db from the request (see destroy_req_db). */
431 req->priv_data = NULL;
435 static void attachdb_done(struct ctdb_connection *ctdb,
436 struct ctdb_request *req,
439 struct ctdb_db *db = _db;
440 struct ctdb_request *req2;
441 struct ctdb_reply_control *reply;
442 enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
444 if (db->persistent) {
445 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
448 reply = unpack_reply_control(req, control);
449 if (!reply || reply->status != 0) {
450 /* We failed. Hand request to user and have them discover it
451 * via ctdb_attachdb_recv. */
452 db->callback(ctdb, req, db->private_data);
455 db->id = *(uint32_t *)reply->data;
457 /* Now we do another call, to get the dbpath. */
458 req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
460 &db->id, sizeof(db->id),
461 attachdb_getdbpath_done, db);
463 db->callback(ctdb, req, db->private_data);
470 static void destroy_req_db(struct ctdb_request *req)
472 /* Incomplete db is in priv_data. */
473 free(req->priv_data);
474 /* second request is chained off this one. */
476 ctdb_request_free(req->extra);
480 struct ctdb_request *
481 ctdb_attachdb_send(struct ctdb_connection *ctdb,
482 const char *name, int persistent, uint32_t tdb_flags,
483 ctdb_callback_t callback, void *private_data)
485 struct ctdb_request *req;
489 /* FIXME: Search if db already open. */
490 db = malloc(sizeof(*db));
496 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
498 opcode = CTDB_CONTROL_DB_ATTACH;
501 req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
502 strlen(name) + 1, attachdb_done, db);
509 db->tdb_flags = tdb_flags;
510 db->persistent = persistent;
511 db->callback = callback;
512 db->private_data = private_data;
514 req->extra_destructor = destroy_req_db;
515 /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
518 /* Flags get overloaded into srvid. */
519 req->hdr.control->srvid = tdb_flags;
524 struct ctdb_db *ctdb_db;
527 /* This will always be true by the time user sees this. */
529 struct ctdb_ltdb_header *hdr;
531 /* For convenience, we stash original callback here. */
532 ctdb_rrl_callback_t callback;
535 void ctdb_release_lock(struct ctdb_lock *lock)
538 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
543 static void ctdb_free_lock(struct ctdb_lock *lock)
546 /* FIXME: report error. Callback never released the lock */
547 ctdb_release_lock(lock);
554 /* We keep the lock if local node is the dmaster. */
555 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
557 struct ctdb_ltdb_header *hdr;
559 if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
563 hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
564 if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
570 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
575 /* If they shutdown before we hand them the lock, we free it here. */
576 static void destroy_lock(struct ctdb_request *req)
578 ctdb_release_lock(req->extra);
579 ctdb_free_lock(req->extra);
582 static void readrecordlock_retry(struct ctdb_connection *ctdb,
583 struct ctdb_request *req, void *private)
585 struct ctdb_lock *lock = req->extra;
586 struct ctdb_reply_call *reply;
589 /* OK, we've received reply to noop migration */
590 reply = unpack_reply_call(req, CTDB_NULL_FUNC);
591 if (!reply || reply->status != 0) {
592 lock->callback(lock->ctdb_db, NULL, tdb_null, private);
593 ctdb_request_free(req); /* Also frees lock. */
594 ctdb_free_lock(lock);
598 /* Can we get lock now? */
599 if (try_readrecordlock(lock, &data)) {
600 /* Now it's their responsibility to free lock & request! */
601 req->extra_destructor = NULL;
602 lock->callback(lock->ctdb_db, lock, data, private);
603 ctdb_free_lock(lock);
607 /* Retransmit the same request again (we lost race). */
608 io_elem_reset(req->io);
609 DLIST_ADD(ctdb->outq, req);
613 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
614 ctdb_rrl_callback_t callback, void *cbdata)
616 struct ctdb_request *req;
617 struct ctdb_lock *lock;
621 lock = malloc(sizeof(*lock) + key.dsize);
625 lock->key.dptr = (void *)(lock + 1);
626 memcpy(lock->key.dptr, key.dptr, key.dsize);
627 lock->key.dsize = key.dsize;
628 lock->ctdb_db = ctdb_db;
633 if (try_readrecordlock(lock, &data)) {
634 callback(ctdb_db, lock, data, cbdata);
635 ctdb_free_lock(lock);
639 /* Slow path: create request. */
640 req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
641 + key.dsize, readrecordlock_retry, cbdata);
643 ctdb_release_lock(lock);
644 ctdb_free_lock(lock);
648 req->extra_destructor = destroy_lock;
649 /* We store the original callback in the lock, and use our own. */
650 lock->callback = callback;
652 io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
653 new_reqid(ctdb_db->ctdb));
655 req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
656 req->hdr.call->db_id = ctdb_db->id;
657 req->hdr.call->callid = CTDB_NULL_FUNC;
658 req->hdr.call->hopcount = 0;
659 req->hdr.call->keylen = key.dsize;
660 req->hdr.call->calldatalen = 0;
661 memcpy(req->hdr.call->data, key.dptr, key.dsize);
662 DLIST_ADD(ctdb_db->ctdb->outq, req);
666 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data)
668 if (lock->ctdb_db->persistent) {
669 /* FIXME: Report error. */
674 /* FIXME: Report error. */
678 return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr,