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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #ifdef CLUSTER_SUPPORT
26 #include "librpc/gen_ndr/messaging.h"
27 #include "librpc/gen_ndr/ndr_messaging.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
, 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
65 * Register a srvid with ctdbd
67 static NTSTATUS
register_with_ctdbd(struct ctdbd_connection
*conn
,
72 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
73 CTDB_CONTROL_REGISTER_SRVID
, srvid
,
74 tdb_null
, NULL
, NULL
, &cstatus
);
78 * get our vnn from the cluster
80 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32
*vnn
)
84 status
= ctdbd_control(conn
,
85 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_VNN
, 0,
86 tdb_null
, NULL
, NULL
, &cstatus
);
87 if (!NT_STATUS_IS_OK(status
)) {
88 cluster_fatal("ctdbd_control failed\n");
90 *vnn
= (uint32_t)cstatus
;
94 uint32
ctdbd_vnn(const struct ctdbd_connection
*conn
)
100 * Get us a ctdb connection
103 static NTSTATUS
ctdbd_connect(TALLOC_CTX
*mem_ctx
,
104 struct packet_context
**presult
)
106 struct packet_context
*result
;
107 const char *sockname
= lp_ctdbd_socket();
108 struct sockaddr_un addr
;
111 if (!sockname
|| !*sockname
) {
112 sockname
= CTDB_PATH
;
115 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
117 DEBUG(3, ("Could not create socket: %s\n", strerror(errno
)));
118 return map_nt_error_from_unix(errno
);
122 addr
.sun_family
= AF_UNIX
;
123 strncpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
125 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
126 DEBUG(0, ("connect(%s) failed: %s\n", sockname
,
129 return map_nt_error_from_unix(errno
);
132 if (!(result
= packet_init(mem_ctx
, fd
))) {
134 return NT_STATUS_NO_MEMORY
;
142 * Do we have a complete ctdb packet in the queue?
145 static BOOL
ctdb_req_complete(const struct data_blob
*data
,
151 if (data
->length
< sizeof(msglen
)) {
155 msglen
= *((uint32
*)data
->data
);
157 DEBUG(10, ("msglen = %d\n", msglen
));
159 if (msglen
< sizeof(struct ctdb_req_header
)) {
160 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
161 "the req_header\n", msglen
,
162 sizeof(struct ctdb_req_header
)));
163 cluster_fatal("ctdbd protocol error\n");
166 if (data
->length
>= msglen
) {
175 * State necessary to defer an incoming message while we are waiting for a
179 struct deferred_msg_state
{
180 struct messaging_context
*msg_ctx
;
181 struct messaging_rec
*rec
;
185 * Timed event handler for the deferred message
188 static void deferred_message_dispatch(struct event_context
*event_ctx
,
189 struct timed_event
*te
,
190 const struct timeval
*now
,
193 struct deferred_msg_state
*state
= talloc_get_type_abort(
194 private_data
, struct deferred_msg_state
);
196 messaging_dispatch_rec(state
->msg_ctx
, state
->rec
);
201 struct req_pull_state
{
207 * Pull a ctdb request out of the incoming packet queue
210 static NTSTATUS
ctdb_req_pull(const struct data_blob
*data
,
213 struct req_pull_state
*state
= (struct req_pull_state
*)private_data
;
215 state
->req
= data_blob_talloc(state
->mem_ctx
, data
->data
,
217 if (state
->req
.data
== NULL
) {
218 return NT_STATUS_NO_MEMORY
;
224 * Fetch a messaging_rec from an incoming ctdb style message
227 static struct messaging_rec
*ctdb_pull_messaging_rec(TALLOC_CTX
*mem_ctx
,
228 size_t overall_length
,
229 struct ctdb_req_message
*msg
)
231 struct messaging_rec
*result
;
235 if ((overall_length
< offsetof(struct ctdb_req_message
, data
))
237 < offsetof(struct ctdb_req_message
, data
) + msg
->datalen
)) {
239 cluster_fatal("got invalid msg length");
242 if (!(result
= TALLOC_P(mem_ctx
, struct messaging_rec
))) {
243 DEBUG(0, ("talloc failed\n"));
247 blob
= data_blob_const(msg
->data
, msg
->datalen
);
249 status
= ndr_pull_struct_blob(
250 &blob
, result
, result
,
251 (ndr_pull_flags_fn_t
)ndr_pull_messaging_rec
);
253 if (!NT_STATUS_IS_OK(status
)) {
254 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
264 * Read a full ctdbd request. If we have a messaging context, defer incoming
265 * messages that might come in between.
268 static NTSTATUS
ctdb_read_req(struct ctdbd_connection
*conn
, uint32 reqid
,
269 TALLOC_CTX
*mem_ctx
, void *result
)
271 struct ctdb_req_header
*hdr
;
272 struct req_pull_state state
;
277 status
= packet_fd_read_sync(conn
->pkt
);
279 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_BUSY
)) {
284 if (!NT_STATUS_IS_OK(status
)) {
285 DEBUG(3, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
286 cluster_fatal("ctdbd died\n");
290 state
.mem_ctx
= mem_ctx
;
292 if (!packet_handler(conn
->pkt
, ctdb_req_complete
, ctdb_req_pull
,
300 if (!NT_STATUS_IS_OK(status
)) {
301 DEBUG(3, ("Could not read packet: %s\n", nt_errstr(status
)));
302 cluster_fatal("ctdbd died\n");
305 hdr
= (struct ctdb_req_header
*)state
.req
.data
;
307 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
308 struct timed_event
*evt
;
309 struct deferred_msg_state
*msg_state
;
310 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
312 if (conn
->msg_ctx
== NULL
) {
313 DEBUG(1, ("Got a message without having a msg ctx, "
314 "dropping msg %llu\n", msg
->srvid
));
318 if ((conn
->release_ip_handler
!= NULL
)
319 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
320 /* must be dispatched immediately */
321 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
322 conn
->release_ip_handler((const char *)msg
->data
,
323 conn
->release_ip_priv
);
328 if (!(msg_state
= TALLOC_P(NULL
, struct deferred_msg_state
))) {
329 DEBUG(0, ("talloc failed\n"));
334 if (!(msg_state
->rec
= ctdb_pull_messaging_rec(
335 msg_state
, state
.req
.length
, msg
))) {
336 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
337 TALLOC_FREE(msg_state
);
344 msg_state
->msg_ctx
= conn
->msg_ctx
;
347 * We're waiting for a call reply, but an async message has
348 * crossed. Defer dispatching to the toplevel event loop.
350 evt
= event_add_timed(conn
->msg_ctx
->event_ctx
,
351 conn
->msg_ctx
->event_ctx
,
353 "deferred_message_dispatch",
354 deferred_message_dispatch
,
357 DEBUG(0, ("event_add_timed failed\n"));
358 TALLOC_FREE(msg_state
);
366 if (hdr
->reqid
!= reqid
) {
367 /* we got the wrong reply */
368 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
369 "been %u\n", hdr
->reqid
, reqid
));
374 *((void **)result
) = talloc_move(mem_ctx
, &hdr
);
380 * Get us a ctdbd connection
383 NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
384 struct ctdbd_connection
**pconn
)
386 struct ctdbd_connection
*conn
;
389 if (!(conn
= TALLOC_ZERO_P(mem_ctx
, struct ctdbd_connection
))) {
390 DEBUG(0, ("talloc failed\n"));
391 return NT_STATUS_NO_MEMORY
;
394 status
= ctdbd_connect(conn
, &conn
->pkt
);
396 if (!NT_STATUS_IS_OK(status
)) {
397 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status
)));
401 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
403 if (!NT_STATUS_IS_OK(status
)) {
404 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
408 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
409 sizeof(conn
->rand_srvid
));
411 status
= register_with_ctdbd(conn
, conn
->rand_srvid
);
413 if (!NT_STATUS_IS_OK(status
)) {
414 DEBUG(5, ("Could not register random srvid: %s\n",
428 * Get us a ctdbd connection and register us as a process
431 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
432 struct ctdbd_connection
**pconn
)
434 struct ctdbd_connection
*conn
;
437 status
= ctdbd_init_connection(mem_ctx
, &conn
);
439 if (!NT_STATUS_IS_OK(status
)) {
443 status
= register_with_ctdbd(conn
, (uint64_t)sys_getpid());
444 if (!NT_STATUS_IS_OK(status
)) {
448 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
);
449 if (!NT_STATUS_IS_OK(status
)) {
462 * Packet handler to receive and handle a ctdb message
464 static NTSTATUS
ctdb_handle_message(const struct data_blob
*data
,
467 struct ctdbd_connection
*conn
= talloc_get_type_abort(
468 private_data
, struct ctdbd_connection
);
469 struct ctdb_req_message
*msg
;
470 struct messaging_rec
*msg_rec
;
472 msg
= (struct ctdb_req_message
*)data
->data
;
474 if (msg
->hdr
.operation
!= CTDB_REQ_MESSAGE
) {
475 DEBUG(0, ("Received async msg of type %u, discarding\n",
476 msg
->hdr
.operation
));
477 return NT_STATUS_INVALID_PARAMETER
;
480 if ((conn
->release_ip_handler
!= NULL
)
481 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
482 /* must be dispatched immediately */
483 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
484 conn
->release_ip_handler((const char *)msg
->data
,
485 conn
->release_ip_priv
);
489 SMB_ASSERT(conn
->msg_ctx
!= NULL
);
491 if (msg
->srvid
== CTDB_SRVID_RECONFIGURE
) {
492 DEBUG(0,("Got cluster reconfigure message\n"));
494 * when the cluster is reconfigured, we need to clean the brl
497 messaging_send(conn
->msg_ctx
, procid_self(),
498 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
501 * it's possible that we have just rejoined the cluster after
502 * an outage. In that case our pending locks could have been
503 * removed from the lockdb, so retry them once more
505 message_send_all(conn
->msg_ctx
, MSG_SMB_UNLOCK
, NULL
, 0, NULL
);
511 /* only messages to our pid or the broadcast are valid here */
512 if (msg
->srvid
!= sys_getpid() && msg
->srvid
!= MSG_SRVID_SAMBA
) {
513 DEBUG(0,("Got unexpected message with srvid=%llu\n",
514 (unsigned long long)msg
->srvid
));
518 if (!(msg_rec
= ctdb_pull_messaging_rec(NULL
, data
->length
, msg
))) {
519 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
520 return NT_STATUS_NO_MEMORY
;
523 messaging_dispatch_rec(conn
->msg_ctx
, msg_rec
);
525 TALLOC_FREE(msg_rec
);
530 * The ctdbd socket is readable asynchronuously
533 static void ctdbd_socket_handler(struct event_context
*event_ctx
,
534 struct fd_event
*event
,
538 struct ctdbd_connection
*conn
= talloc_get_type_abort(
539 private_data
, struct ctdbd_connection
);
543 status
= packet_fd_read(conn
->pkt
);
545 if (!NT_STATUS_IS_OK(status
)) {
546 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
547 cluster_fatal("ctdbd died\n");
550 while (packet_handler(conn
->pkt
, ctdb_req_complete
,
551 ctdb_handle_message
, conn
, &status
)) {
552 if (!NT_STATUS_IS_OK(status
)) {
553 DEBUG(10, ("could not handle incoming message: %s\n",
560 * Prepare a ctdbd connection to receive messages
563 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
564 struct messaging_context
*msg_ctx
)
566 SMB_ASSERT(conn
->msg_ctx
== NULL
);
567 SMB_ASSERT(conn
->fde
== NULL
);
569 if (!(conn
->fde
= event_add_fd(msg_ctx
->event_ctx
, conn
,
570 packet_get_fd(conn
->pkt
),
572 ctdbd_socket_handler
,
574 DEBUG(0, ("event_add_fd failed\n"));
575 return NT_STATUS_NO_MEMORY
;
578 conn
->msg_ctx
= msg_ctx
;
584 * Send a messaging message across a ctdbd
587 NTSTATUS
ctdbd_messaging_send(struct ctdbd_connection
*conn
,
588 uint32 dst_vnn
, uint64 dst_srvid
,
589 struct messaging_rec
*msg
)
591 struct ctdb_req_message r
;
596 if (!(mem_ctx
= talloc_init("ctdbd_messaging_send"))) {
597 DEBUG(0, ("talloc failed\n"));
598 return NT_STATUS_NO_MEMORY
;
601 status
= ndr_push_struct_blob(
603 (ndr_push_flags_fn_t
)ndr_push_messaging_rec
);
605 if (!NT_STATUS_IS_OK(status
)) {
606 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
611 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + blob
.length
;
612 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
613 r
.hdr
.ctdb_version
= CTDB_VERSION
;
614 r
.hdr
.generation
= 1;
615 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
616 r
.hdr
.destnode
= dst_vnn
;
617 r
.hdr
.srcnode
= conn
->our_vnn
;
620 r
.datalen
= blob
.length
;
622 status
= packet_send(
624 data_blob_const(&r
, offsetof(struct ctdb_req_message
, data
)),
627 if (!NT_STATUS_IS_OK(status
)) {
628 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status
)));
632 status
= packet_flush(conn
->pkt
);
634 if (!NT_STATUS_IS_OK(status
)) {
635 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
636 cluster_fatal("cluster dispatch daemon msg write error\n");
639 status
= NT_STATUS_OK
;
641 TALLOC_FREE(mem_ctx
);
646 * send/recv a generic ctdb control message
648 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
649 uint32_t vnn
, uint32 opcode
,
650 uint64_t srvid
, TDB_DATA data
,
651 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
654 struct ctdb_req_control req
;
655 struct ctdb_reply_control
*reply
= NULL
;
656 struct ctdbd_connection
*new_conn
= NULL
;
660 status
= ctdbd_init_connection(NULL
, &new_conn
);
662 if (!NT_STATUS_IS_OK(status
)) {
663 DEBUG(10, ("Could not init temp connection: %s\n",
672 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
673 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
674 req
.hdr
.ctdb_version
= CTDB_VERSION
;
675 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
676 req
.hdr
.reqid
= ++conn
->reqid
;
677 req
.hdr
.destnode
= vnn
;
680 req
.datalen
= data
.dsize
;
682 status
= packet_send(
684 data_blob_const(&req
, offsetof(struct ctdb_req_control
, data
)),
687 if (!NT_STATUS_IS_OK(status
)) {
688 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
692 status
= packet_flush(conn
->pkt
);
694 if (!NT_STATUS_IS_OK(status
)) {
695 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
696 cluster_fatal("cluster dispatch daemon control write error\n");
699 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
701 if (!NT_STATUS_IS_OK(status
)) {
702 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
706 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
707 DEBUG(0, ("received invalid reply\n"));
712 if (!(outdata
->dptr
= (uint8
*)talloc_memdup(
713 mem_ctx
, reply
->data
, reply
->datalen
))) {
715 return NT_STATUS_NO_MEMORY
;
717 outdata
->dsize
= reply
->datalen
;
720 (*cstatus
) = reply
->status
;
723 status
= NT_STATUS_OK
;
726 TALLOC_FREE(new_conn
);
732 * see if a remote process exists
734 BOOL
ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32 vnn
, pid_t pid
)
740 data
.dptr
= (uint8_t*)&pid
;
741 data
.dsize
= sizeof(pid
);
743 status
= ctdbd_control(conn
, vnn
, CTDB_CONTROL_PROCESS_EXISTS
, 0,
744 data
, NULL
, NULL
, &cstatus
);
745 if (!NT_STATUS_IS_OK(status
)) {
746 DEBUG(0, (__location__
" ctdb_control for process_exists "
757 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
758 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
764 data
.dptr
= (uint8_t*)&db_id
;
765 data
.dsize
= sizeof(db_id
);
767 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
768 CTDB_CONTROL_GETDBPATH
, 0, data
,
769 mem_ctx
, &data
, &cstatus
);
770 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
771 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
775 return (char *)data
.dptr
;
779 * attach to a ctdb database
781 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
782 const char *name
, uint32_t *db_id
, int tdb_flags
)
788 data
.dptr
= (uint8_t*)name
;
789 data
.dsize
= strlen(name
)+1;
791 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
792 CTDB_CONTROL_DB_ATTACH
, 0, data
,
793 NULL
, &data
, &cstatus
);
794 if (!NT_STATUS_IS_OK(status
)) {
795 DEBUG(0, (__location__
" ctdb_control for db_attach "
796 "failed: %s\n", nt_errstr(status
)));
800 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
801 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
802 return NT_STATUS_INTERNAL_ERROR
;
805 *db_id
= *(uint32_t *)data
.dptr
;
806 talloc_free(data
.dptr
);
808 if (!(tdb_flags
& TDB_SEQNUM
)) {
812 data
.dptr
= (uint8_t *)db_id
;
813 data
.dsize
= sizeof(*db_id
);
815 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
816 CTDB_CONTROL_ENABLE_SEQNUM
, 0, data
,
817 NULL
, NULL
, &cstatus
);
818 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
819 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
821 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
829 * force the migration of a record to this node
831 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32 db_id
,
834 struct ctdb_req_call req
;
835 struct ctdb_reply_call
*reply
;
840 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
841 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
842 req
.hdr
.ctdb_version
= CTDB_VERSION
;
843 req
.hdr
.operation
= CTDB_REQ_CALL
;
844 req
.hdr
.reqid
= ++conn
->reqid
;
845 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
846 req
.callid
= CTDB_NULL_FUNC
;
848 req
.keylen
= key
.dsize
;
850 status
= packet_send(
852 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
855 if (!NT_STATUS_IS_OK(status
)) {
856 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
860 status
= packet_flush(conn
->pkt
);
862 if (!NT_STATUS_IS_OK(status
)) {
863 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
864 cluster_fatal("cluster dispatch daemon control write error\n");
867 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
869 if (!NT_STATUS_IS_OK(status
)) {
870 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
874 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
875 DEBUG(0, ("received invalid reply\n"));
876 status
= NT_STATUS_INTERNAL_ERROR
;
880 status
= NT_STATUS_OK
;
888 * remotely fetch a record without locking it or forcing a migration
890 NTSTATUS
ctdbd_fetch(struct ctdbd_connection
*conn
, uint32 db_id
,
891 TDB_DATA key
, TALLOC_CTX
*mem_ctx
, TDB_DATA
*data
)
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
;
905 req
.callid
= CTDB_FETCH_FUNC
;
907 req
.keylen
= key
.dsize
;
909 status
= packet_send(
911 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
914 if (!NT_STATUS_IS_OK(status
)) {
915 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status
)));
919 status
= packet_flush(conn
->pkt
);
921 if (!NT_STATUS_IS_OK(status
)) {
922 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
923 cluster_fatal("cluster dispatch daemon control write error\n");
926 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
928 if (!NT_STATUS_IS_OK(status
)) {
929 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
933 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
934 DEBUG(0, ("received invalid reply\n"));
935 status
= NT_STATUS_INTERNAL_ERROR
;
939 data
->dsize
= reply
->datalen
;
940 if (data
->dsize
== 0) {
945 data
->dptr
= (uint8
*)talloc_memdup(mem_ctx
, &reply
->data
[0],
947 if (data
->dptr
== NULL
) {
948 DEBUG(0, ("talloc failed\n"));
949 status
= NT_STATUS_NO_MEMORY
;
954 status
= NT_STATUS_OK
;
960 struct ctdbd_traverse_state
{
961 void (*fn
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
966 * Handle a traverse record coming in on the ctdbd connection
969 static NTSTATUS
ctdb_traverse_handler(const struct data_blob
*blob
,
972 struct ctdbd_traverse_state
*state
=
973 (struct ctdbd_traverse_state
*)private_data
;
975 struct ctdb_req_message
*m
;
976 struct ctdb_rec_data
*d
;
979 m
= (struct ctdb_req_message
*)blob
->data
;
981 if (blob
->length
< sizeof(*m
) || m
->hdr
.length
!= blob
->length
) {
982 DEBUG(0, ("Got invalid message of length %d\n",
984 return NT_STATUS_UNEXPECTED_IO_ERROR
;
987 d
= (struct ctdb_rec_data
*)&m
->data
[0];
988 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
989 DEBUG(0, ("Got invalid traverse data of length %d\n",
991 return NT_STATUS_UNEXPECTED_IO_ERROR
;
994 key
.dsize
= d
->keylen
;
995 key
.dptr
= &d
->data
[0];
996 data
.dsize
= d
->datalen
;
997 data
.dptr
= &d
->data
[d
->keylen
];
999 if (key
.dsize
== 0 && data
.dsize
== 0) {
1000 /* end of traverse */
1001 return NT_STATUS_END_OF_FILE
;
1004 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1005 DEBUG(0, ("Got invalid ltdb header length %d\n",
1007 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1009 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1010 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1013 state
->fn(key
, data
, state
->private_data
);
1016 return NT_STATUS_OK
;
1020 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1021 connection to ctdbd to avoid the hairy recursive and async problems with
1025 NTSTATUS
ctdbd_traverse(uint32 db_id
,
1026 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1027 void *private_data
),
1030 struct ctdbd_connection
*conn
;
1034 struct ctdb_traverse_start t
;
1036 struct ctdbd_traverse_state state
;
1038 status
= ctdbd_init_connection(NULL
, &conn
);
1041 t
.srvid
= conn
->rand_srvid
;
1042 t
.reqid
= ++conn
->reqid
;
1044 data
.dptr
= (uint8_t *)&t
;
1045 data
.dsize
= sizeof(t
);
1047 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1048 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
,
1049 data
, NULL
, NULL
, &cstatus
);
1051 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1053 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1056 if (NT_STATUS_IS_OK(status
)) {
1058 * We need a mapping here
1060 status
= NT_STATUS_UNSUCCESSFUL
;
1066 state
.private_data
= private_data
;
1070 status
= NT_STATUS_OK
;
1072 if (packet_handler(conn
->pkt
, ctdb_req_complete
,
1073 ctdb_traverse_handler
, &state
, &status
)) {
1075 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1076 status
= NT_STATUS_OK
;
1081 * There might be more in the queue
1086 if (!NT_STATUS_IS_OK(status
)) {
1090 status
= packet_fd_read_sync(conn
->pkt
);
1092 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1093 status
= NT_STATUS_OK
;
1096 if (!NT_STATUS_IS_OK(status
)) {
1097 cluster_fatal("ctdbd died\n");
1107 * Register us as a server for a particular tcp connection
1110 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1111 const struct sockaddr_in
*server
,
1112 const struct sockaddr_in
*client
,
1113 void (*release_ip_handler
)(const char *ip_addr
,
1114 void *private_data
),
1117 struct ctdb_control_tcp p
;
1122 * Only one connection so far
1124 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1127 * We want to be told about IP releases
1130 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
);
1131 if (!NT_STATUS_IS_OK(status
)) {
1139 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1140 * can send an extra ack to trigger a reset for our client, so it
1141 * immediately reconnects
1143 data
.dptr
= (uint8_t *)&p
;
1144 data
.dsize
= sizeof(p
);
1146 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1147 CTDB_CONTROL_TCP_CLIENT
,
1148 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1152 * We want to handle reconfigure events
1154 NTSTATUS
ctdbd_register_reconfigure(struct ctdbd_connection
*conn
)
1156 return register_with_ctdbd(conn
, CTDB_SRVID_RECONFIGURE
);
1161 NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
1162 struct ctdbd_connection
**pconn
)
1164 return NT_STATUS_NOT_IMPLEMENTED
;