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"
27 #include "ctdbd_conn.h"
29 /* paths to these include files come from --with-ctdb= in configure */
31 #include "ctdb_private.h"
33 struct ctdbd_connection {
34 struct messaging_context *msg_ctx;
38 struct packet_context *pkt;
41 void (*release_ip_handler)(const char *ip_addr, void *private_data);
42 void *release_ip_priv;
45 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
46 uint32_t vnn, uint32 opcode,
47 uint64_t srvid, uint32_t flags, TDB_DATA data,
48 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
52 * exit on fatal communications errors with the ctdbd daemon
54 static void cluster_fatal(const char *why)
56 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
57 /* we don't use smb_panic() as we don't want to delay to write
58 a core file. We need to release this process id immediately
59 so that someone else can take over without getting sharing
67 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
69 if (DEBUGLEVEL < 10) {
72 DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
73 (int)hdr->length, (int)hdr->ctdb_magic,
74 (int)hdr->ctdb_version, (int)hdr->generation,
75 (int)hdr->operation, (int)hdr->reqid));
79 * Register a srvid with ctdbd
81 static NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn,
86 return ctdbd_control(conn, CTDB_CURRENT_NODE,
87 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
88 tdb_null, NULL, NULL, &cstatus);
92 * get our vnn from the cluster
94 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
98 status = ctdbd_control(conn,
99 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
100 tdb_null, NULL, NULL, &cstatus);
101 if (!NT_STATUS_IS_OK(status)) {
102 cluster_fatal("ctdbd_control failed\n");
104 *vnn = (uint32_t)cstatus;
108 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
110 return conn->our_vnn;
114 * Get us a ctdb connection
117 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
118 struct packet_context **presult)
120 struct packet_context *result;
121 const char *sockname = lp_ctdbd_socket();
122 struct sockaddr_un addr;
125 if (!sockname || !*sockname) {
126 sockname = CTDB_PATH;
129 fd = socket(AF_UNIX, SOCK_STREAM, 0);
131 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
132 return map_nt_error_from_unix(errno);
136 addr.sun_family = AF_UNIX;
137 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
139 if (sys_connect(fd, (struct sockaddr *)(void *)&addr) == -1) {
140 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
143 return map_nt_error_from_unix(errno);
146 if (!(result = packet_init(mem_ctx, fd))) {
148 return NT_STATUS_NO_MEMORY;
156 * Do we have a complete ctdb packet in the queue?
159 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
165 if (available < sizeof(msglen)) {
169 msglen = *((uint32 *)buf);
171 DEBUG(10, ("msglen = %d\n", msglen));
173 if (msglen < sizeof(struct ctdb_req_header)) {
174 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
175 "the req_header\n", (int)msglen,
176 (int)sizeof(struct ctdb_req_header)));
177 cluster_fatal("ctdbd protocol error\n");
180 if (available < msglen) {
189 * State necessary to defer an incoming message while we are waiting for a
193 struct deferred_msg_state {
194 struct messaging_context *msg_ctx;
195 struct messaging_rec *rec;
199 * Timed event handler for the deferred message
202 static void deferred_message_dispatch(struct event_context *event_ctx,
203 struct timed_event *te,
207 struct deferred_msg_state *state = talloc_get_type_abort(
208 private_data, struct deferred_msg_state);
210 messaging_dispatch_rec(state->msg_ctx, state->rec);
215 struct req_pull_state {
221 * Pull a ctdb request out of the incoming packet queue
224 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
227 struct req_pull_state *state = (struct req_pull_state *)private_data;
229 state->req.data = talloc_move(state->mem_ctx, &buf);
230 state->req.length = length;
235 * Fetch a messaging_rec from an incoming ctdb style message
238 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
239 size_t overall_length,
240 struct ctdb_req_message *msg)
242 struct messaging_rec *result;
244 enum ndr_err_code ndr_err;
246 if ((overall_length < offsetof(struct ctdb_req_message, data))
248 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
250 cluster_fatal("got invalid msg length");
253 if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
254 DEBUG(0, ("talloc failed\n"));
258 blob = data_blob_const(msg->data, msg->datalen);
260 ndr_err = ndr_pull_struct_blob(
261 &blob, result, result,
262 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
264 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
265 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
266 ndr_errstr(ndr_err)));
271 if (DEBUGLEVEL >= 10) {
272 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
273 NDR_PRINT_DEBUG(messaging_rec, result);
279 static NTSTATUS ctdb_packet_fd_read_sync(struct packet_context *ctx)
281 struct timeval timeout;
282 struct timeval *ptimeout;
284 timeout = timeval_set(lp_ctdb_timeout(), 0);
285 ptimeout = (timeout.tv_sec != 0) ? &timeout : NULL;
287 return packet_fd_read_sync(ctx, ptimeout);
291 * Read a full ctdbd request. If we have a messaging context, defer incoming
292 * messages that might come in between.
295 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
296 TALLOC_CTX *mem_ctx, void *result)
298 struct ctdb_req_header *hdr;
299 struct req_pull_state state;
304 status = ctdb_packet_fd_read_sync(conn->pkt);
306 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
309 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
314 if (!NT_STATUS_IS_OK(status)) {
315 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
316 cluster_fatal("ctdbd died\n");
322 state.mem_ctx = mem_ctx;
324 if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
329 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
333 if (!NT_STATUS_IS_OK(status)) {
334 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
335 cluster_fatal("ctdbd died\n");
338 hdr = (struct ctdb_req_header *)state.req.data;
340 DEBUG(10, ("Received ctdb packet\n"));
341 ctdb_packet_dump(hdr);
343 if (hdr->operation == CTDB_REQ_MESSAGE) {
344 struct timed_event *evt;
345 struct deferred_msg_state *msg_state;
346 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
348 if (conn->msg_ctx == NULL) {
349 DEBUG(1, ("Got a message without having a msg ctx, "
350 "dropping msg %llu\n",
351 (long long unsigned)msg->srvid));
355 if ((conn->release_ip_handler != NULL)
356 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
357 /* must be dispatched immediately */
358 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
359 conn->release_ip_handler((const char *)msg->data,
360 conn->release_ip_priv);
365 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
366 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
368 DEBUG(1, ("ctdb_read_req: Got %s message\n",
369 (msg->srvid == CTDB_SRVID_RECONFIGURE)
370 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
372 messaging_send(conn->msg_ctx,
373 messaging_server_id(conn->msg_ctx),
374 MSG_SMB_BRL_VALIDATE, &data_blob_null);
375 messaging_send(conn->msg_ctx,
376 messaging_server_id(conn->msg_ctx),
377 MSG_DBWRAP_G_LOCK_RETRY,
383 msg_state = TALLOC_P(NULL, struct deferred_msg_state);
384 if (msg_state == NULL) {
385 DEBUG(0, ("talloc failed\n"));
390 if (!(msg_state->rec = ctdb_pull_messaging_rec(
391 msg_state, state.req.length, msg))) {
392 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
393 TALLOC_FREE(msg_state);
400 msg_state->msg_ctx = conn->msg_ctx;
403 * We're waiting for a call reply, but an async message has
404 * crossed. Defer dispatching to the toplevel event loop.
406 evt = event_add_timed(conn->msg_ctx->event_ctx,
407 conn->msg_ctx->event_ctx,
409 deferred_message_dispatch,
412 DEBUG(0, ("event_add_timed failed\n"));
413 TALLOC_FREE(msg_state);
421 if (hdr->reqid != reqid) {
422 /* we got the wrong reply */
423 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
424 "been %u\n", hdr->reqid, reqid));
429 *((void **)result) = talloc_move(mem_ctx, &hdr);
435 * Get us a ctdbd connection
438 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
439 struct ctdbd_connection **pconn)
441 struct ctdbd_connection *conn;
444 if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
445 DEBUG(0, ("talloc failed\n"));
446 return NT_STATUS_NO_MEMORY;
449 status = ctdbd_connect(conn, &conn->pkt);
451 if (!NT_STATUS_IS_OK(status)) {
452 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
456 status = get_cluster_vnn(conn, &conn->our_vnn);
458 if (!NT_STATUS_IS_OK(status)) {
459 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
463 generate_random_buffer((unsigned char *)&conn->rand_srvid,
464 sizeof(conn->rand_srvid));
466 status = register_with_ctdbd(conn, conn->rand_srvid);
468 if (!NT_STATUS_IS_OK(status)) {
469 DEBUG(5, ("Could not register random srvid: %s\n",
483 * Get us a ctdbd connection and register us as a process
486 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
487 struct ctdbd_connection **pconn)
489 struct ctdbd_connection *conn;
492 status = ctdbd_init_connection(mem_ctx, &conn);
494 if (!NT_STATUS_IS_OK(status)) {
498 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
499 if (!NT_STATUS_IS_OK(status)) {
503 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
504 if (!NT_STATUS_IS_OK(status)) {
508 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
509 if (!NT_STATUS_IS_OK(status)) {
521 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
523 return conn->msg_ctx;
526 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
528 return packet_get_fd(conn->pkt);
532 * Packet handler to receive and handle a ctdb message
534 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
537 struct ctdbd_connection *conn = talloc_get_type_abort(
538 private_data, struct ctdbd_connection);
539 struct ctdb_req_message *msg;
540 struct messaging_rec *msg_rec;
542 msg = (struct ctdb_req_message *)buf;
544 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
545 DEBUG(0, ("Received async msg of type %u, discarding\n",
546 msg->hdr.operation));
548 return NT_STATUS_INVALID_PARAMETER;
551 if ((conn->release_ip_handler != NULL)
552 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
553 /* must be dispatched immediately */
554 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
555 conn->release_ip_handler((const char *)msg->data,
556 conn->release_ip_priv);
561 SMB_ASSERT(conn->msg_ctx != NULL);
563 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
564 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
565 DEBUG(0,("Got cluster reconfigure message\n"));
567 * when the cluster is reconfigured or someone of the
568 * family has passed away (SAMBA_NOTIFY), we need to
569 * clean the brl database
571 messaging_send(conn->msg_ctx,
572 messaging_server_id(conn->msg_ctx),
573 MSG_SMB_BRL_VALIDATE, &data_blob_null);
575 messaging_send(conn->msg_ctx,
576 messaging_server_id(conn->msg_ctx),
577 MSG_DBWRAP_G_LOCK_RETRY,
584 /* only messages to our pid or the broadcast are valid here */
585 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
586 DEBUG(0,("Got unexpected message with srvid=%llu\n",
587 (unsigned long long)msg->srvid));
592 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
593 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
595 return NT_STATUS_NO_MEMORY;
598 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
600 TALLOC_FREE(msg_rec);
606 * The ctdbd socket is readable asynchronuously
609 static void ctdbd_socket_handler(struct event_context *event_ctx,
610 struct fd_event *event,
614 struct ctdbd_connection *conn = talloc_get_type_abort(
615 private_data, struct ctdbd_connection);
619 status = packet_fd_read(conn->pkt);
621 if (!NT_STATUS_IS_OK(status)) {
622 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
623 cluster_fatal("ctdbd died\n");
626 while (packet_handler(conn->pkt, ctdb_req_complete,
627 ctdb_handle_message, conn, &status)) {
628 if (!NT_STATUS_IS_OK(status)) {
629 DEBUG(10, ("could not handle incoming message: %s\n",
636 * Prepare a ctdbd connection to receive messages
639 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
640 struct messaging_context *msg_ctx)
642 SMB_ASSERT(conn->msg_ctx == NULL);
643 SMB_ASSERT(conn->fde == NULL);
645 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
646 packet_get_fd(conn->pkt),
648 ctdbd_socket_handler,
650 DEBUG(0, ("event_add_fd failed\n"));
651 return NT_STATUS_NO_MEMORY;
654 conn->msg_ctx = msg_ctx;
660 * Send a messaging message across a ctdbd
663 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
664 uint32 dst_vnn, uint64 dst_srvid,
665 struct messaging_rec *msg)
667 struct ctdb_req_message r;
671 enum ndr_err_code ndr_err;
673 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
674 DEBUG(0, ("talloc failed\n"));
675 return NT_STATUS_NO_MEMORY;
678 ndr_err = ndr_push_struct_blob(
680 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
682 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
683 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
684 ndr_errstr(ndr_err)));
685 status = ndr_map_error2ntstatus(ndr_err);
689 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
690 r.hdr.ctdb_magic = CTDB_MAGIC;
691 r.hdr.ctdb_version = CTDB_VERSION;
692 r.hdr.generation = 1;
693 r.hdr.operation = CTDB_REQ_MESSAGE;
694 r.hdr.destnode = dst_vnn;
695 r.hdr.srcnode = conn->our_vnn;
698 r.datalen = blob.length;
700 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
701 ctdb_packet_dump(&r.hdr);
703 status = packet_send(
705 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
708 if (!NT_STATUS_IS_OK(status)) {
709 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
713 status = packet_flush(conn->pkt);
715 if (!NT_STATUS_IS_OK(status)) {
716 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
717 cluster_fatal("cluster dispatch daemon msg write error\n");
720 status = NT_STATUS_OK;
722 TALLOC_FREE(mem_ctx);
727 * send/recv a generic ctdb control message
729 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
730 uint32_t vnn, uint32 opcode,
731 uint64_t srvid, uint32_t flags,
733 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
736 struct ctdb_req_control req;
737 struct ctdb_reply_control *reply = NULL;
738 struct ctdbd_connection *new_conn = NULL;
741 /* the samba3 ctdb code can't handle NOREPLY yet */
742 flags &= ~CTDB_CTRL_FLAG_NOREPLY;
745 status = ctdbd_init_connection(NULL, &new_conn);
747 if (!NT_STATUS_IS_OK(status)) {
748 DEBUG(10, ("Could not init temp connection: %s\n",
757 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
758 req.hdr.ctdb_magic = CTDB_MAGIC;
759 req.hdr.ctdb_version = CTDB_VERSION;
760 req.hdr.operation = CTDB_REQ_CONTROL;
761 req.hdr.reqid = ++conn->reqid;
762 req.hdr.destnode = vnn;
765 req.datalen = data.dsize;
767 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
768 ctdb_packet_dump(&req.hdr);
770 status = packet_send(
772 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
773 data_blob_const(data.dptr, data.dsize));
775 if (!NT_STATUS_IS_OK(status)) {
776 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
780 status = packet_flush(conn->pkt);
782 if (!NT_STATUS_IS_OK(status)) {
783 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
784 cluster_fatal("cluster dispatch daemon control write error\n");
787 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
788 TALLOC_FREE(new_conn);
792 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
794 if (!NT_STATUS_IS_OK(status)) {
795 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
799 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
800 DEBUG(0, ("received invalid reply\n"));
805 if (!(outdata->dptr = (uint8 *)talloc_memdup(
806 mem_ctx, reply->data, reply->datalen))) {
808 return NT_STATUS_NO_MEMORY;
810 outdata->dsize = reply->datalen;
813 (*cstatus) = reply->status;
816 status = NT_STATUS_OK;
819 TALLOC_FREE(new_conn);
825 * see if a remote process exists
827 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
833 data.dptr = (uint8_t*)&pid;
834 data.dsize = sizeof(pid);
836 status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
837 data, NULL, NULL, &cstatus);
838 if (!NT_STATUS_IS_OK(status)) {
839 DEBUG(0, (__location__ " ctdb_control for process_exists "
850 char *ctdbd_dbpath(struct ctdbd_connection *conn,
851 TALLOC_CTX *mem_ctx, uint32_t db_id)
857 data.dptr = (uint8_t*)&db_id;
858 data.dsize = sizeof(db_id);
860 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
861 CTDB_CONTROL_GETDBPATH, 0, 0, data,
862 mem_ctx, &data, &cstatus);
863 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
864 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
868 return (char *)data.dptr;
872 * attach to a ctdb database
874 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
875 const char *name, uint32_t *db_id, int tdb_flags)
880 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
882 data.dptr = (uint8_t*)name;
883 data.dsize = strlen(name)+1;
885 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
887 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
888 : CTDB_CONTROL_DB_ATTACH,
889 tdb_flags, 0, data, NULL, &data, &cstatus);
890 if (!NT_STATUS_IS_OK(status)) {
891 DEBUG(0, (__location__ " ctdb_control for db_attach "
892 "failed: %s\n", nt_errstr(status)));
896 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
897 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
898 return NT_STATUS_INTERNAL_ERROR;
901 *db_id = *(uint32_t *)data.dptr;
902 talloc_free(data.dptr);
904 if (!(tdb_flags & TDB_SEQNUM)) {
908 data.dptr = (uint8_t *)db_id;
909 data.dsize = sizeof(*db_id);
911 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
912 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
913 NULL, NULL, &cstatus);
914 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
915 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
917 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
925 * force the migration of a record to this node
927 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
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;
941 req.flags = CTDB_IMMEDIATE_MIGRATION;
942 req.callid = CTDB_NULL_FUNC;
944 req.keylen = key.dsize;
946 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
947 ctdb_packet_dump(&req.hdr);
949 status = packet_send(
951 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
952 data_blob_const(key.dptr, key.dsize));
954 if (!NT_STATUS_IS_OK(status)) {
955 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
959 status = packet_flush(conn->pkt);
961 if (!NT_STATUS_IS_OK(status)) {
962 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
963 cluster_fatal("cluster dispatch daemon control write error\n");
966 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
968 if (!NT_STATUS_IS_OK(status)) {
969 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
973 if (reply->hdr.operation != CTDB_REPLY_CALL) {
974 DEBUG(0, ("received invalid reply\n"));
975 status = NT_STATUS_INTERNAL_ERROR;
979 status = NT_STATUS_OK;
987 * remotely fetch a record without locking it or forcing a migration
989 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
990 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
992 struct ctdb_req_call req;
993 struct ctdb_reply_call *reply;
998 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
999 req.hdr.ctdb_magic = CTDB_MAGIC;
1000 req.hdr.ctdb_version = CTDB_VERSION;
1001 req.hdr.operation = CTDB_REQ_CALL;
1002 req.hdr.reqid = ++conn->reqid;
1004 req.callid = CTDB_FETCH_FUNC;
1006 req.keylen = key.dsize;
1008 status = packet_send(
1010 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1011 data_blob_const(key.dptr, key.dsize));
1013 if (!NT_STATUS_IS_OK(status)) {
1014 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
1018 status = packet_flush(conn->pkt);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1022 cluster_fatal("cluster dispatch daemon control write error\n");
1025 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1032 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1033 DEBUG(0, ("received invalid reply\n"));
1034 status = NT_STATUS_INTERNAL_ERROR;
1038 data->dsize = reply->datalen;
1039 if (data->dsize == 0) {
1044 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1046 if (data->dptr == NULL) {
1047 DEBUG(0, ("talloc failed\n"));
1048 status = NT_STATUS_NO_MEMORY;
1053 status = NT_STATUS_OK;
1059 struct ctdbd_traverse_state {
1060 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1065 * Handle a traverse record coming in on the ctdbd connection
1068 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1071 struct ctdbd_traverse_state *state =
1072 (struct ctdbd_traverse_state *)private_data;
1074 struct ctdb_req_message *m;
1075 struct ctdb_rec_data *d;
1078 m = (struct ctdb_req_message *)buf;
1080 if (length < sizeof(*m) || m->hdr.length != length) {
1081 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1083 return NT_STATUS_UNEXPECTED_IO_ERROR;
1086 d = (struct ctdb_rec_data *)&m->data[0];
1087 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1088 DEBUG(0, ("Got invalid traverse data of length %d\n",
1091 return NT_STATUS_UNEXPECTED_IO_ERROR;
1094 key.dsize = d->keylen;
1095 key.dptr = &d->data[0];
1096 data.dsize = d->datalen;
1097 data.dptr = &d->data[d->keylen];
1099 if (key.dsize == 0 && data.dsize == 0) {
1100 /* end of traverse */
1101 return NT_STATUS_END_OF_FILE;
1104 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1105 DEBUG(0, ("Got invalid ltdb header length %d\n",
1108 return NT_STATUS_UNEXPECTED_IO_ERROR;
1110 data.dsize -= sizeof(struct ctdb_ltdb_header);
1111 data.dptr += sizeof(struct ctdb_ltdb_header);
1114 state->fn(key, data, state->private_data);
1118 return NT_STATUS_OK;
1122 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1123 connection to ctdbd to avoid the hairy recursive and async problems with
1127 NTSTATUS ctdbd_traverse(uint32 db_id,
1128 void (*fn)(TDB_DATA key, TDB_DATA data,
1129 void *private_data),
1132 struct ctdbd_connection *conn;
1136 struct ctdb_traverse_start t;
1138 struct ctdbd_traverse_state state;
1140 status = ctdbd_init_connection(NULL, &conn);
1141 if (!NT_STATUS_IS_OK(status)) {
1142 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1143 nt_errstr(status)));
1148 t.srvid = conn->rand_srvid;
1149 t.reqid = ++conn->reqid;
1151 data.dptr = (uint8_t *)&t;
1152 data.dsize = sizeof(t);
1154 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1155 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1156 data, NULL, NULL, &cstatus);
1158 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1160 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1163 if (NT_STATUS_IS_OK(status)) {
1165 * We need a mapping here
1167 status = NT_STATUS_UNSUCCESSFUL;
1173 state.private_data = private_data;
1177 status = NT_STATUS_OK;
1179 if (packet_handler(conn->pkt, ctdb_req_complete,
1180 ctdb_traverse_handler, &state, &status)) {
1182 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1183 status = NT_STATUS_OK;
1188 * There might be more in the queue
1193 if (!NT_STATUS_IS_OK(status)) {
1197 status = ctdb_packet_fd_read_sync(conn->pkt);
1199 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1201 * There might be more in the queue
1206 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1207 status = NT_STATUS_OK;
1211 if (!NT_STATUS_IS_OK(status)) {
1212 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1213 cluster_fatal("ctdbd died\n");
1223 This is used to canonicalize a ctdb_sock_addr structure.
1225 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1226 struct sockaddr_storage *out)
1228 memcpy(out, in, sizeof (*out));
1231 if (in->ss_family == AF_INET6) {
1232 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1233 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1234 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1235 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1236 memset(out, 0, sizeof(*out));
1237 #ifdef HAVE_SOCK_SIN_LEN
1238 out4->sin_len = sizeof(*out);
1240 out4->sin_family = AF_INET;
1241 out4->sin_port = in6->sin6_port;
1242 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
1249 * Register us as a server for a particular tcp connection
1252 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1253 const struct sockaddr_storage *_server,
1254 const struct sockaddr_storage *_client,
1255 void (*release_ip_handler)(const char *ip_addr,
1256 void *private_data),
1260 * we still use ctdb_control_tcp for ipv4
1261 * because we want to work against older ctdb
1262 * versions at runtime
1264 struct ctdb_control_tcp p4;
1265 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1266 struct ctdb_control_tcp_addr p;
1270 struct sockaddr_storage client;
1271 struct sockaddr_storage server;
1274 * Only one connection so far
1276 SMB_ASSERT(conn->release_ip_handler == NULL);
1278 smbd_ctdb_canonicalize_ip(_client, &client);
1279 smbd_ctdb_canonicalize_ip(_server, &server);
1281 switch (client.ss_family) {
1283 p4.dest = *(struct sockaddr_in *)(void *)&server;
1284 p4.src = *(struct sockaddr_in *)(void *)&client;
1285 data.dptr = (uint8_t *)&p4;
1286 data.dsize = sizeof(p4);
1288 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1290 p.dest.ip6 = *(struct sockaddr_in6 *)(void *)&server;
1291 p.src.ip6 = *(struct sockaddr_in6 *)(void *)&client;
1292 data.dptr = (uint8_t *)&p;
1293 data.dsize = sizeof(p);
1297 return NT_STATUS_INTERNAL_ERROR;
1300 conn->release_ip_handler = release_ip_handler;
1303 * We want to be told about IP releases
1306 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1307 if (!NT_STATUS_IS_OK(status)) {
1312 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1313 * can send an extra ack to trigger a reset for our client, so it
1314 * immediately reconnects
1316 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1317 CTDB_CONTROL_TCP_CLIENT, 0,
1318 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1322 * We want to handle reconfigure events
1324 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1326 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1330 call a control on the local node
1332 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
1333 uint64_t srvid, uint32_t flags, TDB_DATA data,
1334 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1337 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1340 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1342 struct ctdb_client_notify_register reg_data;
1347 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1349 reg_data.notify_data[0] = 0;
1351 struct_len = offsetof(struct ctdb_client_notify_register,
1352 notify_data) + reg_data.len;
1354 status = ctdbd_control_local(
1355 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1356 make_tdb_data((uint8_t *)®_data, struct_len),
1357 NULL, NULL, &cstatus);
1358 if (!NT_STATUS_IS_OK(status)) {
1359 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1360 nt_errstr(status)));
1365 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1367 struct ctdb_client_notify_deregister dereg_data;
1371 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1373 status = ctdbd_control_local(
1374 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1375 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1376 NULL, NULL, &cstatus);
1377 if (!NT_STATUS_IS_OK(status)) {
1378 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1379 nt_errstr(status)));