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 #include "ctdbd_conn.h"
25 #include "system/select.h"
26 #include "lib/sys_rw_data.h"
27 #include "lib/util/iov_buf.h"
31 /* paths to these include files come from --with-ctdb= in configure */
34 #include "ctdb_private.h"
36 struct ctdbd_srvid_cb
{
38 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
40 const uint8_t *msg
, size_t msglen
,
45 struct ctdbd_connection
{
46 struct messaging_context
*msg_ctx
;
50 struct ctdbd_srvid_cb
*callbacks
;
52 struct tevent_fd
*fde
;
55 static uint32_t ctdbd_next_reqid(struct ctdbd_connection
*conn
)
58 if (conn
->reqid
== 0) {
64 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
65 uint32_t vnn
, uint32_t opcode
,
66 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
67 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
71 * exit on fatal communications errors with the ctdbd daemon
73 static void cluster_fatal(const char *why
)
75 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
76 /* we don't use smb_panic() as we don't want to delay to write
77 a core file. We need to release this process id immediately
78 so that someone else can take over without getting sharing
86 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
88 if (DEBUGLEVEL
< 11) {
91 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
92 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
93 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
94 (int)hdr
->operation
, (int)hdr
->reqid
));
98 * Register a srvid with ctdbd
100 NTSTATUS
register_with_ctdbd(struct ctdbd_connection
*conn
, uint64_t srvid
,
101 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
103 const uint8_t *msg
, size_t msglen
,
110 size_t num_callbacks
;
111 struct ctdbd_srvid_cb
*tmp
;
113 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
114 CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
115 tdb_null
, NULL
, NULL
, &cstatus
);
116 if (!NT_STATUS_IS_OK(status
)) {
120 num_callbacks
= talloc_array_length(conn
->callbacks
);
122 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
125 return NT_STATUS_NO_MEMORY
;
127 conn
->callbacks
= tmp
;
129 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
130 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
136 static int ctdbd_msg_call_back(struct ctdbd_connection
*conn
,
137 struct ctdb_req_message
*msg
)
140 size_t i
, num_callbacks
;
142 msg_len
= msg
->hdr
.length
;
143 if (msg_len
< offsetof(struct ctdb_req_message
, data
)) {
144 DEBUG(10, ("%s: len %u too small\n", __func__
,
148 msg_len
-= offsetof(struct ctdb_req_message
, data
);
150 if (msg_len
< msg
->datalen
) {
151 DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__
,
152 (unsigned)msg_len
, (unsigned)msg
->datalen
));
156 num_callbacks
= talloc_array_length(conn
->callbacks
);
158 for (i
=0; i
<num_callbacks
; i
++) {
159 struct ctdbd_srvid_cb
*cb
= &conn
->callbacks
[i
];
161 if ((cb
->srvid
== msg
->srvid
) && (cb
->cb
!= NULL
)) {
164 ret
= cb
->cb(msg
->hdr
.srcnode
, msg
->hdr
.destnode
,
165 msg
->srvid
, msg
->data
, msg
->datalen
,
176 * get our vnn from the cluster
178 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
182 status
= ctdbd_control(conn
,
183 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_PNN
, 0, 0,
184 tdb_null
, NULL
, NULL
, &cstatus
);
185 if (!NT_STATUS_IS_OK(status
)) {
186 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
189 *vnn
= (uint32_t)cstatus
;
194 * Are we active (i.e. not banned or stopped?)
196 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
201 struct ctdb_node_map
*m
;
202 uint32_t failure_flags
;
206 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
207 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
208 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
209 if (!NT_STATUS_IS_OK(status
)) {
210 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
213 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
214 DEBUG(2, ("Received invalid ctdb data\n"));
218 m
= (struct ctdb_node_map
*)outdata
.dptr
;
220 for (i
=0; i
<m
->num
; i
++) {
221 if (vnn
== m
->nodes
[i
].pnn
) {
227 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
232 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
233 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
235 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
236 DEBUG(2, ("Node has status %x, not active\n",
237 (int)m
->nodes
[i
].flags
));
243 TALLOC_FREE(outdata
.dptr
);
247 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
249 return conn
->our_vnn
;
252 const char *lp_ctdbd_socket(void)
256 ret
= lp__ctdbd_socket();
257 if (ret
!= NULL
&& strlen(ret
) > 0) {
265 * Get us a ctdb connection
268 static int ctdbd_connect(const char *sockname
, int *pfd
)
270 struct sockaddr_un addr
= { 0, };
275 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
278 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
282 addr
.sun_family
= AF_UNIX
;
284 namelen
= strlcpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
285 if (namelen
>= sizeof(addr
.sun_path
)) {
286 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
292 salen
= sizeof(struct sockaddr_un
);
294 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
296 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
306 static int ctdb_read_packet(int fd
, TALLOC_CTX
*mem_ctx
,
307 struct ctdb_req_header
**result
)
309 int timeout
= lp_ctdb_timeout();
310 struct ctdb_req_header
*req
;
320 ret
= poll_one_fd(fd
, POLLIN
, timeout
, &revents
);
332 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
340 if (msglen
< sizeof(struct ctdb_req_header
)) {
344 req
= talloc_size(mem_ctx
, msglen
);
348 talloc_set_name_const(req
, "struct ctdb_req_header");
350 req
->length
= msglen
;
352 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
353 msglen
- sizeof(msglen
));
366 * Read a full ctdbd request. If we have a messaging context, defer incoming
367 * messages that might come in between.
370 static int ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
371 TALLOC_CTX
*mem_ctx
, struct ctdb_req_header
**result
)
373 struct ctdb_req_header
*hdr
;
378 ret
= ctdb_read_packet(conn
->fd
, mem_ctx
, &hdr
);
380 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
381 cluster_fatal("ctdbd died\n");
384 DEBUG(11, ("Received ctdb packet\n"));
385 ctdb_packet_dump(hdr
);
387 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
388 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
390 if (conn
->msg_ctx
== NULL
) {
391 DEBUG(1, ("Got a message without having a msg ctx, "
392 "dropping msg %llu\n",
393 (long long unsigned)msg
->srvid
));
398 ret
= ctdbd_msg_call_back(conn
, msg
);
408 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
409 /* we got the wrong reply */
410 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
411 "been %u\n", hdr
->reqid
, reqid
));
416 *result
= talloc_move(mem_ctx
, &hdr
);
421 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
)
427 * Get us a ctdbd connection
430 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
431 struct ctdbd_connection
**pconn
)
433 const char *sockname
= lp_ctdbd_socket();
434 struct ctdbd_connection
*conn
;
438 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
439 DEBUG(0, ("talloc failed\n"));
440 return NT_STATUS_NO_MEMORY
;
443 ret
= ctdbd_connect(sockname
, &conn
->fd
);
445 status
= map_nt_error_from_unix(ret
);
446 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret
)));
449 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
451 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
453 if (!NT_STATUS_IS_OK(status
)) {
454 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
458 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
459 DEBUG(2, ("Node is not working, can not connect\n"));
460 status
= NT_STATUS_INTERNAL_DB_ERROR
;
464 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
465 sizeof(conn
->rand_srvid
));
467 status
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
469 if (!NT_STATUS_IS_OK(status
)) {
470 DEBUG(5, ("Could not register random srvid: %s\n",
484 * Get us a ctdbd connection and register us as a process
487 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
488 struct ctdbd_connection
**pconn
)
490 struct ctdbd_connection
*conn
;
493 status
= ctdbd_init_connection(mem_ctx
, &conn
);
495 if (!NT_STATUS_IS_OK(status
)) {
499 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
, NULL
, NULL
);
500 if (!NT_STATUS_IS_OK(status
)) {
512 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
514 return conn
->msg_ctx
;
517 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
523 * Packet handler to receive and handle a ctdb message
525 static int ctdb_handle_message(struct ctdbd_connection
*conn
,
526 struct ctdb_req_header
*hdr
)
528 struct ctdb_req_message
*msg
;
530 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
531 DEBUG(0, ("Received async msg of type %u, discarding\n",
536 msg
= (struct ctdb_req_message
*)hdr
;
538 ctdbd_msg_call_back(conn
, msg
);
544 * The ctdbd socket is readable asynchronuously
547 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
548 struct tevent_fd
*event
,
552 struct ctdbd_connection
*conn
= talloc_get_type_abort(
553 private_data
, struct ctdbd_connection
);
554 struct ctdb_req_header
*hdr
= NULL
;
557 ret
= ctdb_read_packet(conn
->fd
, talloc_tos(), &hdr
);
559 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
560 cluster_fatal("ctdbd died\n");
563 ret
= ctdb_handle_message(conn
, hdr
);
568 DEBUG(10, ("could not handle incoming message: %s\n",
574 * Prepare a ctdbd connection to receive messages
577 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
578 struct messaging_context
*msg_ctx
)
580 SMB_ASSERT(conn
->msg_ctx
== NULL
);
581 SMB_ASSERT(conn
->fde
== NULL
);
583 if (!(conn
->fde
= tevent_add_fd(messaging_tevent_context(msg_ctx
),
587 ctdbd_socket_handler
,
589 DEBUG(0, ("event_add_fd failed\n"));
590 return NT_STATUS_NO_MEMORY
;
593 conn
->msg_ctx
= msg_ctx
;
598 NTSTATUS
ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
599 uint32_t dst_vnn
, uint64_t dst_srvid
,
600 const struct iovec
*iov
, int iovlen
)
602 struct ctdb_req_message r
;
603 struct iovec iov2
[iovlen
+1];
604 size_t buflen
= iov_buflen(iov
, iovlen
);
607 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + buflen
;
608 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
609 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
610 r
.hdr
.generation
= 1;
611 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
612 r
.hdr
.destnode
= dst_vnn
;
613 r
.hdr
.srcnode
= conn
->our_vnn
;
618 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
619 ctdb_packet_dump(&r
.hdr
);
621 iov2
[0].iov_base
= &r
;
622 iov2
[0].iov_len
= offsetof(struct ctdb_req_message
, data
);
623 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
625 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
626 if (nwritten
== -1) {
627 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
628 cluster_fatal("cluster dispatch daemon msg write error\n");
635 * send/recv a generic ctdb control message
637 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
638 uint32_t vnn
, uint32_t opcode
,
639 uint64_t srvid
, uint32_t flags
,
641 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
644 struct ctdb_req_control req
;
645 struct ctdb_req_header
*hdr
;
646 struct ctdb_reply_control
*reply
= NULL
;
647 struct ctdbd_connection
*new_conn
= NULL
;
654 status
= ctdbd_init_connection(NULL
, &new_conn
);
656 if (!NT_STATUS_IS_OK(status
)) {
657 DEBUG(10, ("Could not init temp connection: %s\n",
666 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
667 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
668 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
669 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
670 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
671 req
.hdr
.destnode
= vnn
;
674 req
.datalen
= data
.dsize
;
677 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
678 ctdb_packet_dump(&req
.hdr
);
680 iov
[0].iov_base
= &req
;
681 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
682 iov
[1].iov_base
= data
.dptr
;
683 iov
[1].iov_len
= data
.dsize
;
685 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
686 if (nwritten
== -1) {
687 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
688 cluster_fatal("cluster dispatch daemon msg write error\n");
691 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
692 TALLOC_FREE(new_conn
);
699 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
701 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
702 status
= map_nt_error_from_unix(ret
);
706 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
707 DEBUG(0, ("received invalid reply\n"));
710 reply
= (struct ctdb_reply_control
*)hdr
;
713 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
714 mem_ctx
, reply
->data
, reply
->datalen
))) {
716 return NT_STATUS_NO_MEMORY
;
718 outdata
->dsize
= reply
->datalen
;
721 (*cstatus
) = reply
->status
;
724 status
= NT_STATUS_OK
;
727 TALLOC_FREE(new_conn
);
733 * see if a remote process exists
735 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
743 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
744 DEBUG(10, ("ctdb_processes_exist failed\n"));
750 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
751 const struct server_id
*pids
, int num_pids
,
754 TALLOC_CTX
*frame
= talloc_stackframe();
759 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
760 if (reqids
== NULL
) {
764 for (i
=0; i
<num_pids
; i
++) {
765 struct ctdb_req_control req
;
771 reqids
[i
] = ctdbd_next_reqid(conn
);
776 * pids[i].pid is uint64_t, scale down to pid_t which
777 * is the wire protocol towards ctdb.
781 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
782 (int)pids
[i
].vnn
, (int)pid
,
785 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
786 req
.hdr
.length
+= sizeof(pid
);
787 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
788 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
789 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
790 req
.hdr
.reqid
= reqids
[i
];
791 req
.hdr
.destnode
= pids
[i
].vnn
;
792 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
794 req
.datalen
= sizeof(pid
);
797 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
798 ctdb_packet_dump(&req
.hdr
);
800 iov
[0].iov_base
= &req
;
801 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
802 iov
[1].iov_base
= &pid
;
803 iov
[1].iov_len
= sizeof(pid
);
805 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
806 if (nwritten
== -1) {
807 DEBUG(10, ("write_data_iov failed: %s\n",
815 while (num_received
< num_pids
) {
816 struct ctdb_req_header
*hdr
;
817 struct ctdb_reply_control
*reply
;
821 ret
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
823 DEBUG(10, ("ctdb_read_req failed: %s\n",
828 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
829 DEBUG(10, ("Received invalid reply\n"));
832 reply
= (struct ctdb_reply_control
*)hdr
;
834 reqid
= reply
->hdr
.reqid
;
836 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
838 for (i
=0; i
<num_pids
; i
++) {
839 if (reqid
== reqids
[i
]) {
844 DEBUG(10, ("Received unknown record number %u\n",
848 results
[i
] = ((reply
->status
) == 0);
862 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
863 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
867 TDB_DATA rdata
= {0};
870 data
.dptr
= (uint8_t*)&db_id
;
871 data
.dsize
= sizeof(db_id
);
873 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
874 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
875 mem_ctx
, &rdata
, &cstatus
);
876 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
877 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
881 return (char *)rdata
.dptr
;
885 * attach to a ctdb database
887 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
888 const char *name
, uint32_t *db_id
, int tdb_flags
)
893 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
895 data
= string_term_tdb_data(name
);
897 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
899 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
900 : CTDB_CONTROL_DB_ATTACH
,
901 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
902 if (!NT_STATUS_IS_OK(status
)) {
903 DEBUG(0, (__location__
" ctdb_control for db_attach "
904 "failed: %s\n", nt_errstr(status
)));
908 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
909 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
910 return NT_STATUS_INTERNAL_ERROR
;
913 *db_id
= *(uint32_t *)data
.dptr
;
914 talloc_free(data
.dptr
);
916 if (!(tdb_flags
& TDB_SEQNUM
)) {
920 data
.dptr
= (uint8_t *)db_id
;
921 data
.dsize
= sizeof(*db_id
);
923 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
924 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
925 NULL
, NULL
, &cstatus
);
926 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
927 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
929 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
937 * force the migration of a record to this node
939 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
942 struct ctdb_req_call req
;
943 struct ctdb_req_header
*hdr
;
951 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
952 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
953 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
954 req
.hdr
.operation
= CTDB_REQ_CALL
;
955 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
956 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
957 req
.callid
= CTDB_NULL_FUNC
;
959 req
.keylen
= key
.dsize
;
961 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
962 ctdb_packet_dump(&req
.hdr
);
964 iov
[0].iov_base
= &req
;
965 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
966 iov
[1].iov_base
= key
.dptr
;
967 iov
[1].iov_len
= key
.dsize
;
969 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
970 if (nwritten
== -1) {
971 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
972 cluster_fatal("cluster dispatch daemon msg write error\n");
975 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
977 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
978 status
= map_nt_error_from_unix(ret
);
982 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
983 DEBUG(0, ("received invalid reply\n"));
984 status
= NT_STATUS_INTERNAL_ERROR
;
988 status
= NT_STATUS_OK
;
996 * Fetch a record and parse it
998 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
999 TDB_DATA key
, bool local_copy
,
1000 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1001 void *private_data
),
1004 struct ctdb_req_call req
;
1005 struct ctdb_req_header
*hdr
= NULL
;
1006 struct ctdb_reply_call
*reply
;
1007 struct iovec iov
[2];
1013 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1017 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1018 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1019 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1020 req
.hdr
.operation
= CTDB_REQ_CALL
;
1021 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1023 req
.callid
= CTDB_FETCH_FUNC
;
1025 req
.keylen
= key
.dsize
;
1027 iov
[0].iov_base
= &req
;
1028 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1029 iov
[1].iov_base
= key
.dptr
;
1030 iov
[1].iov_len
= key
.dsize
;
1032 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1033 if (nwritten
== -1) {
1034 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1035 cluster_fatal("cluster dispatch daemon msg write error\n");
1038 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1040 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
1041 status
= map_nt_error_from_unix(ret
);
1045 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
1046 DEBUG(0, ("received invalid reply\n"));
1047 status
= NT_STATUS_INTERNAL_ERROR
;
1050 reply
= (struct ctdb_reply_call
*)hdr
;
1052 if (reply
->datalen
== 0) {
1054 * Treat an empty record as non-existing
1056 status
= NT_STATUS_NOT_FOUND
;
1060 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1063 status
= NT_STATUS_OK
;
1070 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1071 connection to ctdbd to avoid the hairy recursive and async problems with
1075 NTSTATUS
ctdbd_traverse(uint32_t db_id
,
1076 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1077 void *private_data
),
1080 struct ctdbd_connection
*conn
;
1084 struct ctdb_traverse_start t
;
1088 status
= ctdbd_init_connection(NULL
, &conn
);
1090 if (!NT_STATUS_IS_OK(status
)) {
1091 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1092 nt_errstr(status
)));
1097 t
.srvid
= conn
->rand_srvid
;
1098 t
.reqid
= ctdbd_next_reqid(conn
);
1100 data
.dptr
= (uint8_t *)&t
;
1101 data
.dsize
= sizeof(t
);
1103 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1104 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1105 data
, NULL
, NULL
, &cstatus
);
1107 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1109 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1112 if (NT_STATUS_IS_OK(status
)) {
1114 * We need a mapping here
1116 status
= NT_STATUS_UNSUCCESSFUL
;
1123 struct ctdb_req_header
*hdr
= NULL
;
1124 struct ctdb_req_message
*m
;
1125 struct ctdb_rec_data
*d
;
1128 ret
= ctdb_read_packet(conn
->fd
, conn
, &hdr
);
1130 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1132 cluster_fatal("ctdbd died\n");
1135 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
1136 DEBUG(0, ("Got operation %u, expected a message\n",
1137 (unsigned)hdr
->operation
));
1139 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1142 m
= (struct ctdb_req_message
*)hdr
;
1143 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1144 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1145 DEBUG(0, ("Got invalid traverse data of length %d\n",
1148 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1151 key
.dsize
= d
->keylen
;
1152 key
.dptr
= &d
->data
[0];
1153 data
.dsize
= d
->datalen
;
1154 data
.dptr
= &d
->data
[d
->keylen
];
1156 if (key
.dsize
== 0 && data
.dsize
== 0) {
1157 /* end of traverse */
1159 return NT_STATUS_OK
;
1162 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1163 DEBUG(0, ("Got invalid ltdb header length %d\n",
1166 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1168 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1169 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1172 fn(key
, data
, private_data
);
1175 return NT_STATUS_OK
;
1179 This is used to canonicalize a ctdb_sock_addr structure.
1181 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1182 struct sockaddr_storage
*out
)
1184 memcpy(out
, in
, sizeof (*out
));
1187 if (in
->ss_family
== AF_INET6
) {
1188 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1189 const struct sockaddr_in6
*in6
=
1190 (const struct sockaddr_in6
*)in
;
1191 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1192 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1193 memset(out
, 0, sizeof(*out
));
1194 #ifdef HAVE_SOCK_SIN_LEN
1195 out4
->sin_len
= sizeof(*out
);
1197 out4
->sin_family
= AF_INET
;
1198 out4
->sin_port
= in6
->sin6_port
;
1199 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1206 * Register us as a server for a particular tcp connection
1209 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1210 const struct sockaddr_storage
*_server
,
1211 const struct sockaddr_storage
*_client
,
1212 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
1214 const uint8_t *msg
, size_t msglen
,
1215 void *private_data
),
1218 struct ctdb_control_tcp_addr p
;
1219 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1221 struct sockaddr_storage client
;
1222 struct sockaddr_storage server
;
1225 * Only one connection so far
1228 smbd_ctdb_canonicalize_ip(_client
, &client
);
1229 smbd_ctdb_canonicalize_ip(_server
, &server
);
1231 switch (client
.ss_family
) {
1233 memcpy(&p
.dest
.ip
, &server
, sizeof(p
.dest
.ip
));
1234 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1237 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1238 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1241 return NT_STATUS_INTERNAL_ERROR
;
1245 * We want to be told about IP releases
1248 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1250 if (!NT_STATUS_IS_OK(status
)) {
1255 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1256 * can send an extra ack to trigger a reset for our client, so it
1257 * immediately reconnects
1259 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1260 CTDB_CONTROL_TCP_CLIENT
, 0,
1261 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1265 call a control on the local node
1267 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1268 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1269 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1272 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1275 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1277 struct ctdb_client_notify_register reg_data
;
1282 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1284 reg_data
.notify_data
[0] = 0;
1286 struct_len
= offsetof(struct ctdb_client_notify_register
,
1287 notify_data
) + reg_data
.len
;
1289 status
= ctdbd_control_local(
1290 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1291 make_tdb_data((uint8_t *)®_data
, struct_len
),
1292 NULL
, NULL
, &cstatus
);
1293 if (!NT_STATUS_IS_OK(status
)) {
1294 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1295 nt_errstr(status
)));
1300 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1302 struct ctdb_client_notify_deregister dereg_data
;
1306 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1308 status
= ctdbd_control_local(
1309 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1310 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1311 NULL
, NULL
, &cstatus
);
1312 if (!NT_STATUS_IS_OK(status
)) {
1313 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1314 nt_errstr(status
)));
1319 NTSTATUS
ctdbd_probe(void)
1322 * Do a very early check if ctdbd is around to avoid an abort and core
1325 struct ctdbd_connection
*conn
= NULL
;
1328 status
= ctdbd_messaging_connection(talloc_tos(), &conn
);
1331 * We only care if we can connect.