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/>.
24 #include "ctdbd_conn.h"
25 #include "system/select.h"
30 * It is not possible to include ctdb.h and tdb_compat.h (included via
31 * some other include above) without warnings. This fixes those
39 #ifdef typesafe_cb_preargs
40 #undef typesafe_cb_preargs
43 #ifdef typesafe_cb_postargs
44 #undef typesafe_cb_postargs
47 /* paths to these include files come from --with-ctdb= in configure */
50 #include "ctdb_private.h"
52 struct ctdbd_connection {
53 struct messaging_context *msg_ctx;
58 struct tevent_fd *fde;
60 bool (*release_ip_handler)(const char *ip_addr, void *private_data);
61 void *release_ip_priv;
64 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
67 if (conn->reqid == 0) {
73 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
74 uint32_t vnn, uint32_t opcode,
75 uint64_t srvid, uint32_t flags, TDB_DATA data,
76 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
80 * exit on fatal communications errors with the ctdbd daemon
82 static void cluster_fatal(const char *why)
84 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
85 /* we don't use smb_panic() as we don't want to delay to write
86 a core file. We need to release this process id immediately
87 so that someone else can take over without getting sharing
95 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
97 if (DEBUGLEVEL < 11) {
100 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
101 (int)hdr->length, (int)hdr->ctdb_magic,
102 (int)hdr->ctdb_version, (int)hdr->generation,
103 (int)hdr->operation, (int)hdr->reqid));
107 * Register a srvid with ctdbd
109 NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid)
113 return ctdbd_control(conn, CTDB_CURRENT_NODE,
114 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
115 tdb_null, NULL, NULL, &cstatus);
119 * get our vnn from the cluster
121 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
125 status = ctdbd_control(conn,
126 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
127 tdb_null, NULL, NULL, &cstatus);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
132 *vnn = (uint32_t)cstatus;
137 * Are we active (i.e. not banned or stopped?)
139 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
144 struct ctdb_node_map *m;
145 uint32_t failure_flags;
149 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
150 CTDB_CONTROL_GET_NODEMAP, 0, 0,
151 tdb_null, talloc_tos(), &outdata, &cstatus);
152 if (!NT_STATUS_IS_OK(status)) {
153 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
156 if ((cstatus != 0) || (outdata.dptr == NULL)) {
157 DEBUG(2, ("Received invalid ctdb data\n"));
161 m = (struct ctdb_node_map *)outdata.dptr;
163 for (i=0; i<m->num; i++) {
164 if (vnn == m->nodes[i].pnn) {
170 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
175 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
176 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
178 if ((m->nodes[i].flags & failure_flags) != 0) {
179 DEBUG(2, ("Node has status %x, not active\n",
180 (int)m->nodes[i].flags));
186 TALLOC_FREE(outdata.dptr);
190 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
192 return conn->our_vnn;
195 const char *lp_ctdbd_socket(void)
199 ret = lp__ctdbd_socket();
200 if (ret != NULL && strlen(ret) > 0) {
208 * Get us a ctdb connection
211 static int ctdbd_connect(int *pfd)
213 const char *sockname = lp_ctdbd_socket();
214 struct sockaddr_un addr = { 0, };
219 fd = socket(AF_UNIX, SOCK_STREAM, 0);
222 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
226 addr.sun_family = AF_UNIX;
228 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
229 if (namelen >= sizeof(addr.sun_path)) {
230 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
236 salen = sizeof(struct sockaddr_un);
238 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
240 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
251 * State necessary to defer an incoming message while we are waiting for a
255 struct deferred_msg_state {
256 struct messaging_context *msg_ctx;
257 struct messaging_rec *rec;
261 * Timed event handler for the deferred message
264 static void deferred_message_dispatch(struct tevent_context *event_ctx,
265 struct tevent_timer *te,
269 struct deferred_msg_state *state = talloc_get_type_abort(
270 private_data, struct deferred_msg_state);
272 messaging_dispatch_rec(state->msg_ctx, state->rec);
278 * Fetch a messaging_rec from an incoming ctdb style message
281 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
282 size_t overall_length,
283 struct ctdb_req_message *msg)
285 struct messaging_rec *result;
287 enum ndr_err_code ndr_err;
289 if ((overall_length < offsetof(struct ctdb_req_message, data))
291 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
293 cluster_fatal("got invalid msg length");
296 if (!(result = talloc(mem_ctx, struct messaging_rec))) {
297 DEBUG(0, ("talloc failed\n"));
301 blob = data_blob_const(msg->data, msg->datalen);
303 ndr_err = ndr_pull_struct_blob(
304 &blob, result, result,
305 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
307 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
308 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
309 ndr_errstr(ndr_err)));
314 if (DEBUGLEVEL >= 11) {
315 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
316 NDR_PRINT_DEBUG(messaging_rec, result);
322 static NTSTATUS ctdb_read_packet(int fd, TALLOC_CTX *mem_ctx,
323 struct ctdb_req_header **result)
325 int timeout = lp_ctdb_timeout();
326 struct ctdb_req_header *req;
336 ret = poll_one_fd(fd, POLLIN, timeout, &revents);
338 return map_nt_error_from_unix(errno);
341 return NT_STATUS_IO_TIMEOUT;
344 return NT_STATUS_UNEXPECTED_IO_ERROR;
348 status = read_data(fd, (char *)&msglen, sizeof(msglen));
349 if (!NT_STATUS_IS_OK(status)) {
353 if (msglen < sizeof(struct ctdb_req_header)) {
354 return NT_STATUS_UNEXPECTED_IO_ERROR;
357 req = talloc_size(mem_ctx, msglen);
359 return NT_STATUS_NO_MEMORY;
361 talloc_set_name_const(req, "struct ctdb_req_header");
363 req->length = msglen;
365 status = read_data(fd, ((char *)req) + sizeof(msglen),
366 msglen - sizeof(msglen));
367 if (!NT_STATUS_IS_OK(status)) {
376 * Read a full ctdbd request. If we have a messaging context, defer incoming
377 * messages that might come in between.
380 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
382 struct ctdb_req_header **result)
384 struct ctdb_req_header *hdr;
389 status = ctdb_read_packet(conn->fd, mem_ctx, &hdr);
390 if (!NT_STATUS_IS_OK(status)) {
391 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
392 cluster_fatal("ctdbd died\n");
395 DEBUG(11, ("Received ctdb packet\n"));
396 ctdb_packet_dump(hdr);
398 if (hdr->operation == CTDB_REQ_MESSAGE) {
399 struct tevent_timer *evt;
400 struct deferred_msg_state *msg_state;
401 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
403 if (conn->msg_ctx == NULL) {
404 DEBUG(1, ("Got a message without having a msg ctx, "
405 "dropping msg %llu\n",
406 (long long unsigned)msg->srvid));
410 if ((conn->release_ip_handler != NULL)
411 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
414 /* must be dispatched immediately */
415 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
416 ret = conn->release_ip_handler((const char *)msg->data,
417 conn->release_ip_priv);
420 * We need to release the ip,
421 * so return an error to the upper layers.
423 * We make sure we don't trigger this again.
425 conn->release_ip_handler = NULL;
426 conn->release_ip_priv = NULL;
427 return NT_STATUS_ADDRESS_CLOSED;
433 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
434 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
436 DEBUG(1, ("ctdb_read_req: Got %s message\n",
437 (msg->srvid == CTDB_SRVID_RECONFIGURE)
438 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
440 messaging_send(conn->msg_ctx,
441 messaging_server_id(conn->msg_ctx),
442 MSG_SMB_BRL_VALIDATE, &data_blob_null);
447 msg_state = talloc(NULL, struct deferred_msg_state);
448 if (msg_state == NULL) {
449 DEBUG(0, ("talloc failed\n"));
454 if (!(msg_state->rec = ctdb_pull_messaging_rec(
455 msg_state, msg->hdr.length, msg))) {
456 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
457 TALLOC_FREE(msg_state);
464 msg_state->msg_ctx = conn->msg_ctx;
467 * We're waiting for a call reply, but an async message has
468 * crossed. Defer dispatching to the toplevel event loop.
470 evt = tevent_add_timer(messaging_tevent_context(conn->msg_ctx),
471 messaging_tevent_context(conn->msg_ctx),
473 deferred_message_dispatch,
476 DEBUG(0, ("event_add_timed failed\n"));
477 TALLOC_FREE(msg_state);
485 if ((reqid != 0) && (hdr->reqid != reqid)) {
486 /* we got the wrong reply */
487 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
488 "been %u\n", hdr->reqid, reqid));
493 *result = talloc_move(mem_ctx, &hdr);
498 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
504 * Get us a ctdbd connection
507 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
508 struct ctdbd_connection **pconn)
510 struct ctdbd_connection *conn;
514 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
515 DEBUG(0, ("talloc failed\n"));
516 return NT_STATUS_NO_MEMORY;
519 ret = ctdbd_connect(&conn->fd);
521 status = map_nt_error_from_unix(errno);
522 DEBUG(10, ("ctdbd_connect failed: %s\n", strerror(errno)));
525 talloc_set_destructor(conn, ctdbd_connection_destructor);
527 status = get_cluster_vnn(conn, &conn->our_vnn);
529 if (!NT_STATUS_IS_OK(status)) {
530 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
534 if (!ctdbd_working(conn, conn->our_vnn)) {
535 DEBUG(2, ("Node is not working, can not connect\n"));
536 status = NT_STATUS_INTERNAL_DB_ERROR;
540 generate_random_buffer((unsigned char *)&conn->rand_srvid,
541 sizeof(conn->rand_srvid));
543 status = register_with_ctdbd(conn, conn->rand_srvid);
545 if (!NT_STATUS_IS_OK(status)) {
546 DEBUG(5, ("Could not register random srvid: %s\n",
560 * Get us a ctdbd connection and register us as a process
563 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
564 struct ctdbd_connection **pconn)
566 struct ctdbd_connection *conn;
569 status = ctdbd_init_connection(mem_ctx, &conn);
571 if (!NT_STATUS_IS_OK(status)) {
575 status = register_with_ctdbd(conn, (uint64_t)getpid());
576 if (!NT_STATUS_IS_OK(status)) {
580 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
581 if (!NT_STATUS_IS_OK(status)) {
585 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
586 if (!NT_STATUS_IS_OK(status)) {
598 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
600 return conn->msg_ctx;
603 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
609 * Packet handler to receive and handle a ctdb message
611 static NTSTATUS ctdb_handle_message(struct messaging_context *msg_ctx,
612 struct ctdbd_connection *conn,
613 struct ctdb_req_header *hdr)
615 struct ctdb_req_message *msg;
616 struct messaging_rec *msg_rec;
618 if (hdr->operation != CTDB_REQ_MESSAGE) {
619 DEBUG(0, ("Received async msg of type %u, discarding\n",
621 return NT_STATUS_INVALID_PARAMETER;
624 msg = (struct ctdb_req_message *)hdr;
626 if ((conn->release_ip_handler != NULL)
627 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
630 /* must be dispatched immediately */
631 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
632 ret = conn->release_ip_handler((const char *)msg->data,
633 conn->release_ip_priv);
636 * We need to release the ip.
638 * We make sure we don't trigger this again.
640 conn->release_ip_handler = NULL;
641 conn->release_ip_priv = NULL;
646 SMB_ASSERT(conn->msg_ctx != NULL);
648 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
649 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
650 DEBUG(0,("Got cluster reconfigure message\n"));
652 * when the cluster is reconfigured or someone of the
653 * family has passed away (SAMBA_NOTIFY), we need to
654 * clean the brl database
656 messaging_send(conn->msg_ctx,
657 messaging_server_id(conn->msg_ctx),
658 MSG_SMB_BRL_VALIDATE, &data_blob_null);
663 /* only messages to our pid or the broadcast are valid here */
664 if (msg->srvid != getpid() && msg->srvid != MSG_SRVID_SAMBA) {
665 DEBUG(0,("Got unexpected message with srvid=%llu\n",
666 (unsigned long long)msg->srvid));
670 msg_rec = ctdb_pull_messaging_rec(talloc_tos(), msg->hdr.length, msg);
671 if (msg_rec == NULL) {
672 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
673 return NT_STATUS_NO_MEMORY;
675 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
680 * The ctdbd socket is readable asynchronuously
683 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
684 struct tevent_fd *event,
688 struct ctdbd_connection *conn = talloc_get_type_abort(
689 private_data, struct ctdbd_connection);
690 struct ctdb_req_header *hdr;
693 status = ctdb_read_packet(conn->fd, talloc_tos(), &hdr);
694 if (!NT_STATUS_IS_OK(status)) {
695 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
696 cluster_fatal("ctdbd died\n");
699 status = ctdb_handle_message(conn->msg_ctx, conn, hdr);
700 if (!NT_STATUS_IS_OK(status)) {
701 DEBUG(10, ("could not handle incoming message: %s\n",
707 * Prepare a ctdbd connection to receive messages
710 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
711 struct messaging_context *msg_ctx)
713 SMB_ASSERT(conn->msg_ctx == NULL);
714 SMB_ASSERT(conn->fde == NULL);
716 if (!(conn->fde = tevent_add_fd(messaging_tevent_context(msg_ctx),
720 ctdbd_socket_handler,
722 DEBUG(0, ("event_add_fd failed\n"));
723 return NT_STATUS_NO_MEMORY;
726 conn->msg_ctx = msg_ctx;
732 * Send a messaging message across a ctdbd
735 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
736 uint32_t dst_vnn, uint64_t dst_srvid,
737 struct messaging_rec *msg)
741 enum ndr_err_code ndr_err;
743 ndr_err = ndr_push_struct_blob(
744 &blob, talloc_tos(), msg,
745 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
747 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
748 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
749 ndr_errstr(ndr_err)));
750 return ndr_map_error2ntstatus(ndr_err);
753 status = ctdbd_messaging_send_blob(conn, dst_vnn, dst_srvid,
754 blob.data, blob.length);
755 TALLOC_FREE(blob.data);
759 NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
760 uint32_t dst_vnn, uint64_t dst_srvid,
761 const uint8_t *buf, size_t buflen)
763 struct ctdb_req_message r;
767 r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
768 r.hdr.ctdb_magic = CTDB_MAGIC;
769 r.hdr.ctdb_version = CTDB_VERSION;
770 r.hdr.generation = 1;
771 r.hdr.operation = CTDB_REQ_MESSAGE;
772 r.hdr.destnode = dst_vnn;
773 r.hdr.srcnode = conn->our_vnn;
778 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
779 ctdb_packet_dump(&r.hdr);
781 iov[0].iov_base = &r;
782 iov[0].iov_len = offsetof(struct ctdb_req_message, data);
783 iov[1].iov_base = discard_const_p(uint8_t, buf);
784 iov[1].iov_len = buflen;
786 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
787 if (nwritten == -1) {
788 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
789 cluster_fatal("cluster dispatch daemon msg write error\n");
796 * send/recv a generic ctdb control message
798 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
799 uint32_t vnn, uint32_t opcode,
800 uint64_t srvid, uint32_t flags,
802 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
805 struct ctdb_req_control req;
806 struct ctdb_req_header *hdr;
807 struct ctdb_reply_control *reply = NULL;
808 struct ctdbd_connection *new_conn = NULL;
814 status = ctdbd_init_connection(NULL, &new_conn);
816 if (!NT_STATUS_IS_OK(status)) {
817 DEBUG(10, ("Could not init temp connection: %s\n",
826 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
827 req.hdr.ctdb_magic = CTDB_MAGIC;
828 req.hdr.ctdb_version = CTDB_VERSION;
829 req.hdr.operation = CTDB_REQ_CONTROL;
830 req.hdr.reqid = ctdbd_next_reqid(conn);
831 req.hdr.destnode = vnn;
834 req.datalen = data.dsize;
837 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
838 ctdb_packet_dump(&req.hdr);
840 iov[0].iov_base = &req;
841 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
842 iov[1].iov_base = data.dptr;
843 iov[1].iov_len = data.dsize;
845 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
846 if (nwritten == -1) {
847 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
848 cluster_fatal("cluster dispatch daemon msg write error\n");
851 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
852 TALLOC_FREE(new_conn);
859 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
861 if (!NT_STATUS_IS_OK(status)) {
862 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
866 if (hdr->operation != CTDB_REPLY_CONTROL) {
867 DEBUG(0, ("received invalid reply\n"));
870 reply = (struct ctdb_reply_control *)hdr;
873 if (!(outdata->dptr = (uint8 *)talloc_memdup(
874 mem_ctx, reply->data, reply->datalen))) {
876 return NT_STATUS_NO_MEMORY;
878 outdata->dsize = reply->datalen;
881 (*cstatus) = reply->status;
884 status = NT_STATUS_OK;
887 TALLOC_FREE(new_conn);
893 * see if a remote process exists
895 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
903 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
904 DEBUG(10, ("ctdb_processes_exist failed\n"));
910 bool ctdb_processes_exist(struct ctdbd_connection *conn,
911 const struct server_id *pids, int num_pids,
914 TALLOC_CTX *frame = talloc_stackframe();
920 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
921 if (reqids == NULL) {
925 for (i=0; i<num_pids; i++) {
926 struct ctdb_req_control req;
932 reqids[i] = ctdbd_next_reqid(conn);
937 * pids[i].pid is uint64_t, scale down to pid_t which
938 * is the wire protocol towards ctdb.
942 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
943 (int)pids[i].vnn, (int)pid,
946 req.hdr.length = offsetof(struct ctdb_req_control, data);
947 req.hdr.length += sizeof(pid);
948 req.hdr.ctdb_magic = CTDB_MAGIC;
949 req.hdr.ctdb_version = CTDB_VERSION;
950 req.hdr.operation = CTDB_REQ_CONTROL;
951 req.hdr.reqid = reqids[i];
952 req.hdr.destnode = pids[i].vnn;
953 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
955 req.datalen = sizeof(pid);
958 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
959 ctdb_packet_dump(&req.hdr);
961 iov[0].iov_base = &req;
962 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
963 iov[1].iov_base = &pid;
964 iov[1].iov_len = sizeof(pid);
966 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
967 if (nwritten == -1) {
968 status = map_nt_error_from_unix(errno);
969 DEBUG(10, ("write_data_iov failed: %s\n",
977 while (num_received < num_pids) {
978 struct ctdb_req_header *hdr;
979 struct ctdb_reply_control *reply;
982 status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
983 if (!NT_STATUS_IS_OK(status)) {
984 DEBUG(10, ("ctdb_read_req failed: %s\n",
989 if (hdr->operation != CTDB_REPLY_CONTROL) {
990 DEBUG(10, ("Received invalid reply\n"));
993 reply = (struct ctdb_reply_control *)hdr;
995 reqid = reply->hdr.reqid;
997 DEBUG(10, ("Received reqid %d\n", (int)reqid));
999 for (i=0; i<num_pids; i++) {
1000 if (reqid == reqids[i]) {
1004 if (i == num_pids) {
1005 DEBUG(10, ("Received unknown record number %u\n",
1009 results[i] = ((reply->status) == 0);
1020 struct ctdb_vnn_list {
1023 unsigned num_srvids;
1024 unsigned num_filled;
1026 unsigned *pid_indexes;
1030 * Get a list of all vnns mentioned in a list of
1031 * server_ids. vnn_indexes tells where in the vnns array we have to
1034 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
1035 const struct server_id *pids, unsigned num_pids,
1036 struct ctdb_vnn_list **pvnns,
1037 unsigned *pnum_vnns)
1039 struct ctdb_vnn_list *vnns = NULL;
1040 unsigned *vnn_indexes = NULL;
1041 unsigned i, num_vnns = 0;
1043 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
1044 if (vnn_indexes == NULL) {
1045 DEBUG(1, ("talloc_array failed\n"));
1049 for (i=0; i<num_pids; i++) {
1051 uint32_t vnn = pids[i].vnn;
1053 for (j=0; j<num_vnns; j++) {
1054 if (vnn == vnns[j].vnn) {
1062 * Already in the array
1064 vnns[j].num_srvids += 1;
1067 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
1070 DEBUG(1, ("talloc_realloc failed\n"));
1073 vnns[num_vnns].vnn = vnn;
1074 vnns[num_vnns].num_srvids = 1;
1075 vnns[num_vnns].num_filled = 0;
1078 for (i=0; i<num_vnns; i++) {
1079 struct ctdb_vnn_list *vnn = &vnns[i];
1081 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
1082 if (vnn->srvids == NULL) {
1083 DEBUG(1, ("talloc_array failed\n"));
1086 vnn->pid_indexes = talloc_array(vnns, unsigned,
1088 if (vnn->pid_indexes == NULL) {
1089 DEBUG(1, ("talloc_array failed\n"));
1093 for (i=0; i<num_pids; i++) {
1094 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
1095 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
1096 vnn->pid_indexes[vnn->num_filled] = i;
1097 vnn->num_filled += 1;
1100 TALLOC_FREE(vnn_indexes);
1102 *pnum_vnns = num_vnns;
1106 TALLOC_FREE(vnn_indexes);
1110 bool ctdb_serverids_exist_supported(struct ctdbd_connection *conn)
1112 #ifndef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1114 #else /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1116 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1119 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
1120 const struct server_id *pids, unsigned num_pids,
1123 #ifndef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1125 #else /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1126 unsigned i, num_received;
1128 struct ctdb_vnn_list *vnns = NULL;
1131 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1132 &vnns, &num_vnns)) {
1133 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1137 for (i=0; i<num_vnns; i++) {
1138 struct ctdb_vnn_list *vnn = &vnns[i];
1139 struct ctdb_req_control req;
1140 struct iovec iov[2];
1143 vnn->reqid = ctdbd_next_reqid(conn);
1147 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1148 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1150 req.hdr.length = offsetof(struct ctdb_req_control, data);
1151 req.hdr.ctdb_magic = CTDB_MAGIC;
1152 req.hdr.ctdb_version = CTDB_VERSION;
1153 req.hdr.operation = CTDB_REQ_CONTROL;
1154 req.hdr.reqid = vnn->reqid;
1155 req.hdr.destnode = vnn->vnn;
1156 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1158 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1159 req.hdr.length += req.datalen;
1162 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1163 ctdb_packet_dump(&req.hdr);
1165 iov[0].iov_base = &req;
1166 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
1167 iov[1].iov_base = vnn->srvids;
1168 iov[1].iov_len = req.datalen;
1170 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1171 if (nwritten == -1) {
1172 status = map_nt_error_from_unix(errno);
1173 DEBUG(10, ("write_data_iov failed: %s\n",
1181 while (num_received < num_vnns) {
1182 struct ctdb_req_header *hdr;
1183 struct ctdb_reply_control *reply;
1184 struct ctdb_vnn_list *vnn;
1186 uint8_t *reply_data;
1188 status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
1189 if (!NT_STATUS_IS_OK(status)) {
1190 DEBUG(1, ("ctdb_read_req failed: %s\n",
1191 nt_errstr(status)));
1195 if (hdr->operation != CTDB_REPLY_CONTROL) {
1196 DEBUG(1, ("Received invalid reply %u\n",
1197 (unsigned)reply->hdr.operation));
1200 reply = (struct ctdb_reply_control *)hdr;
1202 reqid = reply->hdr.reqid;
1204 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1206 for (i=0; i<num_vnns; i++) {
1207 if (reqid == vnns[i].reqid) {
1211 if (i == num_vnns) {
1212 DEBUG(1, ("Received unknown reqid number %u\n",
1217 DEBUG(10, ("Found index %u\n", i));
1221 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1222 (unsigned)vnn->vnn, vnn->num_srvids,
1223 (unsigned)reply->datalen));
1225 if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
1229 reply_data = reply->data;
1232 * Got an error reply
1234 DEBUG(5, ("Received short reply len %d, status %u, "
1236 (unsigned)reply->datalen,
1237 (unsigned)reply->status,
1238 (unsigned)reply->errorlen));
1239 dump_data(5, reply->data, reply->errorlen);
1242 * This will trigger everything set to false
1247 for (i=0; i<vnn->num_srvids; i++) {
1248 int idx = vnn->pid_indexes[i];
1250 if (pids[i].unique_id ==
1251 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1252 results[idx] = true;
1256 (reply_data != NULL) &&
1257 ((reply_data[i/8] & (1<<(i%8))) != 0);
1267 cluster_fatal("serverids_exist failed");
1269 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1275 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1276 TALLOC_CTX *mem_ctx, uint32_t db_id)
1282 data.dptr = (uint8_t*)&db_id;
1283 data.dsize = sizeof(db_id);
1285 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1286 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1287 mem_ctx, &data, &cstatus);
1288 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1289 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1293 return (char *)data.dptr;
1297 * attach to a ctdb database
1299 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1300 const char *name, uint32_t *db_id, int tdb_flags)
1305 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1307 data = string_term_tdb_data(name);
1309 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1311 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1312 : CTDB_CONTROL_DB_ATTACH,
1313 tdb_flags, 0, data, NULL, &data, &cstatus);
1314 if (!NT_STATUS_IS_OK(status)) {
1315 DEBUG(0, (__location__ " ctdb_control for db_attach "
1316 "failed: %s\n", nt_errstr(status)));
1320 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1321 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1322 return NT_STATUS_INTERNAL_ERROR;
1325 *db_id = *(uint32_t *)data.dptr;
1326 talloc_free(data.dptr);
1328 if (!(tdb_flags & TDB_SEQNUM)) {
1329 return NT_STATUS_OK;
1332 data.dptr = (uint8_t *)db_id;
1333 data.dsize = sizeof(*db_id);
1335 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1336 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1337 NULL, NULL, &cstatus);
1338 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1339 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1341 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1345 return NT_STATUS_OK;
1349 * force the migration of a record to this node
1351 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
1354 struct ctdb_req_call req;
1355 struct ctdb_req_header *hdr;
1356 struct iovec iov[2];
1362 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1363 req.hdr.ctdb_magic = CTDB_MAGIC;
1364 req.hdr.ctdb_version = CTDB_VERSION;
1365 req.hdr.operation = CTDB_REQ_CALL;
1366 req.hdr.reqid = ctdbd_next_reqid(conn);
1367 req.flags = CTDB_IMMEDIATE_MIGRATION;
1368 req.callid = CTDB_NULL_FUNC;
1370 req.keylen = key.dsize;
1372 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1373 ctdb_packet_dump(&req.hdr);
1375 iov[0].iov_base = &req;
1376 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1377 iov[1].iov_base = key.dptr;
1378 iov[1].iov_len = key.dsize;
1380 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1381 if (nwritten == -1) {
1382 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1383 cluster_fatal("cluster dispatch daemon msg write error\n");
1386 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1388 if (!NT_STATUS_IS_OK(status)) {
1389 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1393 if (hdr->operation != CTDB_REPLY_CALL) {
1394 DEBUG(0, ("received invalid reply\n"));
1395 status = NT_STATUS_INTERNAL_ERROR;
1399 status = NT_STATUS_OK;
1407 * Fetch a record and parse it
1409 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
1410 TDB_DATA key, bool local_copy,
1411 void (*parser)(TDB_DATA key, TDB_DATA data,
1412 void *private_data),
1415 struct ctdb_req_call req;
1416 struct ctdb_req_header *hdr = NULL;
1417 struct ctdb_reply_call *reply;
1418 struct iovec iov[2];
1423 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1424 flags = local_copy ? CTDB_WANT_READONLY : 0;
1431 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1432 req.hdr.ctdb_magic = CTDB_MAGIC;
1433 req.hdr.ctdb_version = CTDB_VERSION;
1434 req.hdr.operation = CTDB_REQ_CALL;
1435 req.hdr.reqid = ctdbd_next_reqid(conn);
1437 req.callid = CTDB_FETCH_FUNC;
1439 req.keylen = key.dsize;
1441 iov[0].iov_base = &req;
1442 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1443 iov[1].iov_base = key.dptr;
1444 iov[1].iov_len = key.dsize;
1446 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1447 if (nwritten == -1) {
1448 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1449 cluster_fatal("cluster dispatch daemon msg write error\n");
1452 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1454 if (!NT_STATUS_IS_OK(status)) {
1455 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1459 if (hdr->operation != CTDB_REPLY_CALL) {
1460 DEBUG(0, ("received invalid reply\n"));
1461 status = NT_STATUS_INTERNAL_ERROR;
1464 reply = (struct ctdb_reply_call *)hdr;
1466 if (reply->datalen == 0) {
1468 * Treat an empty record as non-existing
1470 status = NT_STATUS_NOT_FOUND;
1474 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1477 status = NT_STATUS_OK;
1484 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1485 connection to ctdbd to avoid the hairy recursive and async problems with
1489 NTSTATUS ctdbd_traverse(uint32_t db_id,
1490 void (*fn)(TDB_DATA key, TDB_DATA data,
1491 void *private_data),
1494 struct ctdbd_connection *conn;
1498 struct ctdb_traverse_start t;
1502 status = ctdbd_init_connection(NULL, &conn);
1504 if (!NT_STATUS_IS_OK(status)) {
1505 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1506 nt_errstr(status)));
1511 t.srvid = conn->rand_srvid;
1512 t.reqid = ctdbd_next_reqid(conn);
1514 data.dptr = (uint8_t *)&t;
1515 data.dsize = sizeof(t);
1517 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1518 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1519 data, NULL, NULL, &cstatus);
1521 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1523 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1526 if (NT_STATUS_IS_OK(status)) {
1528 * We need a mapping here
1530 status = NT_STATUS_UNSUCCESSFUL;
1537 struct ctdb_req_header *hdr;
1538 struct ctdb_req_message *m;
1539 struct ctdb_rec_data *d;
1541 status = ctdb_read_packet(conn->fd, conn, &hdr);
1542 if (!NT_STATUS_IS_OK(status)) {
1543 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1544 nt_errstr(status)));
1545 cluster_fatal("ctdbd died\n");
1548 if (hdr->operation != CTDB_REQ_MESSAGE) {
1549 DEBUG(0, ("Got operation %u, expected a message\n",
1550 (unsigned)hdr->operation));
1552 return NT_STATUS_UNEXPECTED_IO_ERROR;
1555 m = (struct ctdb_req_message *)hdr;
1556 d = (struct ctdb_rec_data *)&m->data[0];
1557 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1558 DEBUG(0, ("Got invalid traverse data of length %d\n",
1561 return NT_STATUS_UNEXPECTED_IO_ERROR;
1564 key.dsize = d->keylen;
1565 key.dptr = &d->data[0];
1566 data.dsize = d->datalen;
1567 data.dptr = &d->data[d->keylen];
1569 if (key.dsize == 0 && data.dsize == 0) {
1570 /* end of traverse */
1572 return NT_STATUS_OK;
1575 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1576 DEBUG(0, ("Got invalid ltdb header length %d\n",
1579 return NT_STATUS_UNEXPECTED_IO_ERROR;
1581 data.dsize -= sizeof(struct ctdb_ltdb_header);
1582 data.dptr += sizeof(struct ctdb_ltdb_header);
1585 fn(key, data, private_data);
1588 return NT_STATUS_OK;
1592 This is used to canonicalize a ctdb_sock_addr structure.
1594 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1595 struct sockaddr_storage *out)
1597 memcpy(out, in, sizeof (*out));
1600 if (in->ss_family == AF_INET6) {
1601 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1602 const struct sockaddr_in6 *in6 =
1603 (const struct sockaddr_in6 *)in;
1604 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1605 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1606 memset(out, 0, sizeof(*out));
1607 #ifdef HAVE_SOCK_SIN_LEN
1608 out4->sin_len = sizeof(*out);
1610 out4->sin_family = AF_INET;
1611 out4->sin_port = in6->sin6_port;
1612 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1619 * Register us as a server for a particular tcp connection
1622 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1623 const struct sockaddr_storage *_server,
1624 const struct sockaddr_storage *_client,
1625 bool (*release_ip_handler)(const char *ip_addr,
1626 void *private_data),
1630 * we still use ctdb_control_tcp for ipv4
1631 * because we want to work against older ctdb
1632 * versions at runtime
1634 struct ctdb_control_tcp p4;
1635 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1636 struct ctdb_control_tcp_addr p;
1640 struct sockaddr_storage client;
1641 struct sockaddr_storage server;
1644 * Only one connection so far
1646 SMB_ASSERT(conn->release_ip_handler == NULL);
1648 smbd_ctdb_canonicalize_ip(_client, &client);
1649 smbd_ctdb_canonicalize_ip(_server, &server);
1651 switch (client.ss_family) {
1653 memcpy(&p4.dest, &server, sizeof(p4.dest));
1654 memcpy(&p4.src, &client, sizeof(p4.src));
1655 data.dptr = (uint8_t *)&p4;
1656 data.dsize = sizeof(p4);
1658 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1660 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1661 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1662 data.dptr = (uint8_t *)&p;
1663 data.dsize = sizeof(p);
1667 return NT_STATUS_INTERNAL_ERROR;
1670 conn->release_ip_handler = release_ip_handler;
1671 conn->release_ip_priv = private_data;
1674 * We want to be told about IP releases
1677 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1678 if (!NT_STATUS_IS_OK(status)) {
1683 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1684 * can send an extra ack to trigger a reset for our client, so it
1685 * immediately reconnects
1687 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1688 CTDB_CONTROL_TCP_CLIENT, 0,
1689 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1693 * We want to handle reconfigure events
1695 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1697 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1701 call a control on the local node
1703 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1704 uint64_t srvid, uint32_t flags, TDB_DATA data,
1705 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1708 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1711 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1713 struct ctdb_client_notify_register reg_data;
1718 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1720 reg_data.notify_data[0] = 0;
1722 struct_len = offsetof(struct ctdb_client_notify_register,
1723 notify_data) + reg_data.len;
1725 status = ctdbd_control_local(
1726 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1727 make_tdb_data((uint8_t *)®_data, struct_len),
1728 NULL, NULL, &cstatus);
1729 if (!NT_STATUS_IS_OK(status)) {
1730 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1731 nt_errstr(status)));
1736 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1738 struct ctdb_client_notify_deregister dereg_data;
1742 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1744 status = ctdbd_control_local(
1745 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1746 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1747 NULL, NULL, &cstatus);
1748 if (!NT_STATUS_IS_OK(status)) {
1749 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1750 nt_errstr(status)));
1755 NTSTATUS ctdbd_probe(void)
1758 * Do a very early check if ctdbd is around to avoid an abort and core
1761 struct ctdbd_connection *conn = NULL;
1764 status = ctdbd_messaging_connection(talloc_tos(), &conn);
1767 * We only care if we can connect.