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 void (*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
;
54 bool (*release_ip_handler
)(const char *ip_addr
, void *private_data
);
55 void *release_ip_priv
;
58 static uint32_t ctdbd_next_reqid(struct ctdbd_connection
*conn
)
61 if (conn
->reqid
== 0) {
67 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
68 uint32_t vnn
, uint32_t opcode
,
69 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
70 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
74 * exit on fatal communications errors with the ctdbd daemon
76 static void cluster_fatal(const char *why
)
78 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
79 /* we don't use smb_panic() as we don't want to delay to write
80 a core file. We need to release this process id immediately
81 so that someone else can take over without getting sharing
89 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
91 if (DEBUGLEVEL
< 11) {
94 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
95 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
96 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
97 (int)hdr
->operation
, (int)hdr
->reqid
));
101 * Register a srvid with ctdbd
103 NTSTATUS
register_with_ctdbd(struct ctdbd_connection
*conn
, uint64_t srvid
,
104 void (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
106 const uint8_t *msg
, size_t msglen
,
113 size_t num_callbacks
;
114 struct ctdbd_srvid_cb
*tmp
;
116 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
117 CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
118 tdb_null
, NULL
, NULL
, &cstatus
);
119 if (!NT_STATUS_IS_OK(status
)) {
123 num_callbacks
= talloc_array_length(conn
->callbacks
);
125 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
128 return NT_STATUS_NO_MEMORY
;
130 conn
->callbacks
= tmp
;
132 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
133 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
139 static void ctdbd_msg_call_back(struct ctdbd_connection
*conn
,
140 struct ctdb_req_message
*msg
)
143 size_t i
, num_callbacks
;
145 msg_len
= msg
->hdr
.length
;
146 if (msg_len
< offsetof(struct ctdb_req_message
, data
)) {
147 DEBUG(10, ("%s: len %u too small\n", __func__
,
151 msg_len
-= offsetof(struct ctdb_req_message
, data
);
153 if (msg_len
< msg
->datalen
) {
154 DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__
,
155 (unsigned)msg_len
, (unsigned)msg
->datalen
));
159 num_callbacks
= talloc_array_length(conn
->callbacks
);
161 for (i
=0; i
<num_callbacks
; i
++) {
162 struct ctdbd_srvid_cb
*cb
= &conn
->callbacks
[i
];
164 if ((cb
->srvid
== msg
->srvid
) && (cb
->cb
!= NULL
)) {
165 cb
->cb(msg
->hdr
.srcnode
, msg
->hdr
.destnode
,
166 msg
->srvid
, msg
->data
, msg
->datalen
,
173 * get our vnn from the cluster
175 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
179 status
= ctdbd_control(conn
,
180 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_PNN
, 0, 0,
181 tdb_null
, NULL
, NULL
, &cstatus
);
182 if (!NT_STATUS_IS_OK(status
)) {
183 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
186 *vnn
= (uint32_t)cstatus
;
191 * Are we active (i.e. not banned or stopped?)
193 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
198 struct ctdb_node_map
*m
;
199 uint32_t failure_flags
;
203 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
204 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
205 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
206 if (!NT_STATUS_IS_OK(status
)) {
207 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
210 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
211 DEBUG(2, ("Received invalid ctdb data\n"));
215 m
= (struct ctdb_node_map
*)outdata
.dptr
;
217 for (i
=0; i
<m
->num
; i
++) {
218 if (vnn
== m
->nodes
[i
].pnn
) {
224 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
229 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
230 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
232 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
233 DEBUG(2, ("Node has status %x, not active\n",
234 (int)m
->nodes
[i
].flags
));
240 TALLOC_FREE(outdata
.dptr
);
244 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
246 return conn
->our_vnn
;
249 const char *lp_ctdbd_socket(void)
253 ret
= lp__ctdbd_socket();
254 if (ret
!= NULL
&& strlen(ret
) > 0) {
262 * Get us a ctdb connection
265 static int ctdbd_connect(int *pfd
)
267 const char *sockname
= lp_ctdbd_socket();
268 struct sockaddr_un addr
= { 0, };
273 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
276 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
280 addr
.sun_family
= AF_UNIX
;
282 namelen
= strlcpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
283 if (namelen
>= sizeof(addr
.sun_path
)) {
284 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
290 salen
= sizeof(struct sockaddr_un
);
292 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
294 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
304 static NTSTATUS
ctdb_read_packet(int fd
, TALLOC_CTX
*mem_ctx
,
305 struct ctdb_req_header
**result
)
307 int timeout
= lp_ctdb_timeout();
308 struct ctdb_req_header
*req
;
318 ret
= poll_one_fd(fd
, POLLIN
, timeout
, &revents
);
320 return map_nt_error_from_unix(errno
);
323 return NT_STATUS_IO_TIMEOUT
;
326 return NT_STATUS_UNEXPECTED_IO_ERROR
;
330 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
332 return map_nt_error_from_unix(errno
);
335 return NT_STATUS_UNEXPECTED_IO_ERROR
;
338 if (msglen
< sizeof(struct ctdb_req_header
)) {
339 return NT_STATUS_UNEXPECTED_IO_ERROR
;
342 req
= talloc_size(mem_ctx
, msglen
);
344 return NT_STATUS_NO_MEMORY
;
346 talloc_set_name_const(req
, "struct ctdb_req_header");
348 req
->length
= msglen
;
350 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
351 msglen
- sizeof(msglen
));
353 return map_nt_error_from_unix(errno
);
356 return NT_STATUS_UNEXPECTED_IO_ERROR
;
364 * Read a full ctdbd request. If we have a messaging context, defer incoming
365 * messages that might come in between.
368 static NTSTATUS
ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
370 struct ctdb_req_header
**result
)
372 struct ctdb_req_header
*hdr
;
377 status
= ctdb_read_packet(conn
->fd
, mem_ctx
, &hdr
);
378 if (!NT_STATUS_IS_OK(status
)) {
379 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status
)));
380 cluster_fatal("ctdbd died\n");
383 DEBUG(11, ("Received ctdb packet\n"));
384 ctdb_packet_dump(hdr
);
386 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
387 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
389 if (conn
->msg_ctx
== NULL
) {
390 DEBUG(1, ("Got a message without having a msg ctx, "
391 "dropping msg %llu\n",
392 (long long unsigned)msg
->srvid
));
396 if ((conn
->release_ip_handler
!= NULL
)
397 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
400 /* must be dispatched immediately */
401 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
402 ret
= conn
->release_ip_handler((const char *)msg
->data
,
403 conn
->release_ip_priv
);
408 * We need to release the ip,
409 * so return an error to the upper layers.
411 * We make sure we don't trigger this again.
413 conn
->release_ip_handler
= NULL
;
414 conn
->release_ip_priv
= NULL
;
415 return NT_STATUS_ADDRESS_CLOSED
;
420 ctdbd_msg_call_back(conn
, msg
);
425 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
426 /* we got the wrong reply */
427 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
428 "been %u\n", hdr
->reqid
, reqid
));
433 *result
= talloc_move(mem_ctx
, &hdr
);
438 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
)
444 * Get us a ctdbd connection
447 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
448 struct ctdbd_connection
**pconn
)
450 struct ctdbd_connection
*conn
;
454 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
455 DEBUG(0, ("talloc failed\n"));
456 return NT_STATUS_NO_MEMORY
;
459 ret
= ctdbd_connect(&conn
->fd
);
461 status
= map_nt_error_from_unix(ret
);
462 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret
)));
465 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
467 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
469 if (!NT_STATUS_IS_OK(status
)) {
470 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
474 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
475 DEBUG(2, ("Node is not working, can not connect\n"));
476 status
= NT_STATUS_INTERNAL_DB_ERROR
;
480 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
481 sizeof(conn
->rand_srvid
));
483 status
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
485 if (!NT_STATUS_IS_OK(status
)) {
486 DEBUG(5, ("Could not register random srvid: %s\n",
500 * Get us a ctdbd connection and register us as a process
503 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
504 struct ctdbd_connection
**pconn
)
506 struct ctdbd_connection
*conn
;
509 status
= ctdbd_init_connection(mem_ctx
, &conn
);
511 if (!NT_STATUS_IS_OK(status
)) {
515 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
, NULL
, NULL
);
516 if (!NT_STATUS_IS_OK(status
)) {
528 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
530 return conn
->msg_ctx
;
533 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
539 * Packet handler to receive and handle a ctdb message
541 static NTSTATUS
ctdb_handle_message(struct ctdbd_connection
*conn
,
542 struct ctdb_req_header
*hdr
)
544 struct ctdb_req_message
*msg
;
546 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
547 DEBUG(0, ("Received async msg of type %u, discarding\n",
549 return NT_STATUS_INVALID_PARAMETER
;
552 msg
= (struct ctdb_req_message
*)hdr
;
554 if ((conn
->release_ip_handler
!= NULL
)
555 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
558 /* must be dispatched immediately */
559 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
560 ret
= conn
->release_ip_handler((const char *)msg
->data
,
561 conn
->release_ip_priv
);
564 * We need to release the ip.
566 * We make sure we don't trigger this again.
568 conn
->release_ip_handler
= NULL
;
569 conn
->release_ip_priv
= NULL
;
574 ctdbd_msg_call_back(conn
, msg
);
580 * The ctdbd socket is readable asynchronuously
583 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
584 struct tevent_fd
*event
,
588 struct ctdbd_connection
*conn
= talloc_get_type_abort(
589 private_data
, struct ctdbd_connection
);
590 struct ctdb_req_header
*hdr
= NULL
;
593 status
= ctdb_read_packet(conn
->fd
, talloc_tos(), &hdr
);
594 if (!NT_STATUS_IS_OK(status
)) {
595 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status
)));
596 cluster_fatal("ctdbd died\n");
599 status
= ctdb_handle_message(conn
, hdr
);
603 if (!NT_STATUS_IS_OK(status
)) {
604 DEBUG(10, ("could not handle incoming message: %s\n",
610 * Prepare a ctdbd connection to receive messages
613 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
614 struct messaging_context
*msg_ctx
)
616 SMB_ASSERT(conn
->msg_ctx
== NULL
);
617 SMB_ASSERT(conn
->fde
== NULL
);
619 if (!(conn
->fde
= tevent_add_fd(messaging_tevent_context(msg_ctx
),
623 ctdbd_socket_handler
,
625 DEBUG(0, ("event_add_fd failed\n"));
626 return NT_STATUS_NO_MEMORY
;
629 conn
->msg_ctx
= msg_ctx
;
634 NTSTATUS
ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
635 uint32_t dst_vnn
, uint64_t dst_srvid
,
636 const struct iovec
*iov
, int iovlen
)
638 struct ctdb_req_message r
;
639 struct iovec iov2
[iovlen
+1];
640 size_t buflen
= iov_buflen(iov
, iovlen
);
643 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + buflen
;
644 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
645 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
646 r
.hdr
.generation
= 1;
647 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
648 r
.hdr
.destnode
= dst_vnn
;
649 r
.hdr
.srcnode
= conn
->our_vnn
;
654 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
655 ctdb_packet_dump(&r
.hdr
);
657 iov2
[0].iov_base
= &r
;
658 iov2
[0].iov_len
= offsetof(struct ctdb_req_message
, data
);
659 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
661 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
662 if (nwritten
== -1) {
663 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
664 cluster_fatal("cluster dispatch daemon msg write error\n");
671 * send/recv a generic ctdb control message
673 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
674 uint32_t vnn
, uint32_t opcode
,
675 uint64_t srvid
, uint32_t flags
,
677 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
680 struct ctdb_req_control req
;
681 struct ctdb_req_header
*hdr
;
682 struct ctdb_reply_control
*reply
= NULL
;
683 struct ctdbd_connection
*new_conn
= NULL
;
689 status
= ctdbd_init_connection(NULL
, &new_conn
);
691 if (!NT_STATUS_IS_OK(status
)) {
692 DEBUG(10, ("Could not init temp connection: %s\n",
701 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
702 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
703 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
704 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
705 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
706 req
.hdr
.destnode
= vnn
;
709 req
.datalen
= data
.dsize
;
712 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
713 ctdb_packet_dump(&req
.hdr
);
715 iov
[0].iov_base
= &req
;
716 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
717 iov
[1].iov_base
= data
.dptr
;
718 iov
[1].iov_len
= data
.dsize
;
720 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
721 if (nwritten
== -1) {
722 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
723 cluster_fatal("cluster dispatch daemon msg write error\n");
726 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
727 TALLOC_FREE(new_conn
);
734 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
736 if (!NT_STATUS_IS_OK(status
)) {
737 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
741 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
742 DEBUG(0, ("received invalid reply\n"));
745 reply
= (struct ctdb_reply_control
*)hdr
;
748 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
749 mem_ctx
, reply
->data
, reply
->datalen
))) {
751 return NT_STATUS_NO_MEMORY
;
753 outdata
->dsize
= reply
->datalen
;
756 (*cstatus
) = reply
->status
;
759 status
= NT_STATUS_OK
;
762 TALLOC_FREE(new_conn
);
768 * see if a remote process exists
770 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
778 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
779 DEBUG(10, ("ctdb_processes_exist failed\n"));
785 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
786 const struct server_id
*pids
, int num_pids
,
789 TALLOC_CTX
*frame
= talloc_stackframe();
795 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
796 if (reqids
== NULL
) {
800 for (i
=0; i
<num_pids
; i
++) {
801 struct ctdb_req_control req
;
807 reqids
[i
] = ctdbd_next_reqid(conn
);
812 * pids[i].pid is uint64_t, scale down to pid_t which
813 * is the wire protocol towards ctdb.
817 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
818 (int)pids
[i
].vnn
, (int)pid
,
821 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
822 req
.hdr
.length
+= sizeof(pid
);
823 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
824 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
825 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
826 req
.hdr
.reqid
= reqids
[i
];
827 req
.hdr
.destnode
= pids
[i
].vnn
;
828 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
830 req
.datalen
= sizeof(pid
);
833 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
834 ctdb_packet_dump(&req
.hdr
);
836 iov
[0].iov_base
= &req
;
837 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
838 iov
[1].iov_base
= &pid
;
839 iov
[1].iov_len
= sizeof(pid
);
841 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
842 if (nwritten
== -1) {
843 status
= map_nt_error_from_unix(errno
);
844 DEBUG(10, ("write_data_iov failed: %s\n",
852 while (num_received
< num_pids
) {
853 struct ctdb_req_header
*hdr
;
854 struct ctdb_reply_control
*reply
;
857 status
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
858 if (!NT_STATUS_IS_OK(status
)) {
859 DEBUG(10, ("ctdb_read_req failed: %s\n",
864 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
865 DEBUG(10, ("Received invalid reply\n"));
868 reply
= (struct ctdb_reply_control
*)hdr
;
870 reqid
= reply
->hdr
.reqid
;
872 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
874 for (i
=0; i
<num_pids
; i
++) {
875 if (reqid
== reqids
[i
]) {
880 DEBUG(10, ("Received unknown record number %u\n",
884 results
[i
] = ((reply
->status
) == 0);
895 struct ctdb_vnn_list
{
901 unsigned *pid_indexes
;
905 * Get a list of all vnns mentioned in a list of
906 * server_ids. vnn_indexes tells where in the vnns array we have to
909 static bool ctdb_collect_vnns(TALLOC_CTX
*mem_ctx
,
910 const struct server_id
*pids
, unsigned num_pids
,
911 struct ctdb_vnn_list
**pvnns
,
914 struct ctdb_vnn_list
*vnns
= NULL
;
915 unsigned *vnn_indexes
= NULL
;
916 unsigned i
, num_vnns
= 0;
918 vnn_indexes
= talloc_array(mem_ctx
, unsigned, num_pids
);
919 if (vnn_indexes
== NULL
) {
920 DEBUG(1, ("talloc_array failed\n"));
924 for (i
=0; i
<num_pids
; i
++) {
926 uint32_t vnn
= pids
[i
].vnn
;
928 for (j
=0; j
<num_vnns
; j
++) {
929 if (vnn
== vnns
[j
].vnn
) {
937 * Already in the array
939 vnns
[j
].num_srvids
+= 1;
942 vnns
= talloc_realloc(mem_ctx
, vnns
, struct ctdb_vnn_list
,
945 DEBUG(1, ("talloc_realloc failed\n"));
948 vnns
[num_vnns
].vnn
= vnn
;
949 vnns
[num_vnns
].num_srvids
= 1;
950 vnns
[num_vnns
].num_filled
= 0;
953 for (i
=0; i
<num_vnns
; i
++) {
954 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
956 vnn
->srvids
= talloc_array(vnns
, uint64_t, vnn
->num_srvids
);
957 if (vnn
->srvids
== NULL
) {
958 DEBUG(1, ("talloc_array failed\n"));
961 vnn
->pid_indexes
= talloc_array(vnns
, unsigned,
963 if (vnn
->pid_indexes
== NULL
) {
964 DEBUG(1, ("talloc_array failed\n"));
968 for (i
=0; i
<num_pids
; i
++) {
969 struct ctdb_vnn_list
*vnn
= &vnns
[vnn_indexes
[i
]];
970 vnn
->srvids
[vnn
->num_filled
] = pids
[i
].unique_id
;
971 vnn
->pid_indexes
[vnn
->num_filled
] = i
;
972 vnn
->num_filled
+= 1;
975 TALLOC_FREE(vnn_indexes
);
977 *pnum_vnns
= num_vnns
;
981 TALLOC_FREE(vnn_indexes
);
985 bool ctdb_serverids_exist_supported(struct ctdbd_connection
*conn
)
990 bool ctdb_serverids_exist(struct ctdbd_connection
*conn
,
991 const struct server_id
*pids
, unsigned num_pids
,
994 unsigned i
, num_received
;
996 struct ctdb_vnn_list
*vnns
= NULL
;
999 if (!ctdb_collect_vnns(talloc_tos(), pids
, num_pids
,
1000 &vnns
, &num_vnns
)) {
1001 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1005 for (i
=0; i
<num_vnns
; i
++) {
1006 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
1007 struct ctdb_req_control req
;
1008 struct iovec iov
[2];
1011 vnn
->reqid
= ctdbd_next_reqid(conn
);
1015 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1016 (int)vnn
->vnn
, (int)vnn
->reqid
, vnn
->num_srvids
));
1018 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
1019 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1020 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1021 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
1022 req
.hdr
.reqid
= vnn
->reqid
;
1023 req
.hdr
.destnode
= vnn
->vnn
;
1024 req
.opcode
= CTDB_CONTROL_CHECK_SRVIDS
;
1026 req
.datalen
= sizeof(uint64_t) * vnn
->num_srvids
;
1027 req
.hdr
.length
+= req
.datalen
;
1030 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1031 ctdb_packet_dump(&req
.hdr
);
1033 iov
[0].iov_base
= &req
;
1034 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
1035 iov
[1].iov_base
= vnn
->srvids
;
1036 iov
[1].iov_len
= req
.datalen
;
1038 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1039 if (nwritten
== -1) {
1040 status
= map_nt_error_from_unix(errno
);
1041 DEBUG(10, ("write_data_iov failed: %s\n",
1049 while (num_received
< num_vnns
) {
1050 struct ctdb_req_header
*hdr
;
1051 struct ctdb_reply_control
*reply
;
1052 struct ctdb_vnn_list
*vnn
;
1054 uint8_t *reply_data
;
1056 status
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
1057 if (!NT_STATUS_IS_OK(status
)) {
1058 DEBUG(1, ("ctdb_read_req failed: %s\n",
1059 nt_errstr(status
)));
1063 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
1064 DEBUG(1, ("Received invalid reply %u\n",
1065 (unsigned)hdr
->operation
));
1068 reply
= (struct ctdb_reply_control
*)hdr
;
1070 reqid
= reply
->hdr
.reqid
;
1072 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1074 for (i
=0; i
<num_vnns
; i
++) {
1075 if (reqid
== vnns
[i
].reqid
) {
1079 if (i
== num_vnns
) {
1080 DEBUG(1, ("Received unknown reqid number %u\n",
1085 DEBUG(10, ("Found index %u\n", i
));
1089 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1090 (unsigned)vnn
->vnn
, vnn
->num_srvids
,
1091 (unsigned)reply
->datalen
));
1093 if (reply
->datalen
>= ((vnn
->num_srvids
+7)/8)) {
1097 reply_data
= reply
->data
;
1100 * Got an error reply
1102 DEBUG(5, ("Received short reply len %d, status %u, "
1104 (unsigned)reply
->datalen
,
1105 (unsigned)reply
->status
,
1106 (unsigned)reply
->errorlen
));
1107 dump_data(5, reply
->data
, reply
->errorlen
);
1110 * This will trigger everything set to false
1115 for (i
=0; i
<vnn
->num_srvids
; i
++) {
1116 int idx
= vnn
->pid_indexes
[i
];
1118 if (pids
[i
].unique_id
==
1119 SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
1120 results
[idx
] = true;
1124 (reply_data
!= NULL
) &&
1125 ((reply_data
[i
/8] & (1<<(i
%8))) != 0);
1135 cluster_fatal("serverids_exist failed");
1142 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
1143 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
1147 TDB_DATA rdata
= {0};
1148 int32_t cstatus
= 0;
1150 data
.dptr
= (uint8_t*)&db_id
;
1151 data
.dsize
= sizeof(db_id
);
1153 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1154 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
1155 mem_ctx
, &rdata
, &cstatus
);
1156 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1157 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
1161 return (char *)rdata
.dptr
;
1165 * attach to a ctdb database
1167 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
1168 const char *name
, uint32_t *db_id
, int tdb_flags
)
1173 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
1175 data
= string_term_tdb_data(name
);
1177 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1179 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1180 : CTDB_CONTROL_DB_ATTACH
,
1181 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
1182 if (!NT_STATUS_IS_OK(status
)) {
1183 DEBUG(0, (__location__
" ctdb_control for db_attach "
1184 "failed: %s\n", nt_errstr(status
)));
1188 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
1189 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
1190 return NT_STATUS_INTERNAL_ERROR
;
1193 *db_id
= *(uint32_t *)data
.dptr
;
1194 talloc_free(data
.dptr
);
1196 if (!(tdb_flags
& TDB_SEQNUM
)) {
1197 return NT_STATUS_OK
;
1200 data
.dptr
= (uint8_t *)db_id
;
1201 data
.dsize
= sizeof(*db_id
);
1203 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1204 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
1205 NULL
, NULL
, &cstatus
);
1206 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1207 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
1209 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
1213 return NT_STATUS_OK
;
1217 * force the migration of a record to this node
1219 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
1222 struct ctdb_req_call req
;
1223 struct ctdb_req_header
*hdr
;
1224 struct iovec iov
[2];
1230 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1231 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1232 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1233 req
.hdr
.operation
= CTDB_REQ_CALL
;
1234 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1235 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1236 req
.callid
= CTDB_NULL_FUNC
;
1238 req
.keylen
= key
.dsize
;
1240 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1241 ctdb_packet_dump(&req
.hdr
);
1243 iov
[0].iov_base
= &req
;
1244 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1245 iov
[1].iov_base
= key
.dptr
;
1246 iov
[1].iov_len
= key
.dsize
;
1248 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1249 if (nwritten
== -1) {
1250 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1251 cluster_fatal("cluster dispatch daemon msg write error\n");
1254 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1256 if (!NT_STATUS_IS_OK(status
)) {
1257 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1261 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
1262 DEBUG(0, ("received invalid reply\n"));
1263 status
= NT_STATUS_INTERNAL_ERROR
;
1267 status
= NT_STATUS_OK
;
1275 * Fetch a record and parse it
1277 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1278 TDB_DATA key
, bool local_copy
,
1279 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1280 void *private_data
),
1283 struct ctdb_req_call req
;
1284 struct ctdb_req_header
*hdr
= NULL
;
1285 struct ctdb_reply_call
*reply
;
1286 struct iovec iov
[2];
1291 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1295 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1296 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1297 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1298 req
.hdr
.operation
= CTDB_REQ_CALL
;
1299 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1301 req
.callid
= CTDB_FETCH_FUNC
;
1303 req
.keylen
= key
.dsize
;
1305 iov
[0].iov_base
= &req
;
1306 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1307 iov
[1].iov_base
= key
.dptr
;
1308 iov
[1].iov_len
= key
.dsize
;
1310 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1311 if (nwritten
== -1) {
1312 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1313 cluster_fatal("cluster dispatch daemon msg write error\n");
1316 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1318 if (!NT_STATUS_IS_OK(status
)) {
1319 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1323 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
1324 DEBUG(0, ("received invalid reply\n"));
1325 status
= NT_STATUS_INTERNAL_ERROR
;
1328 reply
= (struct ctdb_reply_call
*)hdr
;
1330 if (reply
->datalen
== 0) {
1332 * Treat an empty record as non-existing
1334 status
= NT_STATUS_NOT_FOUND
;
1338 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1341 status
= NT_STATUS_OK
;
1348 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1349 connection to ctdbd to avoid the hairy recursive and async problems with
1353 NTSTATUS
ctdbd_traverse(uint32_t db_id
,
1354 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1355 void *private_data
),
1358 struct ctdbd_connection
*conn
;
1362 struct ctdb_traverse_start t
;
1366 status
= ctdbd_init_connection(NULL
, &conn
);
1368 if (!NT_STATUS_IS_OK(status
)) {
1369 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1370 nt_errstr(status
)));
1375 t
.srvid
= conn
->rand_srvid
;
1376 t
.reqid
= ctdbd_next_reqid(conn
);
1378 data
.dptr
= (uint8_t *)&t
;
1379 data
.dsize
= sizeof(t
);
1381 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1382 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1383 data
, NULL
, NULL
, &cstatus
);
1385 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1387 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1390 if (NT_STATUS_IS_OK(status
)) {
1392 * We need a mapping here
1394 status
= NT_STATUS_UNSUCCESSFUL
;
1401 struct ctdb_req_header
*hdr
= NULL
;
1402 struct ctdb_req_message
*m
;
1403 struct ctdb_rec_data
*d
;
1405 status
= ctdb_read_packet(conn
->fd
, conn
, &hdr
);
1406 if (!NT_STATUS_IS_OK(status
)) {
1407 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1408 nt_errstr(status
)));
1409 cluster_fatal("ctdbd died\n");
1412 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
1413 DEBUG(0, ("Got operation %u, expected a message\n",
1414 (unsigned)hdr
->operation
));
1416 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1419 m
= (struct ctdb_req_message
*)hdr
;
1420 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1421 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1422 DEBUG(0, ("Got invalid traverse data of length %d\n",
1425 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1428 key
.dsize
= d
->keylen
;
1429 key
.dptr
= &d
->data
[0];
1430 data
.dsize
= d
->datalen
;
1431 data
.dptr
= &d
->data
[d
->keylen
];
1433 if (key
.dsize
== 0 && data
.dsize
== 0) {
1434 /* end of traverse */
1436 return NT_STATUS_OK
;
1439 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1440 DEBUG(0, ("Got invalid ltdb header length %d\n",
1443 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1445 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1446 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1449 fn(key
, data
, private_data
);
1452 return NT_STATUS_OK
;
1456 This is used to canonicalize a ctdb_sock_addr structure.
1458 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1459 struct sockaddr_storage
*out
)
1461 memcpy(out
, in
, sizeof (*out
));
1464 if (in
->ss_family
== AF_INET6
) {
1465 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1466 const struct sockaddr_in6
*in6
=
1467 (const struct sockaddr_in6
*)in
;
1468 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1469 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1470 memset(out
, 0, sizeof(*out
));
1471 #ifdef HAVE_SOCK_SIN_LEN
1472 out4
->sin_len
= sizeof(*out
);
1474 out4
->sin_family
= AF_INET
;
1475 out4
->sin_port
= in6
->sin6_port
;
1476 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1483 * Register us as a server for a particular tcp connection
1486 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1487 const struct sockaddr_storage
*_server
,
1488 const struct sockaddr_storage
*_client
,
1489 bool (*release_ip_handler
)(const char *ip_addr
,
1490 void *private_data
),
1493 struct ctdb_control_tcp_addr p
;
1494 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1496 struct sockaddr_storage client
;
1497 struct sockaddr_storage server
;
1500 * Only one connection so far
1502 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1504 smbd_ctdb_canonicalize_ip(_client
, &client
);
1505 smbd_ctdb_canonicalize_ip(_server
, &server
);
1507 switch (client
.ss_family
) {
1509 memcpy(&p
.dest
.ip
, &server
, sizeof(p
.dest
.ip
));
1510 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1513 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1514 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1517 return NT_STATUS_INTERNAL_ERROR
;
1520 conn
->release_ip_handler
= release_ip_handler
;
1521 conn
->release_ip_priv
= private_data
;
1524 * We want to be told about IP releases
1527 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
, NULL
, NULL
);
1528 if (!NT_STATUS_IS_OK(status
)) {
1533 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1534 * can send an extra ack to trigger a reset for our client, so it
1535 * immediately reconnects
1537 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1538 CTDB_CONTROL_TCP_CLIENT
, 0,
1539 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1543 call a control on the local node
1545 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1546 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1547 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1550 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1553 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1555 struct ctdb_client_notify_register reg_data
;
1560 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1562 reg_data
.notify_data
[0] = 0;
1564 struct_len
= offsetof(struct ctdb_client_notify_register
,
1565 notify_data
) + reg_data
.len
;
1567 status
= ctdbd_control_local(
1568 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1569 make_tdb_data((uint8_t *)®_data
, struct_len
),
1570 NULL
, NULL
, &cstatus
);
1571 if (!NT_STATUS_IS_OK(status
)) {
1572 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1573 nt_errstr(status
)));
1578 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1580 struct ctdb_client_notify_deregister dereg_data
;
1584 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1586 status
= ctdbd_control_local(
1587 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1588 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1589 NULL
, NULL
, &cstatus
);
1590 if (!NT_STATUS_IS_OK(status
)) {
1591 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1592 nt_errstr(status
)));
1597 NTSTATUS
ctdbd_probe(void)
1600 * Do a very early check if ctdbd is around to avoid an abort and core
1603 struct ctdbd_connection
*conn
= NULL
;
1606 status
= ctdbd_messaging_connection(talloc_tos(), &conn
);
1609 * We only care if we can connect.