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/>.
22 #include "system/network.h"
23 #include "system/filesys.h"
24 #include "system/locale.h"
30 #include "lib/tdb_wrap/tdb_wrap.h"
31 #include "lib/util/dlinklist.h"
32 #include "lib/util/time.h"
33 #include "lib/util/debug.h"
34 #include "lib/util/samba_util.h"
36 #include "ctdb_private.h"
37 #include "ctdb_client.h"
39 #include "common/reqid.h"
40 #include "common/system.h"
41 #include "common/common.h"
42 #include "common/logging.h"
45 allocate a packet for use in client<->daemon communication
47 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
49 enum ctdb_operation operation,
50 size_t length, size_t slength,
54 struct ctdb_req_header *hdr;
56 length = MAX(length, slength);
57 size = (length+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
59 hdr = (struct ctdb_req_header *)talloc_zero_size(mem_ctx, size);
61 DEBUG(DEBUG_ERR,("Unable to allocate packet for operation %u of length %u\n",
62 operation, (unsigned)length));
65 talloc_set_name_const(hdr, type);
67 hdr->operation = operation;
68 hdr->ctdb_magic = CTDB_MAGIC;
69 hdr->ctdb_version = CTDB_PROTOCOL;
70 hdr->srcnode = ctdb->pnn;
72 hdr->generation = ctdb->vnn_map->generation;
79 local version of ctdb_call
81 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
82 struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx,
83 TDB_DATA *data, bool updatetdb)
85 struct ctdb_call_info *c;
86 struct ctdb_registered_call *fn;
87 struct ctdb_context *ctdb = ctdb_db->ctdb;
89 c = talloc_zero(mem_ctx, struct ctdb_call_info);
90 CTDB_NO_MEMORY(ctdb, c);
93 c->call_data = &call->call_data;
94 c->record_data.dptr = talloc_memdup(c, data->dptr, data->dsize);
95 c->record_data.dsize = data->dsize;
96 CTDB_NO_MEMORY(ctdb, c->record_data.dptr);
99 for (fn=ctdb_db->calls;fn;fn=fn->next) {
100 if (fn->id == (uint32_t)call->call_id) {
105 ctdb_set_error(ctdb, "Unknown call id %u\n", call->call_id);
110 if (fn->fn(c) != 0) {
111 ctdb_set_error(ctdb, "ctdb_call %u failed\n", call->call_id);
116 /* we need to force the record to be written out if this was a remote access */
117 if (c->new_data == NULL) {
118 c->new_data = &c->record_data;
121 if (c->new_data && updatetdb) {
122 /* XXX check that we always have the lock here? */
123 if (ctdb_ltdb_store(ctdb_db, call->key, header, *c->new_data) != 0) {
124 ctdb_set_error(ctdb, "ctdb_call tdb_store failed\n");
131 call->reply_data = *c->reply_data;
133 talloc_steal(call, call->reply_data.dptr);
134 talloc_set_name_const(call->reply_data.dptr, __location__);
136 call->reply_data.dptr = NULL;
137 call->reply_data.dsize = 0;
139 call->status = c->status;
148 queue a packet for sending from client to daemon
150 static int ctdb_client_queue_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
152 return ctdb_queue_send(ctdb->daemon.queue, (uint8_t *)hdr, hdr->length);
157 called when a CTDB_REPLY_CALL packet comes in in the client
159 This packet comes in response to a CTDB_REQ_CALL request packet. It
160 contains any reply data from the call
162 static void ctdb_client_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
164 struct ctdb_reply_call_old *c = (struct ctdb_reply_call_old *)hdr;
165 struct ctdb_client_call_state *state;
167 state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_client_call_state);
169 DEBUG(DEBUG_ERR,(__location__ " reqid %u not found\n", hdr->reqid));
173 if (hdr->reqid != state->reqid) {
174 /* we found a record but it was the wrong one */
175 DEBUG(DEBUG_ERR, ("Dropped client call reply with reqid:%u\n",hdr->reqid));
179 state->call->reply_data.dptr = c->data;
180 state->call->reply_data.dsize = c->datalen;
181 state->call->status = c->status;
183 talloc_steal(state, c);
185 state->state = CTDB_CALL_DONE;
187 if (state->async.fn) {
188 state->async.fn(state);
192 void ctdb_request_message(struct ctdb_context *ctdb,
193 struct ctdb_req_header *hdr)
195 struct ctdb_req_message_old *c = (struct ctdb_req_message_old *)hdr;
198 data.dsize = c->datalen;
199 data.dptr = talloc_memdup(c, &c->data[0], c->datalen);
200 if (data.dptr == NULL) {
201 DEBUG(DEBUG_ERR, (__location__ " Memory allocation failure\n"));
205 srvid_dispatch(ctdb->srv, c->srvid, CTDB_SRVID_ALL, data);
208 static void ctdb_client_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
211 this is called in the client, when data comes in from the daemon
213 void ctdb_client_read_cb(uint8_t *data, size_t cnt, void *args)
215 struct ctdb_context *ctdb = talloc_get_type(args, struct ctdb_context);
216 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
219 /* place the packet as a child of a tmp_ctx. We then use
220 talloc_free() below to free it. If any of the calls want
221 to keep it, then they will steal it somewhere else, and the
222 talloc_free() will be a no-op */
223 tmp_ctx = talloc_new(ctdb);
224 talloc_steal(tmp_ctx, hdr);
227 DEBUG(DEBUG_CRIT,("Daemon has exited - shutting down client\n"));
231 if (cnt < sizeof(*hdr)) {
232 DEBUG(DEBUG_CRIT,("Bad packet length %u in client\n", (unsigned)cnt));
235 if (cnt != hdr->length) {
236 ctdb_set_error(ctdb, "Bad header length %u expected %u in client\n",
237 (unsigned)hdr->length, (unsigned)cnt);
241 if (hdr->ctdb_magic != CTDB_MAGIC) {
242 ctdb_set_error(ctdb, "Non CTDB packet rejected in client\n");
246 if (hdr->ctdb_version != CTDB_PROTOCOL) {
247 ctdb_set_error(ctdb, "Bad CTDB version 0x%x rejected in client\n", hdr->ctdb_version);
251 switch (hdr->operation) {
252 case CTDB_REPLY_CALL:
253 ctdb_client_reply_call(ctdb, hdr);
256 case CTDB_REQ_MESSAGE:
257 ctdb_request_message(ctdb, hdr);
260 case CTDB_REPLY_CONTROL:
261 ctdb_client_reply_control(ctdb, hdr);
265 DEBUG(DEBUG_CRIT,("bogus operation code:%u\n",hdr->operation));
269 talloc_free(tmp_ctx);
273 connect to a unix domain socket
275 int ctdb_socket_connect(struct ctdb_context *ctdb)
277 struct sockaddr_un addr;
280 memset(&addr, 0, sizeof(addr));
281 addr.sun_family = AF_UNIX;
282 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path)-1);
284 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
285 if (ctdb->daemon.sd == -1) {
286 DEBUG(DEBUG_ERR,(__location__ " Failed to open client socket. Errno:%s(%d)\n", strerror(errno), errno));
290 if (connect(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
293 "Failed to connect client socket to daemon (%s)\n",
295 close(ctdb->daemon.sd);
296 ctdb->daemon.sd = -1;
300 ret = set_blocking(ctdb->daemon.sd, false);
304 " failed to set socket non-blocking (%s)\n",
306 close(ctdb->daemon.sd);
307 ctdb->daemon.sd = -1;
311 set_close_on_exec(ctdb->daemon.sd);
313 ctdb->daemon.queue = ctdb_queue_setup(ctdb, ctdb, ctdb->daemon.sd,
315 ctdb_client_read_cb, ctdb, "to-ctdbd");
320 struct ctdb_record_handle {
321 struct ctdb_db_context *ctdb_db;
324 struct ctdb_ltdb_header header;
329 make a recv call to the local ctdb daemon - called from client context
331 This is called when the program wants to wait for a ctdb_call to complete and get the
332 results. This call will block unless the call has already completed.
334 int ctdb_call_recv(struct ctdb_client_call_state *state, struct ctdb_call *call)
340 while (state->state < CTDB_CALL_DONE) {
341 tevent_loop_once(state->ctdb_db->ctdb->ev);
343 if (state->state != CTDB_CALL_DONE) {
344 DEBUG(DEBUG_ERR,(__location__ " ctdb_call_recv failed\n"));
349 if (state->call->reply_data.dsize) {
350 call->reply_data.dptr = talloc_memdup(state->ctdb_db,
351 state->call->reply_data.dptr,
352 state->call->reply_data.dsize);
353 call->reply_data.dsize = state->call->reply_data.dsize;
355 call->reply_data.dptr = NULL;
356 call->reply_data.dsize = 0;
358 call->status = state->call->status;
368 destroy a ctdb_call in client
370 static int ctdb_client_call_destructor(struct ctdb_client_call_state *state)
372 reqid_remove(state->ctdb_db->ctdb->idr, state->reqid);
377 construct an event driven local ctdb_call
379 this is used so that locally processed ctdb_call requests are processed
380 in an event driven manner
382 static struct ctdb_client_call_state *ctdb_client_call_local_send(struct ctdb_db_context *ctdb_db,
383 struct ctdb_call *call,
384 struct ctdb_ltdb_header *header,
387 struct ctdb_client_call_state *state;
388 struct ctdb_context *ctdb = ctdb_db->ctdb;
391 state = talloc_zero(ctdb_db, struct ctdb_client_call_state);
392 CTDB_NO_MEMORY_NULL(ctdb, state);
393 state->call = talloc_zero(state, struct ctdb_call);
394 CTDB_NO_MEMORY_NULL(ctdb, state->call);
396 talloc_steal(state, data->dptr);
398 state->state = CTDB_CALL_DONE;
399 *(state->call) = *call;
400 state->ctdb_db = ctdb_db;
402 ret = ctdb_call_local(ctdb_db, state->call, header, state, data, true);
404 DEBUG(DEBUG_DEBUG,("ctdb_call_local() failed, ignoring return code %d\n", ret));
411 make a ctdb call to the local daemon - async send. Called from client context.
413 This constructs a ctdb_call request and queues it for processing.
414 This call never blocks.
416 struct ctdb_client_call_state *ctdb_call_send(struct ctdb_db_context *ctdb_db,
417 struct ctdb_call *call)
419 struct ctdb_client_call_state *state;
420 struct ctdb_context *ctdb = ctdb_db->ctdb;
421 struct ctdb_ltdb_header header;
425 struct ctdb_req_call_old *c;
427 /* if the domain socket is not yet open, open it */
428 if (ctdb->daemon.sd==-1) {
429 ctdb_socket_connect(ctdb);
432 ret = ctdb_ltdb_lock(ctdb_db, call->key);
434 DEBUG(DEBUG_ERR,(__location__ " Failed to get chainlock\n"));
438 ret = ctdb_ltdb_fetch(ctdb_db, call->key, &header, ctdb_db, &data);
440 if ((call->flags & CTDB_IMMEDIATE_MIGRATION) && (header.flags & CTDB_REC_RO_HAVE_DELEGATIONS)) {
444 if (ret == 0 && header.dmaster == ctdb->pnn) {
445 state = ctdb_client_call_local_send(ctdb_db, call, &header, &data);
446 talloc_free(data.dptr);
447 ctdb_ltdb_unlock(ctdb_db, call->key);
451 ctdb_ltdb_unlock(ctdb_db, call->key);
452 talloc_free(data.dptr);
454 state = talloc_zero(ctdb_db, struct ctdb_client_call_state);
456 DEBUG(DEBUG_ERR, (__location__ " failed to allocate state\n"));
459 state->call = talloc_zero(state, struct ctdb_call);
460 if (state->call == NULL) {
461 DEBUG(DEBUG_ERR, (__location__ " failed to allocate state->call\n"));
465 len = offsetof(struct ctdb_req_call_old, data) + call->key.dsize + call->call_data.dsize;
466 c = ctdbd_allocate_pkt(ctdb, state, CTDB_REQ_CALL, len, struct ctdb_req_call_old);
468 DEBUG(DEBUG_ERR, (__location__ " failed to allocate packet\n"));
472 state->reqid = reqid_new(ctdb->idr, state);
473 state->ctdb_db = ctdb_db;
474 talloc_set_destructor(state, ctdb_client_call_destructor);
476 c->hdr.reqid = state->reqid;
477 c->flags = call->flags;
478 c->db_id = ctdb_db->db_id;
479 c->callid = call->call_id;
481 c->keylen = call->key.dsize;
482 c->calldatalen = call->call_data.dsize;
483 memcpy(&c->data[0], call->key.dptr, call->key.dsize);
484 memcpy(&c->data[call->key.dsize],
485 call->call_data.dptr, call->call_data.dsize);
486 *(state->call) = *call;
487 state->call->call_data.dptr = &c->data[call->key.dsize];
488 state->call->key.dptr = &c->data[0];
490 state->state = CTDB_CALL_WAIT;
493 ctdb_client_queue_pkt(ctdb, &c->hdr);
500 full ctdb_call. Equivalent to a ctdb_call_send() followed by a ctdb_call_recv()
502 int ctdb_call(struct ctdb_db_context *ctdb_db, struct ctdb_call *call)
504 struct ctdb_client_call_state *state;
506 state = ctdb_call_send(ctdb_db, call);
507 return ctdb_call_recv(state, call);
512 tell the daemon what messaging srvid we will use, and register the message
513 handler function in the client
515 int ctdb_client_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
516 srvid_handler_fn handler,
522 res = ctdb_control(ctdb, CTDB_CURRENT_NODE, srvid,
523 CTDB_CONTROL_REGISTER_SRVID, 0,
524 tdb_null, NULL, NULL, &status, NULL, NULL);
525 if (res != 0 || status != 0) {
527 ("Failed to register srvid %llu\n",
528 (unsigned long long)srvid));
532 /* also need to register the handler with our own ctdb structure */
533 return srvid_register(ctdb->srv, ctdb, srvid, handler, private_data);
537 tell the daemon we no longer want a srvid
539 int ctdb_client_remove_message_handler(struct ctdb_context *ctdb,
540 uint64_t srvid, void *private_data)
545 res = ctdb_control(ctdb, CTDB_CURRENT_NODE, srvid,
546 CTDB_CONTROL_DEREGISTER_SRVID, 0,
547 tdb_null, NULL, NULL, &status, NULL, NULL);
548 if (res != 0 || status != 0) {
550 ("Failed to deregister srvid %llu\n",
551 (unsigned long long)srvid));
555 /* also need to register the handler with our own ctdb structure */
556 srvid_deregister(ctdb->srv, srvid, private_data);
561 send a message - from client context
563 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t pnn,
564 uint64_t srvid, TDB_DATA data)
566 struct ctdb_req_message_old *r;
569 len = offsetof(struct ctdb_req_message_old, data) + data.dsize;
570 r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE,
571 len, struct ctdb_req_message_old);
572 CTDB_NO_MEMORY(ctdb, r);
574 r->hdr.destnode = pnn;
576 r->datalen = data.dsize;
577 memcpy(&r->data[0], data.dptr, data.dsize);
579 res = ctdb_client_queue_pkt(ctdb, &r->hdr);
586 called when a control completes or timesout to invoke the callback
587 function the user provided
589 static void invoke_control_callback(struct tevent_context *ev,
590 struct tevent_timer *te,
591 struct timeval t, void *private_data)
593 struct ctdb_client_control_state *state;
594 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
597 state = talloc_get_type(private_data, struct ctdb_client_control_state);
598 talloc_steal(tmp_ctx, state);
600 ret = ctdb_control_recv(state->ctdb, state, state,
605 DEBUG(DEBUG_DEBUG,("ctdb_control_recv() failed, ignoring return code %d\n", ret));
608 talloc_free(tmp_ctx);
612 called when a CTDB_REPLY_CONTROL packet comes in in the client
614 This packet comes in response to a CTDB_REQ_CONTROL request packet. It
615 contains any reply data from the control
617 static void ctdb_client_reply_control(struct ctdb_context *ctdb,
618 struct ctdb_req_header *hdr)
620 struct ctdb_reply_control_old *c = (struct ctdb_reply_control_old *)hdr;
621 struct ctdb_client_control_state *state;
623 state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_client_control_state);
625 DEBUG(DEBUG_ERR,(__location__ " reqid %u not found\n", hdr->reqid));
629 if (hdr->reqid != state->reqid) {
630 /* we found a record but it was the wrong one */
631 DEBUG(DEBUG_ERR, ("Dropped orphaned reply control with reqid:%u\n",hdr->reqid));
635 state->outdata.dptr = c->data;
636 state->outdata.dsize = c->datalen;
637 state->status = c->status;
639 state->errormsg = talloc_strndup(state,
640 (char *)&c->data[c->datalen],
644 /* state->outdata now uses resources from c so we don't want c
645 to just dissappear from under us while state is still alive
647 talloc_steal(state, c);
649 state->state = CTDB_CONTROL_DONE;
651 /* if we had a callback registered for this control, pull the response
652 and call the callback.
654 if (state->async.fn) {
655 tevent_add_timer(ctdb->ev, state, timeval_zero(),
656 invoke_control_callback, state);
662 destroy a ctdb_control in client
664 static int ctdb_client_control_destructor(struct ctdb_client_control_state *state)
666 reqid_remove(state->ctdb->idr, state->reqid);
671 /* time out handler for ctdb_control */
672 static void control_timeout_func(struct tevent_context *ev,
673 struct tevent_timer *te,
674 struct timeval t, void *private_data)
676 struct ctdb_client_control_state *state = talloc_get_type(private_data, struct ctdb_client_control_state);
678 DEBUG(DEBUG_ERR,(__location__ " control timed out. reqid:%u opcode:%u "
679 "dstnode:%u\n", state->reqid, state->c->opcode,
680 state->c->hdr.destnode));
682 state->state = CTDB_CONTROL_TIMEOUT;
684 /* if we had a callback registered for this control, pull the response
685 and call the callback.
687 if (state->async.fn) {
688 tevent_add_timer(state->ctdb->ev, state, timeval_zero(),
689 invoke_control_callback, state);
693 /* async version of send control request */
694 struct ctdb_client_control_state *ctdb_control_send(struct ctdb_context *ctdb,
695 uint32_t destnode, uint64_t srvid,
696 uint32_t opcode, uint32_t flags, TDB_DATA data,
698 struct timeval *timeout,
701 struct ctdb_client_control_state *state;
703 struct ctdb_req_control_old *c;
710 /* if the domain socket is not yet open, open it */
711 if (ctdb->daemon.sd==-1) {
712 ctdb_socket_connect(ctdb);
715 state = talloc_zero(mem_ctx, struct ctdb_client_control_state);
716 CTDB_NO_MEMORY_NULL(ctdb, state);
719 state->reqid = reqid_new(ctdb->idr, state);
720 state->state = CTDB_CONTROL_WAIT;
721 state->errormsg = NULL;
723 talloc_set_destructor(state, ctdb_client_control_destructor);
725 len = offsetof(struct ctdb_req_control_old, data) + data.dsize;
726 c = ctdbd_allocate_pkt(ctdb, state, CTDB_REQ_CONTROL,
727 len, struct ctdb_req_control_old);
729 CTDB_NO_MEMORY_NULL(ctdb, c);
730 c->hdr.reqid = state->reqid;
731 c->hdr.destnode = destnode;
736 c->datalen = data.dsize;
738 memcpy(&c->data[0], data.dptr, data.dsize);
742 if (timeout && !timeval_is_zero(timeout)) {
743 tevent_add_timer(ctdb->ev, state, *timeout,
744 control_timeout_func, state);
747 ret = ctdb_client_queue_pkt(ctdb, &(c->hdr));
753 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
762 /* async version of receive control reply */
763 int ctdb_control_recv(struct ctdb_context *ctdb,
764 struct ctdb_client_control_state *state,
766 TDB_DATA *outdata, int32_t *status, char **errormsg)
770 if (status != NULL) {
773 if (errormsg != NULL) {
781 /* prevent double free of state */
782 tmp_ctx = talloc_new(ctdb);
783 talloc_steal(tmp_ctx, state);
785 /* loop one event at a time until we either timeout or the control
788 while (state->state == CTDB_CONTROL_WAIT) {
789 tevent_loop_once(ctdb->ev);
792 if (state->state != CTDB_CONTROL_DONE) {
793 DEBUG(DEBUG_ERR,(__location__ " ctdb_control_recv failed\n"));
794 if (state->async.fn) {
795 state->async.fn(state);
797 talloc_free(tmp_ctx);
801 if (state->errormsg) {
802 int s = (state->status == 0 ? -1 : state->status);
803 DEBUG(DEBUG_ERR,("ctdb_control error: '%s'\n", state->errormsg));
805 (*errormsg) = talloc_move(mem_ctx, &state->errormsg);
807 if (state->async.fn) {
808 state->async.fn(state);
810 talloc_free(tmp_ctx);
815 *outdata = state->outdata;
816 outdata->dptr = talloc_memdup(mem_ctx, outdata->dptr, outdata->dsize);
820 *status = state->status;
823 if (state->async.fn) {
824 state->async.fn(state);
827 talloc_free(tmp_ctx);
834 send a ctdb control message
835 timeout specifies how long we should wait for a reply.
836 if timeout is NULL we wait indefinitely
838 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid,
839 uint32_t opcode, uint32_t flags, TDB_DATA data,
840 TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
841 struct timeval *timeout,
844 struct ctdb_client_control_state *state;
846 state = ctdb_control_send(ctdb, destnode, srvid, opcode,
847 flags, data, mem_ctx,
850 /* FIXME: Error conditions in ctdb_control_send return NULL without
851 * setting errormsg. So, there is no way to distinguish between sucess
852 * and failure when CTDB_CTRL_FLAG_NOREPLY is set */
853 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
854 if (status != NULL) {
860 return ctdb_control_recv(ctdb, state, mem_ctx, outdata, status,
865 get vnn map from a remote node
867 int ctdb_ctrl_getvnnmap(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, TALLOC_CTX *mem_ctx, struct ctdb_vnn_map **vnnmap)
872 struct ctdb_vnn_map_wire *map;
874 ret = ctdb_control(ctdb, destnode, 0,
875 CTDB_CONTROL_GETVNNMAP, 0, tdb_null,
876 mem_ctx, &outdata, &res, &timeout, NULL);
877 if (ret != 0 || res != 0) {
878 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getvnnmap failed\n"));
882 map = (struct ctdb_vnn_map_wire *)outdata.dptr;
883 if (outdata.dsize < offsetof(struct ctdb_vnn_map_wire, map) ||
884 outdata.dsize != map->size*sizeof(uint32_t) + offsetof(struct ctdb_vnn_map_wire, map)) {
885 DEBUG(DEBUG_ERR,("Bad vnn map size received in ctdb_ctrl_getvnnmap\n"));
889 (*vnnmap) = talloc(mem_ctx, struct ctdb_vnn_map);
890 CTDB_NO_MEMORY(ctdb, *vnnmap);
891 (*vnnmap)->generation = map->generation;
892 (*vnnmap)->size = map->size;
893 (*vnnmap)->map = talloc_array(*vnnmap, uint32_t, map->size);
895 CTDB_NO_MEMORY(ctdb, (*vnnmap)->map);
896 memcpy((*vnnmap)->map, map->map, sizeof(uint32_t)*map->size);
897 talloc_free(outdata.dptr);
904 get the recovery mode of a remote node
906 struct ctdb_client_control_state *
907 ctdb_ctrl_getrecmode_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode)
909 return ctdb_control_send(ctdb, destnode, 0,
910 CTDB_CONTROL_GET_RECMODE, 0, tdb_null,
911 mem_ctx, &timeout, NULL);
914 int ctdb_ctrl_getrecmode_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmode)
919 ret = ctdb_control_recv(ctdb, state, mem_ctx, NULL, &res, NULL);
921 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_getrecmode_recv failed\n"));
926 *recmode = (uint32_t)res;
932 int ctdb_ctrl_getrecmode(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmode)
934 struct ctdb_client_control_state *state;
936 state = ctdb_ctrl_getrecmode_send(ctdb, mem_ctx, timeout, destnode);
937 return ctdb_ctrl_getrecmode_recv(ctdb, mem_ctx, state, recmode);
944 set the recovery mode of a remote node
946 int ctdb_ctrl_setrecmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmode)
952 data.dsize = sizeof(uint32_t);
953 data.dptr = (unsigned char *)&recmode;
955 ret = ctdb_control(ctdb, destnode, 0,
956 CTDB_CONTROL_SET_RECMODE, 0, data,
957 NULL, NULL, &res, &timeout, NULL);
958 if (ret != 0 || res != 0) {
959 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setrecmode failed\n"));
969 get the recovery master of a remote node
971 struct ctdb_client_control_state *
972 ctdb_ctrl_getrecmaster_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
973 struct timeval timeout, uint32_t destnode)
975 return ctdb_control_send(ctdb, destnode, 0,
976 CTDB_CONTROL_GET_RECMASTER, 0, tdb_null,
977 mem_ctx, &timeout, NULL);
980 int ctdb_ctrl_getrecmaster_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmaster)
985 ret = ctdb_control_recv(ctdb, state, mem_ctx, NULL, &res, NULL);
987 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_getrecmaster_recv failed\n"));
992 *recmaster = (uint32_t)res;
998 int ctdb_ctrl_getrecmaster(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmaster)
1000 struct ctdb_client_control_state *state;
1002 state = ctdb_ctrl_getrecmaster_send(ctdb, mem_ctx, timeout, destnode);
1003 return ctdb_ctrl_getrecmaster_recv(ctdb, mem_ctx, state, recmaster);
1008 set the recovery master of a remote node
1010 int ctdb_ctrl_setrecmaster(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmaster)
1017 data.dsize = sizeof(uint32_t);
1018 data.dptr = (unsigned char *)&recmaster;
1020 ret = ctdb_control(ctdb, destnode, 0,
1021 CTDB_CONTROL_SET_RECMASTER, 0, data,
1022 NULL, NULL, &res, &timeout, NULL);
1023 if (ret != 0 || res != 0) {
1024 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for setrecmaster failed\n"));
1033 get a list of databases off a remote node
1035 int ctdb_ctrl_getdbmap(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode,
1036 TALLOC_CTX *mem_ctx, struct ctdb_dbid_map_old **dbmap)
1042 ret = ctdb_control(ctdb, destnode, 0,
1043 CTDB_CONTROL_GET_DBMAP, 0, tdb_null,
1044 mem_ctx, &outdata, &res, &timeout, NULL);
1045 if (ret != 0 || res != 0) {
1046 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getdbmap failed ret:%d res:%d\n", ret, res));
1050 *dbmap = (struct ctdb_dbid_map_old *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
1051 talloc_free(outdata.dptr);
1057 get a list of nodes (vnn and flags ) from a remote node
1059 int ctdb_ctrl_getnodemap(struct ctdb_context *ctdb,
1060 struct timeval timeout, uint32_t destnode,
1061 TALLOC_CTX *mem_ctx, struct ctdb_node_map_old **nodemap)
1067 ret = ctdb_control(ctdb, destnode, 0,
1068 CTDB_CONTROL_GET_NODEMAP, 0, tdb_null,
1069 mem_ctx, &outdata, &res, &timeout, NULL);
1070 if (ret != 0 || res != 0 || outdata.dsize == 0) {
1071 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getnodes failed ret:%d res:%d\n", ret, res));
1075 *nodemap = (struct ctdb_node_map_old *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
1076 talloc_free(outdata.dptr);
1080 int ctdb_ctrl_get_runstate(struct ctdb_context *ctdb,
1081 struct timeval timeout,
1089 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_GET_RUNSTATE, 0,
1090 tdb_null, ctdb, &outdata, &res, &timeout, NULL);
1091 if (ret != 0 || res != 0) {
1092 DEBUG(DEBUG_ERR,("ctdb_control for get_runstate failed\n"));
1093 return ret != 0 ? ret : res;
1096 if (outdata.dsize != sizeof(uint32_t)) {
1097 DEBUG(DEBUG_ERR,("Invalid return data in get_runstate\n"));
1098 talloc_free(outdata.dptr);
1102 if (runstate != NULL) {
1103 *runstate = *(uint32_t *)outdata.dptr;
1105 talloc_free(outdata.dptr);
1111 find the real path to a ltdb
1113 int ctdb_ctrl_getdbpath(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx,
1120 data.dptr = (uint8_t *)&dbid;
1121 data.dsize = sizeof(dbid);
1123 ret = ctdb_control(ctdb, destnode, 0,
1124 CTDB_CONTROL_GETDBPATH, 0, data,
1125 mem_ctx, &data, &res, &timeout, NULL);
1126 if (ret != 0 || res != 0) {
1130 (*path) = talloc_strndup(mem_ctx, (const char *)data.dptr, data.dsize);
1131 if ((*path) == NULL) {
1135 talloc_free(data.dptr);
1141 find the name of a db
1143 int ctdb_ctrl_getdbname(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx,
1150 data.dptr = (uint8_t *)&dbid;
1151 data.dsize = sizeof(dbid);
1153 ret = ctdb_control(ctdb, destnode, 0,
1154 CTDB_CONTROL_GET_DBNAME, 0, data,
1155 mem_ctx, &data, &res, &timeout, NULL);
1156 if (ret != 0 || res != 0) {
1160 (*name) = talloc_strndup(mem_ctx, (const char *)data.dptr, data.dsize);
1161 if ((*name) == NULL) {
1165 talloc_free(data.dptr);
1173 int ctdb_ctrl_createdb(struct ctdb_context *ctdb, struct timeval timeout,
1174 uint32_t destnode, TALLOC_CTX *mem_ctx,
1175 const char *name, uint8_t db_flags, uint32_t *db_id)
1182 data.dptr = discard_const(name);
1183 data.dsize = strlen(name)+1;
1185 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
1186 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
1187 } else if (db_flags & CTDB_DB_FLAGS_REPLICATED) {
1188 opcode = CTDB_CONTROL_DB_ATTACH_REPLICATED;
1190 opcode = CTDB_CONTROL_DB_ATTACH;
1193 ret = ctdb_control(ctdb, destnode, 0, opcode, 0, data,
1194 mem_ctx, &data, &res, &timeout, NULL);
1196 if (ret != 0 || res != 0) {
1200 if (data.dsize != sizeof(uint32_t)) {
1201 TALLOC_FREE(data.dptr);
1204 if (db_id != NULL) {
1205 *db_id = *(uint32_t *)data.dptr;
1207 talloc_free(data.dptr);
1213 get debug level on a node
1215 int ctdb_ctrl_get_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, int32_t *level)
1221 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_GET_DEBUG, 0, tdb_null,
1222 ctdb, &data, &res, NULL, NULL);
1223 if (ret != 0 || res != 0) {
1226 if (data.dsize != sizeof(int32_t)) {
1227 DEBUG(DEBUG_ERR,("Bad control reply size in ctdb_get_debuglevel (got %u)\n",
1228 (unsigned)data.dsize));
1231 *level = *(int32_t *)data.dptr;
1232 talloc_free(data.dptr);
1239 int ctdb_ctrl_db_open_flags(struct ctdb_context *ctdb, uint32_t db_id,
1242 TDB_DATA indata, outdata;
1246 indata.dptr = (uint8_t *)&db_id;
1247 indata.dsize = sizeof(db_id);
1249 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0,
1250 CTDB_CONTROL_DB_OPEN_FLAGS, 0, indata,
1251 ctdb, &outdata, &res, NULL, NULL);
1252 if (ret != 0 || res != 0) {
1253 D_ERR("ctdb control for db open flags failed\n");
1257 if (outdata.dsize != sizeof(int32_t)) {
1258 D_ERR(__location__ " expected %zi bytes, received %zi bytes\n",
1259 sizeof(int32_t), outdata.dsize);
1260 talloc_free(outdata.dptr);
1264 *tdb_flags = *(int32_t *)outdata.dptr;
1265 talloc_free(outdata.dptr);
1270 attach to a specific database - client call
1272 struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb,
1273 struct timeval timeout,
1277 struct ctdb_db_context *ctdb_db;
1281 ctdb_db = ctdb_db_handle(ctdb, name);
1286 ctdb_db = talloc_zero(ctdb, struct ctdb_db_context);
1287 CTDB_NO_MEMORY_NULL(ctdb, ctdb_db);
1289 ctdb_db->ctdb = ctdb;
1290 ctdb_db->db_name = talloc_strdup(ctdb_db, name);
1291 CTDB_NO_MEMORY_NULL(ctdb, ctdb_db->db_name);
1293 /* tell ctdb daemon to attach */
1294 ret = ctdb_ctrl_createdb(ctdb, timeout, CTDB_CURRENT_NODE,
1295 ctdb_db, name, db_flags, &ctdb_db->db_id);
1297 DEBUG(DEBUG_ERR,("Failed to attach to database '%s'\n", name));
1298 talloc_free(ctdb_db);
1302 ret = ctdb_ctrl_getdbpath(ctdb, timeout, CTDB_CURRENT_NODE, ctdb_db->db_id, ctdb_db, &ctdb_db->db_path);
1304 DEBUG(DEBUG_ERR,("Failed to get dbpath for database '%s'\n", name));
1305 talloc_free(ctdb_db);
1309 ret = ctdb_ctrl_db_open_flags(ctdb, ctdb_db->db_id, &tdb_flags);
1311 D_ERR("Failed to get tdb_flags for database '%s'\n", name);
1312 talloc_free(ctdb_db);
1316 ctdb_db->ltdb = tdb_wrap_open(ctdb_db, ctdb_db->db_path, 0, tdb_flags,
1318 if (ctdb_db->ltdb == NULL) {
1319 ctdb_set_error(ctdb, "Failed to open tdb '%s'\n", ctdb_db->db_path);
1320 talloc_free(ctdb_db);
1324 ctdb_db->db_flags = db_flags;
1326 DLIST_ADD(ctdb->db_list, ctdb_db);
1328 /* add well known functions */
1329 ctdb_set_call(ctdb_db, ctdb_null_func, CTDB_NULL_FUNC);
1330 ctdb_set_call(ctdb_db, ctdb_fetch_func, CTDB_FETCH_FUNC);
1331 ctdb_set_call(ctdb_db, ctdb_fetch_with_header_func, CTDB_FETCH_WITH_HEADER_FUNC);
1337 setup a call for a database
1339 int ctdb_set_call(struct ctdb_db_context *ctdb_db, ctdb_fn_t fn, uint32_t id)
1341 struct ctdb_registered_call *call;
1343 /* register locally */
1344 call = talloc(ctdb_db, struct ctdb_registered_call);
1348 DLIST_ADD(ctdb_db->calls, call);
1352 /* Freeze all databases */
1353 int ctdb_ctrl_freeze(struct ctdb_context *ctdb, struct timeval timeout,
1359 ret = ctdb_control(ctdb, destnode, 0,
1360 CTDB_CONTROL_FREEZE, 0, tdb_null,
1361 NULL, NULL, &res, &timeout, NULL);
1362 if (ret != 0 || res != 0) {
1363 DEBUG(DEBUG_ERR, ("ctdb_ctrl_freeze_priority failed\n"));
1371 get pnn of a node, or -1
1373 int ctdb_ctrl_getpnn(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode)
1378 ret = ctdb_control(ctdb, destnode, 0,
1379 CTDB_CONTROL_GET_PNN, 0, tdb_null,
1380 NULL, NULL, &res, &timeout, NULL);
1382 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for getpnn failed\n"));
1389 int ctdb_ctrl_get_public_ips_flags(struct ctdb_context *ctdb,
1390 struct timeval timeout, uint32_t destnode,
1391 TALLOC_CTX *mem_ctx,
1393 struct ctdb_public_ip_list_old **ips)
1399 ret = ctdb_control(ctdb, destnode, 0,
1400 CTDB_CONTROL_GET_PUBLIC_IPS, flags, tdb_null,
1401 mem_ctx, &outdata, &res, &timeout, NULL);
1402 if (ret != 0 || res != 0) {
1403 DEBUG(DEBUG_ERR,(__location__
1404 " ctdb_control for getpublicips failed ret:%d res:%d\n",
1409 *ips = (struct ctdb_public_ip_list_old *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
1410 talloc_free(outdata.dptr);
1415 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb,
1416 struct timeval timeout, uint32_t destnode,
1417 TALLOC_CTX *mem_ctx,
1418 struct ctdb_public_ip_list_old **ips)
1420 return ctdb_ctrl_get_public_ips_flags(ctdb, timeout,
1425 int ctdb_ctrl_get_ifaces(struct ctdb_context *ctdb,
1426 struct timeval timeout, uint32_t destnode,
1427 TALLOC_CTX *mem_ctx,
1428 struct ctdb_iface_list_old **_ifaces)
1433 struct ctdb_iface_list_old *ifaces;
1437 ret = ctdb_control(ctdb, destnode, 0,
1438 CTDB_CONTROL_GET_IFACES, 0, tdb_null,
1439 mem_ctx, &outdata, &res, &timeout, NULL);
1440 if (ret != 0 || res != 0) {
1441 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get ifaces "
1442 "failed ret:%d res:%d\n",
1447 len = offsetof(struct ctdb_iface_list_old, ifaces);
1448 if (len > outdata.dsize) {
1449 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get ifaces "
1450 "returned invalid data with size %u > %u\n",
1451 (unsigned int)outdata.dsize,
1452 (unsigned int)len));
1453 dump_data(DEBUG_DEBUG, outdata.dptr, outdata.dsize);
1457 ifaces = (struct ctdb_iface_list_old *)outdata.dptr;
1458 len += ifaces->num*sizeof(struct ctdb_iface);
1460 if (len > outdata.dsize) {
1461 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get ifaces "
1462 "returned invalid data with size %u > %u\n",
1463 (unsigned int)outdata.dsize,
1464 (unsigned int)len));
1465 dump_data(DEBUG_DEBUG, outdata.dptr, outdata.dsize);
1469 /* make sure we null terminate the returned strings */
1470 for (i=0; i < ifaces->num; i++) {
1471 ifaces->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
1474 *_ifaces = (struct ctdb_iface_list_old *)talloc_memdup(mem_ctx,
1477 talloc_free(outdata.dptr);
1478 if (*_ifaces == NULL) {
1479 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get ifaces "
1480 "talloc_memdup size %u failed\n",
1481 (unsigned int)outdata.dsize));
1489 set/clear the permanent disabled bit on a remote node
1491 int ctdb_ctrl_modflags(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode,
1492 uint32_t set, uint32_t clear)
1496 struct ctdb_node_map_old *nodemap=NULL;
1497 struct ctdb_node_flag_change c;
1498 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1503 /* find the recovery master */
1504 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, timeout, CTDB_CURRENT_NODE, &recmaster);
1506 DEBUG(DEBUG_ERR, (__location__ " Unable to get recmaster from local node\n"));
1507 talloc_free(tmp_ctx);
1512 /* read the node flags from the recmaster */
1513 ret = ctdb_ctrl_getnodemap(ctdb, timeout, recmaster, tmp_ctx, &nodemap);
1515 DEBUG(DEBUG_ERR, (__location__ " Unable to get nodemap from node %u\n", destnode));
1516 talloc_free(tmp_ctx);
1519 if (destnode >= nodemap->num) {
1520 DEBUG(DEBUG_ERR,(__location__ " Nodemap from recmaster does not contain node %d\n", destnode));
1521 talloc_free(tmp_ctx);
1526 c.old_flags = nodemap->nodes[destnode].flags;
1527 c.new_flags = c.old_flags;
1529 c.new_flags &= ~clear;
1531 data.dsize = sizeof(c);
1532 data.dptr = (unsigned char *)&c;
1534 /* send the flags update to all connected nodes */
1535 nodes = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
1537 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_MODIFY_FLAGS,
1539 timeout, false, data,
1542 DEBUG(DEBUG_ERR, (__location__ " Unable to update nodeflags on remote nodes\n"));
1544 talloc_free(tmp_ctx);
1548 talloc_free(tmp_ctx);
1556 int ctdb_ctrl_get_all_tunables(struct ctdb_context *ctdb,
1557 struct timeval timeout,
1559 struct ctdb_tunable_list *tunables)
1565 ret = ctdb_control(ctdb, destnode, 0, CTDB_CONTROL_GET_ALL_TUNABLES, 0, tdb_null, ctdb,
1566 &outdata, &res, &timeout, NULL);
1567 if (ret != 0 || res != 0) {
1568 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for get all tunables failed\n"));
1572 if (outdata.dsize != sizeof(*tunables)) {
1573 DEBUG(DEBUG_ERR,(__location__ " bad data size %u in ctdb_ctrl_get_all_tunables should be %u\n",
1574 (unsigned)outdata.dsize, (unsigned)sizeof(*tunables)));
1578 *tunables = *(struct ctdb_tunable_list *)outdata.dptr;
1579 talloc_free(outdata.dptr);
1586 void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags)
1588 ctdb->flags |= flags;
1591 const char *ctdb_get_socketname(struct ctdb_context *ctdb)
1593 return ctdb->daemon.name;
1597 return the pnn of this node
1599 uint32_t ctdb_get_pnn(struct ctdb_context *ctdb)
1605 callback for the async helpers used when sending the same control
1606 to multiple nodes in parallell.
1608 static void async_callback(struct ctdb_client_control_state *state)
1610 struct client_async_data *data = talloc_get_type(state->async.private_data, struct client_async_data);
1611 struct ctdb_context *ctdb = talloc_get_type(state->ctdb, struct ctdb_context);
1615 uint32_t destnode = state->c->hdr.destnode;
1618 outdata.dptr = NULL;
1620 /* one more node has responded with recmode data */
1623 /* if we failed to push the db, then return an error and let
1624 the main loop try again.
1626 if (state->state != CTDB_CONTROL_DONE) {
1627 if ( !data->dont_log_errors) {
1628 DEBUG(DEBUG_ERR,("Async operation failed with state %d, opcode:%u\n", state->state, data->opcode));
1631 if (state->state == CTDB_CONTROL_TIMEOUT) {
1636 if (data->fail_callback) {
1637 data->fail_callback(ctdb, destnode, res, outdata,
1638 data->callback_data);
1643 state->async.fn = NULL;
1645 ret = ctdb_control_recv(ctdb, state, data, &outdata, &res, NULL);
1646 if ((ret != 0) || (res != 0)) {
1647 if ( !data->dont_log_errors) {
1648 DEBUG(DEBUG_ERR,("Async operation failed with ret=%d res=%d opcode=%u\n", ret, (int)res, data->opcode));
1651 if (data->fail_callback) {
1652 data->fail_callback(ctdb, destnode, res, outdata,
1653 data->callback_data);
1656 if ((ret == 0) && (data->callback != NULL)) {
1657 data->callback(ctdb, destnode, res, outdata,
1658 data->callback_data);
1663 void ctdb_client_async_add(struct client_async_data *data, struct ctdb_client_control_state *state)
1665 /* set up the callback functions */
1666 state->async.fn = async_callback;
1667 state->async.private_data = data;
1669 /* one more control to wait for to complete */
1674 /* wait for up to the maximum number of seconds allowed
1675 or until all nodes we expect a response from has replied
1677 int ctdb_client_async_wait(struct ctdb_context *ctdb, struct client_async_data *data)
1679 while (data->count > 0) {
1680 tevent_loop_once(ctdb->ev);
1682 if (data->fail_count != 0) {
1683 if (!data->dont_log_errors) {
1684 DEBUG(DEBUG_ERR,("Async wait failed - fail_count=%u\n",
1694 perform a simple control on the listed nodes
1695 The control cannot return data
1697 int ctdb_client_async_control(struct ctdb_context *ctdb,
1698 enum ctdb_controls opcode,
1701 struct timeval timeout,
1702 bool dont_log_errors,
1704 client_async_callback client_callback,
1705 client_async_callback fail_callback,
1706 void *callback_data)
1708 struct client_async_data *async_data;
1709 struct ctdb_client_control_state *state;
1712 async_data = talloc_zero(ctdb, struct client_async_data);
1713 CTDB_NO_MEMORY_FATAL(ctdb, async_data);
1714 async_data->dont_log_errors = dont_log_errors;
1715 async_data->callback = client_callback;
1716 async_data->fail_callback = fail_callback;
1717 async_data->callback_data = callback_data;
1718 async_data->opcode = opcode;
1720 num_nodes = talloc_get_size(nodes) / sizeof(uint32_t);
1722 /* loop over all nodes and send an async control to each of them */
1723 for (j=0; j<num_nodes; j++) {
1724 uint32_t pnn = nodes[j];
1726 state = ctdb_control_send(ctdb, pnn, srvid, opcode,
1727 0, data, async_data, &timeout, NULL);
1728 if (state == NULL) {
1729 DEBUG(DEBUG_ERR,(__location__ " Failed to call async control %u\n", (unsigned)opcode));
1730 talloc_free(async_data);
1734 ctdb_client_async_add(async_data, state);
1737 if (ctdb_client_async_wait(ctdb, async_data) != 0) {
1738 talloc_free(async_data);
1742 talloc_free(async_data);
1746 uint32_t *list_of_vnnmap_nodes(struct ctdb_context *ctdb,
1747 struct ctdb_vnn_map *vnn_map,
1748 TALLOC_CTX *mem_ctx,
1751 unsigned int i, j, num_nodes;
1754 for (i=num_nodes=0;i<vnn_map->size;i++) {
1755 if (vnn_map->map[i] == ctdb->pnn && !include_self) {
1761 nodes = talloc_array(mem_ctx, uint32_t, num_nodes);
1762 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
1764 for (i=j=0;i<vnn_map->size;i++) {
1765 if (vnn_map->map[i] == ctdb->pnn && !include_self) {
1768 nodes[j++] = vnn_map->map[i];
1774 /* Get list of nodes not including those with flags specified by mask */
1775 static uint32_t *list_of_nodes(struct ctdb_context *ctdb,
1776 struct ctdb_node_map_old *node_map,
1777 TALLOC_CTX *mem_ctx,
1781 unsigned int i, j, num_nodes;
1782 uint32_t exclude_pnn;
1785 exclude_pnn = include_self ? CTDB_UNKNOWN_PNN : ctdb->pnn;
1787 for (i=num_nodes=0;i<node_map->num;i++) {
1788 if (node_map->nodes[i].flags & mask) {
1791 if (node_map->nodes[i].pnn == exclude_pnn) {
1797 nodes = talloc_array(mem_ctx, uint32_t, num_nodes);
1798 CTDB_NO_MEMORY_FATAL(ctdb, nodes);
1800 for (i=j=0;i<node_map->num;i++) {
1801 if (node_map->nodes[i].flags & mask) {
1804 if (node_map->nodes[i].pnn == exclude_pnn) {
1807 nodes[j++] = node_map->nodes[i].pnn;
1813 uint32_t *list_of_active_nodes(struct ctdb_context *ctdb,
1814 struct ctdb_node_map_old *node_map,
1815 TALLOC_CTX *mem_ctx,
1818 return list_of_nodes(ctdb,
1821 NODE_FLAGS_INACTIVE,
1825 uint32_t *list_of_connected_nodes(struct ctdb_context *ctdb,
1826 struct ctdb_node_map_old *node_map,
1827 TALLOC_CTX *mem_ctx,
1830 return list_of_nodes(ctdb,
1833 NODE_FLAGS_DISCONNECTED,
1838 get capabilities of a remote node
1840 struct ctdb_client_control_state *
1841 ctdb_ctrl_getcapabilities_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode)
1843 return ctdb_control_send(ctdb, destnode, 0,
1844 CTDB_CONTROL_GET_CAPABILITIES, 0, tdb_null,
1845 mem_ctx, &timeout, NULL);
1848 int ctdb_ctrl_getcapabilities_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *capabilities)
1854 ret = ctdb_control_recv(ctdb, state, mem_ctx, &outdata, &res, NULL);
1855 if ( (ret != 0) || (res != 0) ) {
1856 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_getcapabilities_recv failed\n"));
1861 *capabilities = *((uint32_t *)outdata.dptr);
1867 int ctdb_ctrl_getcapabilities(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *capabilities)
1869 struct ctdb_client_control_state *state;
1870 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1873 state = ctdb_ctrl_getcapabilities_send(ctdb, tmp_ctx, timeout, destnode);
1874 ret = ctdb_ctrl_getcapabilities_recv(ctdb, tmp_ctx, state, capabilities);
1875 talloc_free(tmp_ctx);
1879 static void get_capabilities_callback(struct ctdb_context *ctdb,
1880 uint32_t node_pnn, int32_t res,
1881 TDB_DATA outdata, void *callback_data)
1883 struct ctdb_node_capabilities *caps =
1884 talloc_get_type(callback_data,
1885 struct ctdb_node_capabilities);
1887 if ( (outdata.dsize != sizeof(uint32_t)) || (outdata.dptr == NULL) ) {
1888 DEBUG(DEBUG_ERR, (__location__ " Invalid length/pointer for getcap callback : %u %p\n", (unsigned)outdata.dsize, outdata.dptr));
1892 if (node_pnn >= talloc_array_length(caps)) {
1894 (__location__ " unexpected PNN %u\n", node_pnn));
1898 caps[node_pnn].retrieved = true;
1899 caps[node_pnn].capabilities = *((uint32_t *)outdata.dptr);
1902 struct ctdb_node_capabilities *
1903 ctdb_get_capabilities(struct ctdb_context *ctdb,
1904 TALLOC_CTX *mem_ctx,
1905 struct timeval timeout,
1906 struct ctdb_node_map_old *nodemap)
1910 struct ctdb_node_capabilities *ret;
1912 nodes = list_of_active_nodes(ctdb, nodemap, mem_ctx, true);
1914 ret = talloc_array(mem_ctx, struct ctdb_node_capabilities,
1916 CTDB_NO_MEMORY_NULL(ctdb, ret);
1917 /* Prepopulate the expected PNNs */
1918 for (i = 0; i < talloc_array_length(ret); i++) {
1919 ret[i].retrieved = false;
1922 res = ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_CAPABILITIES,
1925 get_capabilities_callback, NULL,
1929 (__location__ " Failed to read node capabilities.\n"));
1937 ctdb_get_node_capabilities(struct ctdb_node_capabilities *caps,
1940 if (pnn < talloc_array_length(caps) && caps[pnn].retrieved) {
1941 return &caps[pnn].capabilities;
1947 bool ctdb_node_has_capabilities(struct ctdb_node_capabilities *caps,
1949 uint32_t capabilities_required)
1951 uint32_t *capp = ctdb_get_node_capabilities(caps, pnn);
1952 return (capp != NULL) &&
1953 ((*capp & capabilities_required) == capabilities_required);
1957 recovery daemon ping to main daemon
1959 int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb)
1964 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_RECD_PING, 0, tdb_null,
1965 ctdb, NULL, &res, NULL, NULL);
1966 if (ret != 0 || res != 0) {
1967 DEBUG(DEBUG_ERR,("Failed to send recd ping\n"));
1975 tell the main daemon how long it took to lock the reclock file
1977 int ctdb_ctrl_report_recd_lock_latency(struct ctdb_context *ctdb, struct timeval timeout, double latency)
1983 data.dptr = (uint8_t *)&latency;
1984 data.dsize = sizeof(latency);
1986 ret = ctdb_control(ctdb, CTDB_CURRENT_NODE, 0, CTDB_CONTROL_RECD_RECLOCK_LATENCY, 0, data,
1987 ctdb, NULL, &res, NULL, NULL);
1988 if (ret != 0 || res != 0) {
1989 DEBUG(DEBUG_ERR,("Failed to send recd reclock latency\n"));
1996 int ctdb_ctrl_set_ban(struct ctdb_context *ctdb, struct timeval timeout,
1997 uint32_t destnode, struct ctdb_ban_state *bantime)
2003 data.dsize = sizeof(*bantime);
2004 data.dptr = (uint8_t *)bantime;
2006 ret = ctdb_control(ctdb, destnode, 0,
2007 CTDB_CONTROL_SET_BAN_STATE, 0, data,
2008 NULL, NULL, &res, &timeout, NULL);
2009 if (ret != 0 || res != 0) {
2010 DEBUG(DEBUG_ERR,(__location__ " ctdb_control for set ban state failed\n"));
2017 struct ctdb_client_control_state *
2018 ctdb_ctrl_updaterecord_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA data)
2020 struct ctdb_client_control_state *handle;
2021 struct ctdb_marshall_buffer *m;
2022 struct ctdb_rec_data_old *rec;
2025 m = talloc_zero(mem_ctx, struct ctdb_marshall_buffer);
2027 DEBUG(DEBUG_ERR, ("Failed to allocate marshall buffer for update record\n"));
2031 m->db_id = ctdb_db->db_id;
2033 rec = ctdb_marshall_record(m, 0, key, header, data);
2035 DEBUG(DEBUG_ERR,("Failed to marshall record for update record\n"));
2039 m = talloc_realloc_size(mem_ctx, m, rec->length + offsetof(struct ctdb_marshall_buffer, data));
2041 DEBUG(DEBUG_CRIT,(__location__ " Failed to expand recdata\n"));
2046 memcpy((uint8_t *)m + offsetof(struct ctdb_marshall_buffer, data), rec, rec->length);
2049 outdata.dptr = (uint8_t *)m;
2050 outdata.dsize = talloc_get_size(m);
2052 handle = ctdb_control_send(ctdb, destnode, 0,
2053 CTDB_CONTROL_UPDATE_RECORD, 0, outdata,
2054 mem_ctx, &timeout, NULL);
2059 int ctdb_ctrl_updaterecord_recv(struct ctdb_context *ctdb, struct ctdb_client_control_state *state)
2064 ret = ctdb_control_recv(ctdb, state, state, NULL, &res, NULL);
2065 if ( (ret != 0) || (res != 0) ){
2066 DEBUG(DEBUG_ERR,(__location__ " ctdb_ctrl_update_record_recv failed\n"));
2074 ctdb_ctrl_updaterecord(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA data)
2076 struct ctdb_client_control_state *state;
2078 state = ctdb_ctrl_updaterecord_send(ctdb, mem_ctx, timeout, destnode, ctdb_db, key, header, data);
2079 return ctdb_ctrl_updaterecord_recv(ctdb, state);