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
;
107 uint32
ctdbd_vnn(const struct ctdbd_connection
*conn
)
109 return conn
->our_vnn
;
113 * Get us a ctdb connection
116 static NTSTATUS
ctdbd_connect(TALLOC_CTX
*mem_ctx
,
117 struct packet_context
**presult
)
119 struct packet_context
*result
;
120 const char *sockname
= lp_ctdbd_socket();
121 struct sockaddr_un addr
;
124 if (!sockname
|| !*sockname
) {
125 sockname
= CTDB_PATH
;
128 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
130 DEBUG(3, ("Could not create socket: %s\n", strerror(errno
)));
131 return map_nt_error_from_unix(errno
);
135 addr
.sun_family
= AF_UNIX
;
136 strncpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
138 if (sys_connect(fd
, (struct sockaddr
*)&addr
) == -1) {
139 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
142 return map_nt_error_from_unix(errno
);
145 if (!(result
= packet_init(mem_ctx
, fd
))) {
147 return NT_STATUS_NO_MEMORY
;
155 * Do we have a complete ctdb packet in the queue?
158 static bool ctdb_req_complete(const uint8_t *buf
, size_t available
,
164 if (available
< sizeof(msglen
)) {
168 msglen
= *((uint32
*)buf
);
170 DEBUG(10, ("msglen = %d\n", msglen
));
172 if (msglen
< sizeof(struct ctdb_req_header
)) {
173 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
174 "the req_header\n", (int)msglen
,
175 (int)sizeof(struct ctdb_req_header
)));
176 cluster_fatal("ctdbd protocol error\n");
179 if (available
< msglen
) {
188 * State necessary to defer an incoming message while we are waiting for a
192 struct deferred_msg_state
{
193 struct messaging_context
*msg_ctx
;
194 struct messaging_rec
*rec
;
198 * Timed event handler for the deferred message
201 static void deferred_message_dispatch(struct event_context
*event_ctx
,
202 struct timed_event
*te
,
206 struct deferred_msg_state
*state
= talloc_get_type_abort(
207 private_data
, struct deferred_msg_state
);
209 messaging_dispatch_rec(state
->msg_ctx
, state
->rec
);
214 struct req_pull_state
{
220 * Pull a ctdb request out of the incoming packet queue
223 static NTSTATUS
ctdb_req_pull(uint8_t *buf
, size_t length
,
226 struct req_pull_state
*state
= (struct req_pull_state
*)private_data
;
228 state
->req
.data
= talloc_move(state
->mem_ctx
, &buf
);
229 state
->req
.length
= length
;
234 * Fetch a messaging_rec from an incoming ctdb style message
237 static struct messaging_rec
*ctdb_pull_messaging_rec(TALLOC_CTX
*mem_ctx
,
238 size_t overall_length
,
239 struct ctdb_req_message
*msg
)
241 struct messaging_rec
*result
;
243 enum ndr_err_code ndr_err
;
245 if ((overall_length
< offsetof(struct ctdb_req_message
, data
))
247 < offsetof(struct ctdb_req_message
, data
) + msg
->datalen
)) {
249 cluster_fatal("got invalid msg length");
252 if (!(result
= TALLOC_P(mem_ctx
, struct messaging_rec
))) {
253 DEBUG(0, ("talloc failed\n"));
257 blob
= data_blob_const(msg
->data
, msg
->datalen
);
259 ndr_err
= ndr_pull_struct_blob(
260 &blob
, result
, NULL
, result
,
261 (ndr_pull_flags_fn_t
)ndr_pull_messaging_rec
);
263 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
264 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
265 ndr_errstr(ndr_err
)));
270 if (DEBUGLEVEL
>= 10) {
271 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
272 NDR_PRINT_DEBUG(messaging_rec
, result
);
279 * Read a full ctdbd request. If we have a messaging context, defer incoming
280 * messages that might come in between.
283 static NTSTATUS
ctdb_read_req(struct ctdbd_connection
*conn
, uint32 reqid
,
284 TALLOC_CTX
*mem_ctx
, void *result
)
286 struct ctdb_req_header
*hdr
;
287 struct req_pull_state state
;
292 status
= packet_fd_read_sync(conn
->pkt
);
294 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_BUSY
)) {
297 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
302 if (!NT_STATUS_IS_OK(status
)) {
303 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
304 cluster_fatal("ctdbd died\n");
310 state
.mem_ctx
= mem_ctx
;
312 if (!packet_handler(conn
->pkt
, ctdb_req_complete
, ctdb_req_pull
,
317 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
321 if (!NT_STATUS_IS_OK(status
)) {
322 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status
)));
323 cluster_fatal("ctdbd died\n");
326 hdr
= (struct ctdb_req_header
*)state
.req
.data
;
328 DEBUG(10, ("Received ctdb packet\n"));
329 ctdb_packet_dump(hdr
);
331 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
332 struct timed_event
*evt
;
333 struct deferred_msg_state
*msg_state
;
334 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
336 if (conn
->msg_ctx
== NULL
) {
337 DEBUG(1, ("Got a message without having a msg ctx, "
338 "dropping msg %llu\n",
339 (long long unsigned)msg
->srvid
));
343 if ((conn
->release_ip_handler
!= NULL
)
344 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
345 /* must be dispatched immediately */
346 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
347 conn
->release_ip_handler((const char *)msg
->data
,
348 conn
->release_ip_priv
);
353 if (msg
->srvid
== CTDB_SRVID_RECONFIGURE
) {
354 DEBUG(0,("Got cluster reconfigure message in ctdb_read_req\n"));
355 messaging_send(conn
->msg_ctx
, procid_self(),
356 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
361 if (!(msg_state
= TALLOC_P(NULL
, struct deferred_msg_state
))) {
362 DEBUG(0, ("talloc failed\n"));
367 if (!(msg_state
->rec
= ctdb_pull_messaging_rec(
368 msg_state
, state
.req
.length
, msg
))) {
369 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
370 TALLOC_FREE(msg_state
);
377 msg_state
->msg_ctx
= conn
->msg_ctx
;
380 * We're waiting for a call reply, but an async message has
381 * crossed. Defer dispatching to the toplevel event loop.
383 evt
= event_add_timed(conn
->msg_ctx
->event_ctx
,
384 conn
->msg_ctx
->event_ctx
,
386 deferred_message_dispatch
,
389 DEBUG(0, ("event_add_timed failed\n"));
390 TALLOC_FREE(msg_state
);
398 if (hdr
->reqid
!= reqid
) {
399 /* we got the wrong reply */
400 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
401 "been %u\n", hdr
->reqid
, reqid
));
406 *((void **)result
) = talloc_move(mem_ctx
, &hdr
);
412 * Get us a ctdbd connection
415 NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
416 struct ctdbd_connection
**pconn
)
418 struct ctdbd_connection
*conn
;
421 if (!(conn
= TALLOC_ZERO_P(mem_ctx
, struct ctdbd_connection
))) {
422 DEBUG(0, ("talloc failed\n"));
423 return NT_STATUS_NO_MEMORY
;
426 status
= ctdbd_connect(conn
, &conn
->pkt
);
428 if (!NT_STATUS_IS_OK(status
)) {
429 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status
)));
433 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
435 if (!NT_STATUS_IS_OK(status
)) {
436 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
440 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
441 sizeof(conn
->rand_srvid
));
443 status
= register_with_ctdbd(conn
, conn
->rand_srvid
);
445 if (!NT_STATUS_IS_OK(status
)) {
446 DEBUG(5, ("Could not register random srvid: %s\n",
460 * Get us a ctdbd connection and register us as a process
463 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
464 struct ctdbd_connection
**pconn
)
466 struct ctdbd_connection
*conn
;
469 status
= ctdbd_init_connection(mem_ctx
, &conn
);
471 if (!NT_STATUS_IS_OK(status
)) {
475 status
= register_with_ctdbd(conn
, (uint64_t)sys_getpid());
476 if (!NT_STATUS_IS_OK(status
)) {
480 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
);
481 if (!NT_STATUS_IS_OK(status
)) {
494 * Packet handler to receive and handle a ctdb message
496 static NTSTATUS
ctdb_handle_message(uint8_t *buf
, size_t length
,
499 struct ctdbd_connection
*conn
= talloc_get_type_abort(
500 private_data
, struct ctdbd_connection
);
501 struct ctdb_req_message
*msg
;
502 struct messaging_rec
*msg_rec
;
504 msg
= (struct ctdb_req_message
*)buf
;
506 if (msg
->hdr
.operation
!= CTDB_REQ_MESSAGE
) {
507 DEBUG(0, ("Received async msg of type %u, discarding\n",
508 msg
->hdr
.operation
));
510 return NT_STATUS_INVALID_PARAMETER
;
513 if ((conn
->release_ip_handler
!= NULL
)
514 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
515 /* must be dispatched immediately */
516 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
517 conn
->release_ip_handler((const char *)msg
->data
,
518 conn
->release_ip_priv
);
523 SMB_ASSERT(conn
->msg_ctx
!= NULL
);
525 if (msg
->srvid
== CTDB_SRVID_RECONFIGURE
) {
526 DEBUG(0,("Got cluster reconfigure message\n"));
528 * when the cluster is reconfigured, we need to clean the brl
531 messaging_send(conn
->msg_ctx
, procid_self(),
532 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
535 * it's possible that we have just rejoined the cluster after
536 * an outage. In that case our pending locks could have been
537 * removed from the lockdb, so retry them once more
539 message_send_all(conn
->msg_ctx
, MSG_SMB_UNLOCK
, NULL
, 0, NULL
);
547 /* only messages to our pid or the broadcast are valid here */
548 if (msg
->srvid
!= sys_getpid() && msg
->srvid
!= MSG_SRVID_SAMBA
) {
549 DEBUG(0,("Got unexpected message with srvid=%llu\n",
550 (unsigned long long)msg
->srvid
));
555 if (!(msg_rec
= ctdb_pull_messaging_rec(NULL
, length
, msg
))) {
556 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
558 return NT_STATUS_NO_MEMORY
;
561 messaging_dispatch_rec(conn
->msg_ctx
, msg_rec
);
563 TALLOC_FREE(msg_rec
);
569 * The ctdbd socket is readable asynchronuously
572 static void ctdbd_socket_handler(struct event_context
*event_ctx
,
573 struct fd_event
*event
,
577 struct ctdbd_connection
*conn
= talloc_get_type_abort(
578 private_data
, struct ctdbd_connection
);
582 status
= packet_fd_read(conn
->pkt
);
584 if (!NT_STATUS_IS_OK(status
)) {
585 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
586 cluster_fatal("ctdbd died\n");
589 while (packet_handler(conn
->pkt
, ctdb_req_complete
,
590 ctdb_handle_message
, conn
, &status
)) {
591 if (!NT_STATUS_IS_OK(status
)) {
592 DEBUG(10, ("could not handle incoming message: %s\n",
599 * Prepare a ctdbd connection to receive messages
602 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
603 struct messaging_context
*msg_ctx
)
605 SMB_ASSERT(conn
->msg_ctx
== NULL
);
606 SMB_ASSERT(conn
->fde
== NULL
);
608 if (!(conn
->fde
= event_add_fd(msg_ctx
->event_ctx
, conn
,
609 packet_get_fd(conn
->pkt
),
611 ctdbd_socket_handler
,
613 DEBUG(0, ("event_add_fd failed\n"));
614 return NT_STATUS_NO_MEMORY
;
617 conn
->msg_ctx
= msg_ctx
;
623 * Send a messaging message across a ctdbd
626 NTSTATUS
ctdbd_messaging_send(struct ctdbd_connection
*conn
,
627 uint32 dst_vnn
, uint64 dst_srvid
,
628 struct messaging_rec
*msg
)
630 struct ctdb_req_message r
;
634 enum ndr_err_code ndr_err
;
636 if (!(mem_ctx
= talloc_init("ctdbd_messaging_send"))) {
637 DEBUG(0, ("talloc failed\n"));
638 return NT_STATUS_NO_MEMORY
;
641 ndr_err
= ndr_push_struct_blob(
642 &blob
, mem_ctx
, NULL
, msg
,
643 (ndr_push_flags_fn_t
)ndr_push_messaging_rec
);
645 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
646 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
647 ndr_errstr(ndr_err
)));
648 status
= ndr_map_error2ntstatus(ndr_err
);
652 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + blob
.length
;
653 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
654 r
.hdr
.ctdb_version
= CTDB_VERSION
;
655 r
.hdr
.generation
= 1;
656 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
657 r
.hdr
.destnode
= dst_vnn
;
658 r
.hdr
.srcnode
= conn
->our_vnn
;
661 r
.datalen
= blob
.length
;
663 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
664 ctdb_packet_dump(&r
.hdr
);
666 status
= packet_send(
668 data_blob_const(&r
, offsetof(struct ctdb_req_message
, data
)),
671 if (!NT_STATUS_IS_OK(status
)) {
672 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status
)));
676 status
= packet_flush(conn
->pkt
);
678 if (!NT_STATUS_IS_OK(status
)) {
679 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
680 cluster_fatal("cluster dispatch daemon msg write error\n");
683 status
= NT_STATUS_OK
;
685 TALLOC_FREE(mem_ctx
);
690 * send/recv a generic ctdb control message
692 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
693 uint32_t vnn
, uint32 opcode
,
694 uint64_t srvid
, uint32_t flags
,
696 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
699 struct ctdb_req_control req
;
700 struct ctdb_reply_control
*reply
= NULL
;
701 struct ctdbd_connection
*new_conn
= NULL
;
704 /* the samba3 ctdb code can't handle NOREPLY yet */
705 flags
&= ~CTDB_CTRL_FLAG_NOREPLY
;
708 status
= ctdbd_init_connection(NULL
, &new_conn
);
710 if (!NT_STATUS_IS_OK(status
)) {
711 DEBUG(10, ("Could not init temp connection: %s\n",
720 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
721 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
722 req
.hdr
.ctdb_version
= CTDB_VERSION
;
723 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
724 req
.hdr
.reqid
= ++conn
->reqid
;
725 req
.hdr
.destnode
= vnn
;
728 req
.datalen
= data
.dsize
;
730 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
731 ctdb_packet_dump(&req
.hdr
);
733 status
= packet_send(
735 data_blob_const(&req
, offsetof(struct ctdb_req_control
, data
)),
736 data_blob_const(data
.dptr
, data
.dsize
));
738 if (!NT_STATUS_IS_OK(status
)) {
739 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
743 status
= packet_flush(conn
->pkt
);
745 if (!NT_STATUS_IS_OK(status
)) {
746 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
747 cluster_fatal("cluster dispatch daemon control write error\n");
750 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
751 TALLOC_FREE(new_conn
);
755 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
757 if (!NT_STATUS_IS_OK(status
)) {
758 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
762 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
763 DEBUG(0, ("received invalid reply\n"));
768 if (!(outdata
->dptr
= (uint8
*)talloc_memdup(
769 mem_ctx
, reply
->data
, reply
->datalen
))) {
771 return NT_STATUS_NO_MEMORY
;
773 outdata
->dsize
= reply
->datalen
;
776 (*cstatus
) = reply
->status
;
779 status
= NT_STATUS_OK
;
782 TALLOC_FREE(new_conn
);
788 * see if a remote process exists
790 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32 vnn
, pid_t pid
)
796 data
.dptr
= (uint8_t*)&pid
;
797 data
.dsize
= sizeof(pid
);
799 status
= ctdbd_control(conn
, vnn
, CTDB_CONTROL_PROCESS_EXISTS
, 0, 0,
800 data
, NULL
, NULL
, &cstatus
);
801 if (!NT_STATUS_IS_OK(status
)) {
802 DEBUG(0, (__location__
" ctdb_control for process_exists "
813 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
814 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
820 data
.dptr
= (uint8_t*)&db_id
;
821 data
.dsize
= sizeof(db_id
);
823 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
824 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
825 mem_ctx
, &data
, &cstatus
);
826 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
827 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
831 return (char *)data
.dptr
;
835 * attach to a ctdb database
837 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
838 const char *name
, uint32_t *db_id
, int tdb_flags
)
843 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
845 data
.dptr
= (uint8_t*)name
;
846 data
.dsize
= strlen(name
)+1;
848 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
850 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
851 : CTDB_CONTROL_DB_ATTACH
,
852 0, 0, data
, NULL
, &data
, &cstatus
);
853 if (!NT_STATUS_IS_OK(status
)) {
854 DEBUG(0, (__location__
" ctdb_control for db_attach "
855 "failed: %s\n", nt_errstr(status
)));
859 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
860 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
861 return NT_STATUS_INTERNAL_ERROR
;
864 *db_id
= *(uint32_t *)data
.dptr
;
865 talloc_free(data
.dptr
);
867 if (!(tdb_flags
& TDB_SEQNUM
)) {
871 data
.dptr
= (uint8_t *)db_id
;
872 data
.dsize
= sizeof(*db_id
);
874 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
875 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
876 NULL
, NULL
, &cstatus
);
877 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
878 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
880 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
888 * force the migration of a record to this node
890 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32 db_id
,
893 struct ctdb_req_call req
;
894 struct ctdb_reply_call
*reply
;
899 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
900 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
901 req
.hdr
.ctdb_version
= CTDB_VERSION
;
902 req
.hdr
.operation
= CTDB_REQ_CALL
;
903 req
.hdr
.reqid
= ++conn
->reqid
;
904 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
905 req
.callid
= CTDB_NULL_FUNC
;
907 req
.keylen
= key
.dsize
;
909 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
910 ctdb_packet_dump(&req
.hdr
);
912 status
= packet_send(
914 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
915 data_blob_const(key
.dptr
, key
.dsize
));
917 if (!NT_STATUS_IS_OK(status
)) {
918 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
922 status
= packet_flush(conn
->pkt
);
924 if (!NT_STATUS_IS_OK(status
)) {
925 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
926 cluster_fatal("cluster dispatch daemon control write error\n");
929 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
931 if (!NT_STATUS_IS_OK(status
)) {
932 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
936 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
937 DEBUG(0, ("received invalid reply\n"));
938 status
= NT_STATUS_INTERNAL_ERROR
;
942 status
= NT_STATUS_OK
;
950 * remotely fetch a record without locking it or forcing a migration
952 NTSTATUS
ctdbd_fetch(struct ctdbd_connection
*conn
, uint32 db_id
,
953 TDB_DATA key
, TALLOC_CTX
*mem_ctx
, TDB_DATA
*data
)
955 struct ctdb_req_call req
;
956 struct ctdb_reply_call
*reply
;
961 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
962 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
963 req
.hdr
.ctdb_version
= CTDB_VERSION
;
964 req
.hdr
.operation
= CTDB_REQ_CALL
;
965 req
.hdr
.reqid
= ++conn
->reqid
;
967 req
.callid
= CTDB_FETCH_FUNC
;
969 req
.keylen
= key
.dsize
;
971 status
= packet_send(
973 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
974 data_blob_const(key
.dptr
, key
.dsize
));
976 if (!NT_STATUS_IS_OK(status
)) {
977 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
981 status
= packet_flush(conn
->pkt
);
983 if (!NT_STATUS_IS_OK(status
)) {
984 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
985 cluster_fatal("cluster dispatch daemon control write error\n");
988 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
990 if (!NT_STATUS_IS_OK(status
)) {
991 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
995 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
996 DEBUG(0, ("received invalid reply\n"));
997 status
= NT_STATUS_INTERNAL_ERROR
;
1001 data
->dsize
= reply
->datalen
;
1002 if (data
->dsize
== 0) {
1007 data
->dptr
= (uint8
*)talloc_memdup(mem_ctx
, &reply
->data
[0],
1009 if (data
->dptr
== NULL
) {
1010 DEBUG(0, ("talloc failed\n"));
1011 status
= NT_STATUS_NO_MEMORY
;
1016 status
= NT_STATUS_OK
;
1022 struct ctdbd_traverse_state
{
1023 void (*fn
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1028 * Handle a traverse record coming in on the ctdbd connection
1031 static NTSTATUS
ctdb_traverse_handler(uint8_t *buf
, size_t length
,
1034 struct ctdbd_traverse_state
*state
=
1035 (struct ctdbd_traverse_state
*)private_data
;
1037 struct ctdb_req_message
*m
;
1038 struct ctdb_rec_data
*d
;
1041 m
= (struct ctdb_req_message
*)buf
;
1043 if (length
< sizeof(*m
) || m
->hdr
.length
!= length
) {
1044 DEBUG(0, ("Got invalid message of length %d\n", (int)length
));
1046 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1049 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1050 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1051 DEBUG(0, ("Got invalid traverse data of length %d\n",
1054 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1057 key
.dsize
= d
->keylen
;
1058 key
.dptr
= &d
->data
[0];
1059 data
.dsize
= d
->datalen
;
1060 data
.dptr
= &d
->data
[d
->keylen
];
1062 if (key
.dsize
== 0 && data
.dsize
== 0) {
1063 /* end of traverse */
1064 return NT_STATUS_END_OF_FILE
;
1067 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1068 DEBUG(0, ("Got invalid ltdb header length %d\n",
1071 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1073 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1074 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1077 state
->fn(key
, data
, state
->private_data
);
1081 return NT_STATUS_OK
;
1085 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1086 connection to ctdbd to avoid the hairy recursive and async problems with
1090 NTSTATUS
ctdbd_traverse(uint32 db_id
,
1091 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1092 void *private_data
),
1095 struct ctdbd_connection
*conn
;
1099 struct ctdb_traverse_start t
;
1101 struct ctdbd_traverse_state state
;
1103 status
= ctdbd_init_connection(NULL
, &conn
);
1106 t
.srvid
= conn
->rand_srvid
;
1107 t
.reqid
= ++conn
->reqid
;
1109 data
.dptr
= (uint8_t *)&t
;
1110 data
.dsize
= sizeof(t
);
1112 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1113 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1114 data
, NULL
, NULL
, &cstatus
);
1116 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1118 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1121 if (NT_STATUS_IS_OK(status
)) {
1123 * We need a mapping here
1125 status
= NT_STATUS_UNSUCCESSFUL
;
1131 state
.private_data
= private_data
;
1135 status
= NT_STATUS_OK
;
1137 if (packet_handler(conn
->pkt
, ctdb_req_complete
,
1138 ctdb_traverse_handler
, &state
, &status
)) {
1140 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1141 status
= NT_STATUS_OK
;
1146 * There might be more in the queue
1151 if (!NT_STATUS_IS_OK(status
)) {
1155 status
= packet_fd_read_sync(conn
->pkt
);
1157 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1159 * There might be more in the queue
1164 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1165 status
= NT_STATUS_OK
;
1168 if (!NT_STATUS_IS_OK(status
)) {
1169 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status
)));
1170 cluster_fatal("ctdbd died\n");
1180 * Register us as a server for a particular tcp connection
1183 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1184 const struct sockaddr
*server
,
1185 const struct sockaddr
*client
,
1186 void (*release_ip_handler
)(const char *ip_addr
,
1187 void *private_data
),
1190 struct ctdb_control_tcp_vnn p
;
1195 * Only one connection so far
1197 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1199 switch (client
->sa_family
) {
1201 p
.dest
.ip
= *(struct sockaddr_in
*)server
;
1202 p
.src
.ip
= *(struct sockaddr_in
*)client
;
1206 p
.dest
.ip6
= *(struct sockaddr_in6
*)server
;
1207 p
.src
.ip6
= *(struct sockaddr_in6
*)client
;
1211 return NT_STATUS_INTERNAL_ERROR
;
1214 conn
->release_ip_handler
= release_ip_handler
;
1217 * We want to be told about IP releases
1220 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
);
1221 if (!NT_STATUS_IS_OK(status
)) {
1226 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1227 * can send an extra ack to trigger a reset for our client, so it
1228 * immediately reconnects
1230 data
.dptr
= (uint8_t *)&p
;
1231 data
.dsize
= sizeof(p
);
1233 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1234 CTDB_CONTROL_TCP_ADD
, 0,
1235 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1239 * We want to handle reconfigure events
1241 NTSTATUS
ctdbd_register_reconfigure(struct ctdbd_connection
*conn
)
1243 return register_with_ctdbd(conn
, CTDB_SRVID_RECONFIGURE
);
1247 call a control on the local node
1249 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32 opcode
,
1250 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1251 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1254 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1259 NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
1260 struct ctdbd_connection
**pconn
)
1262 return NT_STATUS_NOT_IMPLEMENTED
;