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, uint32_t flags, 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, 0,
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, 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;
108 * Are we active (i.e. not banned or stopped?)
110 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
115 struct ctdb_node_map *m;
116 uint32_t failure_flags;
120 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
121 CTDB_CONTROL_GET_NODEMAP, 0, 0,
122 tdb_null, talloc_tos(), &outdata, &cstatus);
123 if (!NT_STATUS_IS_OK(status)) {
124 cluster_fatal("ctdbd_control failed\n");
126 if ((cstatus != 0) || (outdata.dptr == NULL)) {
127 DEBUG(2, ("Received invalid ctdb data\n"));
131 m = (struct ctdb_node_map *)outdata.dptr;
133 for (i=0; i<m->num; i++) {
134 if (vnn == m->nodes[i].pnn) {
140 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
145 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
146 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
148 if ((m->nodes[i].flags & failure_flags) != 0) {
149 DEBUG(2, ("Node has status %x, not active\n",
150 (int)m->nodes[i].flags));
156 TALLOC_FREE(outdata.dptr);
160 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
162 return conn->our_vnn;
166 * Get us a ctdb connection
169 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
170 struct packet_context **presult)
172 struct packet_context *result;
173 const char *sockname = lp_ctdbd_socket();
174 struct sockaddr_un addr;
177 if (!sockname || !*sockname) {
178 sockname = CTDB_PATH;
181 fd = socket(AF_UNIX, SOCK_STREAM, 0);
183 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
184 return map_nt_error_from_unix(errno);
188 addr.sun_family = AF_UNIX;
189 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
191 if (sys_connect(fd, (struct sockaddr *)&addr) == -1) {
192 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
195 return map_nt_error_from_unix(errno);
198 if (!(result = packet_init(mem_ctx, fd))) {
200 return NT_STATUS_NO_MEMORY;
208 * Do we have a complete ctdb packet in the queue?
211 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
217 if (available < sizeof(msglen)) {
221 msglen = *((uint32 *)buf);
223 DEBUG(10, ("msglen = %d\n", msglen));
225 if (msglen < sizeof(struct ctdb_req_header)) {
226 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
227 "the req_header\n", (int)msglen,
228 (int)sizeof(struct ctdb_req_header)));
229 cluster_fatal("ctdbd protocol error\n");
232 if (available < msglen) {
241 * State necessary to defer an incoming message while we are waiting for a
245 struct deferred_msg_state {
246 struct messaging_context *msg_ctx;
247 struct messaging_rec *rec;
251 * Timed event handler for the deferred message
254 static void deferred_message_dispatch(struct event_context *event_ctx,
255 struct timed_event *te,
259 struct deferred_msg_state *state = talloc_get_type_abort(
260 private_data, struct deferred_msg_state);
262 messaging_dispatch_rec(state->msg_ctx, state->rec);
267 struct req_pull_state {
273 * Pull a ctdb request out of the incoming packet queue
276 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
279 struct req_pull_state *state = (struct req_pull_state *)private_data;
281 state->req.data = talloc_move(state->mem_ctx, &buf);
282 state->req.length = length;
287 * Fetch a messaging_rec from an incoming ctdb style message
290 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
291 size_t overall_length,
292 struct ctdb_req_message *msg)
294 struct messaging_rec *result;
296 enum ndr_err_code ndr_err;
298 if ((overall_length < offsetof(struct ctdb_req_message, data))
300 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
302 cluster_fatal("got invalid msg length");
305 if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
306 DEBUG(0, ("talloc failed\n"));
310 blob = data_blob_const(msg->data, msg->datalen);
312 ndr_err = ndr_pull_struct_blob(
313 &blob, result, NULL, result,
314 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
316 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
317 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
318 ndr_errstr(ndr_err)));
323 if (DEBUGLEVEL >= 10) {
324 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
325 NDR_PRINT_DEBUG(messaging_rec, result);
331 static NTSTATUS ctdb_packet_fd_read_sync(struct packet_context *ctx)
333 struct timeval timeout;
334 struct timeval *ptimeout;
336 timeout = timeval_set(lp_ctdb_timeout(), 0);
337 ptimeout = (timeout.tv_sec != 0) ? &timeout : NULL;
339 return packet_fd_read_sync(ctx, ptimeout);
343 * Read a full ctdbd request. If we have a messaging context, defer incoming
344 * messages that might come in between.
347 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
348 TALLOC_CTX *mem_ctx, void *result)
350 struct ctdb_req_header *hdr;
351 struct req_pull_state state;
356 status = ctdb_packet_fd_read_sync(conn->pkt);
358 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
361 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
366 if (!NT_STATUS_IS_OK(status)) {
367 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
368 cluster_fatal("ctdbd died\n");
374 state.mem_ctx = mem_ctx;
376 if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
381 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
385 if (!NT_STATUS_IS_OK(status)) {
386 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
387 cluster_fatal("ctdbd died\n");
390 hdr = (struct ctdb_req_header *)state.req.data;
392 DEBUG(10, ("Received ctdb packet\n"));
393 ctdb_packet_dump(hdr);
395 if (hdr->operation == CTDB_REQ_MESSAGE) {
396 struct timed_event *evt;
397 struct deferred_msg_state *msg_state;
398 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
400 if (conn->msg_ctx == NULL) {
401 DEBUG(1, ("Got a message without having a msg ctx, "
402 "dropping msg %llu\n",
403 (long long unsigned)msg->srvid));
407 if ((conn->release_ip_handler != NULL)
408 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
409 /* must be dispatched immediately */
410 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
411 conn->release_ip_handler((const char *)msg->data,
412 conn->release_ip_priv);
417 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
418 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
420 DEBUG(1, ("ctdb_read_req: Got %s message\n",
421 (msg->srvid == CTDB_SRVID_RECONFIGURE)
422 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
424 messaging_send(conn->msg_ctx, procid_self(),
425 MSG_SMB_BRL_VALIDATE, &data_blob_null);
426 messaging_send(conn->msg_ctx, procid_self(),
427 MSG_DBWRAP_G_LOCK_RETRY,
433 if (!(msg_state = TALLOC_P(NULL, struct deferred_msg_state))) {
434 DEBUG(0, ("talloc failed\n"));
439 if (!(msg_state->rec = ctdb_pull_messaging_rec(
440 msg_state, state.req.length, msg))) {
441 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
442 TALLOC_FREE(msg_state);
449 msg_state->msg_ctx = conn->msg_ctx;
452 * We're waiting for a call reply, but an async message has
453 * crossed. Defer dispatching to the toplevel event loop.
455 evt = event_add_timed(conn->msg_ctx->event_ctx,
456 conn->msg_ctx->event_ctx,
458 deferred_message_dispatch,
461 DEBUG(0, ("event_add_timed failed\n"));
462 TALLOC_FREE(msg_state);
470 if (hdr->reqid != reqid) {
471 /* we got the wrong reply */
472 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
473 "been %u\n", hdr->reqid, reqid));
478 *((void **)result) = talloc_move(mem_ctx, &hdr);
484 * Get us a ctdbd connection
487 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
488 struct ctdbd_connection **pconn)
490 struct ctdbd_connection *conn;
493 if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
494 DEBUG(0, ("talloc failed\n"));
495 return NT_STATUS_NO_MEMORY;
498 status = ctdbd_connect(conn, &conn->pkt);
500 if (!NT_STATUS_IS_OK(status)) {
501 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
505 status = get_cluster_vnn(conn, &conn->our_vnn);
507 if (!NT_STATUS_IS_OK(status)) {
508 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
512 if (!ctdbd_working(conn, conn->our_vnn)) {
513 DEBUG(2, ("Node is not working, can not connect\n"));
514 status = NT_STATUS_INTERNAL_DB_ERROR;
518 generate_random_buffer((unsigned char *)&conn->rand_srvid,
519 sizeof(conn->rand_srvid));
521 status = register_with_ctdbd(conn, conn->rand_srvid);
523 if (!NT_STATUS_IS_OK(status)) {
524 DEBUG(5, ("Could not register random srvid: %s\n",
538 * Get us a ctdbd connection and register us as a process
541 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
542 struct ctdbd_connection **pconn)
544 struct ctdbd_connection *conn;
547 status = ctdbd_init_connection(mem_ctx, &conn);
549 if (!NT_STATUS_IS_OK(status)) {
553 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
554 if (!NT_STATUS_IS_OK(status)) {
558 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
559 if (!NT_STATUS_IS_OK(status)) {
563 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
564 if (!NT_STATUS_IS_OK(status)) {
576 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
578 return conn->msg_ctx;
581 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
583 return packet_get_fd(conn->pkt);
587 * Packet handler to receive and handle a ctdb message
589 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
592 struct ctdbd_connection *conn = talloc_get_type_abort(
593 private_data, struct ctdbd_connection);
594 struct ctdb_req_message *msg;
595 struct messaging_rec *msg_rec;
597 msg = (struct ctdb_req_message *)buf;
599 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
600 DEBUG(0, ("Received async msg of type %u, discarding\n",
601 msg->hdr.operation));
603 return NT_STATUS_INVALID_PARAMETER;
606 if ((conn->release_ip_handler != NULL)
607 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
608 /* must be dispatched immediately */
609 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
610 conn->release_ip_handler((const char *)msg->data,
611 conn->release_ip_priv);
616 SMB_ASSERT(conn->msg_ctx != NULL);
618 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
619 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
620 DEBUG(0,("Got cluster reconfigure message\n"));
622 * when the cluster is reconfigured or someone of the
623 * family has passed away (SAMBA_NOTIFY), we need to
624 * clean the brl database
626 messaging_send(conn->msg_ctx, procid_self(),
627 MSG_SMB_BRL_VALIDATE, &data_blob_null);
629 messaging_send(conn->msg_ctx, procid_self(),
630 MSG_DBWRAP_G_LOCK_RETRY,
638 /* only messages to our pid or the broadcast are valid here */
639 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
640 DEBUG(0,("Got unexpected message with srvid=%llu\n",
641 (unsigned long long)msg->srvid));
646 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
647 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
649 return NT_STATUS_NO_MEMORY;
652 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
654 TALLOC_FREE(msg_rec);
660 * The ctdbd socket is readable asynchronuously
663 static void ctdbd_socket_handler(struct event_context *event_ctx,
664 struct fd_event *event,
668 struct ctdbd_connection *conn = talloc_get_type_abort(
669 private_data, struct ctdbd_connection);
673 status = packet_fd_read(conn->pkt);
675 if (!NT_STATUS_IS_OK(status)) {
676 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
677 cluster_fatal("ctdbd died\n");
680 while (packet_handler(conn->pkt, ctdb_req_complete,
681 ctdb_handle_message, conn, &status)) {
682 if (!NT_STATUS_IS_OK(status)) {
683 DEBUG(10, ("could not handle incoming message: %s\n",
690 * Prepare a ctdbd connection to receive messages
693 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
694 struct messaging_context *msg_ctx)
696 SMB_ASSERT(conn->msg_ctx == NULL);
697 SMB_ASSERT(conn->fde == NULL);
699 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
700 packet_get_fd(conn->pkt),
702 ctdbd_socket_handler,
704 DEBUG(0, ("event_add_fd failed\n"));
705 return NT_STATUS_NO_MEMORY;
708 conn->msg_ctx = msg_ctx;
714 * Send a messaging message across a ctdbd
717 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
718 uint32 dst_vnn, uint64 dst_srvid,
719 struct messaging_rec *msg)
721 struct ctdb_req_message r;
725 enum ndr_err_code ndr_err;
727 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
728 DEBUG(0, ("talloc failed\n"));
729 return NT_STATUS_NO_MEMORY;
732 ndr_err = ndr_push_struct_blob(
733 &blob, mem_ctx, NULL, msg,
734 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
736 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
737 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
738 ndr_errstr(ndr_err)));
739 status = ndr_map_error2ntstatus(ndr_err);
743 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
744 r.hdr.ctdb_magic = CTDB_MAGIC;
745 r.hdr.ctdb_version = CTDB_VERSION;
746 r.hdr.generation = 1;
747 r.hdr.operation = CTDB_REQ_MESSAGE;
748 r.hdr.destnode = dst_vnn;
749 r.hdr.srcnode = conn->our_vnn;
752 r.datalen = blob.length;
754 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
755 ctdb_packet_dump(&r.hdr);
757 status = packet_send(
759 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
762 if (!NT_STATUS_IS_OK(status)) {
763 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
767 status = packet_flush(conn->pkt);
769 if (!NT_STATUS_IS_OK(status)) {
770 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
771 cluster_fatal("cluster dispatch daemon msg write error\n");
774 status = NT_STATUS_OK;
776 TALLOC_FREE(mem_ctx);
781 * send/recv a generic ctdb control message
783 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
784 uint32_t vnn, uint32 opcode,
785 uint64_t srvid, uint32_t flags,
787 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
790 struct ctdb_req_control req;
791 struct ctdb_reply_control *reply = NULL;
792 struct ctdbd_connection *new_conn = NULL;
795 /* the samba3 ctdb code can't handle NOREPLY yet */
796 flags &= ~CTDB_CTRL_FLAG_NOREPLY;
799 status = ctdbd_init_connection(NULL, &new_conn);
801 if (!NT_STATUS_IS_OK(status)) {
802 DEBUG(10, ("Could not init temp connection: %s\n",
811 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
812 req.hdr.ctdb_magic = CTDB_MAGIC;
813 req.hdr.ctdb_version = CTDB_VERSION;
814 req.hdr.operation = CTDB_REQ_CONTROL;
815 req.hdr.reqid = ++conn->reqid;
816 req.hdr.destnode = vnn;
819 req.datalen = data.dsize;
821 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
822 ctdb_packet_dump(&req.hdr);
824 status = packet_send(
826 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
827 data_blob_const(data.dptr, data.dsize));
829 if (!NT_STATUS_IS_OK(status)) {
830 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
834 status = packet_flush(conn->pkt);
836 if (!NT_STATUS_IS_OK(status)) {
837 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
838 cluster_fatal("cluster dispatch daemon control write error\n");
841 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
842 TALLOC_FREE(new_conn);
846 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
848 if (!NT_STATUS_IS_OK(status)) {
849 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
853 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
854 DEBUG(0, ("received invalid reply\n"));
859 if (!(outdata->dptr = (uint8 *)talloc_memdup(
860 mem_ctx, reply->data, reply->datalen))) {
862 return NT_STATUS_NO_MEMORY;
864 outdata->dsize = reply->datalen;
867 (*cstatus) = reply->status;
870 status = NT_STATUS_OK;
873 TALLOC_FREE(new_conn);
879 * see if a remote process exists
881 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
887 data.dptr = (uint8_t*)&pid;
888 data.dsize = sizeof(pid);
890 status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
891 data, NULL, NULL, &cstatus);
892 if (!NT_STATUS_IS_OK(status)) {
893 DEBUG(0, (__location__ " ctdb_control for process_exists "
904 char *ctdbd_dbpath(struct ctdbd_connection *conn,
905 TALLOC_CTX *mem_ctx, uint32_t db_id)
911 data.dptr = (uint8_t*)&db_id;
912 data.dsize = sizeof(db_id);
914 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
915 CTDB_CONTROL_GETDBPATH, 0, 0, data,
916 mem_ctx, &data, &cstatus);
917 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
918 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
922 return (char *)data.dptr;
926 * attach to a ctdb database
928 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
929 const char *name, uint32_t *db_id, int tdb_flags)
934 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
936 data.dptr = (uint8_t*)name;
937 data.dsize = strlen(name)+1;
939 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
941 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
942 : CTDB_CONTROL_DB_ATTACH,
943 tdb_flags, 0, data, NULL, &data, &cstatus);
944 if (!NT_STATUS_IS_OK(status)) {
945 DEBUG(0, (__location__ " ctdb_control for db_attach "
946 "failed: %s\n", nt_errstr(status)));
950 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
951 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
952 return NT_STATUS_INTERNAL_ERROR;
955 *db_id = *(uint32_t *)data.dptr;
956 talloc_free(data.dptr);
958 if (!(tdb_flags & TDB_SEQNUM)) {
962 data.dptr = (uint8_t *)db_id;
963 data.dsize = sizeof(*db_id);
965 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
966 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
967 NULL, NULL, &cstatus);
968 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
969 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
971 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
979 * force the migration of a record to this node
981 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
984 struct ctdb_req_call req;
985 struct ctdb_reply_call *reply;
990 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
991 req.hdr.ctdb_magic = CTDB_MAGIC;
992 req.hdr.ctdb_version = CTDB_VERSION;
993 req.hdr.operation = CTDB_REQ_CALL;
994 req.hdr.reqid = ++conn->reqid;
995 req.flags = CTDB_IMMEDIATE_MIGRATION;
996 req.callid = CTDB_NULL_FUNC;
998 req.keylen = key.dsize;
1000 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1001 ctdb_packet_dump(&req.hdr);
1003 status = packet_send(
1005 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1006 data_blob_const(key.dptr, key.dsize));
1008 if (!NT_STATUS_IS_OK(status)) {
1009 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
1013 status = packet_flush(conn->pkt);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1017 cluster_fatal("cluster dispatch daemon control write error\n");
1020 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1027 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1028 DEBUG(0, ("received invalid reply\n"));
1029 status = NT_STATUS_INTERNAL_ERROR;
1033 status = NT_STATUS_OK;
1041 * remotely fetch a record without locking it or forcing a migration
1043 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
1044 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
1046 struct ctdb_req_call req;
1047 struct ctdb_reply_call *reply;
1052 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1053 req.hdr.ctdb_magic = CTDB_MAGIC;
1054 req.hdr.ctdb_version = CTDB_VERSION;
1055 req.hdr.operation = CTDB_REQ_CALL;
1056 req.hdr.reqid = ++conn->reqid;
1058 req.callid = CTDB_FETCH_FUNC;
1060 req.keylen = key.dsize;
1062 status = packet_send(
1064 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1065 data_blob_const(key.dptr, key.dsize));
1067 if (!NT_STATUS_IS_OK(status)) {
1068 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
1072 status = packet_flush(conn->pkt);
1074 if (!NT_STATUS_IS_OK(status)) {
1075 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1076 cluster_fatal("cluster dispatch daemon control write error\n");
1079 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1081 if (!NT_STATUS_IS_OK(status)) {
1082 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1086 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1087 DEBUG(0, ("received invalid reply\n"));
1088 status = NT_STATUS_INTERNAL_ERROR;
1092 data->dsize = reply->datalen;
1093 if (data->dsize == 0) {
1098 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1100 if (data->dptr == NULL) {
1101 DEBUG(0, ("talloc failed\n"));
1102 status = NT_STATUS_NO_MEMORY;
1107 status = NT_STATUS_OK;
1113 struct ctdbd_traverse_state {
1114 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1119 * Handle a traverse record coming in on the ctdbd connection
1122 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1125 struct ctdbd_traverse_state *state =
1126 (struct ctdbd_traverse_state *)private_data;
1128 struct ctdb_req_message *m;
1129 struct ctdb_rec_data *d;
1132 m = (struct ctdb_req_message *)buf;
1134 if (length < sizeof(*m) || m->hdr.length != length) {
1135 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1137 return NT_STATUS_UNEXPECTED_IO_ERROR;
1140 d = (struct ctdb_rec_data *)&m->data[0];
1141 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1142 DEBUG(0, ("Got invalid traverse data of length %d\n",
1145 return NT_STATUS_UNEXPECTED_IO_ERROR;
1148 key.dsize = d->keylen;
1149 key.dptr = &d->data[0];
1150 data.dsize = d->datalen;
1151 data.dptr = &d->data[d->keylen];
1153 if (key.dsize == 0 && data.dsize == 0) {
1154 /* end of traverse */
1155 return NT_STATUS_END_OF_FILE;
1158 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1159 DEBUG(0, ("Got invalid ltdb header length %d\n",
1162 return NT_STATUS_UNEXPECTED_IO_ERROR;
1164 data.dsize -= sizeof(struct ctdb_ltdb_header);
1165 data.dptr += sizeof(struct ctdb_ltdb_header);
1168 state->fn(key, data, state->private_data);
1172 return NT_STATUS_OK;
1176 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1177 connection to ctdbd to avoid the hairy recursive and async problems with
1181 NTSTATUS ctdbd_traverse(uint32 db_id,
1182 void (*fn)(TDB_DATA key, TDB_DATA data,
1183 void *private_data),
1186 struct ctdbd_connection *conn;
1190 struct ctdb_traverse_start t;
1192 struct ctdbd_traverse_state state;
1194 status = ctdbd_init_connection(NULL, &conn);
1195 if (!NT_STATUS_IS_OK(status)) {
1196 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1197 nt_errstr(status)));
1202 t.srvid = conn->rand_srvid;
1203 t.reqid = ++conn->reqid;
1205 data.dptr = (uint8_t *)&t;
1206 data.dsize = sizeof(t);
1208 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1209 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1210 data, NULL, NULL, &cstatus);
1212 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1214 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1217 if (NT_STATUS_IS_OK(status)) {
1219 * We need a mapping here
1221 status = NT_STATUS_UNSUCCESSFUL;
1227 state.private_data = private_data;
1231 status = NT_STATUS_OK;
1233 if (packet_handler(conn->pkt, ctdb_req_complete,
1234 ctdb_traverse_handler, &state, &status)) {
1236 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1237 status = NT_STATUS_OK;
1242 * There might be more in the queue
1247 if (!NT_STATUS_IS_OK(status)) {
1251 status = ctdb_packet_fd_read_sync(conn->pkt);
1253 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1255 * There might be more in the queue
1260 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1261 status = NT_STATUS_OK;
1265 if (!NT_STATUS_IS_OK(status)) {
1266 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1267 cluster_fatal("ctdbd died\n");
1277 This is used to canonicalize a ctdb_sock_addr structure.
1279 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1280 struct sockaddr_storage *out)
1282 memcpy(out, in, sizeof (*out));
1285 if (in->ss_family == AF_INET6) {
1286 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1287 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1288 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1289 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1290 memset(out, 0, sizeof(*out));
1291 #ifdef HAVE_SOCK_SIN_LEN
1292 out4->sin_len = sizeof(*out);
1294 out4->sin_family = AF_INET;
1295 out4->sin_port = in6->sin6_port;
1296 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
1303 * Register us as a server for a particular tcp connection
1306 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1307 const struct sockaddr_storage *_server,
1308 const struct sockaddr_storage *_client,
1309 void (*release_ip_handler)(const char *ip_addr,
1310 void *private_data),
1314 * we still use ctdb_control_tcp for ipv4
1315 * because we want to work against older ctdb
1316 * versions at runtime
1318 struct ctdb_control_tcp p4;
1319 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1320 struct ctdb_control_tcp_addr p;
1324 struct sockaddr_storage client;
1325 struct sockaddr_storage server;
1328 * Only one connection so far
1330 SMB_ASSERT(conn->release_ip_handler == NULL);
1332 smbd_ctdb_canonicalize_ip(_client, &client);
1333 smbd_ctdb_canonicalize_ip(_server, &server);
1335 switch (client.ss_family) {
1337 p4.dest = *(struct sockaddr_in *)&server;
1338 p4.src = *(struct sockaddr_in *)&client;
1339 data.dptr = (uint8_t *)&p4;
1340 data.dsize = sizeof(p4);
1342 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1344 p.dest.ip6 = *(struct sockaddr_in6 *)&server;
1345 p.src.ip6 = *(struct sockaddr_in6 *)&client;
1346 data.dptr = (uint8_t *)&p;
1347 data.dsize = sizeof(p);
1351 return NT_STATUS_INTERNAL_ERROR;
1354 conn->release_ip_handler = release_ip_handler;
1357 * We want to be told about IP releases
1360 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1361 if (!NT_STATUS_IS_OK(status)) {
1366 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1367 * can send an extra ack to trigger a reset for our client, so it
1368 * immediately reconnects
1370 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1371 CTDB_CONTROL_TCP_CLIENT, 0,
1372 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1376 * We want to handle reconfigure events
1378 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1380 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1384 call a control on the local node
1386 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
1387 uint64_t srvid, uint32_t flags, TDB_DATA data,
1388 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1391 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1394 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1396 struct ctdb_client_notify_register reg_data;
1401 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1403 reg_data.notify_data[0] = 0;
1405 struct_len = offsetof(struct ctdb_client_notify_register,
1406 notify_data) + reg_data.len;
1408 status = ctdbd_control_local(
1409 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1410 make_tdb_data((uint8_t *)®_data, struct_len),
1411 NULL, NULL, &cstatus);
1412 if (!NT_STATUS_IS_OK(status)) {
1413 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1414 nt_errstr(status)));
1419 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1421 struct ctdb_client_notify_deregister dereg_data;
1425 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1427 status = ctdbd_control_local(
1428 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1429 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1430 NULL, NULL, &cstatus);
1431 if (!NT_STATUS_IS_OK(status)) {
1432 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1433 nt_errstr(status)));
1440 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1441 struct ctdbd_connection **pconn)
1443 return NT_STATUS_NOT_IMPLEMENTED;