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(ctdb, req, &ctdb->pnn) != 0) {
75 /* FIXME: Report error. */
78 ctdb_request_free(ctdb, 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_connection *ctdb, struct ctdb_request *req)
161 if (req->extra_destructor) {
162 req->extra_destructor(ctdb, 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_connection *ctdb,
174 struct ctdb_request *req,
178 struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
180 /* ctdbd or our error if this isn't a reply call. */
181 if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
186 /* Library user error if this isn't a reply to a call. */
187 if (req->hdr.hdr->operation != CTDB_REQ_CALL
188 || req->hdr.call->callid != callid) {
196 /* Sanity-checking wrapper for reply.
198 struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
199 struct ctdb_request *req,
200 enum ctdb_controls control)
203 struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
205 /* Library user error if this isn't a reply to a call. */
206 if (len < sizeof(*inhdr)
207 || req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
212 /* ... or if it was a different control from what we expected. */
213 if (req->hdr.control->opcode != control) {
218 /* ctdbd or our error if this isn't a reply call. */
219 if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
227 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
229 struct ctdb_req_header *hdr;
231 struct ctdb_request *i;
233 hdr = io_elem_data(in, &len);
234 /* FIXME: use len to check packet! */
236 if (hdr->operation == CTDB_REQ_MESSAGE) {
237 deliver_message(ctdb, hdr);
241 for (i = ctdb->doneq; i; i = i->next) {
242 if (i->hdr.hdr->reqid == hdr->reqid) {
243 DLIST_REMOVE(ctdb->doneq, i);
245 i->callback(ctdb, i, i->priv_data);
249 /* FIXME: report this error. */
253 /* Remove "harmless" errors. */
254 static ssize_t real_error(ssize_t ret)
256 if (ret < 0 && (errno == EINTR || errno == EWOULDBLOCK))
261 int ctdb_service(struct ctdb_connection *ctdb, int revents)
267 if (revents & POLLOUT) {
269 if (real_error(write_io_elem(ctdb->fd,
270 ctdb->outq->io)) < 0) {
274 if (io_elem_finished(ctdb->outq->io)) {
275 struct ctdb_request *done = ctdb->outq;
276 DLIST_REMOVE(ctdb->outq, done);
277 /* We add at the head: any dead ones
279 DLIST_ADD(ctdb->doneq, done);
284 while (revents & POLLIN) {
288 ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
295 ret = read_io_elem(ctdb->fd, ctdb->in);
296 if (real_error(ret) < 0 || ret == 0) {
297 /* They closed fd? */
302 } else if (ret < 0) {
303 /* No progress, stop loop. */
305 } else if (io_elem_finished(ctdb->in)) {
306 handle_incoming(ctdb, ctdb->in);
314 /* This is inefficient. We could pull in idtree.c. */
315 static bool reqid_used(const struct ctdb_connection *ctdb, uint32_t reqid)
317 struct ctdb_request *i;
319 for (i = ctdb->outq; i; i = i->next) {
320 if (i->hdr.hdr->reqid == reqid) {
324 for (i = ctdb->doneq; i; i = i->next) {
325 if (i->hdr.hdr->reqid == reqid) {
332 uint32_t new_reqid(struct ctdb_connection *ctdb)
334 while (reqid_used(ctdb, ctdb->next_id)) {
337 return ctdb->next_id++;
340 struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
343 const void *extra_data,
345 ctdb_callback_t callback,
348 struct ctdb_request *req;
349 struct ctdb_req_control *pkt;
351 req = new_ctdb_request(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
355 io_elem_init_req_header(req->io,
356 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
358 pkt = req->hdr.control;
360 pkt->opcode = opcode;
364 pkt->datalen = extra;
365 memcpy(pkt->data, extra_data, extra);
366 DLIST_ADD(ctdb->outq, req);
370 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
371 struct ctdb_request *req,
374 ctdb_request_free(ctdb, req);
377 int ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
379 /* FIXME: If it's not sent, we could just free it right now. */
380 req->callback = ctdb_cancel_callback;
385 struct ctdb_connection *ctdb;
389 struct tdb_context *tdb;
391 ctdb_callback_t callback;
395 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
396 struct ctdb_request *req,
399 struct ctdb_db *db = _db;
401 /* Do callback on original request. */
402 db->callback(ctdb, req->extra, db->private_data);
405 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
406 struct ctdb_request *req)
408 struct ctdb_request *dbpath_req = req->extra;
409 struct ctdb_reply_control *reply;
410 struct ctdb_db *db = req->priv_data;
411 uint32_t tdb_flags = db->tdb_flags;
413 /* Never sent the dbpath request? We've failed. */
415 /* FIXME: Save errno? */
420 reply = unpack_reply_control(ctdb, dbpath_req, CTDB_CONTROL_GETDBPATH);
421 if (!reply || reply->status != 0) {
425 tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
426 tdb_flags |= TDB_DISALLOW_NESTING;
428 db->tdb = tdb_open((char *)reply->data, 0, tdb_flags, O_RDWR, 0);
429 if (db->tdb == NULL) {
433 /* Finally, separate the db from the request (see destroy_req_db). */
434 req->priv_data = NULL;
438 static void attachdb_done(struct ctdb_connection *ctdb,
439 struct ctdb_request *req,
442 struct ctdb_db *db = _db;
443 struct ctdb_request *req2;
444 struct ctdb_reply_control *reply;
445 enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
447 if (db->persistent) {
448 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
451 reply = unpack_reply_control(ctdb, req, control);
452 if (!reply || reply->status != 0) {
453 /* We failed. Hand request to user and have them discover it
454 * via ctdb_attachdb_recv. */
455 db->callback(ctdb, req, db->private_data);
458 db->id = *(uint32_t *)reply->data;
460 /* Now we do another call, to get the dbpath. */
461 req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
463 &db->id, sizeof(db->id),
464 attachdb_getdbpath_done, db);
466 db->callback(ctdb, req, db->private_data);
473 static void destroy_req_db(struct ctdb_connection *ctdb,
474 struct ctdb_request *req)
476 /* Incomplete db is in priv_data. */
477 free(req->priv_data);
478 /* second request is chained off this one. */
480 ctdb_request_free(ctdb, req->extra);
484 struct ctdb_request *
485 ctdb_attachdb_send(struct ctdb_connection *ctdb,
486 const char *name, int persistent, uint32_t tdb_flags,
487 ctdb_callback_t callback, void *private_data)
489 struct ctdb_request *req;
493 /* FIXME: Search if db already open. */
494 db = malloc(sizeof(*db));
500 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
502 opcode = CTDB_CONTROL_DB_ATTACH;
505 req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
506 strlen(name) + 1, attachdb_done, db);
513 db->tdb_flags = tdb_flags;
514 db->persistent = persistent;
515 db->callback = callback;
516 db->private_data = private_data;
518 req->extra_destructor = destroy_req_db;
519 /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
522 /* Flags get overloaded into srvid. */
523 req->hdr.control->srvid = tdb_flags;
528 struct ctdb_db *ctdb_db;
531 /* This will always be true by the time user sees this. */
533 struct ctdb_ltdb_header *hdr;
535 /* For convenience, we stash original callback here. */
536 ctdb_rrl_callback_t callback;
539 void ctdb_release_lock(struct ctdb_lock *lock)
542 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
547 static void ctdb_free_lock(struct ctdb_lock *lock)
550 /* FIXME: report error. Callback never released the lock */
551 ctdb_release_lock(lock);
558 /* We keep the lock if local node is the dmaster. */
559 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
561 struct ctdb_ltdb_header *hdr;
563 if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
567 hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
568 if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
574 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
579 /* If they shutdown before we hand them the lock, we free it here. */
580 static void destroy_lock(struct ctdb_connection *ctdb,
581 struct ctdb_request *req)
583 ctdb_release_lock(req->extra);
584 ctdb_free_lock(req->extra);
587 static void readrecordlock_retry(struct ctdb_connection *ctdb,
588 struct ctdb_request *req, void *private)
590 struct ctdb_lock *lock = req->extra;
591 struct ctdb_reply_call *reply;
594 /* OK, we've received reply to noop migration */
595 reply = unpack_reply_call(ctdb, req, CTDB_NULL_FUNC);
596 if (!reply || reply->status != 0) {
597 lock->callback(lock->ctdb_db, NULL, tdb_null, private);
598 ctdb_request_free(ctdb, req); /* Also frees lock. */
599 ctdb_free_lock(lock);
603 /* Can we get lock now? */
604 if (try_readrecordlock(lock, &data)) {
605 /* Now it's their responsibility to free lock & request! */
606 req->extra_destructor = NULL;
607 lock->callback(lock->ctdb_db, lock, data, private);
608 ctdb_free_lock(lock);
612 /* Retransmit the same request again (we lost race). */
613 io_elem_reset(req->io);
614 DLIST_ADD(ctdb->outq, req);
618 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
619 ctdb_rrl_callback_t callback, void *cbdata)
621 struct ctdb_request *req;
622 struct ctdb_lock *lock;
626 lock = malloc(sizeof(*lock) + key.dsize);
630 lock->key.dptr = (void *)(lock + 1);
631 memcpy(lock->key.dptr, key.dptr, key.dsize);
632 lock->key.dsize = key.dsize;
633 lock->ctdb_db = ctdb_db;
638 if (try_readrecordlock(lock, &data)) {
639 callback(ctdb_db, lock, data, cbdata);
640 ctdb_free_lock(lock);
644 /* Slow path: create request. */
645 req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
646 + key.dsize, readrecordlock_retry, cbdata);
648 ctdb_release_lock(lock);
649 ctdb_free_lock(lock);
653 req->extra_destructor = destroy_lock;
654 /* We store the original callback in the lock, and use our own. */
655 lock->callback = callback;
657 io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
658 new_reqid(ctdb_db->ctdb));
660 req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
661 req->hdr.call->db_id = ctdb_db->id;
662 req->hdr.call->callid = CTDB_NULL_FUNC;
663 req->hdr.call->hopcount = 0;
664 req->hdr.call->keylen = key.dsize;
665 req->hdr.call->calldatalen = 0;
666 memcpy(req->hdr.call->data, key.dptr, key.dsize);
667 DLIST_ADD(ctdb_db->ctdb->outq, req);
671 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data)
673 if (lock->ctdb_db->persistent) {
674 /* FIXME: Report error. */
679 /* FIXME: Report error. */
683 return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr,