4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Ronnie Sahlberg 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "lib/tdb/include/tdb.h"
24 #include "lib/util/dlinklist.h"
25 #include "lib/events/events.h"
26 #include "system/network.h"
27 #include "system/filesys.h"
28 #include "system/locale.h"
30 #include "../include/ctdb_private.h"
31 #include "lib/util/dlinklist.h"
36 allocate a packet for use in client<->daemon communication
38 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
40 enum ctdb_operation operation,
41 size_t length, size_t slength,
45 struct ctdb_req_header *hdr;
47 length = MAX(length, slength);
48 size = (length+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
50 hdr = (struct ctdb_req_header *)talloc_size(mem_ctx, size);
52 DEBUG(DEBUG_ERR,("Unable to allocate packet for operation %u of length %u\n",
53 operation, (unsigned)length));
56 talloc_set_name_const(hdr, type);
57 memset(hdr, 0, slength);
59 hdr->operation = operation;
60 hdr->ctdb_magic = CTDB_MAGIC;
61 hdr->ctdb_version = CTDB_VERSION;
62 hdr->srcnode = ctdb->pnn;
64 hdr->generation = ctdb->vnn_map->generation;
71 local version of ctdb_call
73 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
74 struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx,
75 TDB_DATA *data, uint32_t caller)
77 struct ctdb_call_info *c;
78 struct ctdb_registered_call *fn;
79 struct ctdb_context *ctdb = ctdb_db->ctdb;
81 c = talloc(ctdb, struct ctdb_call_info);
82 CTDB_NO_MEMORY(ctdb, c);
85 c->call_data = &call->call_data;
86 c->record_data.dptr = talloc_memdup(c, data->dptr, data->dsize);
87 c->record_data.dsize = data->dsize;
88 CTDB_NO_MEMORY(ctdb, c->record_data.dptr);
93 for (fn=ctdb_db->calls;fn;fn=fn->next) {
94 if (fn->id == call->call_id) break;
97 ctdb_set_error(ctdb, "Unknown call id %u\n", call->call_id);
102 if (fn->fn(c) != 0) {
103 ctdb_set_error(ctdb, "ctdb_call %u failed\n", call->call_id);
108 if (header->laccessor != caller) {
111 header->laccessor = caller;
114 /* we need to force the record to be written out if this was a remote access,
115 so that the lacount is updated */
116 if (c->new_data == NULL && header->laccessor != ctdb->pnn) {
117 c->new_data = &c->record_data;
121 /* XXX check that we always have the lock here? */
122 if (ctdb_ltdb_store(ctdb_db, call->key, header, *c->new_data) != 0) {
123 ctdb_set_error(ctdb, "ctdb_call tdb_store failed\n");
130 call->reply_data = *c->reply_data;
132 talloc_steal(call, call->reply_data.dptr);
133 talloc_set_name_const(call->reply_data.dptr, __location__);
135 call->reply_data.dptr = NULL;
136 call->reply_data.dsize = 0;
138 call->status = c->status;
147 queue a packet for sending from client to daemon
149 static int ctdb_client_queue_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
151 return ctdb_queue_send(ctdb->daemon.queue, (uint8_t *)hdr, hdr->length);
156 called when a CTDB_REPLY_CALL packet comes in in the client
158 This packet comes in response to a CTDB_REQ_CALL request packet. It
159 contains any reply data from the call
161 static void ctdb_client_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
163 struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
164 struct ctdb_client_call_state *state;
166 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_client_call_state);
168 DEBUG(DEBUG_ERR,(__location__ " reqid %u not found\n", hdr->reqid));
172 if (hdr->reqid != state->reqid) {
173 /* we found a record but it was the wrong one */
174 DEBUG(DEBUG_ERR, ("Dropped client call reply with reqid:%u\n",hdr->reqid));
178 state->call->reply_data.dptr = c->data;
179 state->call->reply_data.dsize = c->datalen;
180 state->call->status = c->status;
182 talloc_steal(state, c);
184 state->state = CTDB_CALL_DONE;
186 if (state->async.fn) {
187 state->async.fn(state);
191 static void ctdb_client_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
194 this is called in the client, when data comes in from the daemon
196 static void ctdb_client_read_cb(uint8_t *data, size_t cnt, void *args)
198 struct ctdb_context *ctdb = talloc_get_type(args, struct ctdb_context);
199 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
202 /* place the packet as a child of a tmp_ctx. We then use
203 talloc_free() below to free it. If any of the calls want
204 to keep it, then they will steal it somewhere else, and the
205 talloc_free() will be a no-op */
206 tmp_ctx = talloc_new(ctdb);
207 talloc_steal(tmp_ctx, hdr);
210 DEBUG(DEBUG_INFO,("Daemon has exited - shutting down client\n"));
214 if (cnt < sizeof(*hdr)) {
215 DEBUG(DEBUG_CRIT,("Bad packet length %u in client\n", (unsigned)cnt));
218 if (cnt != hdr->length) {
219 ctdb_set_error(ctdb, "Bad header length %u expected %u in client\n",
220 (unsigned)hdr->length, (unsigned)cnt);
224 if (hdr->ctdb_magic != CTDB_MAGIC) {
225 ctdb_set_error(ctdb, "Non CTDB packet rejected in client\n");
229 if (hdr->ctdb_version != CTDB_VERSION) {
230 ctdb_set_error(ctdb, "Bad CTDB version 0x%x rejected in client\n", hdr->ctdb_version);
234 switch (hdr->operation) {
235 case CTDB_REPLY_CALL:
236 ctdb_client_reply_call(ctdb, hdr);
239 case CTDB_REQ_MESSAGE:
240 ctdb_request_message(ctdb, hdr);
243 case CTDB_REPLY_CONTROL:
244 ctdb_client_reply_control(ctdb, hdr);
248 DEBUG(DEBUG_CRIT,("bogus operation code:%u\n",hdr->operation));
252 talloc_free(tmp_ctx);
256 connect to a unix domain socket
258 int ctdb_socket_connect(struct ctdb_context *ctdb)
260 struct sockaddr_un addr;
262 memset(&addr, 0, sizeof(addr));
263 addr.sun_family = AF_UNIX;
264 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
266 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
267 if (ctdb->daemon.sd == -1) {
268 DEBUG(DEBUG_ERR,(__location__ " Failed to open client socket. Errno:%s(%d)\n", strerror(errno), errno));
272 set_nonblocking(ctdb->daemon.sd);
273 set_close_on_exec(ctdb->daemon.sd);
275 if (connect(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
276 close(ctdb->daemon.sd);
277 ctdb->daemon.sd = -1;
278 DEBUG(DEBUG_ERR,(__location__ " Failed to connect client socket to daemon. Errno:%s(%d)\n", strerror(errno), errno));
282 ctdb->daemon.queue = ctdb_queue_setup(ctdb, ctdb, ctdb->daemon.sd,
284 ctdb_client_read_cb, ctdb);
289 struct ctdb_record_handle {
290 struct ctdb_db_context *ctdb_db;
293 struct ctdb_ltdb_header header;
298 make a recv call to the local ctdb daemon - called from client context
300 This is called when the program wants to wait for a ctdb_call to complete and get the
301 results. This call will block unless the call has already completed.
303 int ctdb_call_recv(struct ctdb_client_call_state *state, struct ctdb_call *call)
309 while (state->state < CTDB_CALL_DONE) {
310 event_loop_once(state->ctdb_db->ctdb->ev);
312 if (state->state != CTDB_CALL_DONE) {
313 DEBUG(DEBUG_ERR,(__location__ " ctdb_call_recv failed\n"));
318 if (state->call->reply_data.dsize) {
319 call->reply_data.dptr = talloc_memdup(state->ctdb_db,
320 state->call->reply_data.dptr,
321 state->call->reply_data.dsize);
322 call->reply_data.dsize = state->call->reply_data.dsize;
324 call->reply_data.dptr = NULL;
325 call->reply_data.dsize = 0;
327 call->status = state->call->status;
337 destroy a ctdb_call in client
339 static int ctdb_client_call_destructor(struct ctdb_client_call_state *state)
341 ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
346 construct an event driven local ctdb_call
348 this is used so that locally processed ctdb_call requests are processed
349 in an event driven manner
351 static struct ctdb_client_call_state *ctdb_client_call_local_send(struct ctdb_db_context *ctdb_db,
352 struct ctdb_call *call,
353 struct ctdb_ltdb_header *header,
356 struct ctdb_client_call_state *state;
357 struct ctdb_context *ctdb = ctdb_db->ctdb;
360 state = talloc_zero(ctdb_db, struct ctdb_client_call_state);
361 CTDB_NO_MEMORY_NULL(ctdb, state);
362 state->call = talloc_zero(state, struct ctdb_call);
363 CTDB_NO_MEMORY_NULL(ctdb, state->call);
365 talloc_steal(state, data->dptr);
367 state->state = CTDB_CALL_DONE;
368 *(state->call) = *call;
369 state->ctdb_db = ctdb_db;
371 ret = ctdb_call_local(ctdb_db, state->call, header, state, data, ctdb->pnn);
377 make a ctdb call to the local daemon - async send. Called from client context.
379 This constructs a ctdb_call request and queues it for processing.
380 This call never blocks.
382 struct ctdb_client_call_state *ctdb_call_send(struct ctdb_db_context *ctdb_db,
383 struct ctdb_call *call)
385 struct ctdb_client_call_state *state;
386 struct ctdb_context *ctdb = ctdb_db->ctdb;
387 struct ctdb_ltdb_header header;
391 struct ctdb_req_call *c;
393 /* if the domain socket is not yet open, open it */
394 if (ctdb->daemon.sd==-1) {
395 ctdb_socket_connect(ctdb);
398 ret = ctdb_ltdb_lock(ctdb_db, call->key);
400 DEBUG(DEBUG_ERR,(__location__ " Failed to get chainlock\n"));
404 ret = ctdb_ltdb_fetch(ctdb_db, call->key, &header, ctdb_db, &data);
406 if (ret == 0 && header.dmaster == ctdb->pnn) {
407 state = ctdb_client_call_local_send(ctdb_db, call, &header, &data);
408 talloc_free(data.dptr);
409 ctdb_ltdb_unlock(ctdb_db, call->key);
413 ctdb_ltdb_unlock(ctdb_db, call->key);
414 talloc_free(data.dptr);
416 state = talloc_zero(ctdb_db, struct ctdb_client_call_state);
418 DEBUG(DEBUG_ERR, (__location__ " failed to allocate state\n"));
421 state->call = talloc_zero(state, struct ctdb_call);
422 if (state->call == NULL) {
423 DEBUG(DEBUG_ERR, (__location__ " failed to allocate state->call\n"));
427 len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
428 c = ctdbd_allocate_pkt(ctdb, state, CTDB_REQ_CALL, len, struct ctdb_req_call);
430 DEBUG(DEBUG_ERR, (__location__ " failed to allocate packet\n"));
434 state->reqid = ctdb_reqid_new(ctdb, state);
435 state->ctdb_db = ctdb_db;
436 talloc_set_destructor(state, ctdb_client_call_destructor);
438 c->hdr.reqid = state->reqid;
439 c->flags = call->flags;
440 c->db_id = ctdb_db->db_id;
441 c->callid = call->call_id;
443 c->keylen = call->key.dsize;
444 c->calldatalen = call->call_data.dsize;
445 memcpy(&c->data[0], call->key.dptr, call->key.dsize);
446 memcpy(&c->data[call->key.dsize],
447 call->call_data.dptr, call->call_data.dsize);
448 *(state->call) = *call;
449 state->call->call_data.dptr = &c->data[call->key.dsize];
450 state->call->key.dptr = &c->data[0];
452 state->state = CTDB_CALL_WAIT;
455 ctdb_client_queue_pkt(ctdb, &c->hdr);
462 full ctdb_call. Equivalent to a ctdb_call_send() followed by a ctdb_call_recv()
464 int ctdb_call(struct ctdb_db_context *ctdb_db, struct ctdb_call *call)
466 struct ctdb_client_call_state *state;
468 state = ctdb_call_send(ctdb_db, call);
469 return ctdb_call_recv(state, call);
474 tell the daemon what messaging srvid we will use, and register the message
475 handler function in the client
477 int ctdb_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
478 ctdb_message_fn_t handler,
485 res = ctdb_control(ctdb, CTDB_CURRENT_NODE, srvid, CTDB_CONTROL_REGISTER_SRVID, 0,
486 tdb_null, NULL, NULL, &status, NULL, NULL);
487 if (res != 0 || status != 0) {
488 DEBUG(DEBUG_ERR,("Failed to register srvid %llu\n", (unsigned long long)srvid));
492 /* also need to register the handler with our own ctdb structure */
493 return ctdb_register_message_handler(ctdb, ctdb, srvid, handler, private_data);
497 tell the daemon we no longer want a srvid
499 int ctdb_remove_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data)
504 res = ctdb_control(ctdb, CTDB_CURRENT_NODE, srvid, CTDB_CONTROL_DEREGISTER_SRVID, 0,
505 tdb_null, NULL, NULL, &status, NULL, NULL);
506 if (res != 0 || status != 0) {
507 DEBUG(DEBUG_ERR,("Failed to deregister srvid %llu\n", (unsigned long long)srvid));
511 /* also need to register the handler with our own ctdb structure */
512 ctdb_deregister_message_handler(ctdb, srvid, private_data);
518 send a message - from client context
520 int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn,
521 uint64_t srvid, TDB_DATA data)
523 struct ctdb_req_message *r;
526 len = offsetof(struct ctdb_req_message, data) + data.dsize;
527 r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE,
528 len, struct ctdb_req_message);
529 CTDB_NO_MEMORY(ctdb, r);
531 r->hdr.destnode = pnn;
533 r->datalen = data.dsize;
534 memcpy(&r->data[0], data.dptr, data.dsize);
536 res = ctdb_client_queue_pkt(ctdb, &r->hdr);
547 cancel a ctdb_fetch_lock operation, releasing the lock
549 static int fetch_lock_destructor(struct ctdb_record_handle *h)
551 ctdb_ltdb_unlock(h->ctdb_db, h->key);
556 force the migration of a record to this node
558 static int ctdb_client_force_migration(struct ctdb_db_context *ctdb_db, TDB_DATA key)
560 struct ctdb_call call;
562 call.call_id = CTDB_NULL_FUNC;
564 call.flags = CTDB_IMMEDIATE_MIGRATION;
565 return ctdb_call(ctdb_db, &call);
569 get a lock on a record, and return the records data. Blocks until it gets the lock
571 struct ctdb_record_handle *ctdb_fetch_lock(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx,
572 TDB_DATA key, TDB_DATA *data)
575 struct ctdb_record_handle *h;
578 procedure is as follows:
580 1) get the chain lock.
581 2) check if we are dmaster
582 3) if we are the dmaster then return handle
583 4) if not dmaster then ask ctdb daemon to make us dmaster, and wait for
585 5) when we get the reply, goto (1)
588 h = talloc_zero(mem_ctx, struct ctdb_record_handle);
593 h->ctdb_db = ctdb_db;
595 h->key.dptr = talloc_memdup(h, key.dptr, key.dsize);
596 if (h->key.dptr == NULL) {
602 DEBUG(DEBUG_DEBUG,("ctdb_fetch_lock: key=%*.*s\n", (int)key.dsize, (int)key.dsize,
603 (const char *)key.dptr));
606 /* step 1 - get the chain lock */
607 ret = ctdb_ltdb_lock(ctdb_db, key);
609 DEBUG(DEBUG_ERR, (__location__ " failed to lock ltdb record\n"));
614 DEBUG(DEBUG_DEBUG,("ctdb_fetch_lock: got chain lock\n"));
616 talloc_set_destructor(h, fetch_lock_destructor);
618 ret = ctdb_ltdb_fetch(ctdb_db, key, &h->header, h, data);
620 /* when torturing, ensure we test the remote path */
621 if ((ctdb_db->ctdb->flags & CTDB_FLAG_TORTURE) &&
623 h->header.dmaster = (uint32_t)-1;
627 DEBUG(DEBUG_DEBUG,("ctdb_fetch_lock: done local fetch\n"));
629 if (ret != 0 || h->header.dmaster != ctdb_db->ctdb->pnn) {
630 ctdb_ltdb_unlock(ctdb_db, key);
631 ret = ctdb_client_force_migration(ctdb_db, key);
633 DEBUG(DEBUG_DEBUG,("ctdb_fetch_lock: force_migration failed\n"));
640 DEBUG(DEBUG_DEBUG,("ctdb_fetch_lock: we are dmaster - done\n"));
645 store some data to the record that was locked with ctdb_fetch_lock()
647 int ctdb_record_store(struct ctdb_record_handle *h, TDB_DATA data)
649 if (h->ctdb_db->persistent) {
650 DEBUG(DEBUG_ERR, (__location__ " ctdb_record_store prohibited for persistent dbs\n"));
654 return ctdb_ltdb_store(h->ctdb_db, h->key, &h->header, data);
658 non-locking fetch of a record
660 int ctdb_fetch(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx,
661 TDB_DATA key, TDB_DATA *data)
663 struct ctdb_call call;
666 call.call_id = CTDB_FETCH_FUNC;
667 call.call_data.dptr = NULL;
668 call.call_data.dsize = 0;
670 ret = ctdb_call(ctdb_db, &call);
673 *data = call.reply_data;
674 talloc_steal(mem_ctx, data->dptr);
683 called when a control completes or timesout to invoke the callback
684 function the user provided
686 static void invoke_control_callback(struct event_context *ev, struct timed_event *te,
687 struct timeval t, void *private_data)
689 struct ctdb_client_control_state *state;
690 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
693 state = talloc_get_type(private_data, struct ctdb_client_control_state);
694 talloc_steal(tmp_ctx, state);
696 ret = ctdb_control_recv(state->ctdb, state, state,
701 talloc_free(tmp_ctx);
705 called when a CTDB_REPLY_CONTROL packet comes in in the client
707 This packet comes in response to a CTDB_REQ_CONTROL request packet. It
708 contains any reply data from the control
710 static void ctdb_client_reply_control(struct ctdb_context *ctdb,
711 struct ctdb_req_header *hdr)
713 struct ctdb_reply_control *c = (struct ctdb_reply_control *)hdr;
714 struct ctdb_client_control_state *state;
716 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_client_control_state);
718 DEBUG(DEBUG_ERR,(__location__ " reqid %u not found\n", hdr->reqid));
722 if (hdr->reqid != state->reqid) {
723 /* we found a record but it was the wrong one */
724 DEBUG(DEBUG_ERR, ("Dropped orphaned reply control with reqid:%u\n",hdr->reqid));
728 state->outdata.dptr = c->data;
729 state->outdata.dsize = c->datalen;
730 state->status = c->status;
732 state->errormsg = talloc_strndup(state,
733 (char *)&c->data[c->datalen],
737 /* state->outdata now uses resources from c so we dont want c
738 to just dissappear from under us while state is still alive
740 talloc_steal(state, c);
742 state->state = CTDB_CONTROL_DONE;
744 /* if we had a callback registered for this control, pull the response
745 and call the callback.
747 if (state->async.fn) {
748 event_add_timed(ctdb->ev, state, timeval_zero(), invoke_control_callback, state);
754 destroy a ctdb_control in client
756 static int ctdb_control_destructor(struct ctdb_client_control_state *state)
758 ctdb_reqid_remove(state->ctdb, state->reqid);
763 /* time out handler for ctdb_control */
764 static void control_timeout_func(struct event_context *ev, struct timed_event *te,
765 struct timeval t, void *private_data)
767 struct ctdb_client_control_state *state = talloc_get_type(private_data, struct ctdb_client_control_state);
769 DEBUG(DEBUG_ERR,(__location__ " control timed out. reqid:%u opcode:%u "
770 "dstnode:%u\n", state->reqid, state->c->opcode,
771 state->c->hdr.destnode));
773 state->state = CTDB_CONTROL_TIMEOUT;
775 /* if we had a callback registered for this control, pull the response
776 and call the callback.
778 if (state->async.fn) {
779 event_add_timed(state->ctdb->ev, state, timeval_zero(), invoke_control_callback, state);
783 /* async version of send control request */
784 struct ctdb_client_control_state *ctdb_control_send(struct ctdb_context *ctdb,
785 uint32_t destnode, uint64_t srvid,
786 uint32_t opcode, uint32_t flags, TDB_DATA data,
788 struct timeval *timeout,
791 struct ctdb_client_control_state *state;
793 struct ctdb_req_control *c;
800 /* if the domain socket is not yet open, open it */
801 if (ctdb->daemon.sd==-1) {
802 ctdb_socket_connect(ctdb);
805 state = talloc_zero(mem_ctx, struct ctdb_client_control_state);
806 CTDB_NO_MEMORY_NULL(ctdb, state);
809 state->reqid = ctdb_reqid_new(ctdb, state);
810 state->state = CTDB_CONTROL_WAIT;
811 state->errormsg = NULL;
813 talloc_set_destructor(state, ctdb_control_destructor);
815 len = offsetof(struct ctdb_req_control, data) + data.dsize;
816 c = ctdbd_allocate_pkt(ctdb, state, CTDB_REQ_CONTROL,
817 len, struct ctdb_req_control);
819 CTDB_NO_MEMORY_NULL(ctdb, c);
820 c->hdr.reqid = state->reqid;
821 c->hdr.destnode = destnode;
826 c->datalen = data.dsize;
828 memcpy(&c->data[0], data.dptr, data.dsize);
832 if (timeout && !timeval_is_zero(timeout)) {
833 event_add_timed(ctdb->ev, state, *timeout, control_timeout_func, state);
836 ret = ctdb_client_queue_pkt(ctdb, &(c->hdr));
842 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
851 /* async version of receive control reply */
852 int ctdb_control_recv(struct ctdb_context *ctdb,
853 struct ctdb_client_control_state *state,
855 TDB_DATA *outdata, int32_t *status, char **errormsg)
859 if (status != NULL) {
862 if (errormsg != NULL) {
870 /* prevent double free of state */
871 tmp_ctx = talloc_new(ctdb);
872 talloc_steal(tmp_ctx, state);
874 /* loop one event at a time until we either timeout or the control
877 while (state->state == CTDB_CONTROL_WAIT) {
878 event_loop_once(ctdb->ev);
881 if (state->state != CTDB_CONTROL_DONE) {
882 DEBUG(DEBUG_ERR,(__location__ " ctdb_control_recv failed\n"));
883 if (state->async.fn) {
884 state->async.fn(state);
886 talloc_free(tmp_ctx);
890 if (state->errormsg) {
891 DEBUG(DEBUG_ERR,("ctdb_control error: '%s'\n", state->errormsg));
893 (*errormsg) = talloc_move(mem_ctx, &state->errormsg);
895 if (state->async.fn) {
896 state->async.fn(state);
898 talloc_free(tmp_ctx);
903 *outdata = state->outdata;
904 outdata->dptr = talloc_memdup(mem_ctx, outdata->dptr, outdata->dsize);
908 *status = state->status;
911 if (state->async.fn) {
912 state->async.fn(state);
915 talloc_free(tmp_ctx);
922 send a ctdb control message
923 timeout specifies how long we should wait for a reply.
924 if timeout is NULL we wait indefinitely
926 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid,
927 uint32_t opcode, uint32_t flags, TDB_DATA data,
928 TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
929 struct timeval *timeout,
932 struct ctdb_client_control_state *state;
934 state = ctdb_control_send(ctdb, destnode, srvid, opcode,
935 flags, data, mem_ctx,
937 return ctdb_control_recv(ctdb, state, mem_ctx, outdata, status,
945 a process exists call. Returns 0 if process exists, -1 otherwise
947 int ctdb_ctrl_process_exists(struct ctdb_context *ctdb, uint32_t destnode, pid_t pid)
953 data.dptr = (uint8_t*)&pid;
954 data.dsize = sizeof(pid);
956 ret = ctdb_control(ctdb, destnode, 0,
957 CTDB_CONTROL_PROCESS_EXISTS, 0, data,
958 NULL, NULL, &status, NULL, NULL);
960 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for process_exists failed\n"));
968 get remote statistics
970 int ctdb_ctrl_statistics(struct ctdb_context *ctdb, uint32_t destnode, struct ctdb_statistics *status)
976 ret = ctdb_control(ctdb, destnode, 0,
977 CTDB_CONTROL_STATISTICS, 0, tdb_null,
978 ctdb, &data, &res, NULL, NULL);
979 if (ret != 0 || res != 0) {
980 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for statistics failed\n"));
984 if (data.dsize != sizeof(struct ctdb_statistics)) {
985 DEBUG(DEBUG_ERR,(__location__ " Wrong statistics size %u - expected %u\n",
986 (unsigned)data.dsize, (unsigned)sizeof(struct ctdb_statistics)));
990 *status = *(struct ctdb_statistics *)data.dptr;
991 talloc_free(data.dptr);
997 shutdown a remote ctdb node
999 int ctdb_ctrl_shutdown(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
1001 struct ctdb_client_control_state *state;
1003 state = ctdb_control_send(ctdb, destnode, 0,
1004 CTDB_CONTROL_SHUTDOWN, 0, tdb_null,
1005 NULL, &timeout, NULL);
1006 if (state == NULL) {
1007 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for shutdown failed\n"));
1015 get vnn map from a remote node
1017 int ctdb_ctrl_getvnnmap(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, TALLOC_CTX *mem_ctx, struct ctdb_vnn_map **vnnmap)
1022 struct ctdb_vnn_map_wire *map;
1024 ret = ctdb_control(ctdb, destnode, 0,
1025 CTDB_CONTROL_GETVNNMAP, 0, tdb_null,
1026 mem_ctx, &outdata, &res, &timeout, NULL);
1027 if (ret != 0 || res != 0) {
1028 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getvnnmap failed\n"));
1032 map = (struct ctdb_vnn_map_wire *)outdata.dptr;
1033 if (outdata.dsize < offsetof(struct ctdb_vnn_map_wire, map) ||
1034 outdata.dsize != map->size*sizeof(uint32_t) + offsetof(struct ctdb_vnn_map_wire, map)) {
1035 DEBUG(DEBUG_ERR,("Bad vnn map size received in ctdb_ctrl_getvnnmap\n"));
1039 (*vnnmap) = talloc(mem_ctx, struct ctdb_vnn_map);
1040 CTDB_NO_MEMORY(ctdb, *vnnmap);
1041 (*vnnmap)->generation = map->generation;
1042 (*vnnmap)->size = map->size;
1043 (*vnnmap)->map = talloc_array(*vnnmap, uint32_t, map->size);
1045 CTDB_NO_MEMORY(ctdb, (*vnnmap)->map);
1046 memcpy((*vnnmap)->map, map->map, sizeof(uint32_t)*map->size);
1047 talloc_free(outdata.dptr);
1054 get the recovery mode of a remote node
1056 struct ctdb_client_control_state *
1057 ctdb_ctrl_getrecmode_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode)
1059 return ctdb_control_send(ctdb, destnode, 0,
1060 CTDB_CONTROL_GET_RECMODE, 0, tdb_null,
1061 mem_ctx, &timeout, NULL);
1064 int ctdb_ctrl_getrecmode_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmode)
1069 ret = ctdb_control_recv(ctdb, state, mem_ctx, NULL, &res, NULL);
1071 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_getrecmode_recv failed\n"));
1076 *recmode = (uint32_t)res;
1082 int ctdb_ctrl_getrecmode(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmode)
1084 struct ctdb_client_control_state *state;
1086 state = ctdb_ctrl_getrecmode_send(ctdb, mem_ctx, timeout, destnode);
1087 return ctdb_ctrl_getrecmode_recv(ctdb, mem_ctx, state, recmode);
1094 set the recovery mode of a remote node
1096 int ctdb_ctrl_setrecmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmode)
1102 data.dsize = sizeof(uint32_t);
1103 data.dptr = (unsigned char *)&recmode;
1105 ret = ctdb_control(ctdb, destnode, 0,
1106 CTDB_CONTROL_SET_RECMODE, 0, data,
1107 NULL, NULL, &res, &timeout, NULL);
1108 if (ret != 0 || res != 0) {
1109 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setrecmode failed\n"));
1119 get the recovery master of a remote node
1121 struct ctdb_client_control_state *
1122 ctdb_ctrl_getrecmaster_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
1123 struct timeval timeout, uint32_t destnode)
1125 return ctdb_control_send(ctdb, destnode, 0,
1126 CTDB_CONTROL_GET_RECMASTER, 0, tdb_null,
1127 mem_ctx, &timeout, NULL);
1130 int ctdb_ctrl_getrecmaster_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmaster)
1135 ret = ctdb_control_recv(ctdb, state, mem_ctx, NULL, &res, NULL);
1137 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_getrecmaster_recv failed\n"));
1142 *recmaster = (uint32_t)res;
1148 int ctdb_ctrl_getrecmaster(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmaster)
1150 struct ctdb_client_control_state *state;
1152 state = ctdb_ctrl_getrecmaster_send(ctdb, mem_ctx, timeout, destnode);
1153 return ctdb_ctrl_getrecmaster_recv(ctdb, mem_ctx, state, recmaster);
1158 set the recovery master of a remote node
1160 int ctdb_ctrl_setrecmaster(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmaster)
1167 data.dsize = sizeof(uint32_t);
1168 data.dptr = (unsigned char *)&recmaster;
1170 ret = ctdb_control(ctdb, destnode, 0,
1171 CTDB_CONTROL_SET_RECMASTER, 0, data,
1172 NULL, NULL, &res, &timeout, NULL);
1173 if (ret != 0 || res != 0) {
1174 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setrecmaster failed\n"));
1183 get a list of databases off a remote node
1185 int ctdb_ctrl_getdbmap(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode,
1186 TALLOC_CTX *mem_ctx, struct ctdb_dbid_map **dbmap)
1192 ret = ctdb_control(ctdb, destnode, 0,
1193 CTDB_CONTROL_GET_DBMAP, 0, tdb_null,
1194 mem_ctx, &outdata, &res, &timeout, NULL);
1195 if (ret != 0 || res != 0) {
1196 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getdbmap failed ret:%d res:%d\n", ret, res));
1200 *dbmap = (struct ctdb_dbid_map *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
1201 talloc_free(outdata.dptr);
1207 get a list of nodes (vnn and flags ) from a remote node
1209 int ctdb_ctrl_getnodemap(struct ctdb_context *ctdb,
1210 struct timeval timeout, uint32_t destnode,
1211 TALLOC_CTX *mem_ctx, struct ctdb_node_map **nodemap)
1217 ret = ctdb_control(ctdb, destnode, 0,
1218 CTDB_CONTROL_GET_NODEMAP, 0, tdb_null,
1219 mem_ctx, &outdata, &res, &timeout, NULL);
1220 if (ret == 0 && res == -1 && outdata.dsize == 0) {
1221 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getnodes failed, falling back to ipv4-only control\n"));
1222 return ctdb_ctrl_getnodemapv4(ctdb, timeout, destnode, mem_ctx, nodemap);
1224 if (ret != 0 || res != 0 || outdata.dsize == 0) {
1225 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getnodes failed ret:%d res:%d\n", ret, res));
1229 *nodemap = (struct ctdb_node_map *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
1230 talloc_free(outdata.dptr);
1236 old style ipv4-only get a list of nodes (vnn and flags ) from a remote node
1238 int ctdb_ctrl_getnodemapv4(struct ctdb_context *ctdb,
1239 struct timeval timeout, uint32_t destnode,
1240 TALLOC_CTX *mem_ctx, struct ctdb_node_map **nodemap)
1244 struct ctdb_node_mapv4 *nodemapv4;
1247 ret = ctdb_control(ctdb, destnode, 0,
1248 CTDB_CONTROL_GET_NODEMAPv4, 0, tdb_null,
1249 mem_ctx, &outdata, &res, &timeout, NULL);
1250 if (ret != 0 || res != 0 || outdata.dsize == 0) {
1251 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getnodesv4 failed ret:%d res:%d\n", ret, res));
1255 nodemapv4 = (struct ctdb_node_mapv4 *)outdata.dptr;
1257 len = offsetof(struct ctdb_node_map, nodes) + nodemapv4->num*sizeof(struct ctdb_node_and_flags);
1258 (*nodemap) = talloc_zero_size(mem_ctx, len);
1259 CTDB_NO_MEMORY(ctdb, (*nodemap));
1261 (*nodemap)->num = nodemapv4->num;
1262 for (i=0; i<nodemapv4->num; i++) {
1263 (*nodemap)->nodes[i].pnn = nodemapv4->nodes[i].pnn;
1264 (*nodemap)->nodes[i].flags = nodemapv4->nodes[i].flags;
1265 (*nodemap)->nodes[i].addr.ip = nodemapv4->nodes[i].sin;
1266 (*nodemap)->nodes[i].addr.sa.sa_family = AF_INET;
1269 talloc_free(outdata.dptr);
1275 drop the transport, reload the nodes file and restart the transport
1277 int ctdb_ctrl_reload_nodes_file(struct ctdb_context *ctdb,
1278 struct timeval timeout, uint32_t destnode)
1283 ret = ctdb_control(ctdb, destnode, 0,
1284 CTDB_CONTROL_RELOAD_NODES_FILE, 0, tdb_null,
1285 NULL, NULL, &res, &timeout, NULL);
1286 if (ret != 0 || res != 0) {
1287 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for reloadnodesfile failed\n"));
1296 set vnn map on a node
1298 int ctdb_ctrl_setvnnmap(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode,
1299 TALLOC_CTX *mem_ctx, struct ctdb_vnn_map *vnnmap)
1304 struct ctdb_vnn_map_wire *map;
1307 len = offsetof(struct ctdb_vnn_map_wire, map) + sizeof(uint32_t)*vnnmap->size;
1308 map = talloc_size(mem_ctx, len);
1309 CTDB_NO_MEMORY(ctdb, map);
1311 map->generation = vnnmap->generation;
1312 map->size = vnnmap->size;
1313 memcpy(map->map, vnnmap->map, sizeof(uint32_t)*map->size);
1316 data.dptr = (uint8_t *)map;
1318 ret = ctdb_control(ctdb, destnode, 0,
1319 CTDB_CONTROL_SETVNNMAP, 0, data,
1320 NULL, NULL, &res, &timeout, NULL);
1321 if (ret != 0 || res != 0) {
1322 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setvnnmap failed\n"));
1333 async send for pull database
1335 struct ctdb_client_control_state *ctdb_ctrl_pulldb_send(
1336 struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid,
1337 uint32_t lmaster, TALLOC_CTX *mem_ctx, struct timeval timeout)
1340 struct ctdb_control_pulldb *pull;
1341 struct ctdb_client_control_state *state;
1343 pull = talloc(mem_ctx, struct ctdb_control_pulldb);
1344 CTDB_NO_MEMORY_NULL(ctdb, pull);
1347 pull->lmaster = lmaster;
1349 indata.dsize = sizeof(struct ctdb_control_pulldb);
1350 indata.dptr = (unsigned char *)pull;
1352 state = ctdb_control_send(ctdb, destnode, 0,
1353 CTDB_CONTROL_PULL_DB, 0, indata,
1354 mem_ctx, &timeout, NULL);
1361 async recv for pull database
1363 int ctdb_ctrl_pulldb_recv(
1364 struct ctdb_context *ctdb,
1365 TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state,
1371 ret = ctdb_control_recv(ctdb, state, mem_ctx, outdata, &res, NULL);
1372 if ( (ret != 0) || (res != 0) ){
1373 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_pulldb_recv failed\n"));
1381 pull all keys and records for a specific database on a node
1383 int ctdb_ctrl_pulldb(struct ctdb_context *ctdb, uint32_t destnode,
1384 uint32_t dbid, uint32_t lmaster,
1385 TALLOC_CTX *mem_ctx, struct timeval timeout,
1388 struct ctdb_client_control_state *state;
1390 state = ctdb_ctrl_pulldb_send(ctdb, destnode, dbid, lmaster, mem_ctx,
1393 return ctdb_ctrl_pulldb_recv(ctdb, mem_ctx, state, outdata);
1398 change dmaster for all keys in the database to the new value
1400 int ctdb_ctrl_setdmaster(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode,
1401 TALLOC_CTX *mem_ctx, uint32_t dbid, uint32_t dmaster)
1407 indata.dsize = 2*sizeof(uint32_t);
1408 indata.dptr = (unsigned char *)talloc_array(mem_ctx, uint32_t, 2);
1410 ((uint32_t *)(&indata.dptr[0]))[0] = dbid;
1411 ((uint32_t *)(&indata.dptr[0]))[1] = dmaster;
1413 ret = ctdb_control(ctdb, destnode, 0,
1414 CTDB_CONTROL_SET_DMASTER, 0, indata,
1415 NULL, NULL, &res, &timeout, NULL);
1416 if (ret != 0 || res != 0) {
1417 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setdmaster failed\n"));
1425 ping a node, return number of clients connected
1427 int ctdb_ctrl_ping(struct ctdb_context *ctdb, uint32_t destnode)
1432 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_PING, 0,
1433 tdb_null, NULL, NULL, &res, NULL, NULL);
1441 find the real path to a ltdb
1443 int ctdb_ctrl_getdbpath(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx,
1450 data.dptr = (uint8_t *)&dbid;
1451 data.dsize = sizeof(dbid);
1453 ret = ctdb_control(ctdb, destnode, 0,
1454 CTDB_CONTROL_GETDBPATH, 0, data,
1455 mem_ctx, &data, &res, &timeout, NULL);
1456 if (ret != 0 || res != 0) {
1460 (*path) = talloc_strndup(mem_ctx, (const char *)data.dptr, data.dsize);
1461 if ((*path) == NULL) {
1465 talloc_free(data.dptr);
1471 find the name of a db
1473 int ctdb_ctrl_getdbname(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx,
1480 data.dptr = (uint8_t *)&dbid;
1481 data.dsize = sizeof(dbid);
1483 ret = ctdb_control(ctdb, destnode, 0,
1484 CTDB_CONTROL_GET_DBNAME, 0, data,
1485 mem_ctx, &data, &res, &timeout, NULL);
1486 if (ret != 0 || res != 0) {
1490 (*name) = talloc_strndup(mem_ctx, (const char *)data.dptr, data.dsize);
1491 if ((*name) == NULL) {
1495 talloc_free(data.dptr);
1503 int ctdb_ctrl_createdb(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode,
1504 TALLOC_CTX *mem_ctx, const char *name, bool persistent)
1510 data.dptr = discard_const(name);
1511 data.dsize = strlen(name)+1;
1513 ret = ctdb_control(ctdb, destnode, 0,
1514 persistent?CTDB_CONTROL_DB_ATTACH_PERSISTENT:CTDB_CONTROL_DB_ATTACH,
1516 mem_ctx, &data, &res, &timeout, NULL);
1518 if (ret != 0 || res != 0) {
1526 get debug level on a node
1528 int ctdb_ctrl_get_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, int32_t *level)
1534 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_GET_DEBUG, 0, tdb_null,
1535 ctdb, &data, &res, NULL, NULL);
1536 if (ret != 0 || res != 0) {
1539 if (data.dsize != sizeof(int32_t)) {
1540 DEBUG(DEBUG_ERR,("Bad control reply size in ctdb_get_debuglevel (got %u)\n",
1541 (unsigned)data.dsize));
1544 *level = *(int32_t *)data.dptr;
1545 talloc_free(data.dptr);
1550 set debug level on a node
1552 int ctdb_ctrl_set_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, int32_t level)
1558 data.dptr = (uint8_t *)&level;
1559 data.dsize = sizeof(level);
1561 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_SET_DEBUG, 0, data,
1562 NULL, NULL, &res, NULL, NULL);
1563 if (ret != 0 || res != 0) {
1571 get a list of connected nodes
1573 uint32_t *ctdb_get_connected_nodes(struct ctdb_context *ctdb,
1574 struct timeval timeout,
1575 TALLOC_CTX *mem_ctx,
1576 uint32_t *num_nodes)
1578 struct ctdb_node_map *map=NULL;
1584 ret = ctdb_ctrl_getnodemap(ctdb, timeout, CTDB_CURRENT_NODE, mem_ctx, &map);
1589 nodes = talloc_array(mem_ctx, uint32_t, map->num);
1590 if (nodes == NULL) {
1594 for (i=0;i<map->num;i++) {
1595 if (!(map->nodes[i].flags & NODE_FLAGS_DISCONNECTED)) {
1596 nodes[*num_nodes] = map->nodes[i].pnn;
1608 int ctdb_statistics_reset(struct ctdb_context *ctdb, uint32_t destnode)
1613 ret = ctdb_control(ctdb, destnode, 0,
1614 CTDB_CONTROL_STATISTICS_RESET, 0, tdb_null,
1615 NULL, NULL, &res, NULL, NULL);
1616 if (ret != 0 || res != 0) {
1617 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for reset statistics failed\n"));
1624 this is the dummy null procedure that all databases support
1626 static int ctdb_null_func(struct ctdb_call_info *call)
1632 this is a plain fetch procedure that all databases support
1634 static int ctdb_fetch_func(struct ctdb_call_info *call)
1636 call->reply_data = &call->record_data;
1641 attach to a specific database - client call
1643 struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb, const char *name, bool persistent, uint32_t tdb_flags)
1645 struct ctdb_db_context *ctdb_db;
1650 ctdb_db = ctdb_db_handle(ctdb, name);
1655 ctdb_db = talloc_zero(ctdb, struct ctdb_db_context);
1656 CTDB_NO_MEMORY_NULL(ctdb, ctdb_db);
1658 ctdb_db->ctdb = ctdb;
1659 ctdb_db->db_name = talloc_strdup(ctdb_db, name);
1660 CTDB_NO_MEMORY_NULL(ctdb, ctdb_db->db_name);
1662 data.dptr = discard_const(name);
1663 data.dsize = strlen(name)+1;
1665 /* tell ctdb daemon to attach */
1666 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, tdb_flags,
1667 persistent?CTDB_CONTROL_DB_ATTACH_PERSISTENT:CTDB_CONTROL_DB_ATTACH,
1668 0, data, ctdb_db, &data, &res, NULL, NULL);
1669 if (ret != 0 || res != 0 || data.dsize != sizeof(uint32_t)) {
1670 DEBUG(DEBUG_ERR,("Failed to attach to database '%s'\n", name));
1671 talloc_free(ctdb_db);
1675 ctdb_db->db_id = *(uint32_t *)data.dptr;
1676 talloc_free(data.dptr);
1678 ret = ctdb_ctrl_getdbpath(ctdb, timeval_current_ofs(2, 0), CTDB_CURRENT_NODE, ctdb_db->db_id, ctdb_db, &ctdb_db->db_path);
1680 DEBUG(DEBUG_ERR,("Failed to get dbpath for database '%s'\n", name));
1681 talloc_free(ctdb_db);
1685 tdb_flags = persistent?TDB_DEFAULT:TDB_NOSYNC;
1686 if (!ctdb->do_setsched) {
1687 tdb_flags |= TDB_NOMMAP;
1689 tdb_flags |= TDB_DISALLOW_NESTING;
1691 ctdb_db->ltdb = tdb_wrap_open(ctdb, ctdb_db->db_path, 0, tdb_flags, O_RDWR, 0);
1692 if (ctdb_db->ltdb == NULL) {
1693 ctdb_set_error(ctdb, "Failed to open tdb '%s'\n", ctdb_db->db_path);
1694 talloc_free(ctdb_db);
1698 ctdb_db->persistent = persistent;
1700 DLIST_ADD(ctdb->db_list, ctdb_db);
1702 /* add well known functions */
1703 ctdb_set_call(ctdb_db, ctdb_null_func, CTDB_NULL_FUNC);
1704 ctdb_set_call(ctdb_db, ctdb_fetch_func, CTDB_FETCH_FUNC);
1711 setup a call for a database
1713 int ctdb_set_call(struct ctdb_db_context *ctdb_db, ctdb_fn_t fn, uint32_t id)
1715 struct ctdb_registered_call *call;
1720 struct ctdb_control_set_call c;
1723 /* this is no longer valid with the separate daemon architecture */
1724 c.db_id = ctdb_db->db_id;
1728 data.dptr = (uint8_t *)&c;
1729 data.dsize = sizeof(c);
1731 ret = ctdb_control(ctdb_db->ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_SET_CALL, 0,
1732 data, NULL, NULL, &status, NULL, NULL);
1733 if (ret != 0 || status != 0) {
1734 DEBUG(DEBUG_ERR,("ctdb_set_call failed for call %u\n", id));
1739 /* also register locally */
1740 call = talloc(ctdb_db, struct ctdb_registered_call);
1744 DLIST_ADD(ctdb_db->calls, call);
1749 struct traverse_state {
1752 ctdb_traverse_func fn;
1757 called on each key during a ctdb_traverse
1759 static void traverse_handler(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data, void *p)
1761 struct traverse_state *state = (struct traverse_state *)p;
1762 struct ctdb_rec_data *d = (struct ctdb_rec_data *)data.dptr;
1765 if (data.dsize < sizeof(uint32_t) ||
1766 d->length != data.dsize) {
1767 DEBUG(DEBUG_ERR,("Bad data size %u in traverse_handler\n", (unsigned)data.dsize));
1772 key.dsize = d->keylen;
1773 key.dptr = &d->data[0];
1774 data.dsize = d->datalen;
1775 data.dptr = &d->data[d->keylen];
1777 if (key.dsize == 0 && data.dsize == 0) {
1778 /* end of traverse */
1783 if (data.dsize == sizeof(struct ctdb_ltdb_header)) {
1784 /* empty records are deleted records in ctdb */
1788 if (state->fn(ctdb, key, data, state->private_data) != 0) {
1797 start a cluster wide traverse, calling the supplied fn on each record
1798 return the number of records traversed, or -1 on error
1800 int ctdb_traverse(struct ctdb_db_context *ctdb_db, ctdb_traverse_func fn, void *private_data)
1803 struct ctdb_traverse_start t;
1806 uint64_t srvid = (getpid() | 0xFLL<<60);
1807 struct traverse_state state;
1811 state.private_data = private_data;
1814 ret = ctdb_set_message_handler(ctdb_db->ctdb, srvid, traverse_handler, &state);
1816 DEBUG(DEBUG_ERR,("Failed to setup traverse handler\n"));
1820 t.db_id = ctdb_db->db_id;
1824 data.dptr = (uint8_t *)&t;
1825 data.dsize = sizeof(t);
1827 ret = ctdb_control(ctdb_db->ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_TRAVERSE_START, 0,
1828 data, NULL, NULL, &status, NULL, NULL);
1829 if (ret != 0 || status != 0) {
1830 DEBUG(DEBUG_ERR,("ctdb_traverse_all failed\n"));
1831 ctdb_remove_message_handler(ctdb_db->ctdb, srvid, &state);
1835 while (!state.done) {
1836 event_loop_once(ctdb_db->ctdb->ev);
1839 ret = ctdb_remove_message_handler(ctdb_db->ctdb, srvid, &state);
1841 DEBUG(DEBUG_ERR,("Failed to remove ctdb_traverse handler\n"));
1848 #define ISASCII(x) ((x>31)&&(x<128))
1850 called on each key during a catdb
1852 static int dumpdb_fn(struct ctdb_context *ctdb, TDB_DATA key, TDB_DATA data, void *p)
1855 FILE *f = (FILE *)p;
1856 struct ctdb_ltdb_header *h = (struct ctdb_ltdb_header *)data.dptr;
1858 fprintf(f, "key(%u) = \"", (unsigned)key.dsize);
1859 for (i=0;i<key.dsize;i++) {
1860 if (ISASCII(key.dptr[i])) {
1861 fprintf(f, "%c", key.dptr[i]);
1863 fprintf(f, "\\%02X", key.dptr[i]);
1868 fprintf(f, "dmaster: %u\n", h->dmaster);
1869 fprintf(f, "rsn: %llu\n", (unsigned long long)h->rsn);
1871 fprintf(f, "data(%u) = \"", (unsigned)data.dsize);
1872 for (i=sizeof(*h);i<data.dsize;i++) {
1873 if (ISASCII(data.dptr[i])) {
1874 fprintf(f, "%c", data.dptr[i]);
1876 fprintf(f, "\\%02X", data.dptr[i]);
1887 convenience function to list all keys to stdout
1889 int ctdb_dump_db(struct ctdb_db_context *ctdb_db, FILE *f)
1891 return ctdb_traverse(ctdb_db, dumpdb_fn, f);
1895 get the pid of a ctdb daemon
1897 int ctdb_ctrl_getpid(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *pid)
1902 ret = ctdb_control(ctdb, destnode, 0,
1903 CTDB_CONTROL_GET_PID, 0, tdb_null,
1904 NULL, NULL, &res, &timeout, NULL);
1906 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getpid failed\n"));
1917 async freeze send control
1919 struct ctdb_client_control_state *
1920 ctdb_ctrl_freeze_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t priority)
1922 return ctdb_control_send(ctdb, destnode, priority,
1923 CTDB_CONTROL_FREEZE, 0, tdb_null,
1924 mem_ctx, &timeout, NULL);
1928 async freeze recv control
1930 int ctdb_ctrl_freeze_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state)
1935 ret = ctdb_control_recv(ctdb, state, mem_ctx, NULL, &res, NULL);
1936 if ( (ret != 0) || (res != 0) ){
1937 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_freeze_recv failed\n"));
1945 freeze databases of a certain priority
1947 int ctdb_ctrl_freeze_priority(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t priority)
1949 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1950 struct ctdb_client_control_state *state;
1953 state = ctdb_ctrl_freeze_send(ctdb, tmp_ctx, timeout, destnode, priority);
1954 ret = ctdb_ctrl_freeze_recv(ctdb, tmp_ctx, state);
1955 talloc_free(tmp_ctx);
1960 /* Freeze all databases */
1961 int ctdb_ctrl_freeze(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
1965 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
1966 if (ctdb_ctrl_freeze_priority(ctdb, timeout, destnode, i) != 0) {
1974 thaw databases of a certain priority
1976 int ctdb_ctrl_thaw_priority(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t priority)
1981 ret = ctdb_control(ctdb, destnode, priority,
1982 CTDB_CONTROL_THAW, 0, tdb_null,
1983 NULL, NULL, &res, &timeout, NULL);
1984 if (ret != 0 || res != 0) {
1985 DEBUG(DEBUG_ERR,(__location__ " ctdb_control thaw failed\n"));
1992 /* thaw all databases */
1993 int ctdb_ctrl_thaw(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
1995 return ctdb_ctrl_thaw_priority(ctdb, timeout, destnode, 0);
1999 get pnn of a node, or -1
2001 int ctdb_ctrl_getpnn(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
2006 ret = ctdb_control(ctdb, destnode, 0,
2007 CTDB_CONTROL_GET_PNN, 0, tdb_null,
2008 NULL, NULL, &res, &timeout, NULL);
2010 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getpnn failed\n"));
2018 get the monitoring mode of a remote node
2020 int ctdb_ctrl_getmonmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *monmode)
2025 ret = ctdb_control(ctdb, destnode, 0,
2026 CTDB_CONTROL_GET_MONMODE, 0, tdb_null,
2027 NULL, NULL, &res, &timeout, NULL);
2029 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getmonmode failed\n"));
2040 set the monitoring mode of a remote node to active
2042 int ctdb_ctrl_enable_monmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
2047 ret = ctdb_control(ctdb, destnode, 0,
2048 CTDB_CONTROL_ENABLE_MONITOR, 0, tdb_null,
2049 NULL, NULL,NULL, &timeout, NULL);
2051 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for enable_monitor failed\n"));
2061 set the monitoring mode of a remote node to disable
2063 int ctdb_ctrl_disable_monmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
2068 ret = ctdb_control(ctdb, destnode, 0,
2069 CTDB_CONTROL_DISABLE_MONITOR, 0, tdb_null,
2070 NULL, NULL, NULL, &timeout, NULL);
2072 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for disable_monitor failed\n"));
2084 sent to a node to make it take over an ip address
2086 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout,
2087 uint32_t destnode, struct ctdb_public_ip *ip)
2090 struct ctdb_public_ipv4 ipv4;
2094 if (ip->addr.sa.sa_family == AF_INET) {
2096 ipv4.sin = ip->addr.ip;
2098 data.dsize = sizeof(ipv4);
2099 data.dptr = (uint8_t *)&ipv4;
2101 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_TAKEOVER_IPv4, 0, data, NULL,
2102 NULL, &res, &timeout, NULL);
2104 data.dsize = sizeof(*ip);
2105 data.dptr = (uint8_t *)ip;
2107 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_TAKEOVER_IP, 0, data, NULL,
2108 NULL, &res, &timeout, NULL);
2111 if (ret != 0 || res != 0) {
2112 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for takeover_ip failed\n"));
2121 sent to a node to make it release an ip address
2123 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout,
2124 uint32_t destnode, struct ctdb_public_ip *ip)
2127 struct ctdb_public_ipv4 ipv4;
2131 if (ip->addr.sa.sa_family == AF_INET) {
2133 ipv4.sin = ip->addr.ip;
2135 data.dsize = sizeof(ipv4);
2136 data.dptr = (uint8_t *)&ipv4;
2138 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_RELEASE_IPv4, 0, data, NULL,
2139 NULL, &res, &timeout, NULL);
2141 data.dsize = sizeof(*ip);
2142 data.dptr = (uint8_t *)ip;
2144 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_RELEASE_IP, 0, data, NULL,
2145 NULL, &res, &timeout, NULL);
2148 if (ret != 0 || res != 0) {
2149 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for release_ip failed\n"));
2160 int ctdb_ctrl_get_tunable(struct ctdb_context *ctdb,
2161 struct timeval timeout,
2163 const char *name, uint32_t *value)
2165 struct ctdb_control_get_tunable *t;
2166 TDB_DATA data, outdata;
2170 data.dsize = offsetof(struct ctdb_control_get_tunable, name) + strlen(name) + 1;
2171 data.dptr = talloc_size(ctdb, data.dsize);
2172 CTDB_NO_MEMORY(ctdb, data.dptr);
2174 t = (struct ctdb_control_get_tunable *)data.dptr;
2175 t->length = strlen(name)+1;
2176 memcpy(t->name, name, t->length);
2178 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_GET_TUNABLE, 0, data, ctdb,
2179 &outdata, &res, &timeout, NULL);
2180 talloc_free(data.dptr);
2181 if (ret != 0 || res != 0) {
2182 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get_tunable failed\n"));
2186 if (outdata.dsize != sizeof(uint32_t)) {
2187 DEBUG(DEBUG_ERR,("Invalid return data in get_tunable\n"));
2188 talloc_free(outdata.dptr);
2192 *value = *(uint32_t *)outdata.dptr;
2193 talloc_free(outdata.dptr);
2201 int ctdb_ctrl_set_tunable(struct ctdb_context *ctdb,
2202 struct timeval timeout,
2204 const char *name, uint32_t value)
2206 struct ctdb_control_set_tunable *t;
2211 data.dsize = offsetof(struct ctdb_control_set_tunable, name) + strlen(name) + 1;
2212 data.dptr = talloc_size(ctdb, data.dsize);
2213 CTDB_NO_MEMORY(ctdb, data.dptr);
2215 t = (struct ctdb_control_set_tunable *)data.dptr;
2216 t->length = strlen(name)+1;
2217 memcpy(t->name, name, t->length);
2220 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_SET_TUNABLE, 0, data, NULL,
2221 NULL, &res, &timeout, NULL);
2222 talloc_free(data.dptr);
2223 if (ret != 0 || res != 0) {
2224 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set_tunable failed\n"));
2234 int ctdb_ctrl_list_tunables(struct ctdb_context *ctdb,
2235 struct timeval timeout,
2237 TALLOC_CTX *mem_ctx,
2238 const char ***list, uint32_t *count)
2243 struct ctdb_control_list_tunable *t;
2246 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_LIST_TUNABLES, 0, tdb_null,
2247 mem_ctx, &outdata, &res, &timeout, NULL);
2248 if (ret != 0 || res != 0) {
2249 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for list_tunables failed\n"));
2253 t = (struct ctdb_control_list_tunable *)outdata.dptr;
2254 if (outdata.dsize < offsetof(struct ctdb_control_list_tunable, data) ||
2255 t->length > outdata.dsize-offsetof(struct ctdb_control_list_tunable, data)) {
2256 DEBUG(DEBUG_ERR,("Invalid data in list_tunables reply\n"));
2257 talloc_free(outdata.dptr);
2261 p = talloc_strndup(mem_ctx, (char *)t->data, t->length);
2262 CTDB_NO_MEMORY(ctdb, p);
2264 talloc_free(outdata.dptr);
2269 for (s=strtok_r(p, ":", &ptr); s; s=strtok_r(NULL, ":", &ptr)) {
2270 (*list) = talloc_realloc(mem_ctx, *list, const char *, 1+(*count));
2271 CTDB_NO_MEMORY(ctdb, *list);
2272 (*list)[*count] = talloc_strdup(*list, s);
2273 CTDB_NO_MEMORY(ctdb, (*list)[*count]);
2283 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb,
2284 struct timeval timeout, uint32_t destnode,
2285 TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips)
2291 ret = ctdb_control(ctdb, destnode, 0,
2292 CTDB_CONTROL_GET_PUBLIC_IPS, 0, tdb_null,
2293 mem_ctx, &outdata, &res, &timeout, NULL);
2294 if (ret == 0 && res == -1) {
2295 DEBUG(DEBUG_ERR,(__location__ " ctdb_control to get public ips failed, falling back to ipv4-only version\n"));
2296 return ctdb_ctrl_get_public_ipsv4(ctdb, timeout, destnode, mem_ctx, ips);
2298 if (ret != 0 || res != 0) {
2299 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getpublicips failed ret:%d res:%d\n", ret, res));
2303 *ips = (struct ctdb_all_public_ips *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
2304 talloc_free(outdata.dptr);
2309 int ctdb_ctrl_get_public_ipsv4(struct ctdb_context *ctdb,
2310 struct timeval timeout, uint32_t destnode,
2311 TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips)
2316 struct ctdb_all_public_ipsv4 *ipsv4;
2318 ret = ctdb_control(ctdb, destnode, 0,
2319 CTDB_CONTROL_GET_PUBLIC_IPSv4, 0, tdb_null,
2320 mem_ctx, &outdata, &res, &timeout, NULL);
2321 if (ret != 0 || res != 0) {
2322 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getpublicips failed\n"));
2326 ipsv4 = (struct ctdb_all_public_ipsv4 *)outdata.dptr;
2327 len = offsetof(struct ctdb_all_public_ips, ips) +
2328 ipsv4->num*sizeof(struct ctdb_public_ip);
2329 *ips = talloc_zero_size(mem_ctx, len);
2330 CTDB_NO_MEMORY(ctdb, *ips);
2331 (*ips)->num = ipsv4->num;
2332 for (i=0; i<ipsv4->num; i++) {
2333 (*ips)->ips[i].pnn = ipsv4->ips[i].pnn;
2334 (*ips)->ips[i].addr.ip = ipsv4->ips[i].sin;
2337 talloc_free(outdata.dptr);
2343 set/clear the permanent disabled bit on a remote node
2345 int ctdb_ctrl_modflags(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode,
2346 uint32_t set, uint32_t clear)
2350 struct ctdb_node_map *nodemap=NULL;
2351 struct ctdb_node_flag_change c;
2352 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2357 /* find the recovery master */
2358 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, timeout, CTDB_CURRENT_NODE, &recmaster);
2360 DEBUG(DEBUG_ERR, (__location__ " Unable to get recmaster from local node\n"));
2361 talloc_free(tmp_ctx);
2366 /* read the node flags from the recmaster */
2367 ret = ctdb_ctrl_getnodemap(ctdb, timeout, recmaster, tmp_ctx, &nodemap);
2369 DEBUG(DEBUG_ERR, (__location__ " Unable to get nodemap from node %u\n", destnode));
2370 talloc_free(tmp_ctx);
2373 if (destnode >= nodemap->num) {
2374 DEBUG(DEBUG_ERR,(__location__ " Nodemap from recmaster does not contain node %d\n", destnode));
2375 talloc_free(tmp_ctx);
2380 c.old_flags = nodemap->nodes[destnode].flags;
2381 c.new_flags = c.old_flags;
2383 c.new_flags &= ~clear;
2385 data.dsize = sizeof(c);
2386 data.dptr = (unsigned char *)&c;
2388 /* send the flags update to all connected nodes */
2389 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
2391 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_MODIFY_FLAGS,
2393 timeout, false, data,
2396 DEBUG(DEBUG_ERR, (__location__ " Unable to update nodeflags on remote nodes\n"));
2398 talloc_free(tmp_ctx);
2402 talloc_free(tmp_ctx);
2410 int ctdb_ctrl_get_all_tunables(struct ctdb_context *ctdb,
2411 struct timeval timeout,
2413 struct ctdb_tunable *tunables)
2419 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_GET_ALL_TUNABLES, 0, tdb_null, ctdb,
2420 &outdata, &res, &timeout, NULL);
2421 if (ret != 0 || res != 0) {
2422 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get all tunables failed\n"));
2426 if (outdata.dsize != sizeof(*tunables)) {
2427 DEBUG(DEBUG_ERR,(__location__ " bad data size %u in ctdb_ctrl_get_all_tunables should be %u\n",
2428 (unsigned)outdata.dsize, (unsigned)sizeof(*tunables)));
2432 *tunables = *(struct ctdb_tunable *)outdata.dptr;
2433 talloc_free(outdata.dptr);
2438 add a public address to a node
2440 int ctdb_ctrl_add_public_ip(struct ctdb_context *ctdb,
2441 struct timeval timeout,
2443 struct ctdb_control_ip_iface *pub)
2449 data.dsize = offsetof(struct ctdb_control_ip_iface, iface) + pub->len;
2450 data.dptr = (unsigned char *)pub;
2452 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_ADD_PUBLIC_IP, 0, data, NULL,
2453 NULL, &res, &timeout, NULL);
2454 if (ret != 0 || res != 0) {
2455 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for add_public_ip failed\n"));
2463 delete a public address from a node
2465 int ctdb_ctrl_del_public_ip(struct ctdb_context *ctdb,
2466 struct timeval timeout,
2468 struct ctdb_control_ip_iface *pub)
2474 data.dsize = offsetof(struct ctdb_control_ip_iface, iface) + pub->len;
2475 data.dptr = (unsigned char *)pub;
2477 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_DEL_PUBLIC_IP, 0, data, NULL,
2478 NULL, &res, &timeout, NULL);
2479 if (ret != 0 || res != 0) {
2480 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for del_public_ip failed\n"));
2488 kill a tcp connection
2490 int ctdb_ctrl_killtcp(struct ctdb_context *ctdb,
2491 struct timeval timeout,
2493 struct ctdb_control_killtcp *killtcp)
2499 data.dsize = sizeof(struct ctdb_control_killtcp);
2500 data.dptr = (unsigned char *)killtcp;
2502 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_KILL_TCP, 0, data, NULL,
2503 NULL, &res, &timeout, NULL);
2504 if (ret != 0 || res != 0) {
2505 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for killtcp failed\n"));
2515 int ctdb_ctrl_gratious_arp(struct ctdb_context *ctdb,
2516 struct timeval timeout,
2518 ctdb_sock_addr *addr,
2524 struct ctdb_control_gratious_arp *gratious_arp;
2525 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2528 len = strlen(ifname)+1;
2529 gratious_arp = talloc_size(tmp_ctx,
2530 offsetof(struct ctdb_control_gratious_arp, iface) + len);
2531 CTDB_NO_MEMORY(ctdb, gratious_arp);
2533 gratious_arp->addr = *addr;
2534 gratious_arp->len = len;
2535 memcpy(&gratious_arp->iface[0], ifname, len);
2538 data.dsize = offsetof(struct ctdb_control_gratious_arp, iface) + len;
2539 data.dptr = (unsigned char *)gratious_arp;
2541 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_SEND_GRATIOUS_ARP, 0, data, NULL,
2542 NULL, &res, &timeout, NULL);
2543 if (ret != 0 || res != 0) {
2544 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for gratious_arp failed\n"));
2545 talloc_free(tmp_ctx);
2549 talloc_free(tmp_ctx);
2554 get a list of all tcp tickles that a node knows about for a particular vnn
2556 int ctdb_ctrl_get_tcp_tickles(struct ctdb_context *ctdb,
2557 struct timeval timeout, uint32_t destnode,
2558 TALLOC_CTX *mem_ctx,
2559 ctdb_sock_addr *addr,
2560 struct ctdb_control_tcp_tickle_list **list)
2563 TDB_DATA data, outdata;
2566 data.dptr = (uint8_t*)addr;
2567 data.dsize = sizeof(ctdb_sock_addr);
2569 ret = ctdb_control(ctdb, destnode, 0,
2570 CTDB_CONTROL_GET_TCP_TICKLE_LIST, 0, data,
2571 mem_ctx, &outdata, &status, NULL, NULL);
2572 if (ret != 0 || status != 0) {
2573 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get tcp tickles failed\n"));
2577 *list = (struct ctdb_control_tcp_tickle_list *)outdata.dptr;
2583 register a server id
2585 int ctdb_ctrl_register_server_id(struct ctdb_context *ctdb,
2586 struct timeval timeout,
2587 struct ctdb_server_id *id)
2593 data.dsize = sizeof(struct ctdb_server_id);
2594 data.dptr = (unsigned char *)id;
2596 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0,
2597 CTDB_CONTROL_REGISTER_SERVER_ID,
2599 NULL, &res, &timeout, NULL);
2600 if (ret != 0 || res != 0) {
2601 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for register server id failed\n"));
2609 unregister a server id
2611 int ctdb_ctrl_unregister_server_id(struct ctdb_context *ctdb,
2612 struct timeval timeout,
2613 struct ctdb_server_id *id)
2619 data.dsize = sizeof(struct ctdb_server_id);
2620 data.dptr = (unsigned char *)id;
2622 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0,
2623 CTDB_CONTROL_UNREGISTER_SERVER_ID,
2625 NULL, &res, &timeout, NULL);
2626 if (ret != 0 || res != 0) {
2627 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for unregister server id failed\n"));
2636 check if a server id exists
2638 if a server id does exist, return *status == 1, otherwise *status == 0
2640 int ctdb_ctrl_check_server_id(struct ctdb_context *ctdb,
2641 struct timeval timeout,
2643 struct ctdb_server_id *id,
2650 data.dsize = sizeof(struct ctdb_server_id);
2651 data.dptr = (unsigned char *)id;
2653 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_CHECK_SERVER_ID,
2655 NULL, &res, &timeout, NULL);
2657 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for check server id failed\n"));
2671 get the list of server ids that are registered on a node
2673 int ctdb_ctrl_get_server_id_list(struct ctdb_context *ctdb,
2674 TALLOC_CTX *mem_ctx,
2675 struct timeval timeout, uint32_t destnode,
2676 struct ctdb_server_id_list **svid_list)
2682 ret = ctdb_control(ctdb, destnode, 0,
2683 CTDB_CONTROL_GET_SERVER_ID_LIST, 0, tdb_null,
2684 mem_ctx, &outdata, &res, &timeout, NULL);
2685 if (ret != 0 || res != 0) {
2686 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get_server_id_list failed\n"));
2690 *svid_list = (struct ctdb_server_id_list *)talloc_steal(mem_ctx, outdata.dptr);
2696 initialise the ctdb daemon for client applications
2698 NOTE: In current code the daemon does not fork. This is for testing purposes only
2699 and to simplify the code.
2701 struct ctdb_context *ctdb_init(struct event_context *ev)
2704 struct ctdb_context *ctdb;
2706 ctdb = talloc_zero(ev, struct ctdb_context);
2708 DEBUG(DEBUG_ERR,(__location__ " talloc_zero failed.\n"));
2712 ctdb->idr = idr_init(ctdb);
2713 CTDB_NO_MEMORY_NULL(ctdb, ctdb->idr);
2715 ret = ctdb_set_socketname(ctdb, CTDB_PATH);
2717 DEBUG(DEBUG_ERR,(__location__ " ctdb_set_socketname failed.\n"));
2729 void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags)
2731 ctdb->flags |= flags;
2735 setup the local socket name
2737 int ctdb_set_socketname(struct ctdb_context *ctdb, const char *socketname)
2739 ctdb->daemon.name = talloc_strdup(ctdb, socketname);
2740 CTDB_NO_MEMORY(ctdb, ctdb->daemon.name);
2746 return the pnn of this node
2748 uint32_t ctdb_get_pnn(struct ctdb_context *ctdb)
2755 get the uptime of a remote node
2757 struct ctdb_client_control_state *
2758 ctdb_ctrl_uptime_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode)
2760 return ctdb_control_send(ctdb, destnode, 0,
2761 CTDB_CONTROL_UPTIME, 0, tdb_null,
2762 mem_ctx, &timeout, NULL);
2765 int ctdb_ctrl_uptime_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, struct ctdb_uptime **uptime)
2771 ret = ctdb_control_recv(ctdb, state, mem_ctx, &outdata, &res, NULL);
2772 if (ret != 0 || res != 0) {
2773 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_uptime_recv failed\n"));
2777 *uptime = (struct ctdb_uptime *)talloc_steal(mem_ctx, outdata.dptr);
2782 int ctdb_ctrl_uptime(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, struct ctdb_uptime **uptime)
2784 struct ctdb_client_control_state *state;
2786 state = ctdb_ctrl_uptime_send(ctdb, mem_ctx, timeout, destnode);
2787 return ctdb_ctrl_uptime_recv(ctdb, mem_ctx, state, uptime);
2791 send a control to execute the "recovered" event script on a node
2793 int ctdb_ctrl_end_recovery(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
2798 ret = ctdb_control(ctdb, destnode, 0,
2799 CTDB_CONTROL_END_RECOVERY, 0, tdb_null,
2800 NULL, NULL, &status, &timeout, NULL);
2801 if (ret != 0 || status != 0) {
2802 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for end_recovery failed\n"));
2810 callback for the async helpers used when sending the same control
2811 to multiple nodes in parallell.
2813 static void async_callback(struct ctdb_client_control_state *state)
2815 struct client_async_data *data = talloc_get_type(state->async.private_data, struct client_async_data);
2816 struct ctdb_context *ctdb = talloc_get_type(state->ctdb, struct ctdb_context);
2820 uint32_t destnode = state->c->hdr.destnode;
2822 /* one more node has responded with recmode data */
2825 /* if we failed to push the db, then return an error and let
2826 the main loop try again.
2828 if (state->state != CTDB_CONTROL_DONE) {
2829 if ( !data->dont_log_errors) {
2830 DEBUG(DEBUG_ERR,("Async operation failed with state %d, opcode:%u\n", state->state, data->opcode));
2833 if (data->fail_callback) {
2834 data->fail_callback(ctdb, destnode, res, outdata,
2835 data->callback_data);
2840 state->async.fn = NULL;
2842 ret = ctdb_control_recv(ctdb, state, data, &outdata, &res, NULL);
2843 if ((ret != 0) || (res != 0)) {
2844 if ( !data->dont_log_errors) {
2845 DEBUG(DEBUG_ERR,("Async operation failed with ret=%d res=%d opcode=%u\n", ret, (int)res, data->opcode));
2848 if (data->fail_callback) {
2849 data->fail_callback(ctdb, destnode, res, outdata,
2850 data->callback_data);
2853 if ((ret == 0) && (data->callback != NULL)) {
2854 data->callback(ctdb, destnode, res, outdata,
2855 data->callback_data);
2860 void ctdb_client_async_add(struct client_async_data *data, struct ctdb_client_control_state *state)
2862 /* set up the callback functions */
2863 state->async.fn = async_callback;
2864 state->async.private_data = data;
2866 /* one more control to wait for to complete */
2871 /* wait for up to the maximum number of seconds allowed
2872 or until all nodes we expect a response from has replied
2874 int ctdb_client_async_wait(struct ctdb_context *ctdb, struct client_async_data *data)
2876 while (data->count > 0) {
2877 event_loop_once(ctdb->ev);
2879 if (data->fail_count != 0) {
2880 if (!data->dont_log_errors) {
2881 DEBUG(DEBUG_ERR,("Async wait failed - fail_count=%u\n",
2891 perform a simple control on the listed nodes
2892 The control cannot return data
2894 int ctdb_client_async_control(struct ctdb_context *ctdb,
2895 enum ctdb_controls opcode,
2898 struct timeval timeout,
2899 bool dont_log_errors,
2901 client_async_callback client_callback,
2902 client_async_callback fail_callback,
2903 void *callback_data)
2905 struct client_async_data *async_data;
2906 struct ctdb_client_control_state *state;
2909 async_data = talloc_zero(ctdb, struct client_async_data);
2910 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
2911 async_data->dont_log_errors = dont_log_errors;
2912 async_data->callback = client_callback;
2913 async_data->fail_callback = fail_callback;
2914 async_data->callback_data = callback_data;
2915 async_data->opcode = opcode;
2917 num_nodes = talloc_get_size(nodes) / sizeof(uint32_t);
2919 /* loop over all nodes and send an async control to each of them */
2920 for (j=0; j<num_nodes; j++) {
2921 uint32_t pnn = nodes[j];
2923 state = ctdb_control_send(ctdb, pnn, srvid, opcode,
2924 0, data, async_data, &timeout, NULL);
2925 if (state == NULL) {
2926 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control %u\n", (unsigned)opcode));
2927 talloc_free(async_data);
2931 ctdb_client_async_add(async_data, state);
2934 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2935 talloc_free(async_data);
2939 talloc_free(async_data);
2943 uint32_t *list_of_vnnmap_nodes(struct ctdb_context *ctdb,
2944 struct ctdb_vnn_map *vnn_map,
2945 TALLOC_CTX *mem_ctx,
2948 int i, j, num_nodes;
2951 for (i=num_nodes=0;i<vnn_map->size;i++) {
2952 if (vnn_map->map[i] == ctdb->pnn && !include_self) {
2958 nodes = talloc_array(mem_ctx, uint32_t, num_nodes);
2959 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
2961 for (i=j=0;i<vnn_map->size;i++) {
2962 if (vnn_map->map[i] == ctdb->pnn && !include_self) {
2965 nodes[j++] = vnn_map->map[i];
2971 uint32_t *list_of_active_nodes(struct ctdb_context *ctdb,
2972 struct ctdb_node_map *node_map,
2973 TALLOC_CTX *mem_ctx,
2976 int i, j, num_nodes;
2979 for (i=num_nodes=0;i<node_map->num;i++) {
2980 if (node_map->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2983 if (node_map->nodes[i].pnn == ctdb->pnn && !include_self) {
2989 nodes = talloc_array(mem_ctx, uint32_t, num_nodes);
2990 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
2992 for (i=j=0;i<node_map->num;i++) {
2993 if (node_map->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2996 if (node_map->nodes[i].pnn == ctdb->pnn && !include_self) {
2999 nodes[j++] = node_map->nodes[i].pnn;
3005 uint32_t *list_of_active_nodes_except_pnn(struct ctdb_context *ctdb,
3006 struct ctdb_node_map *node_map,
3007 TALLOC_CTX *mem_ctx,
3010 int i, j, num_nodes;
3013 for (i=num_nodes=0;i<node_map->num;i++) {
3014 if (node_map->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3017 if (node_map->nodes[i].pnn == pnn) {
3023 nodes = talloc_array(mem_ctx, uint32_t, num_nodes);
3024 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
3026 for (i=j=0;i<node_map->num;i++) {
3027 if (node_map->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3030 if (node_map->nodes[i].pnn == pnn) {
3033 nodes[j++] = node_map->nodes[i].pnn;
3039 uint32_t *list_of_connected_nodes(struct ctdb_context *ctdb,
3040 struct ctdb_node_map *node_map,
3041 TALLOC_CTX *mem_ctx,
3044 int i, j, num_nodes;
3047 for (i=num_nodes=0;i<node_map->num;i++) {
3048 if (node_map->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
3051 if (node_map->nodes[i].pnn == ctdb->pnn && !include_self) {
3057 nodes = talloc_array(mem_ctx, uint32_t, num_nodes);
3058 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
3060 for (i=j=0;i<node_map->num;i++) {
3061 if (node_map->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
3064 if (node_map->nodes[i].pnn == ctdb->pnn && !include_self) {
3067 nodes[j++] = node_map->nodes[i].pnn;
3074 this is used to test if a pnn lock exists and if it exists will return
3075 the number of connections that pnn has reported or -1 if that recovery
3076 daemon is not running.
3079 ctdb_read_pnn_lock(int fd, int32_t pnn)
3084 lock.l_type = F_WRLCK;
3085 lock.l_whence = SEEK_SET;
3090 if (fcntl(fd, F_GETLK, &lock) != 0) {
3091 DEBUG(DEBUG_ERR, (__location__ " F_GETLK failed with %s\n", strerror(errno)));
3095 if (lock.l_type == F_UNLCK) {
3099 if (pread(fd, &c, 1, pnn) == -1) {
3100 DEBUG(DEBUG_CRIT,(__location__ " failed read pnn count - %s\n", strerror(errno)));
3108 get capabilities of a remote node
3110 struct ctdb_client_control_state *
3111 ctdb_ctrl_getcapabilities_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode)
3113 return ctdb_control_send(ctdb, destnode, 0,
3114 CTDB_CONTROL_GET_CAPABILITIES, 0, tdb_null,
3115 mem_ctx, &timeout, NULL);
3118 int ctdb_ctrl_getcapabilities_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *capabilities)
3124 ret = ctdb_control_recv(ctdb, state, mem_ctx, &outdata, &res, NULL);
3125 if ( (ret != 0) || (res != 0) ) {
3126 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_getcapabilities_recv failed\n"));
3131 *capabilities = *((uint32_t *)outdata.dptr);
3137 int ctdb_ctrl_getcapabilities(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *capabilities)
3139 struct ctdb_client_control_state *state;
3140 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3143 state = ctdb_ctrl_getcapabilities_send(ctdb, tmp_ctx, timeout, destnode);
3144 ret = ctdb_ctrl_getcapabilities_recv(ctdb, tmp_ctx, state, capabilities);
3145 talloc_free(tmp_ctx);
3150 * check whether a transaction is active on a given db on a given node
3152 int32_t ctdb_ctrl_transaction_active(struct ctdb_context *ctdb,
3160 indata.dptr = (uint8_t *)&db_id;
3161 indata.dsize = sizeof(db_id);
3163 ret = ctdb_control(ctdb, destnode, 0,
3164 CTDB_CONTROL_TRANS2_ACTIVE,
3165 0, indata, NULL, NULL, &status,
3169 DEBUG(DEBUG_ERR, (__location__ " ctdb control for transaction_active failed\n"));
3177 struct ctdb_transaction_handle {
3178 struct ctdb_db_context *ctdb_db;
3181 * we store the reads and writes done under a transaction:
3182 * - one list stores both reads and writes (m_all),
3183 * - the other just writes (m_write)
3185 struct ctdb_marshall_buffer *m_all;
3186 struct ctdb_marshall_buffer *m_write;
3189 /* start a transaction on a database */
3190 static int ctdb_transaction_destructor(struct ctdb_transaction_handle *h)
3192 tdb_transaction_cancel(h->ctdb_db->ltdb->tdb);
3196 /* start a transaction on a database */
3197 static int ctdb_transaction_fetch_start(struct ctdb_transaction_handle *h)
3199 struct ctdb_record_handle *rh;
3202 struct ctdb_ltdb_header header;
3203 TALLOC_CTX *tmp_ctx;
3204 const char *keyname = CTDB_TRANSACTION_LOCK_KEY;
3206 struct ctdb_db_context *ctdb_db = h->ctdb_db;
3210 key.dptr = discard_const(keyname);
3211 key.dsize = strlen(keyname);
3213 if (!ctdb_db->persistent) {
3214 DEBUG(DEBUG_ERR,(__location__ " Attempted transaction on non-persistent database\n"));
3219 tmp_ctx = talloc_new(h);
3221 rh = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, NULL);
3223 DEBUG(DEBUG_ERR,(__location__ " Failed to fetch_lock database\n"));
3224 talloc_free(tmp_ctx);
3228 status = ctdb_ctrl_transaction_active(ctdb_db->ctdb,
3232 unsigned long int usec = (1000 + random()) % 100000;
3233 DEBUG(DEBUG_DEBUG, (__location__ " transaction is active "
3234 "on db_id[0x%08x]. waiting for %lu "
3236 ctdb_db->db_id, usec));
3237 talloc_free(tmp_ctx);
3243 * store the pid in the database:
3244 * it is not enough that the node is dmaster...
3247 data.dptr = (unsigned char *)&pid;
3248 data.dsize = sizeof(pid_t);
3250 rh->header.dmaster = ctdb_db->ctdb->pnn;
3251 ret = ctdb_ltdb_store(ctdb_db, key, &(rh->header), data);
3253 DEBUG(DEBUG_ERR, (__location__ " Failed to store pid in "
3254 "transaction record\n"));
3255 talloc_free(tmp_ctx);
3261 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
3263 DEBUG(DEBUG_ERR,(__location__ " Failed to start tdb transaction\n"));
3264 talloc_free(tmp_ctx);
3268 ret = ctdb_ltdb_fetch(ctdb_db, key, &header, tmp_ctx, &data);
3270 DEBUG(DEBUG_ERR,(__location__ " Failed to re-fetch transaction "
3271 "lock record inside transaction\n"));
3272 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
3273 talloc_free(tmp_ctx);
3277 if (header.dmaster != ctdb_db->ctdb->pnn) {
3278 DEBUG(DEBUG_DEBUG,(__location__ " not dmaster any more on "
3279 "transaction lock record\n"));
3280 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
3281 talloc_free(tmp_ctx);
3285 if ((data.dsize != sizeof(pid_t)) || (*(pid_t *)(data.dptr) != pid)) {
3286 DEBUG(DEBUG_DEBUG, (__location__ " my pid is not stored in "
3287 "the transaction lock record\n"));
3288 tdb_transaction_cancel(ctdb_db->ltdb->tdb);
3289 talloc_free(tmp_ctx);
3293 talloc_free(tmp_ctx);
3299 /* start a transaction on a database */
3300 struct ctdb_transaction_handle *ctdb_transaction_start(struct ctdb_db_context *ctdb_db,
3301 TALLOC_CTX *mem_ctx)
3303 struct ctdb_transaction_handle *h;
3306 h = talloc_zero(mem_ctx, struct ctdb_transaction_handle);
3308 DEBUG(DEBUG_ERR,(__location__ " oom for transaction handle\n"));
3312 h->ctdb_db = ctdb_db;
3314 ret = ctdb_transaction_fetch_start(h);
3320 talloc_set_destructor(h, ctdb_transaction_destructor);
3328 fetch a record inside a transaction
3330 int ctdb_transaction_fetch(struct ctdb_transaction_handle *h,
3331 TALLOC_CTX *mem_ctx,
3332 TDB_DATA key, TDB_DATA *data)
3334 struct ctdb_ltdb_header header;
3337 ZERO_STRUCT(header);
3339 ret = ctdb_ltdb_fetch(h->ctdb_db, key, &header, mem_ctx, data);
3340 if (ret == -1 && header.dmaster == (uint32_t)-1) {
3341 /* record doesn't exist yet */
3350 if (!h->in_replay) {
3351 h->m_all = ctdb_marshall_add(h, h->m_all, h->ctdb_db->db_id, 1, key, NULL, *data);
3352 if (h->m_all == NULL) {
3353 DEBUG(DEBUG_ERR,(__location__ " Failed to add to marshalling record\n"));
3362 stores a record inside a transaction
3364 int ctdb_transaction_store(struct ctdb_transaction_handle *h,
3365 TDB_DATA key, TDB_DATA data)
3367 TALLOC_CTX *tmp_ctx = talloc_new(h);
3368 struct ctdb_ltdb_header header;
3372 ZERO_STRUCT(header);
3374 /* we need the header so we can update the RSN */
3375 ret = ctdb_ltdb_fetch(h->ctdb_db, key, &header, tmp_ctx, &olddata);
3376 if (ret == -1 && header.dmaster == (uint32_t)-1) {
3377 /* the record doesn't exist - create one with us as dmaster.
3378 This is only safe because we are in a transaction and this
3379 is a persistent database */
3380 ZERO_STRUCT(header);
3381 } else if (ret != 0) {
3382 DEBUG(DEBUG_ERR,(__location__ " Failed to fetch record\n"));
3383 talloc_free(tmp_ctx);
3387 if (data.dsize == olddata.dsize &&
3388 memcmp(data.dptr, olddata.dptr, data.dsize) == 0) {
3389 /* save writing the same data */
3390 talloc_free(tmp_ctx);
3394 header.dmaster = h->ctdb_db->ctdb->pnn;
3397 if (!h->in_replay) {
3398 h->m_all = ctdb_marshall_add(h, h->m_all, h->ctdb_db->db_id, 0, key, NULL, data);
3399 if (h->m_all == NULL) {
3400 DEBUG(DEBUG_ERR,(__location__ " Failed to add to marshalling record\n"));
3401 talloc_free(tmp_ctx);
3406 h->m_write = ctdb_marshall_add(h, h->m_write, h->ctdb_db->db_id, 0, key, &header, data);
3407 if (h->m_write == NULL) {
3408 DEBUG(DEBUG_ERR,(__location__ " Failed to add to marshalling record\n"));
3409 talloc_free(tmp_ctx);
3413 ret = ctdb_ltdb_store(h->ctdb_db, key, &header, data);
3415 talloc_free(tmp_ctx);
3421 replay a transaction
3423 static int ctdb_replay_transaction(struct ctdb_transaction_handle *h)
3426 struct ctdb_rec_data *rec = NULL;
3428 h->in_replay = true;
3429 talloc_free(h->m_write);
3432 ret = ctdb_transaction_fetch_start(h);
3437 for (i=0;i<h->m_all->count;i++) {
3440 rec = ctdb_marshall_loop_next(h->m_all, rec, NULL, NULL, &key, &data);
3442 DEBUG(DEBUG_ERR, (__location__ " Out of records in ctdb_replay_transaction?\n"));
3446 if (rec->reqid == 0) {
3448 if (ctdb_transaction_store(h, key, data) != 0) {
3453 TALLOC_CTX *tmp_ctx = talloc_new(h);
3455 if (ctdb_transaction_fetch(h, tmp_ctx, key, &data2) != 0) {
3456 talloc_free(tmp_ctx);
3459 if (data2.dsize != data.dsize ||
3460 memcmp(data2.dptr, data.dptr, data.dsize) != 0) {
3461 /* the record has changed on us - we have to give up */
3462 talloc_free(tmp_ctx);
3465 talloc_free(tmp_ctx);
3472 tdb_transaction_cancel(h->ctdb_db->ltdb->tdb);
3478 commit a transaction
3480 int ctdb_transaction_commit(struct ctdb_transaction_handle *h)
3484 struct ctdb_context *ctdb = h->ctdb_db->ctdb;
3485 struct timeval timeout;
3486 enum ctdb_controls failure_control = CTDB_CONTROL_TRANS2_ERROR;
3488 talloc_set_destructor(h, NULL);
3490 /* our commit strategy is quite complex.
3492 - we first try to commit the changes to all other nodes
3494 - if that works, then we commit locally and we are done
3496 - if a commit on another node fails, then we need to cancel
3497 the transaction, then restart the transaction (thus
3498 opening a window of time for a pending recovery to
3499 complete), then replay the transaction, checking all the
3500 reads and writes (checking that reads give the same data,
3501 and writes succeed). Then we retry the transaction to the
3506 if (h->m_write == NULL) {
3507 /* no changes were made */
3508 tdb_transaction_cancel(h->ctdb_db->ltdb->tdb);
3513 /* tell ctdbd to commit to the other nodes */
3514 timeout = timeval_current_ofs(1, 0);
3515 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, h->ctdb_db->db_id,
3516 retries==0?CTDB_CONTROL_TRANS2_COMMIT:CTDB_CONTROL_TRANS2_COMMIT_RETRY, 0,
3517 ctdb_marshall_finish(h->m_write), NULL, NULL, &status,
3519 if (ret != 0 || status != 0) {
3520 tdb_transaction_cancel(h->ctdb_db->ltdb->tdb);
3521 DEBUG(DEBUG_NOTICE, (__location__ " transaction commit%s failed"
3522 ", retrying after 1 second...\n",
3523 (retries==0)?"":"retry "));
3527 failure_control = CTDB_CONTROL_TRANS2_ERROR;
3529 /* work out what error code we will give if we
3530 have to fail the operation */
3531 switch ((enum ctdb_trans2_commit_error)status) {
3532 case CTDB_TRANS2_COMMIT_SUCCESS:
3533 case CTDB_TRANS2_COMMIT_SOMEFAIL:
3534 case CTDB_TRANS2_COMMIT_TIMEOUT:
3535 failure_control = CTDB_CONTROL_TRANS2_ERROR;
3537 case CTDB_TRANS2_COMMIT_ALLFAIL:
3538 failure_control = CTDB_CONTROL_TRANS2_FINISHED;
3543 if (++retries == 100) {
3544 DEBUG(DEBUG_ERR,(__location__ " Giving up transaction on db 0x%08x after %d retries failure_control=%u\n",
3545 h->ctdb_db->db_id, retries, (unsigned)failure_control));
3546 ctdb_control(ctdb, CTDB_CURRENT_NODE, h->ctdb_db->db_id,
3547 failure_control, CTDB_CTRL_FLAG_NOREPLY,
3548 tdb_null, NULL, NULL, NULL, NULL, NULL);
3553 if (ctdb_replay_transaction(h) != 0) {
3554 DEBUG(DEBUG_ERR, (__location__ " Failed to replay "
3555 "transaction on db 0x%08x, "
3556 "failure control =%u\n",
3558 (unsigned)failure_control));
3559 ctdb_control(ctdb, CTDB_CURRENT_NODE, h->ctdb_db->db_id,
3560 failure_control, CTDB_CTRL_FLAG_NOREPLY,
3561 tdb_null, NULL, NULL, NULL, NULL, NULL);
3567 failure_control = CTDB_CONTROL_TRANS2_ERROR;
3570 /* do the real commit locally */
3571 ret = tdb_transaction_commit(h->ctdb_db->ltdb->tdb);
3573 DEBUG(DEBUG_ERR, (__location__ " Failed to commit transaction "
3574 "on db id 0x%08x locally, "
3575 "failure_control=%u\n",
3577 (unsigned)failure_control));
3578 ctdb_control(ctdb, CTDB_CURRENT_NODE, h->ctdb_db->db_id,
3579 failure_control, CTDB_CTRL_FLAG_NOREPLY,
3580 tdb_null, NULL, NULL, NULL, NULL, NULL);
3585 /* tell ctdbd that we are finished with our local commit */
3586 ctdb_control(ctdb, CTDB_CURRENT_NODE, h->ctdb_db->db_id,
3587 CTDB_CONTROL_TRANS2_FINISHED, CTDB_CTRL_FLAG_NOREPLY,
3588 tdb_null, NULL, NULL, NULL, NULL, NULL);
3594 recovery daemon ping to main daemon
3596 int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb)
3601 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_RECD_PING, 0, tdb_null,
3602 ctdb, NULL, &res, NULL, NULL);
3603 if (ret != 0 || res != 0) {
3604 DEBUG(DEBUG_ERR,("Failed to send recd ping\n"));
3611 /* when forking the main daemon and the child process needs to connect back
3612 * to the daemon as a client process, this function can be used to change
3613 * the ctdb context from daemon into client mode
3615 int switch_from_server_to_client(struct ctdb_context *ctdb)
3619 /* shutdown the transport */
3620 if (ctdb->methods) {
3621 ctdb->methods->shutdown(ctdb);
3624 /* get a new event context */
3625 talloc_free(ctdb->ev);
3626 ctdb->ev = event_context_init(ctdb);
3628 close(ctdb->daemon.sd);
3629 ctdb->daemon.sd = -1;
3631 /* the client does not need to be realtime */
3632 if (ctdb->do_setsched) {
3633 ctdb_restore_scheduler(ctdb);
3636 /* initialise ctdb */
3637 ret = ctdb_socket_connect(ctdb);
3639 DEBUG(DEBUG_ALERT, (__location__ " Failed to init ctdb client\n"));
3647 get the status of running the monitor eventscripts: NULL means never run.
3649 int ctdb_ctrl_getscriptstatus(struct ctdb_context *ctdb,
3650 struct timeval timeout, uint32_t destnode,
3651 TALLOC_CTX *mem_ctx, enum ctdb_eventscript_call type,
3652 struct ctdb_scripts_wire **script_status)
3655 TDB_DATA outdata, indata;
3657 uint32_t uinttype = type;
3659 indata.dptr = (uint8_t *)&uinttype;
3660 indata.dsize = sizeof(uinttype);
3662 ret = ctdb_control(ctdb, destnode, 0,
3663 CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS, 0, indata,
3664 mem_ctx, &outdata, &res, &timeout, NULL);
3665 if (ret != 0 || res != 0) {
3666 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getscriptstatus failed ret:%d res:%d\n", ret, res));
3670 if (outdata.dsize == 0) {
3671 *script_status = NULL;
3673 *script_status = (struct ctdb_scripts_wire *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
3674 talloc_free(outdata.dptr);
3681 tell the main daemon how long it took to lock the reclock file
3683 int ctdb_ctrl_report_recd_lock_latency(struct ctdb_context *ctdb, struct timeval timeout, double latency)
3689 data.dptr = (uint8_t *)&latency;
3690 data.dsize = sizeof(latency);
3692 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_RECD_RECLOCK_LATENCY, 0, data,
3693 ctdb, NULL, &res, NULL, NULL);
3694 if (ret != 0 || res != 0) {
3695 DEBUG(DEBUG_ERR,("Failed to send recd reclock latency\n"));
3703 get the name of the reclock file
3705 int ctdb_ctrl_getreclock(struct ctdb_context *ctdb, struct timeval timeout,
3706 uint32_t destnode, TALLOC_CTX *mem_ctx,
3713 ret = ctdb_control(ctdb, destnode, 0,
3714 CTDB_CONTROL_GET_RECLOCK_FILE, 0, tdb_null,
3715 mem_ctx, &data, &res, &timeout, NULL);
3716 if (ret != 0 || res != 0) {
3720 if (data.dsize == 0) {
3723 *name = talloc_strdup(mem_ctx, discard_const(data.dptr));
3725 talloc_free(data.dptr);
3731 set the reclock filename for a node
3733 int ctdb_ctrl_setreclock(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, const char *reclock)
3739 if (reclock == NULL) {
3743 data.dsize = strlen(reclock) + 1;
3744 data.dptr = discard_const(reclock);
3747 ret = ctdb_control(ctdb, destnode, 0,
3748 CTDB_CONTROL_SET_RECLOCK_FILE, 0, data,
3749 NULL, NULL, &res, &timeout, NULL);
3750 if (ret != 0 || res != 0) {
3751 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setreclock failed\n"));
3761 int ctdb_ctrl_stop_node(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
3766 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_STOP_NODE, 0, tdb_null,
3767 ctdb, NULL, &res, &timeout, NULL);
3768 if (ret != 0 || res != 0) {
3769 DEBUG(DEBUG_ERR,("Failed to stop node\n"));
3779 int ctdb_ctrl_continue_node(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
3783 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_CONTINUE_NODE, 0, tdb_null,
3784 ctdb, NULL, NULL, &timeout, NULL);
3786 DEBUG(DEBUG_ERR,("Failed to continue node\n"));
3794 set the natgw state for a node
3796 int ctdb_ctrl_setnatgwstate(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t natgwstate)
3802 data.dsize = sizeof(natgwstate);
3803 data.dptr = (uint8_t *)&natgwstate;
3805 ret = ctdb_control(ctdb, destnode, 0,
3806 CTDB_CONTROL_SET_NATGWSTATE, 0, data,
3807 NULL, NULL, &res, &timeout, NULL);
3808 if (ret != 0 || res != 0) {
3809 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setnatgwstate failed\n"));
3817 set the lmaster role for a node
3819 int ctdb_ctrl_setlmasterrole(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t lmasterrole)
3825 data.dsize = sizeof(lmasterrole);
3826 data.dptr = (uint8_t *)&lmasterrole;
3828 ret = ctdb_control(ctdb, destnode, 0,
3829 CTDB_CONTROL_SET_LMASTERROLE, 0, data,
3830 NULL, NULL, &res, &timeout, NULL);
3831 if (ret != 0 || res != 0) {
3832 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setlmasterrole failed\n"));
3840 set the recmaster role for a node
3842 int ctdb_ctrl_setrecmasterrole(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmasterrole)
3848 data.dsize = sizeof(recmasterrole);
3849 data.dptr = (uint8_t *)&recmasterrole;
3851 ret = ctdb_control(ctdb, destnode, 0,
3852 CTDB_CONTROL_SET_RECMASTERROLE, 0, data,
3853 NULL, NULL, &res, &timeout, NULL);
3854 if (ret != 0 || res != 0) {
3855 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setrecmasterrole failed\n"));
3862 /* enable an eventscript
3864 int ctdb_ctrl_enablescript(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, const char *script)
3870 data.dsize = strlen(script) + 1;
3871 data.dptr = discard_const(script);
3873 ret = ctdb_control(ctdb, destnode, 0,
3874 CTDB_CONTROL_ENABLE_SCRIPT, 0, data,
3875 NULL, NULL, &res, &timeout, NULL);
3876 if (ret != 0 || res != 0) {
3877 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for enablescript failed\n"));
3884 /* disable an eventscript
3886 int ctdb_ctrl_disablescript(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, const char *script)
3892 data.dsize = strlen(script) + 1;
3893 data.dptr = discard_const(script);
3895 ret = ctdb_control(ctdb, destnode, 0,
3896 CTDB_CONTROL_DISABLE_SCRIPT, 0, data,
3897 NULL, NULL, &res, &timeout, NULL);
3898 if (ret != 0 || res != 0) {
3899 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for disablescript failed\n"));
3907 int ctdb_ctrl_set_ban(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, struct ctdb_ban_time *bantime)
3913 data.dsize = sizeof(*bantime);
3914 data.dptr = (uint8_t *)bantime;
3916 ret = ctdb_control(ctdb, destnode, 0,
3917 CTDB_CONTROL_SET_BAN_STATE, 0, data,
3918 NULL, NULL, &res, &timeout, NULL);
3919 if (ret != 0 || res != 0) {
3920 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set ban state failed\n"));
3928 int ctdb_ctrl_get_ban(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, TALLOC_CTX *mem_ctx, struct ctdb_ban_time **bantime)
3933 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3935 ret = ctdb_control(ctdb, destnode, 0,
3936 CTDB_CONTROL_GET_BAN_STATE, 0, tdb_null,
3937 tmp_ctx, &outdata, &res, &timeout, NULL);
3938 if (ret != 0 || res != 0) {
3939 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set ban state failed\n"));
3940 talloc_free(tmp_ctx);
3944 *bantime = (struct ctdb_ban_time *)talloc_steal(mem_ctx, outdata.dptr);
3945 talloc_free(tmp_ctx);
3951 int ctdb_ctrl_set_db_priority(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, struct ctdb_db_priority *db_prio)
3956 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3958 data.dptr = (uint8_t*)db_prio;
3959 data.dsize = sizeof(*db_prio);
3961 ret = ctdb_control(ctdb, destnode, 0,
3962 CTDB_CONTROL_SET_DB_PRIORITY, 0, data,
3963 tmp_ctx, NULL, &res, &timeout, NULL);
3964 if (ret != 0 || res != 0) {
3965 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set_db_priority failed\n"));
3966 talloc_free(tmp_ctx);
3970 talloc_free(tmp_ctx);
3975 int ctdb_ctrl_get_db_priority(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t db_id, uint32_t *priority)
3980 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3982 data.dptr = (uint8_t*)&db_id;
3983 data.dsize = sizeof(db_id);
3985 ret = ctdb_control(ctdb, destnode, 0,
3986 CTDB_CONTROL_GET_DB_PRIORITY, 0, data,
3987 tmp_ctx, NULL, &res, &timeout, NULL);
3988 if (ret != 0 || res < 0) {
3989 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set_db_priority failed\n"));
3990 talloc_free(tmp_ctx);
3998 talloc_free(tmp_ctx);