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 <sys/ioctl.h>
28 #include "libctdb_private.h"
30 #include "local_tdb.h"
32 #include <dlinklist.h>
33 #include <ctdb_protocol.h>
35 /* Remove type-safety macros. */
36 #undef ctdb_attachdb_send
37 #undef ctdb_readrecordlock_async
41 struct ctdb_lock *next, *prev;
43 struct ctdb_db *ctdb_db;
46 /* This will always be set by the time user sees this. */
47 unsigned long held_magic;
48 struct ctdb_ltdb_header *hdr;
50 /* For convenience, we stash original callback here. */
51 ctdb_rrl_callback_t callback;
55 struct ctdb_connection *ctdb;
59 struct tdb_context *tdb;
61 ctdb_callback_t callback;
65 static void remove_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
67 DLIST_REMOVE(ctdb->locks, lock);
70 /* FIXME: for thread safety, need tid info too. */
71 static bool holding_lock(struct ctdb_connection *ctdb)
73 /* For the moment, you can't ever hold more than 1 lock. */
74 return (ctdb->locks != NULL);
77 static void add_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
79 DLIST_ADD(ctdb->locks, lock);
82 static void cleanup_locks(struct ctdb_connection *ctdb, struct ctdb_db *db)
84 struct ctdb_lock *i, *next;
86 for (i = ctdb->locks; i; i = next) {
87 /* Grab next pointer, as release_lock will free i */
89 if (i->ctdb_db == db) {
90 ctdb_release_lock(db, i);
95 /* FIXME: Could be in shared util code with rest of ctdb */
96 static void close_noerr(int fd)
103 /* FIXME: Could be in shared util code with rest of ctdb */
104 static void free_noerr(void *p)
111 /* FIXME: Could be in shared util code with rest of ctdb */
112 static void set_nonblocking(int fd)
115 v = fcntl(fd, F_GETFL, 0);
116 fcntl(fd, F_SETFL, v | O_NONBLOCK);
119 /* FIXME: Could be in shared util code with rest of ctdb */
120 static void set_close_on_exec(int fd)
123 v = fcntl(fd, F_GETFD, 0);
124 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
127 static void set_pnn(struct ctdb_connection *ctdb,
128 struct ctdb_request *req,
131 if (!ctdb_getpnn_recv(ctdb, req, &ctdb->pnn)) {
132 DEBUG(ctdb, LOG_CRIT,
133 "ctdb_connect(async): failed to get pnn");
136 ctdb_request_free(ctdb, req);
139 struct ctdb_connection *ctdb_connect(const char *addr,
140 ctdb_log_fn_t log_fn, void *log_priv)
142 struct ctdb_connection *ctdb;
143 struct sockaddr_un sun;
145 ctdb = malloc(sizeof(*ctdb));
147 /* With no format string, we hope it doesn't use ap! */
149 memset(&ap, 0, sizeof(ap));
151 log_fn(log_priv, LOG_ERR, "ctdb_connect: no memory", ap);
157 ctdb->inqueue = NULL;
158 ctdb->message_handlers = NULL;
160 ctdb->broken = false;
162 ctdb->log_priv = log_priv;
165 memset(&sun, 0, sizeof(sun));
166 sun.sun_family = AF_UNIX;
169 strncpy(sun.sun_path, addr, sizeof(sun.sun_path));
170 ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
174 set_nonblocking(ctdb->fd);
175 set_close_on_exec(ctdb->fd);
177 if (connect(ctdb->fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
180 /* Immediately queue a request to get our pnn. */
181 if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
187 close_noerr(ctdb->fd);
194 void ctdb_disconnect(struct ctdb_connection *ctdb)
196 struct ctdb_request *i;
198 DEBUG(ctdb, LOG_DEBUG, "ctdb_disconnect");
200 while ((i = ctdb->outq) != NULL) {
201 DLIST_REMOVE(ctdb->outq, i);
202 ctdb_request_free(ctdb, i);
205 while ((i = ctdb->doneq) != NULL) {
206 DLIST_REMOVE(ctdb->doneq, i);
207 ctdb_request_free(ctdb, i);
211 free_io_elem(ctdb->in);
213 remove_message_handlers(ctdb);
216 /* Just in case they try to reuse */
221 int ctdb_get_fd(struct ctdb_connection *ctdb)
226 int ctdb_which_events(struct ctdb_connection *ctdb)
235 struct ctdb_request *new_ctdb_request(size_t len,
236 ctdb_callback_t cb, void *cbdata)
238 struct ctdb_request *req = malloc(sizeof(*req));
241 req->io = new_io_elem(len);
246 req->hdr.hdr = io_elem_data(req->io, NULL);
249 req->priv_data = cbdata;
251 req->extra_destructor = NULL;
255 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req)
257 if (req->next || req->prev) {
258 DEBUG(ctdb, LOG_ALERT,
259 "ctdb_request_free: request not complete! ctdb_cancel? %p (id %u)",
260 req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
261 ctdb_cancel(ctdb, req);
264 if (req->extra_destructor) {
265 req->extra_destructor(ctdb, req);
268 free_io_elem(req->reply);
270 free_io_elem(req->io);
274 /* Sanity-checking wrapper for reply. */
275 static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
276 struct ctdb_request *req,
280 struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
282 /* Library user error if this isn't a reply to a call. */
283 if (req->hdr.hdr->operation != CTDB_REQ_CALL) {
285 DEBUG(ctdb, LOG_ALERT,
286 "This was not a ctdbd call request: operation %u",
287 req->hdr.hdr->operation);
291 if (req->hdr.call->callid != callid) {
293 DEBUG(ctdb, LOG_ALERT,
294 "This was not a ctdbd %u call request: %u",
295 callid, req->hdr.call->callid);
299 /* ctdbd or our error if this isn't a reply call. */
300 if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
302 DEBUG(ctdb, LOG_CRIT,
303 "Invalid ctdbd call reply: len %zu, operation %u",
304 len, inhdr->hdr.operation);
311 /* Sanity-checking wrapper for reply. */
312 struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
313 struct ctdb_request *req,
314 enum ctdb_controls control)
317 struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
319 /* Library user error if this isn't a reply to a call. */
320 if (len < sizeof(*inhdr)) {
322 DEBUG(ctdb, LOG_ALERT,
323 "Short ctdbd control reply: %zu bytes", len);
326 if (req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
328 DEBUG(ctdb, LOG_ALERT,
329 "This was not a ctdbd control request: operation %u",
330 req->hdr.hdr->operation);
334 /* ... or if it was a different control from what we expected. */
335 if (req->hdr.control->opcode != control) {
337 DEBUG(ctdb, LOG_ALERT,
338 "This was not an opcode %u ctdbd control request: %u",
339 control, req->hdr.control->opcode);
343 /* ctdbd or our error if this isn't a reply call. */
344 if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
346 DEBUG(ctdb, LOG_CRIT,
347 "Invalid ctdbd control reply: operation %u",
348 inhdr->hdr.operation);
355 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
357 struct ctdb_req_header *hdr;
359 struct ctdb_request *i;
361 hdr = io_elem_data(in, &len);
362 /* FIXME: use len to check packet! */
364 if (hdr->operation == CTDB_REQ_MESSAGE) {
365 deliver_message(ctdb, hdr);
369 for (i = ctdb->doneq; i; i = i->next) {
370 if (i->hdr.hdr->reqid == hdr->reqid) {
371 DLIST_REMOVE(ctdb->doneq, i);
373 i->callback(ctdb, i, i->priv_data);
377 DEBUG(ctdb, LOG_WARNING,
378 "Unexpected ctdbd request reply: operation %u reqid %u",
379 hdr->operation, hdr->reqid);
383 /* Remove "harmless" errors. */
384 static ssize_t real_error(ssize_t ret)
386 if (ret < 0 && (errno == EINTR || errno == EWOULDBLOCK))
391 bool ctdb_service(struct ctdb_connection *ctdb, int revents)
397 if (holding_lock(ctdb)) {
398 DEBUG(ctdb, LOG_ALERT, "Do not block while holding lock!");
401 if (revents & POLLOUT) {
403 if (real_error(write_io_elem(ctdb->fd,
404 ctdb->outq->io)) < 0) {
406 "ctdb_service: error writing to ctdbd");
410 if (io_elem_finished(ctdb->outq->io)) {
411 struct ctdb_request *done = ctdb->outq;
412 DLIST_REMOVE(ctdb->outq, done);
413 /* We add at the head: any dead ones
415 DLIST_ADD(ctdb->doneq, done);
420 while (revents & POLLIN) {
424 if (ioctl(ctdb->fd, FIONREAD, &num_ready) != 0) {
426 "ctdb_service: ioctl(FIONREAD) %d", errno);
430 if (num_ready == 0) {
431 /* the descriptor has been closed or we have all our data */
437 ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
440 "ctdb_service: allocating readbuf");
446 ret = read_io_elem(ctdb->fd, ctdb->in);
447 if (real_error(ret) < 0 || ret == 0) {
448 /* They closed fd? */
452 "ctdb_service: error reading from ctdbd");
455 } else if (ret < 0) {
456 /* No progress, stop loop. */
458 } else if (io_elem_finished(ctdb->in)) {
459 io_elem_queue(ctdb, ctdb->in);
465 while (ctdb->inqueue != NULL) {
466 struct io_elem *io = ctdb->inqueue;
468 io_elem_dequeue(ctdb, io);
469 handle_incoming(ctdb, io);
475 /* This is inefficient. We could pull in idtree.c. */
476 static bool reqid_used(const struct ctdb_connection *ctdb, uint32_t reqid)
478 struct ctdb_request *i;
480 for (i = ctdb->outq; i; i = i->next) {
481 if (i->hdr.hdr->reqid == reqid) {
485 for (i = ctdb->doneq; i; i = i->next) {
486 if (i->hdr.hdr->reqid == reqid) {
493 uint32_t new_reqid(struct ctdb_connection *ctdb)
495 while (reqid_used(ctdb, ctdb->next_id)) {
498 return ctdb->next_id++;
501 struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
504 const void *extra_data,
506 ctdb_callback_t callback,
509 struct ctdb_request *req;
510 struct ctdb_req_control *pkt;
512 req = new_ctdb_request(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
516 io_elem_init_req_header(req->io,
517 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
519 pkt = req->hdr.control;
521 pkt->opcode = opcode;
525 pkt->datalen = extra;
526 memcpy(pkt->data, extra_data, extra);
527 DLIST_ADD(ctdb->outq, req);
531 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
532 struct ctdb_request *req,
535 ctdb_request_free(ctdb, req);
538 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
540 if (!req->next && !req->prev) {
541 DEBUG(ctdb, LOG_ALERT,
542 "ctdb_cancel: request completed! ctdb_request_free? %p (id %u)",
543 req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
544 ctdb_request_free(ctdb, req);
548 DEBUG(ctdb, LOG_DEBUG, "ctdb_cancel: %p (id %u)",
549 req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
551 /* FIXME: If it's not sent, we could just free it right now. */
552 req->callback = ctdb_cancel_callback;
555 void ctdb_detachdb(struct ctdb_connection *ctdb, struct ctdb_db *db)
557 cleanup_locks(ctdb, db);
562 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
563 struct ctdb_request *req,
566 struct ctdb_db *db = _db;
568 /* Do callback on original request. */
569 db->callback(ctdb, req->extra, db->private_data);
572 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
573 struct ctdb_request *req)
575 struct ctdb_request *dbpath_req = req->extra;
576 struct ctdb_reply_control *reply;
577 struct ctdb_db *db = req->priv_data;
578 uint32_t tdb_flags = db->tdb_flags;
579 struct tdb_logging_context log;
581 /* Never sent the dbpath request? We've failed. */
583 /* FIXME: Save errno? */
588 reply = unpack_reply_control(ctdb, dbpath_req, CTDB_CONTROL_GETDBPATH);
592 if (reply->status != 0) {
593 DEBUG(db->ctdb, LOG_ERR,
594 "ctdb_attachdb_recv: reply status %i", reply->status);
598 tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
599 tdb_flags |= TDB_DISALLOW_NESTING;
601 log.log_fn = ctdb_tdb_log_bridge;
602 log.log_private = ctdb;
603 db->tdb = tdb_open_ex((char *)reply->data, 0, tdb_flags, O_RDWR, 0,
605 if (db->tdb == NULL) {
606 DEBUG(db->ctdb, LOG_ERR,
607 "ctdb_attachdb_recv: failed to tdb_open %s",
608 (char *)reply->data);
612 /* Finally, separate the db from the request (see destroy_req_db). */
613 req->priv_data = NULL;
614 DEBUG(db->ctdb, LOG_DEBUG,
615 "ctdb_attachdb_recv: db %p, tdb %s", db, (char *)reply->data);
619 static void attachdb_done(struct ctdb_connection *ctdb,
620 struct ctdb_request *req,
623 struct ctdb_db *db = _db;
624 struct ctdb_request *req2;
625 struct ctdb_reply_control *reply;
626 enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
628 if (db->persistent) {
629 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
632 reply = unpack_reply_control(ctdb, req, control);
633 if (!reply || reply->status != 0) {
636 "ctdb_attachdb_send(async): DB_ATTACH status %i",
639 /* We failed. Hand request to user and have them discover it
640 * via ctdb_attachdb_recv. */
641 db->callback(ctdb, req, db->private_data);
644 db->id = *(uint32_t *)reply->data;
646 /* Now we do another call, to get the dbpath. */
647 req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
649 &db->id, sizeof(db->id),
650 attachdb_getdbpath_done, db);
652 DEBUG(db->ctdb, LOG_ERR,
653 "ctdb_attachdb_send(async): failed to allocate");
654 db->callback(ctdb, req, db->private_data);
659 DEBUG(db->ctdb, LOG_DEBUG,
660 "ctdb_attachdb_send(async): created getdbpath request");
663 static void destroy_req_db(struct ctdb_connection *ctdb,
664 struct ctdb_request *req)
666 /* Incomplete db is in priv_data. */
667 free(req->priv_data);
668 /* second request is chained off this one. */
670 ctdb_request_free(ctdb, req->extra);
674 struct ctdb_request *
675 ctdb_attachdb_send(struct ctdb_connection *ctdb,
676 const char *name, bool persistent, uint32_t tdb_flags,
677 ctdb_callback_t callback, void *private_data)
679 struct ctdb_request *req;
683 /* FIXME: Search if db already open. */
684 db = malloc(sizeof(*db));
690 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
692 opcode = CTDB_CONTROL_DB_ATTACH;
695 req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
696 strlen(name) + 1, attachdb_done, db);
699 "ctdb_attachdb_send: failed allocating DB_ATTACH");
705 db->tdb_flags = tdb_flags;
706 db->persistent = persistent;
707 db->callback = callback;
708 db->private_data = private_data;
710 req->extra_destructor = destroy_req_db;
711 /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
714 /* Flags get overloaded into srvid. */
715 req->hdr.control->srvid = tdb_flags;
716 DEBUG(db->ctdb, LOG_DEBUG,
717 "ctdb_attachdb_send: DB_ATTACH request %p", req);
721 static unsigned long lock_magic(struct ctdb_lock *lock)
723 /* A non-zero magic specific to this structure. */
724 return ((unsigned long)lock->key.dptr
725 ^ (((unsigned long)lock->key.dptr) << 16)
726 ^ 0xBADC0FFEEBADC0DEULL)
730 /* This is only called on locks before they're held. */
731 static void free_lock(struct ctdb_lock *lock)
733 if (lock->held_magic) {
734 DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
735 "free_lock invalid lock %p", lock);
742 void ctdb_release_lock(struct ctdb_db *ctdb_db, struct ctdb_lock *lock)
744 if (lock->held_magic != lock_magic(lock)) {
745 DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
746 "ctdb_release_lock invalid lock %p", lock);
747 } else if (lock->ctdb_db != ctdb_db) {
749 DEBUG(ctdb_db->ctdb, LOG_ALERT,
750 "ctdb_release_lock: wrong ctdb_db.");
752 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
753 DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
754 "ctdb_release_lock %p", lock);
755 remove_lock(lock->ctdb_db->ctdb, lock);
757 lock->held_magic = 0;
762 /* We keep the lock if local node is the dmaster. */
763 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
765 struct ctdb_ltdb_header *hdr;
767 if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
768 DEBUG(lock->ctdb_db->ctdb, LOG_WARNING,
769 "ctdb_readrecordlock_async: failed to chainlock");
773 hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
774 if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
775 DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
776 "ctdb_readrecordlock_async: got local lock");
777 lock->held_magic = lock_magic(lock);
779 add_lock(lock->ctdb_db->ctdb, lock);
783 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
788 /* If they shutdown before we hand them the lock, we free it here. */
789 static void destroy_lock(struct ctdb_connection *ctdb,
790 struct ctdb_request *req)
792 free_lock(req->extra);
795 static void readrecordlock_retry(struct ctdb_connection *ctdb,
796 struct ctdb_request *req, void *private)
798 struct ctdb_lock *lock = req->extra;
799 struct ctdb_reply_call *reply;
802 /* OK, we've received reply to noop migration */
803 reply = unpack_reply_call(ctdb, req, CTDB_NULL_FUNC);
804 if (!reply || reply->status != 0) {
807 "ctdb_readrecordlock_async(async):"
808 " NULL_FUNC returned %i", reply->status);
810 lock->callback(lock->ctdb_db, NULL, tdb_null, private);
811 ctdb_request_free(ctdb, req); /* Also frees lock. */
815 /* Can we get lock now? */
816 if (try_readrecordlock(lock, &data)) {
817 /* Now it's their responsibility to free lock & request! */
818 req->extra_destructor = NULL;
819 lock->callback(lock->ctdb_db, lock, data, private);
820 ctdb_request_free(ctdb, req);
824 /* Retransmit the same request again (we lost race). */
825 io_elem_reset(req->io);
826 DLIST_ADD(ctdb->outq, req);
830 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
831 ctdb_rrl_callback_t callback, void *cbdata)
833 struct ctdb_request *req;
834 struct ctdb_lock *lock;
837 if (holding_lock(ctdb_db->ctdb)) {
838 DEBUG(ctdb_db->ctdb, LOG_ALERT,
839 "ctdb_readrecordlock_async: already holding lock");
844 lock = malloc(sizeof(*lock) + key.dsize);
846 DEBUG(ctdb_db->ctdb, LOG_ERR,
847 "ctdb_readrecordlock_async: lock allocation failed");
850 lock->key.dptr = (void *)(lock + 1);
851 memcpy(lock->key.dptr, key.dptr, key.dsize);
852 lock->key.dsize = key.dsize;
853 lock->ctdb_db = ctdb_db;
855 lock->held_magic = 0;
858 if (try_readrecordlock(lock, &data)) {
859 callback(ctdb_db, lock, data, cbdata);
863 /* Slow path: create request. */
864 req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
865 + key.dsize, readrecordlock_retry, cbdata);
867 DEBUG(ctdb_db->ctdb, LOG_ERR,
868 "ctdb_readrecordlock_async: allocation failed");
873 req->extra_destructor = destroy_lock;
874 /* We store the original callback in the lock, and use our own. */
875 lock->callback = callback;
877 io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
878 new_reqid(ctdb_db->ctdb));
880 req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
881 req->hdr.call->db_id = ctdb_db->id;
882 req->hdr.call->callid = CTDB_NULL_FUNC;
883 req->hdr.call->hopcount = 0;
884 req->hdr.call->keylen = key.dsize;
885 req->hdr.call->calldatalen = 0;
886 memcpy(req->hdr.call->data, key.dptr, key.dsize);
887 DLIST_ADD(ctdb_db->ctdb->outq, req);
891 bool ctdb_writerecord(struct ctdb_db *ctdb_db,
892 struct ctdb_lock *lock, TDB_DATA data)
894 if (lock->ctdb_db != ctdb_db) {
896 DEBUG(ctdb_db->ctdb, LOG_ALERT,
897 "ctdb_writerecord: Can not write, wrong ctdb_db.");
901 if (lock->held_magic != lock_magic(lock)) {
903 DEBUG(ctdb_db->ctdb, LOG_ALERT,
904 "ctdb_writerecord: Can not write. Lock has been released.");
908 if (ctdb_db->persistent) {
910 DEBUG(ctdb_db->ctdb, LOG_ALERT,
911 "ctdb_writerecord: cannot write to persistent db");
915 switch (ctdb_local_store(ctdb_db->tdb, lock->key, lock->hdr, data)) {
917 DEBUG(ctdb_db->ctdb, LOG_DEBUG,
918 "ctdb_writerecord: optimized away noop write.");
926 DEBUG(ctdb_db->ctdb, LOG_CRIT,
927 "ctdb_writerecord: out of memory.");
930 DEBUG(ctdb_db->ctdb, LOG_ALERT,
931 "ctdb_writerecord: record changed under lock?");
933 default: /* TDB already logged. */
941 struct ctdb_traverse_state {
942 struct ctdb_request *handle;
943 struct ctdb_db *ctdb_db;
946 ctdb_traverse_callback_t callback;
950 static void traverse_remhnd_cb(struct ctdb_connection *ctdb,
951 struct ctdb_request *req, void *private_data)
953 struct ctdb_traverse_state *state = private_data;
955 if (!ctdb_remove_message_handler_recv(ctdb, state->handle)) {
957 "Failed to remove message handler for"
959 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
960 TRAVERSE_STATUS_ERROR,
964 ctdb_request_free(ctdb, state->handle);
965 state->handle = NULL;
969 static void msg_h(struct ctdb_connection *ctdb, uint64_t srvid,
970 TDB_DATA data, void *private_data)
972 struct ctdb_traverse_state *state = private_data;
973 struct ctdb_db *ctdb_db = state->ctdb_db;
974 struct ctdb_rec_data *d = (struct ctdb_rec_data *)data.dptr;
977 if (data.dsize < sizeof(uint32_t) ||
978 d->length != data.dsize) {
980 "Bad data size %u in traverse_handler",
981 (unsigned)data.dsize);
982 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
983 TRAVERSE_STATUS_ERROR,
986 state->handle = ctdb_remove_message_handler_send(
987 state->ctdb_db->ctdb, state->srvid,
989 traverse_remhnd_cb, state);
993 key.dsize = d->keylen;
994 key.dptr = &d->data[0];
995 data.dsize = d->datalen;
996 data.dptr = &d->data[d->keylen];
998 if (key.dsize == 0 && data.dsize == 0) {
999 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
1000 TRAVERSE_STATUS_FINISHED,
1003 state->handle = ctdb_remove_message_handler_send(
1004 state->ctdb_db->ctdb, state->srvid,
1006 traverse_remhnd_cb, state);
1010 if (data.dsize <= sizeof(struct ctdb_ltdb_header)) {
1011 /* empty records are deleted records in ctdb */
1015 data.dsize -= sizeof(struct ctdb_ltdb_header);
1016 data.dptr += sizeof(struct ctdb_ltdb_header);
1018 if (state->callback(ctdb, ctdb_db,
1019 TRAVERSE_STATUS_RECORD,
1020 key, data, state->cbdata) != 0) {
1021 state->handle = ctdb_remove_message_handler_send(
1022 state->ctdb_db->ctdb, state->srvid,
1024 traverse_remhnd_cb, state);
1029 static void traverse_start_cb(struct ctdb_connection *ctdb,
1030 struct ctdb_request *req, void *private_data)
1032 struct ctdb_traverse_state *state = private_data;
1034 ctdb_request_free(ctdb, state->handle);
1035 state->handle = NULL;
1038 static void traverse_msghnd_cb(struct ctdb_connection *ctdb,
1039 struct ctdb_request *req, void *private_data)
1041 struct ctdb_traverse_state *state = private_data;
1042 struct ctdb_db *ctdb_db = state->ctdb_db;
1043 struct ctdb_traverse_start t;
1045 if (!ctdb_set_message_handler_recv(ctdb, state->handle)) {
1046 DEBUG(ctdb, LOG_ERR,
1047 "Failed to register message handler for"
1049 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
1050 TRAVERSE_STATUS_ERROR,
1053 ctdb_request_free(ctdb, state->handle);
1054 state->handle = NULL;
1058 ctdb_request_free(ctdb, state->handle);
1059 state->handle = NULL;
1061 t.db_id = ctdb_db->id;
1062 t.srvid = state->srvid;
1065 state->handle = new_ctdb_control_request(ctdb,
1066 CTDB_CONTROL_TRAVERSE_START,
1069 traverse_start_cb, state);
1070 if (state->handle == NULL) {
1071 DEBUG(ctdb, LOG_ERR,
1072 "ctdb_traverse_async:"
1073 " failed to send traverse_start control");
1074 state->callback(state->ctdb_db->ctdb, state->ctdb_db,
1075 TRAVERSE_STATUS_ERROR,
1078 state->handle = ctdb_remove_message_handler_send(
1079 state->ctdb_db->ctdb, state->srvid,
1081 traverse_remhnd_cb, state);
1086 bool ctdb_traverse_async(struct ctdb_db *ctdb_db,
1087 ctdb_traverse_callback_t callback, void *cbdata)
1089 struct ctdb_connection *ctdb = ctdb_db->ctdb;
1090 struct ctdb_traverse_state *state;
1091 static uint32_t tid = 0;
1093 state = malloc(sizeof(struct ctdb_traverse_state));
1094 if (state == NULL) {
1095 DEBUG(ctdb, LOG_ERR,
1096 "ctdb_traverse_async: no memory."
1097 " allocate state failed");
1102 state->srvid = CTDB_SRVID_TRAVERSE_RANGE|tid;
1104 state->callback = callback;
1105 state->cbdata = cbdata;
1106 state->ctdb_db = ctdb_db;
1108 state->handle = ctdb_set_message_handler_send(ctdb_db->ctdb,
1111 traverse_msghnd_cb, state);
1112 if (state->handle == NULL) {
1113 DEBUG(ctdb, LOG_ERR,
1114 "ctdb_traverse_async:"
1115 " failed ctdb_set_message_handler_send");