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 "ctdbd_conn.h"
29 /* paths to these include files come from --with-ctdb= in configure */
31 #include "ctdb_private.h"
33 struct ctdbd_connection
{
34 struct messaging_context
*msg_ctx
;
38 struct packet_context
*pkt
;
41 void (*release_ip_handler
)(const char *ip_addr
, void *private_data
);
42 void *release_ip_priv
;
45 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
46 uint32_t vnn
, uint32 opcode
,
47 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
48 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
52 * exit on fatal communications errors with the ctdbd daemon
54 static void cluster_fatal(const char *why
)
56 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
57 /* we don't use smb_panic() as we don't want to delay to write
58 a core file. We need to release this process id immediately
59 so that someone else can take over without getting sharing
67 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
69 if (DEBUGLEVEL
< 10) {
72 DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
73 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
74 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
75 (int)hdr
->operation
, (int)hdr
->reqid
));
79 * Register a srvid with ctdbd
81 static NTSTATUS
register_with_ctdbd(struct ctdbd_connection
*conn
,
86 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
87 CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
88 tdb_null
, NULL
, NULL
, &cstatus
);
92 * get our vnn from the cluster
94 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32
*vnn
)
98 status
= ctdbd_control(conn
,
99 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_PNN
, 0, 0,
100 tdb_null
, NULL
, NULL
, &cstatus
);
101 if (!NT_STATUS_IS_OK(status
)) {
102 cluster_fatal("ctdbd_control failed\n");
104 *vnn
= (uint32_t)cstatus
;
109 * Are we active (i.e. not banned or stopped?)
111 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
116 struct ctdb_node_map
*m
;
117 uint32_t failure_flags
;
121 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
122 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
123 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
124 if (!NT_STATUS_IS_OK(status
)) {
125 cluster_fatal("ctdbd_control failed\n");
127 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
128 DEBUG(2, ("Received invalid ctdb data\n"));
132 m
= (struct ctdb_node_map
*)outdata
.dptr
;
134 for (i
=0; i
<m
->num
; i
++) {
135 if (vnn
== m
->nodes
[i
].pnn
) {
141 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
146 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
147 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
149 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
150 DEBUG(2, ("Node has status %x, not active\n",
151 (int)m
->nodes
[i
].flags
));
157 TALLOC_FREE(outdata
.dptr
);
161 uint32
ctdbd_vnn(const struct ctdbd_connection
*conn
)
163 return conn
->our_vnn
;
167 * Get us a ctdb connection
170 static NTSTATUS
ctdbd_connect(TALLOC_CTX
*mem_ctx
,
171 struct packet_context
**presult
)
173 struct packet_context
*result
;
174 const char *sockname
= lp_ctdbd_socket();
175 struct sockaddr_un addr
;
178 if (!sockname
|| !*sockname
) {
179 sockname
= CTDB_PATH
;
182 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
184 DEBUG(3, ("Could not create socket: %s\n", strerror(errno
)));
185 return map_nt_error_from_unix(errno
);
189 addr
.sun_family
= AF_UNIX
;
190 strncpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
192 if (sys_connect(fd
, (struct sockaddr
*)(void *)&addr
) == -1) {
193 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
196 return map_nt_error_from_unix(errno
);
199 if (!(result
= packet_init(mem_ctx
, fd
))) {
201 return NT_STATUS_NO_MEMORY
;
209 * Do we have a complete ctdb packet in the queue?
212 static bool ctdb_req_complete(const uint8_t *buf
, size_t available
,
218 if (available
< sizeof(msglen
)) {
222 msglen
= *((uint32
*)buf
);
224 DEBUG(10, ("msglen = %d\n", msglen
));
226 if (msglen
< sizeof(struct ctdb_req_header
)) {
227 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
228 "the req_header\n", (int)msglen
,
229 (int)sizeof(struct ctdb_req_header
)));
230 cluster_fatal("ctdbd protocol error\n");
233 if (available
< msglen
) {
242 * State necessary to defer an incoming message while we are waiting for a
246 struct deferred_msg_state
{
247 struct messaging_context
*msg_ctx
;
248 struct messaging_rec
*rec
;
252 * Timed event handler for the deferred message
255 static void deferred_message_dispatch(struct event_context
*event_ctx
,
256 struct timed_event
*te
,
260 struct deferred_msg_state
*state
= talloc_get_type_abort(
261 private_data
, struct deferred_msg_state
);
263 messaging_dispatch_rec(state
->msg_ctx
, state
->rec
);
268 struct req_pull_state
{
274 * Pull a ctdb request out of the incoming packet queue
277 static NTSTATUS
ctdb_req_pull(uint8_t *buf
, size_t length
,
280 struct req_pull_state
*state
= (struct req_pull_state
*)private_data
;
282 state
->req
.data
= talloc_move(state
->mem_ctx
, &buf
);
283 state
->req
.length
= length
;
288 * Fetch a messaging_rec from an incoming ctdb style message
291 static struct messaging_rec
*ctdb_pull_messaging_rec(TALLOC_CTX
*mem_ctx
,
292 size_t overall_length
,
293 struct ctdb_req_message
*msg
)
295 struct messaging_rec
*result
;
297 enum ndr_err_code ndr_err
;
299 if ((overall_length
< offsetof(struct ctdb_req_message
, data
))
301 < offsetof(struct ctdb_req_message
, data
) + msg
->datalen
)) {
303 cluster_fatal("got invalid msg length");
306 if (!(result
= TALLOC_P(mem_ctx
, struct messaging_rec
))) {
307 DEBUG(0, ("talloc failed\n"));
311 blob
= data_blob_const(msg
->data
, msg
->datalen
);
313 ndr_err
= ndr_pull_struct_blob(
314 &blob
, result
, result
,
315 (ndr_pull_flags_fn_t
)ndr_pull_messaging_rec
);
317 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
318 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
319 ndr_errstr(ndr_err
)));
324 if (DEBUGLEVEL
>= 10) {
325 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
326 NDR_PRINT_DEBUG(messaging_rec
, result
);
332 static NTSTATUS
ctdb_packet_fd_read_sync(struct packet_context
*ctx
)
334 int timeout
= lp_ctdb_timeout();
339 return packet_fd_read_sync(ctx
, timeout
);
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
,
425 messaging_server_id(conn
->msg_ctx
),
426 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
427 messaging_send(conn
->msg_ctx
,
428 messaging_server_id(conn
->msg_ctx
),
429 MSG_DBWRAP_G_LOCK_RETRY
,
435 msg_state
= TALLOC_P(NULL
, struct deferred_msg_state
);
436 if (msg_state
== NULL
) {
437 DEBUG(0, ("talloc failed\n"));
442 if (!(msg_state
->rec
= ctdb_pull_messaging_rec(
443 msg_state
, state
.req
.length
, msg
))) {
444 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
445 TALLOC_FREE(msg_state
);
452 msg_state
->msg_ctx
= conn
->msg_ctx
;
455 * We're waiting for a call reply, but an async message has
456 * crossed. Defer dispatching to the toplevel event loop.
458 evt
= event_add_timed(conn
->msg_ctx
->event_ctx
,
459 conn
->msg_ctx
->event_ctx
,
461 deferred_message_dispatch
,
464 DEBUG(0, ("event_add_timed failed\n"));
465 TALLOC_FREE(msg_state
);
473 if (hdr
->reqid
!= reqid
) {
474 /* we got the wrong reply */
475 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
476 "been %u\n", hdr
->reqid
, reqid
));
481 *((void **)result
) = talloc_move(mem_ctx
, &hdr
);
487 * Get us a ctdbd connection
490 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
491 struct ctdbd_connection
**pconn
)
493 struct ctdbd_connection
*conn
;
496 if (!(conn
= TALLOC_ZERO_P(mem_ctx
, struct ctdbd_connection
))) {
497 DEBUG(0, ("talloc failed\n"));
498 return NT_STATUS_NO_MEMORY
;
501 status
= ctdbd_connect(conn
, &conn
->pkt
);
503 if (!NT_STATUS_IS_OK(status
)) {
504 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status
)));
508 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
510 if (!NT_STATUS_IS_OK(status
)) {
511 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
515 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
516 DEBUG(2, ("Node is not working, can not connect\n"));
517 status
= NT_STATUS_INTERNAL_DB_ERROR
;
521 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
522 sizeof(conn
->rand_srvid
));
524 status
= register_with_ctdbd(conn
, conn
->rand_srvid
);
526 if (!NT_STATUS_IS_OK(status
)) {
527 DEBUG(5, ("Could not register random srvid: %s\n",
541 * Get us a ctdbd connection and register us as a process
544 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
545 struct ctdbd_connection
**pconn
)
547 struct ctdbd_connection
*conn
;
550 status
= ctdbd_init_connection(mem_ctx
, &conn
);
552 if (!NT_STATUS_IS_OK(status
)) {
556 status
= register_with_ctdbd(conn
, (uint64_t)sys_getpid());
557 if (!NT_STATUS_IS_OK(status
)) {
561 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
);
562 if (!NT_STATUS_IS_OK(status
)) {
566 status
= register_with_ctdbd(conn
, CTDB_SRVID_SAMBA_NOTIFY
);
567 if (!NT_STATUS_IS_OK(status
)) {
579 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
581 return conn
->msg_ctx
;
584 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
586 return packet_get_fd(conn
->pkt
);
590 * Packet handler to receive and handle a ctdb message
592 static NTSTATUS
ctdb_handle_message(uint8_t *buf
, size_t length
,
595 struct ctdbd_connection
*conn
= talloc_get_type_abort(
596 private_data
, struct ctdbd_connection
);
597 struct ctdb_req_message
*msg
;
598 struct messaging_rec
*msg_rec
;
600 msg
= (struct ctdb_req_message
*)buf
;
602 if (msg
->hdr
.operation
!= CTDB_REQ_MESSAGE
) {
603 DEBUG(0, ("Received async msg of type %u, discarding\n",
604 msg
->hdr
.operation
));
606 return NT_STATUS_INVALID_PARAMETER
;
609 if ((conn
->release_ip_handler
!= NULL
)
610 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
611 /* must be dispatched immediately */
612 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
613 conn
->release_ip_handler((const char *)msg
->data
,
614 conn
->release_ip_priv
);
619 SMB_ASSERT(conn
->msg_ctx
!= NULL
);
621 if ((msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
622 || (msg
->srvid
== CTDB_SRVID_SAMBA_NOTIFY
)){
623 DEBUG(0,("Got cluster reconfigure message\n"));
625 * when the cluster is reconfigured or someone of the
626 * family has passed away (SAMBA_NOTIFY), we need to
627 * clean the brl database
629 messaging_send(conn
->msg_ctx
,
630 messaging_server_id(conn
->msg_ctx
),
631 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
633 messaging_send(conn
->msg_ctx
,
634 messaging_server_id(conn
->msg_ctx
),
635 MSG_DBWRAP_G_LOCK_RETRY
,
642 /* only messages to our pid or the broadcast are valid here */
643 if (msg
->srvid
!= sys_getpid() && msg
->srvid
!= MSG_SRVID_SAMBA
) {
644 DEBUG(0,("Got unexpected message with srvid=%llu\n",
645 (unsigned long long)msg
->srvid
));
650 if (!(msg_rec
= ctdb_pull_messaging_rec(NULL
, length
, msg
))) {
651 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
653 return NT_STATUS_NO_MEMORY
;
656 messaging_dispatch_rec(conn
->msg_ctx
, msg_rec
);
658 TALLOC_FREE(msg_rec
);
664 * The ctdbd socket is readable asynchronuously
667 static void ctdbd_socket_handler(struct event_context
*event_ctx
,
668 struct fd_event
*event
,
672 struct ctdbd_connection
*conn
= talloc_get_type_abort(
673 private_data
, struct ctdbd_connection
);
677 status
= packet_fd_read(conn
->pkt
);
679 if (!NT_STATUS_IS_OK(status
)) {
680 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
681 cluster_fatal("ctdbd died\n");
684 while (packet_handler(conn
->pkt
, ctdb_req_complete
,
685 ctdb_handle_message
, conn
, &status
)) {
686 if (!NT_STATUS_IS_OK(status
)) {
687 DEBUG(10, ("could not handle incoming message: %s\n",
694 * Prepare a ctdbd connection to receive messages
697 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
698 struct messaging_context
*msg_ctx
)
700 SMB_ASSERT(conn
->msg_ctx
== NULL
);
701 SMB_ASSERT(conn
->fde
== NULL
);
703 if (!(conn
->fde
= event_add_fd(msg_ctx
->event_ctx
, conn
,
704 packet_get_fd(conn
->pkt
),
706 ctdbd_socket_handler
,
708 DEBUG(0, ("event_add_fd failed\n"));
709 return NT_STATUS_NO_MEMORY
;
712 conn
->msg_ctx
= msg_ctx
;
718 * Send a messaging message across a ctdbd
721 NTSTATUS
ctdbd_messaging_send(struct ctdbd_connection
*conn
,
722 uint32 dst_vnn
, uint64 dst_srvid
,
723 struct messaging_rec
*msg
)
725 struct ctdb_req_message r
;
729 enum ndr_err_code ndr_err
;
731 if (!(mem_ctx
= talloc_init("ctdbd_messaging_send"))) {
732 DEBUG(0, ("talloc failed\n"));
733 return NT_STATUS_NO_MEMORY
;
736 ndr_err
= ndr_push_struct_blob(
738 (ndr_push_flags_fn_t
)ndr_push_messaging_rec
);
740 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
741 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
742 ndr_errstr(ndr_err
)));
743 status
= ndr_map_error2ntstatus(ndr_err
);
747 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + blob
.length
;
748 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
749 r
.hdr
.ctdb_version
= CTDB_VERSION
;
750 r
.hdr
.generation
= 1;
751 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
752 r
.hdr
.destnode
= dst_vnn
;
753 r
.hdr
.srcnode
= conn
->our_vnn
;
756 r
.datalen
= blob
.length
;
758 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
759 ctdb_packet_dump(&r
.hdr
);
761 status
= packet_send(
763 data_blob_const(&r
, offsetof(struct ctdb_req_message
, data
)),
766 if (!NT_STATUS_IS_OK(status
)) {
767 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status
)));
771 status
= packet_flush(conn
->pkt
);
773 if (!NT_STATUS_IS_OK(status
)) {
774 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
775 cluster_fatal("cluster dispatch daemon msg write error\n");
778 status
= NT_STATUS_OK
;
780 TALLOC_FREE(mem_ctx
);
785 * send/recv a generic ctdb control message
787 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
788 uint32_t vnn
, uint32 opcode
,
789 uint64_t srvid
, uint32_t flags
,
791 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
794 struct ctdb_req_control req
;
795 struct ctdb_reply_control
*reply
= NULL
;
796 struct ctdbd_connection
*new_conn
= NULL
;
799 /* the samba3 ctdb code can't handle NOREPLY yet */
800 flags
&= ~CTDB_CTRL_FLAG_NOREPLY
;
803 status
= ctdbd_init_connection(NULL
, &new_conn
);
805 if (!NT_STATUS_IS_OK(status
)) {
806 DEBUG(10, ("Could not init temp connection: %s\n",
815 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
816 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
817 req
.hdr
.ctdb_version
= CTDB_VERSION
;
818 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
819 req
.hdr
.reqid
= ++conn
->reqid
;
820 req
.hdr
.destnode
= vnn
;
823 req
.datalen
= data
.dsize
;
826 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
827 ctdb_packet_dump(&req
.hdr
);
829 status
= packet_send(
831 data_blob_const(&req
, offsetof(struct ctdb_req_control
, data
)),
832 data_blob_const(data
.dptr
, data
.dsize
));
834 if (!NT_STATUS_IS_OK(status
)) {
835 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
839 status
= packet_flush(conn
->pkt
);
841 if (!NT_STATUS_IS_OK(status
)) {
842 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
843 cluster_fatal("cluster dispatch daemon control write error\n");
846 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
847 TALLOC_FREE(new_conn
);
854 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
856 if (!NT_STATUS_IS_OK(status
)) {
857 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
861 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
862 DEBUG(0, ("received invalid reply\n"));
867 if (!(outdata
->dptr
= (uint8
*)talloc_memdup(
868 mem_ctx
, reply
->data
, reply
->datalen
))) {
870 return NT_STATUS_NO_MEMORY
;
872 outdata
->dsize
= reply
->datalen
;
875 (*cstatus
) = reply
->status
;
878 status
= NT_STATUS_OK
;
881 TALLOC_FREE(new_conn
);
887 * see if a remote process exists
889 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32 vnn
, pid_t pid
)
895 data
.dptr
= (uint8_t*)&pid
;
896 data
.dsize
= sizeof(pid
);
898 status
= ctdbd_control(conn
, vnn
, CTDB_CONTROL_PROCESS_EXISTS
, 0, 0,
899 data
, NULL
, NULL
, &cstatus
);
900 if (!NT_STATUS_IS_OK(status
)) {
901 DEBUG(0, (__location__
" ctdb_control for process_exists "
912 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
913 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
919 data
.dptr
= (uint8_t*)&db_id
;
920 data
.dsize
= sizeof(db_id
);
922 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
923 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
924 mem_ctx
, &data
, &cstatus
);
925 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
926 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
930 return (char *)data
.dptr
;
934 * attach to a ctdb database
936 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
937 const char *name
, uint32_t *db_id
, int tdb_flags
)
942 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
944 data
.dptr
= (uint8_t*)name
;
945 data
.dsize
= strlen(name
)+1;
947 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
949 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
950 : CTDB_CONTROL_DB_ATTACH
,
951 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
952 if (!NT_STATUS_IS_OK(status
)) {
953 DEBUG(0, (__location__
" ctdb_control for db_attach "
954 "failed: %s\n", nt_errstr(status
)));
958 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
959 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
960 return NT_STATUS_INTERNAL_ERROR
;
963 *db_id
= *(uint32_t *)data
.dptr
;
964 talloc_free(data
.dptr
);
966 if (!(tdb_flags
& TDB_SEQNUM
)) {
970 data
.dptr
= (uint8_t *)db_id
;
971 data
.dsize
= sizeof(*db_id
);
973 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
974 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
975 NULL
, NULL
, &cstatus
);
976 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
977 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
979 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
987 * force the migration of a record to this node
989 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32 db_id
,
992 struct ctdb_req_call req
;
993 struct ctdb_reply_call
*reply
;
998 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
999 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1000 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1001 req
.hdr
.operation
= CTDB_REQ_CALL
;
1002 req
.hdr
.reqid
= ++conn
->reqid
;
1003 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1004 req
.callid
= CTDB_NULL_FUNC
;
1006 req
.keylen
= key
.dsize
;
1008 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1009 ctdb_packet_dump(&req
.hdr
);
1011 status
= packet_send(
1013 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1014 data_blob_const(key
.dptr
, key
.dsize
));
1016 if (!NT_STATUS_IS_OK(status
)) {
1017 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
1021 status
= packet_flush(conn
->pkt
);
1023 if (!NT_STATUS_IS_OK(status
)) {
1024 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1025 cluster_fatal("cluster dispatch daemon control write error\n");
1028 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1030 if (!NT_STATUS_IS_OK(status
)) {
1031 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1035 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1036 DEBUG(0, ("received invalid reply\n"));
1037 status
= NT_STATUS_INTERNAL_ERROR
;
1041 status
= NT_STATUS_OK
;
1049 * remotely fetch a record without locking it or forcing a migration
1051 NTSTATUS
ctdbd_fetch(struct ctdbd_connection
*conn
, uint32 db_id
,
1052 TDB_DATA key
, TALLOC_CTX
*mem_ctx
, TDB_DATA
*data
)
1054 struct ctdb_req_call req
;
1055 struct ctdb_reply_call
*reply
;
1060 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1061 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1062 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1063 req
.hdr
.operation
= CTDB_REQ_CALL
;
1064 req
.hdr
.reqid
= ++conn
->reqid
;
1066 req
.callid
= CTDB_FETCH_FUNC
;
1068 req
.keylen
= key
.dsize
;
1070 status
= packet_send(
1072 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1073 data_blob_const(key
.dptr
, key
.dsize
));
1075 if (!NT_STATUS_IS_OK(status
)) {
1076 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
1080 status
= packet_flush(conn
->pkt
);
1082 if (!NT_STATUS_IS_OK(status
)) {
1083 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1084 cluster_fatal("cluster dispatch daemon control write error\n");
1087 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1089 if (!NT_STATUS_IS_OK(status
)) {
1090 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1094 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1095 DEBUG(0, ("received invalid reply\n"));
1096 status
= NT_STATUS_INTERNAL_ERROR
;
1100 data
->dsize
= reply
->datalen
;
1101 if (data
->dsize
== 0) {
1106 data
->dptr
= (uint8
*)talloc_memdup(mem_ctx
, &reply
->data
[0],
1108 if (data
->dptr
== NULL
) {
1109 DEBUG(0, ("talloc failed\n"));
1110 status
= NT_STATUS_NO_MEMORY
;
1115 status
= NT_STATUS_OK
;
1121 struct ctdbd_traverse_state
{
1122 void (*fn
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1127 * Handle a traverse record coming in on the ctdbd connection
1130 static NTSTATUS
ctdb_traverse_handler(uint8_t *buf
, size_t length
,
1133 struct ctdbd_traverse_state
*state
=
1134 (struct ctdbd_traverse_state
*)private_data
;
1136 struct ctdb_req_message
*m
;
1137 struct ctdb_rec_data
*d
;
1140 m
= (struct ctdb_req_message
*)buf
;
1142 if (length
< sizeof(*m
) || m
->hdr
.length
!= length
) {
1143 DEBUG(0, ("Got invalid message of length %d\n", (int)length
));
1145 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1148 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1149 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1150 DEBUG(0, ("Got invalid traverse data of length %d\n",
1153 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1156 key
.dsize
= d
->keylen
;
1157 key
.dptr
= &d
->data
[0];
1158 data
.dsize
= d
->datalen
;
1159 data
.dptr
= &d
->data
[d
->keylen
];
1161 if (key
.dsize
== 0 && data
.dsize
== 0) {
1162 /* end of traverse */
1163 return NT_STATUS_END_OF_FILE
;
1166 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1167 DEBUG(0, ("Got invalid ltdb header length %d\n",
1170 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1172 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1173 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1176 state
->fn(key
, data
, state
->private_data
);
1180 return NT_STATUS_OK
;
1184 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1185 connection to ctdbd to avoid the hairy recursive and async problems with
1189 NTSTATUS
ctdbd_traverse(uint32 db_id
,
1190 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1191 void *private_data
),
1194 struct ctdbd_connection
*conn
;
1198 struct ctdb_traverse_start t
;
1200 struct ctdbd_traverse_state state
;
1202 status
= ctdbd_init_connection(NULL
, &conn
);
1203 if (!NT_STATUS_IS_OK(status
)) {
1204 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1205 nt_errstr(status
)));
1210 t
.srvid
= conn
->rand_srvid
;
1211 t
.reqid
= ++conn
->reqid
;
1213 data
.dptr
= (uint8_t *)&t
;
1214 data
.dsize
= sizeof(t
);
1216 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1217 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1218 data
, NULL
, NULL
, &cstatus
);
1220 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1222 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1225 if (NT_STATUS_IS_OK(status
)) {
1227 * We need a mapping here
1229 status
= NT_STATUS_UNSUCCESSFUL
;
1235 state
.private_data
= private_data
;
1239 status
= NT_STATUS_OK
;
1241 if (packet_handler(conn
->pkt
, ctdb_req_complete
,
1242 ctdb_traverse_handler
, &state
, &status
)) {
1244 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1245 status
= NT_STATUS_OK
;
1250 * There might be more in the queue
1255 if (!NT_STATUS_IS_OK(status
)) {
1259 status
= ctdb_packet_fd_read_sync(conn
->pkt
);
1261 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1263 * There might be more in the queue
1268 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1269 status
= NT_STATUS_OK
;
1273 if (!NT_STATUS_IS_OK(status
)) {
1274 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status
)));
1275 cluster_fatal("ctdbd died\n");
1285 This is used to canonicalize a ctdb_sock_addr structure.
1287 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1288 struct sockaddr_storage
*out
)
1290 memcpy(out
, in
, sizeof (*out
));
1293 if (in
->ss_family
== AF_INET6
) {
1294 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1295 const struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)in
;
1296 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1297 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1298 memset(out
, 0, sizeof(*out
));
1299 #ifdef HAVE_SOCK_SIN_LEN
1300 out4
->sin_len
= sizeof(*out
);
1302 out4
->sin_family
= AF_INET
;
1303 out4
->sin_port
= in6
->sin6_port
;
1304 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr32
[3], 4);
1311 * Register us as a server for a particular tcp connection
1314 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1315 const struct sockaddr_storage
*_server
,
1316 const struct sockaddr_storage
*_client
,
1317 void (*release_ip_handler
)(const char *ip_addr
,
1318 void *private_data
),
1322 * we still use ctdb_control_tcp for ipv4
1323 * because we want to work against older ctdb
1324 * versions at runtime
1326 struct ctdb_control_tcp p4
;
1327 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1328 struct ctdb_control_tcp_addr p
;
1332 struct sockaddr_storage client
;
1333 struct sockaddr_storage server
;
1336 * Only one connection so far
1338 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1340 smbd_ctdb_canonicalize_ip(_client
, &client
);
1341 smbd_ctdb_canonicalize_ip(_server
, &server
);
1343 switch (client
.ss_family
) {
1345 p4
.dest
= *(struct sockaddr_in
*)(void *)&server
;
1346 p4
.src
= *(struct sockaddr_in
*)(void *)&client
;
1347 data
.dptr
= (uint8_t *)&p4
;
1348 data
.dsize
= sizeof(p4
);
1350 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1352 p
.dest
.ip6
= *(struct sockaddr_in6
*)(void *)&server
;
1353 p
.src
.ip6
= *(struct sockaddr_in6
*)(void *)&client
;
1354 data
.dptr
= (uint8_t *)&p
;
1355 data
.dsize
= sizeof(p
);
1359 return NT_STATUS_INTERNAL_ERROR
;
1362 conn
->release_ip_handler
= release_ip_handler
;
1365 * We want to be told about IP releases
1368 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
);
1369 if (!NT_STATUS_IS_OK(status
)) {
1374 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1375 * can send an extra ack to trigger a reset for our client, so it
1376 * immediately reconnects
1378 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1379 CTDB_CONTROL_TCP_CLIENT
, 0,
1380 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1384 * We want to handle reconfigure events
1386 NTSTATUS
ctdbd_register_reconfigure(struct ctdbd_connection
*conn
)
1388 return register_with_ctdbd(conn
, CTDB_SRVID_RECONFIGURE
);
1392 call a control on the local node
1394 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32 opcode
,
1395 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1396 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1399 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1402 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1404 struct ctdb_client_notify_register reg_data
;
1409 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1411 reg_data
.notify_data
[0] = 0;
1413 struct_len
= offsetof(struct ctdb_client_notify_register
,
1414 notify_data
) + reg_data
.len
;
1416 status
= ctdbd_control_local(
1417 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1418 make_tdb_data((uint8_t *)®_data
, struct_len
),
1419 NULL
, NULL
, &cstatus
);
1420 if (!NT_STATUS_IS_OK(status
)) {
1421 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1422 nt_errstr(status
)));
1427 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1429 struct ctdb_client_notify_deregister dereg_data
;
1433 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1435 status
= ctdbd_control_local(
1436 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1437 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1438 NULL
, NULL
, &cstatus
);
1439 if (!NT_STATUS_IS_OK(status
)) {
1440 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1441 nt_errstr(status
)));