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 #ifdef CLUSTER_SUPPORT
26 #include "ctdbd_conn.h"
27 #include "ctdb_packet.h"
30 /* paths to these include files come from --with-ctdb= in configure */
32 #include "ctdb_private.h"
34 struct ctdbd_connection
{
35 struct messaging_context
*msg_ctx
;
39 struct ctdb_packet_context
*pkt
;
42 void (*release_ip_handler
)(const char *ip_addr
, void *private_data
);
43 void *release_ip_priv
;
46 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
47 uint32_t vnn
, uint32 opcode
,
48 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
49 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
53 * exit on fatal communications errors with the ctdbd daemon
55 static void cluster_fatal(const char *why
)
57 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
58 /* we don't use smb_panic() as we don't want to delay to write
59 a core file. We need to release this process id immediately
60 so that someone else can take over without getting sharing
68 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
70 if (DEBUGLEVEL
< 10) {
73 DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
74 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
75 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
76 (int)hdr
->operation
, (int)hdr
->reqid
));
80 * Register a srvid with ctdbd
82 static NTSTATUS
register_with_ctdbd(struct ctdbd_connection
*conn
,
87 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
88 CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
89 tdb_null
, NULL
, NULL
, &cstatus
);
93 * get our vnn from the cluster
95 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32
*vnn
)
99 status
= ctdbd_control(conn
,
100 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_PNN
, 0, 0,
101 tdb_null
, NULL
, NULL
, &cstatus
);
102 if (!NT_STATUS_IS_OK(status
)) {
103 cluster_fatal("ctdbd_control failed\n");
105 *vnn
= (uint32_t)cstatus
;
110 * Are we active (i.e. not banned or stopped?)
112 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
117 struct ctdb_node_map
*m
;
118 uint32_t failure_flags
;
122 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
123 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
124 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
125 if (!NT_STATUS_IS_OK(status
)) {
126 cluster_fatal("ctdbd_control failed\n");
128 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
129 DEBUG(2, ("Received invalid ctdb data\n"));
133 m
= (struct ctdb_node_map
*)outdata
.dptr
;
135 for (i
=0; i
<m
->num
; i
++) {
136 if (vnn
== m
->nodes
[i
].pnn
) {
142 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
147 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
148 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
150 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
151 DEBUG(2, ("Node has status %x, not active\n",
152 (int)m
->nodes
[i
].flags
));
158 TALLOC_FREE(outdata
.dptr
);
162 uint32
ctdbd_vnn(const struct ctdbd_connection
*conn
)
164 return conn
->our_vnn
;
168 * Get us a ctdb connection
171 static NTSTATUS
ctdbd_connect(TALLOC_CTX
*mem_ctx
,
172 struct ctdb_packet_context
**presult
)
174 struct ctdb_packet_context
*result
;
175 const char *sockname
= lp_ctdbd_socket();
176 struct sockaddr_un addr
;
179 if (!sockname
|| !*sockname
) {
180 sockname
= CTDB_PATH
;
183 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
185 DEBUG(3, ("Could not create socket: %s\n", strerror(errno
)));
186 return map_nt_error_from_unix(errno
);
190 addr
.sun_family
= AF_UNIX
;
191 strncpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
193 if (sys_connect(fd
, (struct sockaddr
*)(void *)&addr
) == -1) {
194 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
197 return map_nt_error_from_unix(errno
);
200 if (!(result
= ctdb_packet_init(mem_ctx
, fd
))) {
202 return NT_STATUS_NO_MEMORY
;
210 * Do we have a complete ctdb packet in the queue?
213 static bool ctdb_req_complete(const uint8_t *buf
, size_t available
,
219 if (available
< sizeof(msglen
)) {
223 msglen
= *((uint32
*)buf
);
225 DEBUG(10, ("msglen = %d\n", msglen
));
227 if (msglen
< sizeof(struct ctdb_req_header
)) {
228 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
229 "the req_header\n", (int)msglen
,
230 (int)sizeof(struct ctdb_req_header
)));
231 cluster_fatal("ctdbd protocol error\n");
234 if (available
< msglen
) {
243 * State necessary to defer an incoming message while we are waiting for a
247 struct deferred_msg_state
{
248 struct messaging_context
*msg_ctx
;
249 struct messaging_rec
*rec
;
253 * Timed event handler for the deferred message
256 static void deferred_message_dispatch(struct event_context
*event_ctx
,
257 struct timed_event
*te
,
261 struct deferred_msg_state
*state
= talloc_get_type_abort(
262 private_data
, struct deferred_msg_state
);
264 messaging_dispatch_rec(state
->msg_ctx
, state
->rec
);
269 struct req_pull_state
{
275 * Pull a ctdb request out of the incoming ctdb_packet queue
278 static NTSTATUS
ctdb_req_pull(uint8_t *buf
, size_t length
,
281 struct req_pull_state
*state
= (struct req_pull_state
*)private_data
;
283 state
->req
.data
= talloc_move(state
->mem_ctx
, &buf
);
284 state
->req
.length
= length
;
289 * Fetch a messaging_rec from an incoming ctdb style message
292 static struct messaging_rec
*ctdb_pull_messaging_rec(TALLOC_CTX
*mem_ctx
,
293 size_t overall_length
,
294 struct ctdb_req_message
*msg
)
296 struct messaging_rec
*result
;
298 enum ndr_err_code ndr_err
;
300 if ((overall_length
< offsetof(struct ctdb_req_message
, data
))
302 < offsetof(struct ctdb_req_message
, data
) + msg
->datalen
)) {
304 cluster_fatal("got invalid msg length");
307 if (!(result
= talloc(mem_ctx
, struct messaging_rec
))) {
308 DEBUG(0, ("talloc failed\n"));
312 blob
= data_blob_const(msg
->data
, msg
->datalen
);
314 ndr_err
= ndr_pull_struct_blob(
315 &blob
, result
, result
,
316 (ndr_pull_flags_fn_t
)ndr_pull_messaging_rec
);
318 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
319 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
320 ndr_errstr(ndr_err
)));
325 if (DEBUGLEVEL
>= 10) {
326 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
327 NDR_PRINT_DEBUG(messaging_rec
, result
);
333 static NTSTATUS
ctdb_packet_fd_read_sync(struct ctdb_packet_context
*ctx
)
335 int timeout
= lp_ctdb_timeout();
340 return ctdb_packet_fd_read_sync_timeout(ctx
, timeout
);
344 * Read a full ctdbd request. If we have a messaging context, defer incoming
345 * messages that might come in between.
348 static NTSTATUS
ctdb_read_req(struct ctdbd_connection
*conn
, uint32 reqid
,
349 TALLOC_CTX
*mem_ctx
, void *result
)
351 struct ctdb_req_header
*hdr
;
352 struct req_pull_state state
;
357 status
= ctdb_packet_fd_read_sync(conn
->pkt
);
359 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_BUSY
)) {
362 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
367 if (!NT_STATUS_IS_OK(status
)) {
368 DEBUG(0, ("ctdb_packet_fd_read failed: %s\n", nt_errstr(status
)));
369 cluster_fatal("ctdbd died\n");
375 state
.mem_ctx
= mem_ctx
;
377 if (!ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
, ctdb_req_pull
,
382 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
386 if (!NT_STATUS_IS_OK(status
)) {
387 DEBUG(0, ("Could not read ctdb_packet: %s\n", nt_errstr(status
)));
388 cluster_fatal("ctdbd died\n");
391 hdr
= (struct ctdb_req_header
*)state
.req
.data
;
393 DEBUG(10, ("Received ctdb packet\n"));
394 ctdb_packet_dump(hdr
);
396 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
397 struct timed_event
*evt
;
398 struct deferred_msg_state
*msg_state
;
399 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
401 if (conn
->msg_ctx
== NULL
) {
402 DEBUG(1, ("Got a message without having a msg ctx, "
403 "dropping msg %llu\n",
404 (long long unsigned)msg
->srvid
));
408 if ((conn
->release_ip_handler
!= NULL
)
409 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
410 /* must be dispatched immediately */
411 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
412 conn
->release_ip_handler((const char *)msg
->data
,
413 conn
->release_ip_priv
);
418 if ((msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
419 || (msg
->srvid
== CTDB_SRVID_SAMBA_NOTIFY
)) {
421 DEBUG(1, ("ctdb_read_req: Got %s message\n",
422 (msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
423 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
425 messaging_send(conn
->msg_ctx
,
426 messaging_server_id(conn
->msg_ctx
),
427 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
428 messaging_send(conn
->msg_ctx
,
429 messaging_server_id(conn
->msg_ctx
),
430 MSG_DBWRAP_G_LOCK_RETRY
,
436 msg_state
= talloc(NULL
, struct deferred_msg_state
);
437 if (msg_state
== NULL
) {
438 DEBUG(0, ("talloc failed\n"));
443 if (!(msg_state
->rec
= ctdb_pull_messaging_rec(
444 msg_state
, state
.req
.length
, msg
))) {
445 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
446 TALLOC_FREE(msg_state
);
453 msg_state
->msg_ctx
= conn
->msg_ctx
;
456 * We're waiting for a call reply, but an async message has
457 * crossed. Defer dispatching to the toplevel event loop.
459 evt
= event_add_timed(conn
->msg_ctx
->event_ctx
,
460 conn
->msg_ctx
->event_ctx
,
462 deferred_message_dispatch
,
465 DEBUG(0, ("event_add_timed failed\n"));
466 TALLOC_FREE(msg_state
);
474 if (hdr
->reqid
!= reqid
) {
475 /* we got the wrong reply */
476 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
477 "been %u\n", hdr
->reqid
, reqid
));
482 *((void **)result
) = talloc_move(mem_ctx
, &hdr
);
488 * Get us a ctdbd connection
491 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
492 struct ctdbd_connection
**pconn
)
494 struct ctdbd_connection
*conn
;
497 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
498 DEBUG(0, ("talloc failed\n"));
499 return NT_STATUS_NO_MEMORY
;
502 status
= ctdbd_connect(conn
, &conn
->pkt
);
504 if (!NT_STATUS_IS_OK(status
)) {
505 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status
)));
509 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
511 if (!NT_STATUS_IS_OK(status
)) {
512 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
516 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
517 DEBUG(2, ("Node is not working, can not connect\n"));
518 status
= NT_STATUS_INTERNAL_DB_ERROR
;
522 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
523 sizeof(conn
->rand_srvid
));
525 status
= register_with_ctdbd(conn
, conn
->rand_srvid
);
527 if (!NT_STATUS_IS_OK(status
)) {
528 DEBUG(5, ("Could not register random srvid: %s\n",
542 * Get us a ctdbd connection and register us as a process
545 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
546 struct ctdbd_connection
**pconn
)
548 struct ctdbd_connection
*conn
;
551 status
= ctdbd_init_connection(mem_ctx
, &conn
);
553 if (!NT_STATUS_IS_OK(status
)) {
557 status
= register_with_ctdbd(conn
, (uint64_t)sys_getpid());
558 if (!NT_STATUS_IS_OK(status
)) {
562 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
);
563 if (!NT_STATUS_IS_OK(status
)) {
567 status
= register_with_ctdbd(conn
, CTDB_SRVID_SAMBA_NOTIFY
);
568 if (!NT_STATUS_IS_OK(status
)) {
580 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
582 return conn
->msg_ctx
;
585 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
587 return ctdb_packet_get_fd(conn
->pkt
);
591 * Packet handler to receive and handle a ctdb message
593 static NTSTATUS
ctdb_handle_message(uint8_t *buf
, size_t length
,
596 struct ctdbd_connection
*conn
= talloc_get_type_abort(
597 private_data
, struct ctdbd_connection
);
598 struct ctdb_req_message
*msg
;
599 struct messaging_rec
*msg_rec
;
601 msg
= (struct ctdb_req_message
*)buf
;
603 if (msg
->hdr
.operation
!= CTDB_REQ_MESSAGE
) {
604 DEBUG(0, ("Received async msg of type %u, discarding\n",
605 msg
->hdr
.operation
));
607 return NT_STATUS_INVALID_PARAMETER
;
610 if ((conn
->release_ip_handler
!= NULL
)
611 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
612 /* must be dispatched immediately */
613 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
614 conn
->release_ip_handler((const char *)msg
->data
,
615 conn
->release_ip_priv
);
620 SMB_ASSERT(conn
->msg_ctx
!= NULL
);
622 if ((msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
623 || (msg
->srvid
== CTDB_SRVID_SAMBA_NOTIFY
)){
624 DEBUG(0,("Got cluster reconfigure message\n"));
626 * when the cluster is reconfigured or someone of the
627 * family has passed away (SAMBA_NOTIFY), we need to
628 * clean the brl database
630 messaging_send(conn
->msg_ctx
,
631 messaging_server_id(conn
->msg_ctx
),
632 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
634 messaging_send(conn
->msg_ctx
,
635 messaging_server_id(conn
->msg_ctx
),
636 MSG_DBWRAP_G_LOCK_RETRY
,
643 /* only messages to our pid or the broadcast are valid here */
644 if (msg
->srvid
!= sys_getpid() && msg
->srvid
!= MSG_SRVID_SAMBA
) {
645 DEBUG(0,("Got unexpected message with srvid=%llu\n",
646 (unsigned long long)msg
->srvid
));
651 if (!(msg_rec
= ctdb_pull_messaging_rec(NULL
, length
, msg
))) {
652 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
654 return NT_STATUS_NO_MEMORY
;
657 messaging_dispatch_rec(conn
->msg_ctx
, msg_rec
);
659 TALLOC_FREE(msg_rec
);
665 * The ctdbd socket is readable asynchronuously
668 static void ctdbd_socket_handler(struct event_context
*event_ctx
,
669 struct fd_event
*event
,
673 struct ctdbd_connection
*conn
= talloc_get_type_abort(
674 private_data
, struct ctdbd_connection
);
678 status
= ctdb_packet_fd_read(conn
->pkt
);
680 if (!NT_STATUS_IS_OK(status
)) {
681 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
682 cluster_fatal("ctdbd died\n");
685 while (ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
686 ctdb_handle_message
, conn
, &status
)) {
687 if (!NT_STATUS_IS_OK(status
)) {
688 DEBUG(10, ("could not handle incoming message: %s\n",
695 * Prepare a ctdbd connection to receive messages
698 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
699 struct messaging_context
*msg_ctx
)
701 SMB_ASSERT(conn
->msg_ctx
== NULL
);
702 SMB_ASSERT(conn
->fde
== NULL
);
704 if (!(conn
->fde
= event_add_fd(msg_ctx
->event_ctx
, conn
,
705 ctdb_packet_get_fd(conn
->pkt
),
707 ctdbd_socket_handler
,
709 DEBUG(0, ("event_add_fd failed\n"));
710 return NT_STATUS_NO_MEMORY
;
713 conn
->msg_ctx
= msg_ctx
;
719 * Send a messaging message across a ctdbd
722 NTSTATUS
ctdbd_messaging_send(struct ctdbd_connection
*conn
,
723 uint32 dst_vnn
, uint64 dst_srvid
,
724 struct messaging_rec
*msg
)
726 struct ctdb_req_message r
;
730 enum ndr_err_code ndr_err
;
732 if (!(mem_ctx
= talloc_init("ctdbd_messaging_send"))) {
733 DEBUG(0, ("talloc failed\n"));
734 return NT_STATUS_NO_MEMORY
;
737 ndr_err
= ndr_push_struct_blob(
739 (ndr_push_flags_fn_t
)ndr_push_messaging_rec
);
741 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
742 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
743 ndr_errstr(ndr_err
)));
744 status
= ndr_map_error2ntstatus(ndr_err
);
748 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + blob
.length
;
749 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
750 r
.hdr
.ctdb_version
= CTDB_VERSION
;
751 r
.hdr
.generation
= 1;
752 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
753 r
.hdr
.destnode
= dst_vnn
;
754 r
.hdr
.srcnode
= conn
->our_vnn
;
757 r
.datalen
= blob
.length
;
759 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
760 ctdb_packet_dump(&r
.hdr
);
762 status
= ctdb_packet_send(
764 data_blob_const(&r
, offsetof(struct ctdb_req_message
, data
)),
767 if (!NT_STATUS_IS_OK(status
)) {
768 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
772 status
= ctdb_packet_flush(conn
->pkt
);
774 if (!NT_STATUS_IS_OK(status
)) {
775 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
776 cluster_fatal("cluster dispatch daemon msg write error\n");
779 status
= NT_STATUS_OK
;
781 TALLOC_FREE(mem_ctx
);
786 * send/recv a generic ctdb control message
788 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
789 uint32_t vnn
, uint32 opcode
,
790 uint64_t srvid
, uint32_t flags
,
792 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
795 struct ctdb_req_control req
;
796 struct ctdb_reply_control
*reply
= NULL
;
797 struct ctdbd_connection
*new_conn
= NULL
;
801 status
= ctdbd_init_connection(NULL
, &new_conn
);
803 if (!NT_STATUS_IS_OK(status
)) {
804 DEBUG(10, ("Could not init temp connection: %s\n",
813 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
814 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
815 req
.hdr
.ctdb_version
= CTDB_VERSION
;
816 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
817 req
.hdr
.reqid
= ++conn
->reqid
;
818 req
.hdr
.destnode
= vnn
;
821 req
.datalen
= data
.dsize
;
824 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
825 ctdb_packet_dump(&req
.hdr
);
827 status
= ctdb_packet_send(
829 data_blob_const(&req
, offsetof(struct ctdb_req_control
, data
)),
830 data_blob_const(data
.dptr
, data
.dsize
));
832 if (!NT_STATUS_IS_OK(status
)) {
833 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
837 status
= ctdb_packet_flush(conn
->pkt
);
839 if (!NT_STATUS_IS_OK(status
)) {
840 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
841 cluster_fatal("cluster dispatch daemon control write error\n");
844 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
845 TALLOC_FREE(new_conn
);
852 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
854 if (!NT_STATUS_IS_OK(status
)) {
855 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
859 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
860 DEBUG(0, ("received invalid reply\n"));
865 if (!(outdata
->dptr
= (uint8
*)talloc_memdup(
866 mem_ctx
, reply
->data
, reply
->datalen
))) {
868 return NT_STATUS_NO_MEMORY
;
870 outdata
->dsize
= reply
->datalen
;
873 (*cstatus
) = reply
->status
;
876 status
= NT_STATUS_OK
;
879 TALLOC_FREE(new_conn
);
885 * see if a remote process exists
887 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32 vnn
, pid_t pid
)
893 data
.dptr
= (uint8_t*)&pid
;
894 data
.dsize
= sizeof(pid
);
896 status
= ctdbd_control(conn
, vnn
, CTDB_CONTROL_PROCESS_EXISTS
, 0, 0,
897 data
, NULL
, NULL
, &cstatus
);
898 if (!NT_STATUS_IS_OK(status
)) {
899 DEBUG(0, (__location__
" ctdb_control for process_exists "
910 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
911 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
917 data
.dptr
= (uint8_t*)&db_id
;
918 data
.dsize
= sizeof(db_id
);
920 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
921 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
922 mem_ctx
, &data
, &cstatus
);
923 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
924 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
928 return (char *)data
.dptr
;
932 * attach to a ctdb database
934 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
935 const char *name
, uint32_t *db_id
, int tdb_flags
)
940 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
942 data
.dptr
= (uint8_t*)name
;
943 data
.dsize
= strlen(name
)+1;
945 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
947 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
948 : CTDB_CONTROL_DB_ATTACH
,
949 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
950 if (!NT_STATUS_IS_OK(status
)) {
951 DEBUG(0, (__location__
" ctdb_control for db_attach "
952 "failed: %s\n", nt_errstr(status
)));
956 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
957 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
958 return NT_STATUS_INTERNAL_ERROR
;
961 *db_id
= *(uint32_t *)data
.dptr
;
962 talloc_free(data
.dptr
);
964 if (!(tdb_flags
& TDB_SEQNUM
)) {
968 data
.dptr
= (uint8_t *)db_id
;
969 data
.dsize
= sizeof(*db_id
);
971 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
972 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
973 NULL
, NULL
, &cstatus
);
974 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
975 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
977 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
985 * force the migration of a record to this node
987 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32 db_id
,
990 struct ctdb_req_call req
;
991 struct ctdb_reply_call
*reply
;
996 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
997 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
998 req
.hdr
.ctdb_version
= CTDB_VERSION
;
999 req
.hdr
.operation
= CTDB_REQ_CALL
;
1000 req
.hdr
.reqid
= ++conn
->reqid
;
1001 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1002 req
.callid
= CTDB_NULL_FUNC
;
1004 req
.keylen
= key
.dsize
;
1006 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1007 ctdb_packet_dump(&req
.hdr
);
1009 status
= ctdb_packet_send(
1011 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1012 data_blob_const(key
.dptr
, key
.dsize
));
1014 if (!NT_STATUS_IS_OK(status
)) {
1015 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
1019 status
= ctdb_packet_flush(conn
->pkt
);
1021 if (!NT_STATUS_IS_OK(status
)) {
1022 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1023 cluster_fatal("cluster dispatch daemon control write error\n");
1026 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1028 if (!NT_STATUS_IS_OK(status
)) {
1029 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1033 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1034 DEBUG(0, ("received invalid reply\n"));
1035 status
= NT_STATUS_INTERNAL_ERROR
;
1039 status
= NT_STATUS_OK
;
1047 * remotely fetch a record without locking it or forcing a migration
1049 NTSTATUS
ctdbd_fetch(struct ctdbd_connection
*conn
, uint32 db_id
,
1050 TDB_DATA key
, TALLOC_CTX
*mem_ctx
, TDB_DATA
*data
)
1052 struct ctdb_req_call req
;
1053 struct ctdb_reply_call
*reply
;
1058 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1059 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1060 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1061 req
.hdr
.operation
= CTDB_REQ_CALL
;
1062 req
.hdr
.reqid
= ++conn
->reqid
;
1064 req
.callid
= CTDB_FETCH_FUNC
;
1066 req
.keylen
= key
.dsize
;
1068 status
= ctdb_packet_send(
1070 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1071 data_blob_const(key
.dptr
, key
.dsize
));
1073 if (!NT_STATUS_IS_OK(status
)) {
1074 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
1078 status
= ctdb_packet_flush(conn
->pkt
);
1080 if (!NT_STATUS_IS_OK(status
)) {
1081 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1082 cluster_fatal("cluster dispatch daemon control write error\n");
1085 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1087 if (!NT_STATUS_IS_OK(status
)) {
1088 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1092 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1093 DEBUG(0, ("received invalid reply\n"));
1094 status
= NT_STATUS_INTERNAL_ERROR
;
1098 data
->dsize
= reply
->datalen
;
1099 if (data
->dsize
== 0) {
1104 data
->dptr
= (uint8
*)talloc_memdup(mem_ctx
, &reply
->data
[0],
1106 if (data
->dptr
== NULL
) {
1107 DEBUG(0, ("talloc failed\n"));
1108 status
= NT_STATUS_NO_MEMORY
;
1113 status
= NT_STATUS_OK
;
1119 struct ctdbd_traverse_state
{
1120 void (*fn
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1125 * Handle a traverse record coming in on the ctdbd connection
1128 static NTSTATUS
ctdb_traverse_handler(uint8_t *buf
, size_t length
,
1131 struct ctdbd_traverse_state
*state
=
1132 (struct ctdbd_traverse_state
*)private_data
;
1134 struct ctdb_req_message
*m
;
1135 struct ctdb_rec_data
*d
;
1138 m
= (struct ctdb_req_message
*)buf
;
1140 if (length
< sizeof(*m
) || m
->hdr
.length
!= length
) {
1141 DEBUG(0, ("Got invalid message of length %d\n", (int)length
));
1143 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1146 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1147 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1148 DEBUG(0, ("Got invalid traverse data of length %d\n",
1151 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1154 key
.dsize
= d
->keylen
;
1155 key
.dptr
= &d
->data
[0];
1156 data
.dsize
= d
->datalen
;
1157 data
.dptr
= &d
->data
[d
->keylen
];
1159 if (key
.dsize
== 0 && data
.dsize
== 0) {
1160 /* end of traverse */
1161 return NT_STATUS_END_OF_FILE
;
1164 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1165 DEBUG(0, ("Got invalid ltdb header length %d\n",
1168 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1170 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1171 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1174 state
->fn(key
, data
, state
->private_data
);
1178 return NT_STATUS_OK
;
1182 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1183 connection to ctdbd to avoid the hairy recursive and async problems with
1187 NTSTATUS
ctdbd_traverse(uint32 db_id
,
1188 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1189 void *private_data
),
1192 struct ctdbd_connection
*conn
;
1196 struct ctdb_traverse_start t
;
1198 struct ctdbd_traverse_state state
;
1200 status
= ctdbd_init_connection(NULL
, &conn
);
1201 if (!NT_STATUS_IS_OK(status
)) {
1202 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1203 nt_errstr(status
)));
1208 t
.srvid
= conn
->rand_srvid
;
1209 t
.reqid
= ++conn
->reqid
;
1211 data
.dptr
= (uint8_t *)&t
;
1212 data
.dsize
= sizeof(t
);
1214 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1215 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1216 data
, NULL
, NULL
, &cstatus
);
1218 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1220 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1223 if (NT_STATUS_IS_OK(status
)) {
1225 * We need a mapping here
1227 status
= NT_STATUS_UNSUCCESSFUL
;
1233 state
.private_data
= private_data
;
1237 status
= NT_STATUS_OK
;
1239 if (ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
1240 ctdb_traverse_handler
, &state
, &status
)) {
1242 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1243 status
= NT_STATUS_OK
;
1248 * There might be more in the queue
1253 if (!NT_STATUS_IS_OK(status
)) {
1257 status
= ctdb_packet_fd_read_sync(conn
->pkt
);
1259 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1261 * There might be more in the queue
1266 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1267 status
= NT_STATUS_OK
;
1271 if (!NT_STATUS_IS_OK(status
)) {
1272 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status
)));
1273 cluster_fatal("ctdbd died\n");
1283 This is used to canonicalize a ctdb_sock_addr structure.
1285 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1286 struct sockaddr_storage
*out
)
1288 memcpy(out
, in
, sizeof (*out
));
1291 if (in
->ss_family
== AF_INET6
) {
1292 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1293 const struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)in
;
1294 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1295 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1296 memset(out
, 0, sizeof(*out
));
1297 #ifdef HAVE_SOCK_SIN_LEN
1298 out4
->sin_len
= sizeof(*out
);
1300 out4
->sin_family
= AF_INET
;
1301 out4
->sin_port
= in6
->sin6_port
;
1302 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr32
[3], 4);
1309 * Register us as a server for a particular tcp connection
1312 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1313 const struct sockaddr_storage
*_server
,
1314 const struct sockaddr_storage
*_client
,
1315 void (*release_ip_handler
)(const char *ip_addr
,
1316 void *private_data
),
1320 * we still use ctdb_control_tcp for ipv4
1321 * because we want to work against older ctdb
1322 * versions at runtime
1324 struct ctdb_control_tcp p4
;
1325 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1326 struct ctdb_control_tcp_addr p
;
1330 struct sockaddr_storage client
;
1331 struct sockaddr_storage server
;
1334 * Only one connection so far
1336 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1338 smbd_ctdb_canonicalize_ip(_client
, &client
);
1339 smbd_ctdb_canonicalize_ip(_server
, &server
);
1341 switch (client
.ss_family
) {
1343 p4
.dest
= *(struct sockaddr_in
*)(void *)&server
;
1344 p4
.src
= *(struct sockaddr_in
*)(void *)&client
;
1345 data
.dptr
= (uint8_t *)&p4
;
1346 data
.dsize
= sizeof(p4
);
1348 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1350 p
.dest
.ip6
= *(struct sockaddr_in6
*)(void *)&server
;
1351 p
.src
.ip6
= *(struct sockaddr_in6
*)(void *)&client
;
1352 data
.dptr
= (uint8_t *)&p
;
1353 data
.dsize
= sizeof(p
);
1357 return NT_STATUS_INTERNAL_ERROR
;
1360 conn
->release_ip_handler
= release_ip_handler
;
1362 * store the IP address of the server socket for later
1363 * comparison in release_ip()
1365 conn
->release_ip_priv
= private_data
;
1368 * We want to be told about IP releases
1371 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
);
1372 if (!NT_STATUS_IS_OK(status
)) {
1377 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1378 * can send an extra ack to trigger a reset for our client, so it
1379 * immediately reconnects
1381 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1382 CTDB_CONTROL_TCP_CLIENT
, 0,
1383 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1387 * We want to handle reconfigure events
1389 NTSTATUS
ctdbd_register_reconfigure(struct ctdbd_connection
*conn
)
1391 return register_with_ctdbd(conn
, CTDB_SRVID_RECONFIGURE
);
1395 call a control on the local node
1397 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32 opcode
,
1398 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1399 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1402 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1405 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1407 struct ctdb_client_notify_register reg_data
;
1412 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1414 reg_data
.notify_data
[0] = 0;
1416 struct_len
= offsetof(struct ctdb_client_notify_register
,
1417 notify_data
) + reg_data
.len
;
1419 status
= ctdbd_control_local(
1420 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1421 make_tdb_data((uint8_t *)®_data
, struct_len
),
1422 NULL
, NULL
, &cstatus
);
1423 if (!NT_STATUS_IS_OK(status
)) {
1424 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1425 nt_errstr(status
)));
1430 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1432 struct ctdb_client_notify_deregister dereg_data
;
1436 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1438 status
= ctdbd_control_local(
1439 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1440 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1441 NULL
, NULL
, &cstatus
);
1442 if (!NT_STATUS_IS_OK(status
)) {
1443 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1444 nt_errstr(status
)));