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
64 * Register a srvid with ctdbd
66 static NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn,
71 return ctdbd_control(conn, CTDB_CURRENT_NODE,
72 CTDB_CONTROL_REGISTER_SRVID, srvid,
73 tdb_null, NULL, NULL, &cstatus);
77 * get our vnn from the cluster
79 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
83 status = ctdbd_control(conn,
84 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_VNN, 0,
85 tdb_null, NULL, NULL, &cstatus);
86 if (!NT_STATUS_IS_OK(status)) {
87 cluster_fatal("ctdbd_control failed\n");
89 *vnn = (uint32_t)cstatus;
93 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
99 * Get us a ctdb connection
102 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
103 struct packet_context **presult)
105 struct packet_context *result;
106 const char *sockname = lp_ctdbd_socket();
107 struct sockaddr_un addr;
110 if (!sockname || !*sockname) {
111 sockname = CTDB_PATH;
114 fd = socket(AF_UNIX, SOCK_STREAM, 0);
116 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
117 return map_nt_error_from_unix(errno);
121 addr.sun_family = AF_UNIX;
122 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
124 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
125 DEBUG(0, ("connect(%s) failed: %s\n", sockname,
128 return map_nt_error_from_unix(errno);
131 if (!(result = packet_init(mem_ctx, fd))) {
133 return NT_STATUS_NO_MEMORY;
141 * Do we have a complete ctdb packet in the queue?
144 static BOOL ctdb_req_complete(const struct data_blob *data,
150 if (data->length < sizeof(msglen)) {
154 msglen = *((uint32 *)data->data);
156 DEBUG(10, ("msglen = %d\n", msglen));
158 if (msglen < sizeof(struct ctdb_req_header)) {
159 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
160 "the req_header\n", msglen,
161 sizeof(struct ctdb_req_header)));
162 cluster_fatal("ctdbd protocol error\n");
165 if (data->length >= msglen) {
174 * State necessary to defer an incoming message while we are waiting for a
178 struct deferred_msg_state {
179 struct messaging_context *msg_ctx;
180 struct messaging_rec *rec;
184 * Timed event handler for the deferred message
187 static void deferred_message_dispatch(struct event_context *event_ctx,
188 struct timed_event *te,
189 const struct timeval *now,
192 struct deferred_msg_state *state = talloc_get_type_abort(
193 private_data, struct deferred_msg_state);
195 messaging_dispatch_rec(state->msg_ctx, state->rec);
200 struct req_pull_state {
206 * Pull a ctdb request out of the incoming packet queue
209 static NTSTATUS ctdb_req_pull(const struct data_blob *data,
212 struct req_pull_state *state = (struct req_pull_state *)private_data;
214 state->req = data_blob_talloc(state->mem_ctx, data->data,
216 if (state->req.data == NULL) {
217 return NT_STATUS_NO_MEMORY;
223 * Fetch a messaging_rec from an incoming ctdb style message
226 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
227 size_t overall_length,
228 struct ctdb_req_message *msg)
230 struct messaging_rec *result;
234 if ((overall_length < offsetof(struct ctdb_req_message, data))
236 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
238 cluster_fatal("got invalid msg length");
241 if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
242 DEBUG(0, ("talloc failed\n"));
246 blob = data_blob_const(msg->data, msg->datalen);
248 status = ndr_pull_struct_blob(
249 &blob, result, result,
250 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
252 if (!NT_STATUS_IS_OK(status)) {
253 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
263 * Read a full ctdbd request. If we have a messaging context, defer incoming
264 * messages that might come in between.
267 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
268 TALLOC_CTX *mem_ctx, void *result)
270 struct ctdb_req_header *hdr;
271 struct req_pull_state state;
276 status = packet_fd_read_sync(conn->pkt);
278 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
283 if (!NT_STATUS_IS_OK(status)) {
284 DEBUG(3, ("packet_fd_read failed: %s\n", nt_errstr(status)));
285 cluster_fatal("ctdbd died\n");
289 state.mem_ctx = mem_ctx;
291 if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
299 if (!NT_STATUS_IS_OK(status)) {
300 DEBUG(3, ("Could not read packet: %s\n", nt_errstr(status)));
301 cluster_fatal("ctdbd died\n");
304 hdr = (struct ctdb_req_header *)state.req.data;
306 if (hdr->operation == CTDB_REQ_MESSAGE) {
307 struct timed_event *evt;
308 struct deferred_msg_state *msg_state;
309 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
311 if (conn->msg_ctx == NULL) {
312 DEBUG(1, ("Got a message without having a msg ctx, "
313 "dropping msg %llu\n", msg->srvid));
317 if ((conn->release_ip_handler != NULL)
318 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
319 /* must be dispatched immediately */
320 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
321 conn->release_ip_handler((const char *)msg->data,
322 conn->release_ip_priv);
327 if (!(msg_state = TALLOC_P(NULL, struct deferred_msg_state))) {
328 DEBUG(0, ("talloc failed\n"));
333 if (!(msg_state->rec = ctdb_pull_messaging_rec(
334 msg_state, state.req.length, msg))) {
335 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
336 TALLOC_FREE(msg_state);
343 msg_state->msg_ctx = conn->msg_ctx;
346 * We're waiting for a call reply, but an async message has
347 * crossed. Defer dispatching to the toplevel event loop.
349 evt = event_add_timed(conn->msg_ctx->event_ctx,
350 conn->msg_ctx->event_ctx,
352 "deferred_message_dispatch",
353 deferred_message_dispatch,
356 DEBUG(0, ("event_add_timed failed\n"));
357 TALLOC_FREE(msg_state);
365 if (hdr->reqid != reqid) {
366 /* we got the wrong reply */
367 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
368 "been %u\n", hdr->reqid, reqid));
373 *((void **)result) = talloc_move(mem_ctx, &hdr);
379 * Get us a ctdbd connection
382 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
383 struct ctdbd_connection **pconn)
385 struct ctdbd_connection *conn;
388 if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
389 DEBUG(0, ("talloc failed\n"));
390 return NT_STATUS_NO_MEMORY;
393 status = ctdbd_connect(conn, &conn->pkt);
395 if (!NT_STATUS_IS_OK(status)) {
396 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
400 status = get_cluster_vnn(conn, &conn->our_vnn);
402 if (!NT_STATUS_IS_OK(status)) {
403 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
407 generate_random_buffer((unsigned char *)&conn->rand_srvid,
408 sizeof(conn->rand_srvid));
410 status = register_with_ctdbd(conn, conn->rand_srvid);
412 if (!NT_STATUS_IS_OK(status)) {
413 DEBUG(5, ("Could not register random srvid: %s\n",
427 * Get us a ctdbd connection and register us as a process
430 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
431 struct ctdbd_connection **pconn)
433 struct ctdbd_connection *conn;
436 status = ctdbd_init_connection(mem_ctx, &conn);
438 if (!NT_STATUS_IS_OK(status)) {
442 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
443 if (!NT_STATUS_IS_OK(status)) {
447 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
448 if (!NT_STATUS_IS_OK(status)) {
461 * Packet handler to receive and handle a ctdb message
463 static NTSTATUS ctdb_handle_message(const struct data_blob *data,
466 struct ctdbd_connection *conn = talloc_get_type_abort(
467 private_data, struct ctdbd_connection);
468 struct ctdb_req_message *msg;
469 struct messaging_rec *msg_rec;
471 msg = (struct ctdb_req_message *)data->data;
473 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
474 DEBUG(0, ("Received async msg of type %u, discarding\n",
475 msg->hdr.operation));
476 return NT_STATUS_INVALID_PARAMETER;
479 if ((conn->release_ip_handler != NULL)
480 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
481 /* must be dispatched immediately */
482 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
483 conn->release_ip_handler((const char *)msg->data,
484 conn->release_ip_priv);
488 SMB_ASSERT(conn->msg_ctx != NULL);
490 if (msg->srvid == CTDB_SRVID_RECONFIGURE) {
491 DEBUG(0,("Got cluster reconfigure message\n"));
493 * when the cluster is reconfigured, we need to clean the brl
496 messaging_send(conn->msg_ctx, procid_self(),
497 MSG_SMB_BRL_VALIDATE, &data_blob_null);
500 * it's possible that we have just rejoined the cluster after
501 * an outage. In that case our pending locks could have been
502 * removed from the lockdb, so retry them once more
504 message_send_all(conn->msg_ctx, MSG_SMB_UNLOCK, NULL, 0, NULL);
510 /* only messages to our pid or the broadcast are valid here */
511 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
512 DEBUG(0,("Got unexpected message with srvid=%llu\n",
513 (unsigned long long)msg->srvid));
517 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, data->length, msg))) {
518 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
519 return NT_STATUS_NO_MEMORY;
522 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
524 TALLOC_FREE(msg_rec);
529 * The ctdbd socket is readable asynchronuously
532 static void ctdbd_socket_handler(struct event_context *event_ctx,
533 struct fd_event *event,
537 struct ctdbd_connection *conn = talloc_get_type_abort(
538 private_data, struct ctdbd_connection);
542 status = packet_fd_read(conn->pkt);
544 if (!NT_STATUS_IS_OK(status)) {
545 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
546 cluster_fatal("ctdbd died\n");
549 while (packet_handler(conn->pkt, ctdb_req_complete,
550 ctdb_handle_message, conn, &status)) {
551 if (!NT_STATUS_IS_OK(status)) {
552 DEBUG(10, ("could not handle incoming message: %s\n",
559 * Prepare a ctdbd connection to receive messages
562 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
563 struct messaging_context *msg_ctx)
565 SMB_ASSERT(conn->msg_ctx == NULL);
566 SMB_ASSERT(conn->fde == NULL);
568 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
569 packet_get_fd(conn->pkt),
571 ctdbd_socket_handler,
573 DEBUG(0, ("event_add_fd failed\n"));
574 return NT_STATUS_NO_MEMORY;
577 conn->msg_ctx = msg_ctx;
583 * Send a messaging message across a ctdbd
586 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
587 uint32 dst_vnn, uint64 dst_srvid,
588 struct messaging_rec *msg)
590 struct ctdb_req_message r;
595 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
596 DEBUG(0, ("talloc failed\n"));
597 return NT_STATUS_NO_MEMORY;
600 status = ndr_push_struct_blob(
602 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
604 if (!NT_STATUS_IS_OK(status)) {
605 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
610 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
611 r.hdr.ctdb_magic = CTDB_MAGIC;
612 r.hdr.ctdb_version = CTDB_VERSION;
613 r.hdr.generation = 1;
614 r.hdr.operation = CTDB_REQ_MESSAGE;
615 r.hdr.destnode = dst_vnn;
616 r.hdr.srcnode = conn->our_vnn;
619 r.datalen = blob.length;
621 status = packet_send(
623 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
626 if (!NT_STATUS_IS_OK(status)) {
627 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
631 status = packet_flush(conn->pkt);
633 if (!NT_STATUS_IS_OK(status)) {
634 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
635 cluster_fatal("cluster dispatch daemon msg write error\n");
638 status = NT_STATUS_OK;
640 TALLOC_FREE(mem_ctx);
645 * send/recv a generic ctdb control message
647 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
648 uint32_t vnn, uint32 opcode,
649 uint64_t srvid, TDB_DATA data,
650 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
653 struct ctdb_req_control req;
654 struct ctdb_reply_control *reply = NULL;
655 struct ctdbd_connection *new_conn = NULL;
659 status = ctdbd_init_connection(NULL, &new_conn);
661 if (!NT_STATUS_IS_OK(status)) {
662 DEBUG(10, ("Could not init temp connection: %s\n",
671 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
672 req.hdr.ctdb_magic = CTDB_MAGIC;
673 req.hdr.ctdb_version = CTDB_VERSION;
674 req.hdr.operation = CTDB_REQ_CONTROL;
675 req.hdr.reqid = ++conn->reqid;
676 req.hdr.destnode = vnn;
679 req.datalen = data.dsize;
681 status = packet_send(
683 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
686 if (!NT_STATUS_IS_OK(status)) {
687 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
691 status = packet_flush(conn->pkt);
693 if (!NT_STATUS_IS_OK(status)) {
694 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
695 cluster_fatal("cluster dispatch daemon control write error\n");
698 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
700 if (!NT_STATUS_IS_OK(status)) {
701 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
705 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
706 DEBUG(0, ("received invalid reply\n"));
711 if (!(outdata->dptr = (uint8 *)talloc_memdup(
712 mem_ctx, reply->data, reply->datalen))) {
714 return NT_STATUS_NO_MEMORY;
716 outdata->dsize = reply->datalen;
719 (*cstatus) = reply->status;
722 status = NT_STATUS_OK;
725 TALLOC_FREE(new_conn);
731 * see if a remote process exists
733 BOOL ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
739 data.dptr = (uint8_t*)&pid;
740 data.dsize = sizeof(pid);
742 status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0,
743 data, NULL, NULL, &cstatus);
744 if (!NT_STATUS_IS_OK(status)) {
745 DEBUG(0, (__location__ " ctdb_control for process_exists "
756 char *ctdbd_dbpath(struct ctdbd_connection *conn,
757 TALLOC_CTX *mem_ctx, uint32_t db_id)
763 data.dptr = (uint8_t*)&db_id;
764 data.dsize = sizeof(db_id);
766 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
767 CTDB_CONTROL_GETDBPATH, 0, data,
768 mem_ctx, &data, &cstatus);
769 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
770 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
774 return (char *)data.dptr;
778 * attach to a ctdb database
780 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
781 const char *name, uint32_t *db_id, int tdb_flags)
787 data.dptr = (uint8_t*)name;
788 data.dsize = strlen(name)+1;
790 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
791 CTDB_CONTROL_DB_ATTACH, 0, data,
792 NULL, &data, &cstatus);
793 if (!NT_STATUS_IS_OK(status)) {
794 DEBUG(0, (__location__ " ctdb_control for db_attach "
795 "failed: %s\n", nt_errstr(status)));
799 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
800 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
801 return NT_STATUS_INTERNAL_ERROR;
804 *db_id = *(uint32_t *)data.dptr;
805 talloc_free(data.dptr);
807 if (!(tdb_flags & TDB_SEQNUM)) {
811 data.dptr = (uint8_t *)db_id;
812 data.dsize = sizeof(*db_id);
814 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
815 CTDB_CONTROL_ENABLE_SEQNUM, 0, data,
816 NULL, NULL, &cstatus);
817 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
818 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
820 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
828 * force the migration of a record to this node
830 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
833 struct ctdb_req_call req;
834 struct ctdb_reply_call *reply;
839 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
840 req.hdr.ctdb_magic = CTDB_MAGIC;
841 req.hdr.ctdb_version = CTDB_VERSION;
842 req.hdr.operation = CTDB_REQ_CALL;
843 req.hdr.reqid = ++conn->reqid;
844 req.flags = CTDB_IMMEDIATE_MIGRATION;
845 req.callid = CTDB_NULL_FUNC;
847 req.keylen = key.dsize;
849 status = packet_send(
851 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
854 if (!NT_STATUS_IS_OK(status)) {
855 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
859 status = packet_flush(conn->pkt);
861 if (!NT_STATUS_IS_OK(status)) {
862 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
863 cluster_fatal("cluster dispatch daemon control write error\n");
866 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
868 if (!NT_STATUS_IS_OK(status)) {
869 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
873 if (reply->hdr.operation != CTDB_REPLY_CALL) {
874 DEBUG(0, ("received invalid reply\n"));
875 status = NT_STATUS_INTERNAL_ERROR;
879 status = NT_STATUS_OK;
887 * remotely fetch a record without locking it or forcing a migration
889 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
890 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
892 struct ctdb_req_call req;
893 struct ctdb_reply_call *reply;
898 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
899 req.hdr.ctdb_magic = CTDB_MAGIC;
900 req.hdr.ctdb_version = CTDB_VERSION;
901 req.hdr.operation = CTDB_REQ_CALL;
902 req.hdr.reqid = ++conn->reqid;
904 req.callid = CTDB_FETCH_FUNC;
906 req.keylen = key.dsize;
908 status = packet_send(
910 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
913 if (!NT_STATUS_IS_OK(status)) {
914 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
918 status = packet_flush(conn->pkt);
920 if (!NT_STATUS_IS_OK(status)) {
921 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
922 cluster_fatal("cluster dispatch daemon control write error\n");
925 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
927 if (!NT_STATUS_IS_OK(status)) {
928 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
932 if (reply->hdr.operation != CTDB_REPLY_CALL) {
933 DEBUG(0, ("received invalid reply\n"));
934 status = NT_STATUS_INTERNAL_ERROR;
938 data->dsize = reply->datalen;
939 if (data->dsize == 0) {
944 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
946 if (data->dptr == NULL) {
947 DEBUG(0, ("talloc failed\n"));
948 status = NT_STATUS_NO_MEMORY;
953 status = NT_STATUS_OK;
959 struct ctdbd_traverse_state {
960 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
965 * Handle a traverse record coming in on the ctdbd connection
968 static NTSTATUS ctdb_traverse_handler(const struct data_blob *blob,
971 struct ctdbd_traverse_state *state =
972 (struct ctdbd_traverse_state *)private_data;
974 struct ctdb_req_message *m;
975 struct ctdb_rec_data *d;
978 m = (struct ctdb_req_message *)blob->data;
980 if (blob->length < sizeof(*m) || m->hdr.length != blob->length) {
981 DEBUG(0, ("Got invalid message of length %d\n",
983 return NT_STATUS_UNEXPECTED_IO_ERROR;
986 d = (struct ctdb_rec_data *)&m->data[0];
987 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
988 DEBUG(0, ("Got invalid traverse data of length %d\n",
990 return NT_STATUS_UNEXPECTED_IO_ERROR;
993 key.dsize = d->keylen;
994 key.dptr = &d->data[0];
995 data.dsize = d->datalen;
996 data.dptr = &d->data[d->keylen];
998 if (key.dsize == 0 && data.dsize == 0) {
999 /* end of traverse */
1000 return NT_STATUS_END_OF_FILE;
1003 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1004 DEBUG(0, ("Got invalid ltdb header length %d\n",
1006 return NT_STATUS_UNEXPECTED_IO_ERROR;
1008 data.dsize -= sizeof(struct ctdb_ltdb_header);
1009 data.dptr += sizeof(struct ctdb_ltdb_header);
1012 state->fn(key, data, state->private_data);
1015 return NT_STATUS_OK;
1019 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1020 connection to ctdbd to avoid the hairy recursive and async problems with
1024 NTSTATUS ctdbd_traverse(uint32 db_id,
1025 void (*fn)(TDB_DATA key, TDB_DATA data,
1026 void *private_data),
1029 struct ctdbd_connection *conn;
1033 struct ctdb_traverse_start t;
1035 struct ctdbd_traverse_state state;
1037 status = ctdbd_init_connection(NULL, &conn);
1040 t.srvid = conn->rand_srvid;
1041 t.reqid = ++conn->reqid;
1043 data.dptr = (uint8_t *)&t;
1044 data.dsize = sizeof(t);
1046 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1047 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid,
1048 data, NULL, NULL, &cstatus);
1050 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1052 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1055 if (NT_STATUS_IS_OK(status)) {
1057 * We need a mapping here
1059 status = NT_STATUS_UNSUCCESSFUL;
1065 state.private_data = private_data;
1069 status = NT_STATUS_OK;
1071 if (packet_handler(conn->pkt, ctdb_req_complete,
1072 ctdb_traverse_handler, &state, &status)) {
1074 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1075 status = NT_STATUS_OK;
1080 * There might be more in the queue
1085 if (!NT_STATUS_IS_OK(status)) {
1089 status = packet_fd_read_sync(conn->pkt);
1091 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1092 status = NT_STATUS_OK;
1095 if (!NT_STATUS_IS_OK(status)) {
1096 cluster_fatal("ctdbd died\n");
1106 * Register us as a server for a particular tcp connection
1109 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1110 const struct sockaddr_in *server,
1111 const struct sockaddr_in *client,
1112 void (*release_ip_handler)(const char *ip_addr,
1113 void *private_data),
1116 struct ctdb_control_tcp p;
1121 * Only one connection so far
1123 SMB_ASSERT(conn->release_ip_handler == NULL);
1126 * We want to be told about IP releases
1129 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1130 if (!NT_STATUS_IS_OK(status)) {
1138 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1139 * can send an extra ack to trigger a reset for our client, so it
1140 * immediately reconnects
1142 data.dptr = (uint8_t *)&p;
1143 data.dsize = sizeof(p);
1145 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1146 CTDB_CONTROL_TCP_CLIENT,
1147 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1151 * We want to handle reconfigure events
1153 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1155 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1160 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1161 struct ctdbd_connection **pconn)
1163 return NT_STATUS_NOT_IMPLEMENTED;