2 Unix SMB/CIFS implementation.
3 Samba internal messaging functions
4 Copyright (C) 2007 by Volker Lendecke
5 Copyright (C) 2007 by Andrew Tridgell
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 #ifdef CLUSTER_SUPPORT
25 #include "librpc/gen_ndr/messaging.h"
26 #include "librpc/gen_ndr/ndr_messaging.h"
28 /* paths to these include files come from --with-ctdb= in configure */
30 #include "ctdb_private.h"
32 struct ctdbd_connection {
33 struct messaging_context *msg_ctx;
37 struct packet_context *pkt;
40 void (*release_ip_handler)(const char *ip_addr, void *private_data);
41 void *release_ip_priv;
44 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
45 uint32_t vnn, uint32 opcode,
46 uint64_t srvid, TDB_DATA data,
47 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
51 * exit on fatal communications errors with the ctdbd daemon
53 static void cluster_fatal(const char *why)
55 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
56 /* we don't use smb_panic() as we don't want to delay to write
57 a core file. We need to release this process id immediately
58 so that someone else can take over without getting sharing
66 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
68 if (DEBUGLEVEL < 10) {
71 DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
72 (int)hdr->length, (int)hdr->ctdb_magic,
73 (int)hdr->ctdb_version, (int)hdr->generation,
74 (int)hdr->operation, (int)hdr->reqid));
78 * Register a srvid with ctdbd
80 static NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn,
85 return ctdbd_control(conn, CTDB_CURRENT_NODE,
86 CTDB_CONTROL_REGISTER_SRVID, srvid,
87 tdb_null, NULL, NULL, &cstatus);
91 * get our vnn from the cluster
93 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
97 status = ctdbd_control(conn,
98 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0,
99 tdb_null, NULL, NULL, &cstatus);
100 if (!NT_STATUS_IS_OK(status)) {
101 cluster_fatal("ctdbd_control failed\n");
103 *vnn = (uint32_t)cstatus;
107 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
109 return conn->our_vnn;
113 * Get us a ctdb connection
116 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
117 struct packet_context **presult)
119 struct packet_context *result;
120 const char *sockname = lp_ctdbd_socket();
121 struct sockaddr_un addr;
124 if (!sockname || !*sockname) {
125 sockname = CTDB_PATH;
128 fd = socket(AF_UNIX, SOCK_STREAM, 0);
130 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
131 return map_nt_error_from_unix(errno);
135 addr.sun_family = AF_UNIX;
136 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
138 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
139 DEBUG(0, ("connect(%s) failed: %s\n", sockname,
142 return map_nt_error_from_unix(errno);
145 if (!(result = packet_init(mem_ctx, fd))) {
147 return NT_STATUS_NO_MEMORY;
155 * Do we have a complete ctdb packet in the queue?
158 static BOOL ctdb_req_complete(const struct data_blob *data,
164 if (data->length < sizeof(msglen)) {
168 msglen = *((uint32 *)data->data);
170 DEBUG(10, ("msglen = %d\n", msglen));
172 if (msglen < sizeof(struct ctdb_req_header)) {
173 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
174 "the req_header\n", (int)msglen,
175 (int)sizeof(struct ctdb_req_header)));
176 cluster_fatal("ctdbd protocol error\n");
179 if (data->length >= msglen) {
188 * State necessary to defer an incoming message while we are waiting for a
192 struct deferred_msg_state {
193 struct messaging_context *msg_ctx;
194 struct messaging_rec *rec;
198 * Timed event handler for the deferred message
201 static void deferred_message_dispatch(struct event_context *event_ctx,
202 struct timed_event *te,
203 const struct timeval *now,
206 struct deferred_msg_state *state = talloc_get_type_abort(
207 private_data, struct deferred_msg_state);
209 messaging_dispatch_rec(state->msg_ctx, state->rec);
214 struct req_pull_state {
220 * Pull a ctdb request out of the incoming packet queue
223 static NTSTATUS ctdb_req_pull(const struct data_blob *data,
226 struct req_pull_state *state = (struct req_pull_state *)private_data;
228 state->req = data_blob_talloc(state->mem_ctx, data->data,
230 if (state->req.data == NULL) {
231 return NT_STATUS_NO_MEMORY;
237 * Fetch a messaging_rec from an incoming ctdb style message
240 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
241 size_t overall_length,
242 struct ctdb_req_message *msg)
244 struct messaging_rec *result;
248 if ((overall_length < offsetof(struct ctdb_req_message, data))
250 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
252 cluster_fatal("got invalid msg length");
255 if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
256 DEBUG(0, ("talloc failed\n"));
260 blob = data_blob_const(msg->data, msg->datalen);
262 status = ndr_pull_struct_blob(
263 &blob, result, result,
264 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
266 if (DEBUGLEVEL >= 10) {
267 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
268 NDR_PRINT_DEBUG(messaging_rec, result);
271 if (!NT_STATUS_IS_OK(status)) {
272 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
282 * Read a full ctdbd request. If we have a messaging context, defer incoming
283 * messages that might come in between.
286 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
287 TALLOC_CTX *mem_ctx, void *result)
289 struct ctdb_req_header *hdr;
290 struct req_pull_state state;
295 status = packet_fd_read_sync(conn->pkt);
297 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
300 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
305 if (!NT_STATUS_IS_OK(status)) {
306 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
307 cluster_fatal("ctdbd died\n");
313 state.mem_ctx = mem_ctx;
315 if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
320 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
324 if (!NT_STATUS_IS_OK(status)) {
325 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
326 cluster_fatal("ctdbd died\n");
329 hdr = (struct ctdb_req_header *)state.req.data;
331 DEBUG(10, ("Received ctdb packet\n"));
332 ctdb_packet_dump(hdr);
334 if (hdr->operation == CTDB_REQ_MESSAGE) {
335 struct timed_event *evt;
336 struct deferred_msg_state *msg_state;
337 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
339 if (conn->msg_ctx == NULL) {
340 DEBUG(1, ("Got a message without having a msg ctx, "
341 "dropping msg %llu\n",
342 (long long unsigned)msg->srvid));
346 if ((conn->release_ip_handler != NULL)
347 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
348 /* must be dispatched immediately */
349 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
350 conn->release_ip_handler((const char *)msg->data,
351 conn->release_ip_priv);
356 if (!(msg_state = TALLOC_P(NULL, struct deferred_msg_state))) {
357 DEBUG(0, ("talloc failed\n"));
362 if (!(msg_state->rec = ctdb_pull_messaging_rec(
363 msg_state, state.req.length, msg))) {
364 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
365 TALLOC_FREE(msg_state);
372 msg_state->msg_ctx = conn->msg_ctx;
375 * We're waiting for a call reply, but an async message has
376 * crossed. Defer dispatching to the toplevel event loop.
378 evt = event_add_timed(conn->msg_ctx->event_ctx,
379 conn->msg_ctx->event_ctx,
381 "deferred_message_dispatch",
382 deferred_message_dispatch,
385 DEBUG(0, ("event_add_timed failed\n"));
386 TALLOC_FREE(msg_state);
394 if (hdr->reqid != reqid) {
395 /* we got the wrong reply */
396 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
397 "been %u\n", hdr->reqid, reqid));
402 *((void **)result) = talloc_move(mem_ctx, &hdr);
408 * Get us a ctdbd connection
411 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
412 struct ctdbd_connection **pconn)
414 struct ctdbd_connection *conn;
417 if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
418 DEBUG(0, ("talloc failed\n"));
419 return NT_STATUS_NO_MEMORY;
422 status = ctdbd_connect(conn, &conn->pkt);
424 if (!NT_STATUS_IS_OK(status)) {
425 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
429 status = get_cluster_vnn(conn, &conn->our_vnn);
431 if (!NT_STATUS_IS_OK(status)) {
432 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
436 generate_random_buffer((unsigned char *)&conn->rand_srvid,
437 sizeof(conn->rand_srvid));
439 status = register_with_ctdbd(conn, conn->rand_srvid);
441 if (!NT_STATUS_IS_OK(status)) {
442 DEBUG(5, ("Could not register random srvid: %s\n",
456 * Get us a ctdbd connection and register us as a process
459 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
460 struct ctdbd_connection **pconn)
462 struct ctdbd_connection *conn;
465 status = ctdbd_init_connection(mem_ctx, &conn);
467 if (!NT_STATUS_IS_OK(status)) {
471 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
472 if (!NT_STATUS_IS_OK(status)) {
476 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
477 if (!NT_STATUS_IS_OK(status)) {
490 * Packet handler to receive and handle a ctdb message
492 static NTSTATUS ctdb_handle_message(const struct data_blob *data,
495 struct ctdbd_connection *conn = talloc_get_type_abort(
496 private_data, struct ctdbd_connection);
497 struct ctdb_req_message *msg;
498 struct messaging_rec *msg_rec;
500 msg = (struct ctdb_req_message *)data->data;
502 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
503 DEBUG(0, ("Received async msg of type %u, discarding\n",
504 msg->hdr.operation));
505 return NT_STATUS_INVALID_PARAMETER;
508 if ((conn->release_ip_handler != NULL)
509 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
510 /* must be dispatched immediately */
511 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
512 conn->release_ip_handler((const char *)msg->data,
513 conn->release_ip_priv);
517 SMB_ASSERT(conn->msg_ctx != NULL);
519 if (msg->srvid == CTDB_SRVID_RECONFIGURE) {
520 DEBUG(0,("Got cluster reconfigure message\n"));
522 * when the cluster is reconfigured, we need to clean the brl
525 messaging_send(conn->msg_ctx, procid_self(),
526 MSG_SMB_BRL_VALIDATE, &data_blob_null);
529 * it's possible that we have just rejoined the cluster after
530 * an outage. In that case our pending locks could have been
531 * removed from the lockdb, so retry them once more
533 message_send_all(conn->msg_ctx, MSG_SMB_UNLOCK, NULL, 0, NULL);
539 /* only messages to our pid or the broadcast are valid here */
540 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
541 DEBUG(0,("Got unexpected message with srvid=%llu\n",
542 (unsigned long long)msg->srvid));
546 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, data->length, msg))) {
547 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
548 return NT_STATUS_NO_MEMORY;
551 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
553 TALLOC_FREE(msg_rec);
558 * The ctdbd socket is readable asynchronuously
561 static void ctdbd_socket_handler(struct event_context *event_ctx,
562 struct fd_event *event,
566 struct ctdbd_connection *conn = talloc_get_type_abort(
567 private_data, struct ctdbd_connection);
571 status = packet_fd_read(conn->pkt);
573 if (!NT_STATUS_IS_OK(status)) {
574 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
575 cluster_fatal("ctdbd died\n");
578 while (packet_handler(conn->pkt, ctdb_req_complete,
579 ctdb_handle_message, conn, &status)) {
580 if (!NT_STATUS_IS_OK(status)) {
581 DEBUG(10, ("could not handle incoming message: %s\n",
588 * Prepare a ctdbd connection to receive messages
591 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
592 struct messaging_context *msg_ctx)
594 SMB_ASSERT(conn->msg_ctx == NULL);
595 SMB_ASSERT(conn->fde == NULL);
597 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
598 packet_get_fd(conn->pkt),
600 ctdbd_socket_handler,
602 DEBUG(0, ("event_add_fd failed\n"));
603 return NT_STATUS_NO_MEMORY;
606 conn->msg_ctx = msg_ctx;
612 * Send a messaging message across a ctdbd
615 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
616 uint32 dst_vnn, uint64 dst_srvid,
617 struct messaging_rec *msg)
619 struct ctdb_req_message r;
624 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
625 DEBUG(0, ("talloc failed\n"));
626 return NT_STATUS_NO_MEMORY;
629 status = ndr_push_struct_blob(
631 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
633 if (!NT_STATUS_IS_OK(status)) {
634 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
639 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
640 r.hdr.ctdb_magic = CTDB_MAGIC;
641 r.hdr.ctdb_version = CTDB_VERSION;
642 r.hdr.generation = 1;
643 r.hdr.operation = CTDB_REQ_MESSAGE;
644 r.hdr.destnode = dst_vnn;
645 r.hdr.srcnode = conn->our_vnn;
648 r.datalen = blob.length;
650 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
651 ctdb_packet_dump(&r.hdr);
653 status = packet_send(
655 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
658 if (!NT_STATUS_IS_OK(status)) {
659 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
663 status = packet_flush(conn->pkt);
665 if (!NT_STATUS_IS_OK(status)) {
666 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
667 cluster_fatal("cluster dispatch daemon msg write error\n");
670 status = NT_STATUS_OK;
672 TALLOC_FREE(mem_ctx);
677 * send/recv a generic ctdb control message
679 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
680 uint32_t vnn, uint32 opcode,
681 uint64_t srvid, TDB_DATA data,
682 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
685 struct ctdb_req_control req;
686 struct ctdb_reply_control *reply = NULL;
687 struct ctdbd_connection *new_conn = NULL;
691 status = ctdbd_init_connection(NULL, &new_conn);
693 if (!NT_STATUS_IS_OK(status)) {
694 DEBUG(10, ("Could not init temp connection: %s\n",
703 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
704 req.hdr.ctdb_magic = CTDB_MAGIC;
705 req.hdr.ctdb_version = CTDB_VERSION;
706 req.hdr.operation = CTDB_REQ_CONTROL;
707 req.hdr.reqid = ++conn->reqid;
708 req.hdr.destnode = vnn;
711 req.datalen = data.dsize;
713 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
714 ctdb_packet_dump(&req.hdr);
716 status = packet_send(
718 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
719 data_blob_const(data.dptr, data.dsize));
721 if (!NT_STATUS_IS_OK(status)) {
722 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
726 status = packet_flush(conn->pkt);
728 if (!NT_STATUS_IS_OK(status)) {
729 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
730 cluster_fatal("cluster dispatch daemon control write error\n");
733 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
735 if (!NT_STATUS_IS_OK(status)) {
736 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
740 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
741 DEBUG(0, ("received invalid reply\n"));
746 if (!(outdata->dptr = (uint8 *)talloc_memdup(
747 mem_ctx, reply->data, reply->datalen))) {
749 return NT_STATUS_NO_MEMORY;
751 outdata->dsize = reply->datalen;
754 (*cstatus) = reply->status;
757 status = NT_STATUS_OK;
760 TALLOC_FREE(new_conn);
766 * see if a remote process exists
768 BOOL ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
774 data.dptr = (uint8_t*)&pid;
775 data.dsize = sizeof(pid);
777 status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0,
778 data, NULL, NULL, &cstatus);
779 if (!NT_STATUS_IS_OK(status)) {
780 DEBUG(0, (__location__ " ctdb_control for process_exists "
791 char *ctdbd_dbpath(struct ctdbd_connection *conn,
792 TALLOC_CTX *mem_ctx, uint32_t db_id)
798 data.dptr = (uint8_t*)&db_id;
799 data.dsize = sizeof(db_id);
801 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
802 CTDB_CONTROL_GETDBPATH, 0, data,
803 mem_ctx, &data, &cstatus);
804 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
805 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
809 return (char *)data.dptr;
813 * attach to a ctdb database
815 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
816 const char *name, uint32_t *db_id, int tdb_flags)
822 data.dptr = (uint8_t*)name;
823 data.dsize = strlen(name)+1;
825 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
826 CTDB_CONTROL_DB_ATTACH, 0, data,
827 NULL, &data, &cstatus);
828 if (!NT_STATUS_IS_OK(status)) {
829 DEBUG(0, (__location__ " ctdb_control for db_attach "
830 "failed: %s\n", nt_errstr(status)));
834 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
835 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
836 return NT_STATUS_INTERNAL_ERROR;
839 *db_id = *(uint32_t *)data.dptr;
840 talloc_free(data.dptr);
842 if (!(tdb_flags & TDB_SEQNUM)) {
846 data.dptr = (uint8_t *)db_id;
847 data.dsize = sizeof(*db_id);
849 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
850 CTDB_CONTROL_ENABLE_SEQNUM, 0, data,
851 NULL, NULL, &cstatus);
852 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
853 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
855 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
863 * force the migration of a record to this node
865 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
868 struct ctdb_req_call req;
869 struct ctdb_reply_call *reply;
874 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
875 req.hdr.ctdb_magic = CTDB_MAGIC;
876 req.hdr.ctdb_version = CTDB_VERSION;
877 req.hdr.operation = CTDB_REQ_CALL;
878 req.hdr.reqid = ++conn->reqid;
879 req.flags = CTDB_IMMEDIATE_MIGRATION;
880 req.callid = CTDB_NULL_FUNC;
882 req.keylen = key.dsize;
884 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
885 ctdb_packet_dump(&req.hdr);
887 status = packet_send(
889 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
890 data_blob_const(key.dptr, key.dsize));
892 if (!NT_STATUS_IS_OK(status)) {
893 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
897 status = packet_flush(conn->pkt);
899 if (!NT_STATUS_IS_OK(status)) {
900 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
901 cluster_fatal("cluster dispatch daemon control write error\n");
904 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
906 if (!NT_STATUS_IS_OK(status)) {
907 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
911 if (reply->hdr.operation != CTDB_REPLY_CALL) {
912 DEBUG(0, ("received invalid reply\n"));
913 status = NT_STATUS_INTERNAL_ERROR;
917 status = NT_STATUS_OK;
925 * remotely fetch a record without locking it or forcing a migration
927 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
928 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
930 struct ctdb_req_call req;
931 struct ctdb_reply_call *reply;
936 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
937 req.hdr.ctdb_magic = CTDB_MAGIC;
938 req.hdr.ctdb_version = CTDB_VERSION;
939 req.hdr.operation = CTDB_REQ_CALL;
940 req.hdr.reqid = ++conn->reqid;
942 req.callid = CTDB_FETCH_FUNC;
944 req.keylen = key.dsize;
946 status = packet_send(
948 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
949 data_blob_const(key.dptr, key.dsize));
951 if (!NT_STATUS_IS_OK(status)) {
952 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
956 status = packet_flush(conn->pkt);
958 if (!NT_STATUS_IS_OK(status)) {
959 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
960 cluster_fatal("cluster dispatch daemon control write error\n");
963 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
965 if (!NT_STATUS_IS_OK(status)) {
966 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
970 if (reply->hdr.operation != CTDB_REPLY_CALL) {
971 DEBUG(0, ("received invalid reply\n"));
972 status = NT_STATUS_INTERNAL_ERROR;
976 data->dsize = reply->datalen;
977 if (data->dsize == 0) {
982 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
984 if (data->dptr == NULL) {
985 DEBUG(0, ("talloc failed\n"));
986 status = NT_STATUS_NO_MEMORY;
991 status = NT_STATUS_OK;
997 struct ctdbd_traverse_state {
998 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1003 * Handle a traverse record coming in on the ctdbd connection
1006 static NTSTATUS ctdb_traverse_handler(const struct data_blob *blob,
1009 struct ctdbd_traverse_state *state =
1010 (struct ctdbd_traverse_state *)private_data;
1012 struct ctdb_req_message *m;
1013 struct ctdb_rec_data *d;
1016 m = (struct ctdb_req_message *)blob->data;
1018 if (blob->length < sizeof(*m) || m->hdr.length != blob->length) {
1019 DEBUG(0, ("Got invalid message of length %d\n",
1020 (int)blob->length));
1021 return NT_STATUS_UNEXPECTED_IO_ERROR;
1024 d = (struct ctdb_rec_data *)&m->data[0];
1025 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1026 DEBUG(0, ("Got invalid traverse data of length %d\n",
1028 return NT_STATUS_UNEXPECTED_IO_ERROR;
1031 key.dsize = d->keylen;
1032 key.dptr = &d->data[0];
1033 data.dsize = d->datalen;
1034 data.dptr = &d->data[d->keylen];
1036 if (key.dsize == 0 && data.dsize == 0) {
1037 /* end of traverse */
1038 return NT_STATUS_END_OF_FILE;
1041 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1042 DEBUG(0, ("Got invalid ltdb header length %d\n",
1044 return NT_STATUS_UNEXPECTED_IO_ERROR;
1046 data.dsize -= sizeof(struct ctdb_ltdb_header);
1047 data.dptr += sizeof(struct ctdb_ltdb_header);
1050 state->fn(key, data, state->private_data);
1053 return NT_STATUS_OK;
1057 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1058 connection to ctdbd to avoid the hairy recursive and async problems with
1062 NTSTATUS ctdbd_traverse(uint32 db_id,
1063 void (*fn)(TDB_DATA key, TDB_DATA data,
1064 void *private_data),
1067 struct ctdbd_connection *conn;
1071 struct ctdb_traverse_start t;
1073 struct ctdbd_traverse_state state;
1075 status = ctdbd_init_connection(NULL, &conn);
1078 t.srvid = conn->rand_srvid;
1079 t.reqid = ++conn->reqid;
1081 data.dptr = (uint8_t *)&t;
1082 data.dsize = sizeof(t);
1084 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1085 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid,
1086 data, NULL, NULL, &cstatus);
1088 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1090 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1093 if (NT_STATUS_IS_OK(status)) {
1095 * We need a mapping here
1097 status = NT_STATUS_UNSUCCESSFUL;
1103 state.private_data = private_data;
1107 status = NT_STATUS_OK;
1109 if (packet_handler(conn->pkt, ctdb_req_complete,
1110 ctdb_traverse_handler, &state, &status)) {
1112 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1113 status = NT_STATUS_OK;
1118 * There might be more in the queue
1123 if (!NT_STATUS_IS_OK(status)) {
1127 status = packet_fd_read_sync(conn->pkt);
1129 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1131 * There might be more in the queue
1136 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1137 status = NT_STATUS_OK;
1140 if (!NT_STATUS_IS_OK(status)) {
1141 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1142 cluster_fatal("ctdbd died\n");
1152 * Register us as a server for a particular tcp connection
1155 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1156 const struct sockaddr_in *server,
1157 const struct sockaddr_in *client,
1158 void (*release_ip_handler)(const char *ip_addr,
1159 void *private_data),
1162 struct ctdb_control_tcp p;
1167 * Only one connection so far
1169 SMB_ASSERT(conn->release_ip_handler == NULL);
1172 * We want to be told about IP releases
1175 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1176 if (!NT_STATUS_IS_OK(status)) {
1184 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1185 * can send an extra ack to trigger a reset for our client, so it
1186 * immediately reconnects
1188 data.dptr = (uint8_t *)&p;
1189 data.dsize = sizeof(p);
1191 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1192 CTDB_CONTROL_TCP_CLIENT,
1193 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1197 * We want to handle reconfigure events
1199 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1201 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1206 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1207 struct ctdbd_connection **pconn)
1209 return NT_STATUS_NOT_IMPLEMENTED;