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
;
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 int (*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 int 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
)) {
167 ret
= cb
->cb(msg
->hdr
.srcnode
, msg
->hdr
.destnode
,
168 msg
->srvid
, msg
->data
, msg
->datalen
,
179 * get our vnn from the cluster
181 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
185 status
= ctdbd_control(conn
,
186 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_PNN
, 0, 0,
187 tdb_null
, NULL
, NULL
, &cstatus
);
188 if (!NT_STATUS_IS_OK(status
)) {
189 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
192 *vnn
= (uint32_t)cstatus
;
197 * Are we active (i.e. not banned or stopped?)
199 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
204 struct ctdb_node_map
*m
;
205 uint32_t failure_flags
;
209 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
210 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
211 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
212 if (!NT_STATUS_IS_OK(status
)) {
213 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
216 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
217 DEBUG(2, ("Received invalid ctdb data\n"));
221 m
= (struct ctdb_node_map
*)outdata
.dptr
;
223 for (i
=0; i
<m
->num
; i
++) {
224 if (vnn
== m
->nodes
[i
].pnn
) {
230 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
235 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
236 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
238 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
239 DEBUG(2, ("Node has status %x, not active\n",
240 (int)m
->nodes
[i
].flags
));
246 TALLOC_FREE(outdata
.dptr
);
250 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
252 return conn
->our_vnn
;
255 const char *lp_ctdbd_socket(void)
259 ret
= lp__ctdbd_socket();
260 if (ret
!= NULL
&& strlen(ret
) > 0) {
268 * Get us a ctdb connection
271 static int ctdbd_connect(int *pfd
)
273 const char *sockname
= lp_ctdbd_socket();
274 struct sockaddr_un addr
= { 0, };
279 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
282 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
286 addr
.sun_family
= AF_UNIX
;
288 namelen
= strlcpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
289 if (namelen
>= sizeof(addr
.sun_path
)) {
290 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
296 salen
= sizeof(struct sockaddr_un
);
298 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
300 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
310 static int ctdb_read_packet(int fd
, TALLOC_CTX
*mem_ctx
,
311 struct ctdb_req_header
**result
)
313 int timeout
= lp_ctdb_timeout();
314 struct ctdb_req_header
*req
;
324 ret
= poll_one_fd(fd
, POLLIN
, timeout
, &revents
);
336 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
344 if (msglen
< sizeof(struct ctdb_req_header
)) {
348 req
= talloc_size(mem_ctx
, msglen
);
352 talloc_set_name_const(req
, "struct ctdb_req_header");
354 req
->length
= msglen
;
356 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
357 msglen
- sizeof(msglen
));
370 * Read a full ctdbd request. If we have a messaging context, defer incoming
371 * messages that might come in between.
374 static int ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
375 TALLOC_CTX
*mem_ctx
, struct ctdb_req_header
**result
)
377 struct ctdb_req_header
*hdr
;
382 ret
= ctdb_read_packet(conn
->fd
, mem_ctx
, &hdr
);
384 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
385 cluster_fatal("ctdbd died\n");
388 DEBUG(11, ("Received ctdb packet\n"));
389 ctdb_packet_dump(hdr
);
391 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
392 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
394 if (conn
->msg_ctx
== NULL
) {
395 DEBUG(1, ("Got a message without having a msg ctx, "
396 "dropping msg %llu\n",
397 (long long unsigned)msg
->srvid
));
402 if ((conn
->release_ip_handler
!= NULL
)
403 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
406 /* must be dispatched immediately */
407 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
408 ok
= conn
->release_ip_handler((const char *)msg
->data
,
409 conn
->release_ip_priv
);
414 * We need to release the ip,
415 * so return an error to the upper layers.
417 * We make sure we don't trigger this again.
419 conn
->release_ip_handler
= NULL
;
420 conn
->release_ip_priv
= NULL
;
421 return EADDRNOTAVAIL
;
426 ret
= ctdbd_msg_call_back(conn
, msg
);
436 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
437 /* we got the wrong reply */
438 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
439 "been %u\n", hdr
->reqid
, reqid
));
444 *result
= talloc_move(mem_ctx
, &hdr
);
449 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
)
455 * Get us a ctdbd connection
458 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
459 struct ctdbd_connection
**pconn
)
461 struct ctdbd_connection
*conn
;
465 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
466 DEBUG(0, ("talloc failed\n"));
467 return NT_STATUS_NO_MEMORY
;
470 ret
= ctdbd_connect(&conn
->fd
);
472 status
= map_nt_error_from_unix(ret
);
473 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret
)));
476 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
478 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
480 if (!NT_STATUS_IS_OK(status
)) {
481 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
485 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
486 DEBUG(2, ("Node is not working, can not connect\n"));
487 status
= NT_STATUS_INTERNAL_DB_ERROR
;
491 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
492 sizeof(conn
->rand_srvid
));
494 status
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
496 if (!NT_STATUS_IS_OK(status
)) {
497 DEBUG(5, ("Could not register random srvid: %s\n",
511 * Get us a ctdbd connection and register us as a process
514 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
515 struct ctdbd_connection
**pconn
)
517 struct ctdbd_connection
*conn
;
520 status
= ctdbd_init_connection(mem_ctx
, &conn
);
522 if (!NT_STATUS_IS_OK(status
)) {
526 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
, NULL
, NULL
);
527 if (!NT_STATUS_IS_OK(status
)) {
539 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
541 return conn
->msg_ctx
;
544 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
550 * Packet handler to receive and handle a ctdb message
552 static int ctdb_handle_message(struct ctdbd_connection
*conn
,
553 struct ctdb_req_header
*hdr
)
555 struct ctdb_req_message
*msg
;
557 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
558 DEBUG(0, ("Received async msg of type %u, discarding\n",
563 msg
= (struct ctdb_req_message
*)hdr
;
565 if ((conn
->release_ip_handler
!= NULL
)
566 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
569 /* must be dispatched immediately */
570 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
571 ret
= conn
->release_ip_handler((const char *)msg
->data
,
572 conn
->release_ip_priv
);
575 * We need to release the ip.
577 * We make sure we don't trigger this again.
579 conn
->release_ip_handler
= NULL
;
580 conn
->release_ip_priv
= NULL
;
585 ctdbd_msg_call_back(conn
, msg
);
591 * The ctdbd socket is readable asynchronuously
594 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
595 struct tevent_fd
*event
,
599 struct ctdbd_connection
*conn
= talloc_get_type_abort(
600 private_data
, struct ctdbd_connection
);
601 struct ctdb_req_header
*hdr
= NULL
;
604 ret
= ctdb_read_packet(conn
->fd
, talloc_tos(), &hdr
);
606 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
607 cluster_fatal("ctdbd died\n");
610 ret
= ctdb_handle_message(conn
, hdr
);
615 DEBUG(10, ("could not handle incoming message: %s\n",
621 * Prepare a ctdbd connection to receive messages
624 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
625 struct messaging_context
*msg_ctx
)
627 SMB_ASSERT(conn
->msg_ctx
== NULL
);
628 SMB_ASSERT(conn
->fde
== NULL
);
630 if (!(conn
->fde
= tevent_add_fd(messaging_tevent_context(msg_ctx
),
634 ctdbd_socket_handler
,
636 DEBUG(0, ("event_add_fd failed\n"));
637 return NT_STATUS_NO_MEMORY
;
640 conn
->msg_ctx
= msg_ctx
;
645 NTSTATUS
ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
646 uint32_t dst_vnn
, uint64_t dst_srvid
,
647 const struct iovec
*iov
, int iovlen
)
649 struct ctdb_req_message r
;
650 struct iovec iov2
[iovlen
+1];
651 size_t buflen
= iov_buflen(iov
, iovlen
);
654 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + buflen
;
655 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
656 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
657 r
.hdr
.generation
= 1;
658 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
659 r
.hdr
.destnode
= dst_vnn
;
660 r
.hdr
.srcnode
= conn
->our_vnn
;
665 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
666 ctdb_packet_dump(&r
.hdr
);
668 iov2
[0].iov_base
= &r
;
669 iov2
[0].iov_len
= offsetof(struct ctdb_req_message
, data
);
670 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
672 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
673 if (nwritten
== -1) {
674 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
675 cluster_fatal("cluster dispatch daemon msg write error\n");
682 * send/recv a generic ctdb control message
684 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
685 uint32_t vnn
, uint32_t opcode
,
686 uint64_t srvid
, uint32_t flags
,
688 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
691 struct ctdb_req_control req
;
692 struct ctdb_req_header
*hdr
;
693 struct ctdb_reply_control
*reply
= NULL
;
694 struct ctdbd_connection
*new_conn
= NULL
;
701 status
= ctdbd_init_connection(NULL
, &new_conn
);
703 if (!NT_STATUS_IS_OK(status
)) {
704 DEBUG(10, ("Could not init temp connection: %s\n",
713 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
714 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
715 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
716 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
717 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
718 req
.hdr
.destnode
= vnn
;
721 req
.datalen
= data
.dsize
;
724 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
725 ctdb_packet_dump(&req
.hdr
);
727 iov
[0].iov_base
= &req
;
728 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
729 iov
[1].iov_base
= data
.dptr
;
730 iov
[1].iov_len
= data
.dsize
;
732 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
733 if (nwritten
== -1) {
734 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
735 cluster_fatal("cluster dispatch daemon msg write error\n");
738 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
739 TALLOC_FREE(new_conn
);
746 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
748 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
749 status
= map_nt_error_from_unix(ret
);
753 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
754 DEBUG(0, ("received invalid reply\n"));
757 reply
= (struct ctdb_reply_control
*)hdr
;
760 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
761 mem_ctx
, reply
->data
, reply
->datalen
))) {
763 return NT_STATUS_NO_MEMORY
;
765 outdata
->dsize
= reply
->datalen
;
768 (*cstatus
) = reply
->status
;
771 status
= NT_STATUS_OK
;
774 TALLOC_FREE(new_conn
);
780 * see if a remote process exists
782 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
790 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
791 DEBUG(10, ("ctdb_processes_exist failed\n"));
797 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
798 const struct server_id
*pids
, int num_pids
,
801 TALLOC_CTX
*frame
= talloc_stackframe();
806 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
807 if (reqids
== NULL
) {
811 for (i
=0; i
<num_pids
; i
++) {
812 struct ctdb_req_control req
;
818 reqids
[i
] = ctdbd_next_reqid(conn
);
823 * pids[i].pid is uint64_t, scale down to pid_t which
824 * is the wire protocol towards ctdb.
828 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
829 (int)pids
[i
].vnn
, (int)pid
,
832 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
833 req
.hdr
.length
+= sizeof(pid
);
834 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
835 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
836 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
837 req
.hdr
.reqid
= reqids
[i
];
838 req
.hdr
.destnode
= pids
[i
].vnn
;
839 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
841 req
.datalen
= sizeof(pid
);
844 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
845 ctdb_packet_dump(&req
.hdr
);
847 iov
[0].iov_base
= &req
;
848 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
849 iov
[1].iov_base
= &pid
;
850 iov
[1].iov_len
= sizeof(pid
);
852 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
853 if (nwritten
== -1) {
854 DEBUG(10, ("write_data_iov failed: %s\n",
862 while (num_received
< num_pids
) {
863 struct ctdb_req_header
*hdr
;
864 struct ctdb_reply_control
*reply
;
868 ret
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
870 DEBUG(10, ("ctdb_read_req failed: %s\n",
875 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
876 DEBUG(10, ("Received invalid reply\n"));
879 reply
= (struct ctdb_reply_control
*)hdr
;
881 reqid
= reply
->hdr
.reqid
;
883 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
885 for (i
=0; i
<num_pids
; i
++) {
886 if (reqid
== reqids
[i
]) {
891 DEBUG(10, ("Received unknown record number %u\n",
895 results
[i
] = ((reply
->status
) == 0);
906 struct ctdb_vnn_list
{
912 unsigned *pid_indexes
;
916 * Get a list of all vnns mentioned in a list of
917 * server_ids. vnn_indexes tells where in the vnns array we have to
920 static bool ctdb_collect_vnns(TALLOC_CTX
*mem_ctx
,
921 const struct server_id
*pids
, unsigned num_pids
,
922 struct ctdb_vnn_list
**pvnns
,
925 struct ctdb_vnn_list
*vnns
= NULL
;
926 unsigned *vnn_indexes
= NULL
;
927 unsigned i
, num_vnns
= 0;
929 vnn_indexes
= talloc_array(mem_ctx
, unsigned, num_pids
);
930 if (vnn_indexes
== NULL
) {
931 DEBUG(1, ("talloc_array failed\n"));
935 for (i
=0; i
<num_pids
; i
++) {
937 uint32_t vnn
= pids
[i
].vnn
;
939 for (j
=0; j
<num_vnns
; j
++) {
940 if (vnn
== vnns
[j
].vnn
) {
948 * Already in the array
950 vnns
[j
].num_srvids
+= 1;
953 vnns
= talloc_realloc(mem_ctx
, vnns
, struct ctdb_vnn_list
,
956 DEBUG(1, ("talloc_realloc failed\n"));
959 vnns
[num_vnns
].vnn
= vnn
;
960 vnns
[num_vnns
].num_srvids
= 1;
961 vnns
[num_vnns
].num_filled
= 0;
964 for (i
=0; i
<num_vnns
; i
++) {
965 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
967 vnn
->srvids
= talloc_array(vnns
, uint64_t, vnn
->num_srvids
);
968 if (vnn
->srvids
== NULL
) {
969 DEBUG(1, ("talloc_array failed\n"));
972 vnn
->pid_indexes
= talloc_array(vnns
, unsigned,
974 if (vnn
->pid_indexes
== NULL
) {
975 DEBUG(1, ("talloc_array failed\n"));
979 for (i
=0; i
<num_pids
; i
++) {
980 struct ctdb_vnn_list
*vnn
= &vnns
[vnn_indexes
[i
]];
981 vnn
->srvids
[vnn
->num_filled
] = pids
[i
].unique_id
;
982 vnn
->pid_indexes
[vnn
->num_filled
] = i
;
983 vnn
->num_filled
+= 1;
986 TALLOC_FREE(vnn_indexes
);
988 *pnum_vnns
= num_vnns
;
992 TALLOC_FREE(vnn_indexes
);
996 bool ctdb_serverids_exist_supported(struct ctdbd_connection
*conn
)
1001 bool ctdb_serverids_exist(struct ctdbd_connection
*conn
,
1002 const struct server_id
*pids
, unsigned num_pids
,
1005 unsigned i
, num_received
;
1006 struct ctdb_vnn_list
*vnns
= NULL
;
1009 if (!ctdb_collect_vnns(talloc_tos(), pids
, num_pids
,
1010 &vnns
, &num_vnns
)) {
1011 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1015 for (i
=0; i
<num_vnns
; i
++) {
1016 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
1017 struct ctdb_req_control req
;
1018 struct iovec iov
[2];
1021 vnn
->reqid
= ctdbd_next_reqid(conn
);
1025 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1026 (int)vnn
->vnn
, (int)vnn
->reqid
, vnn
->num_srvids
));
1028 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
1029 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1030 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1031 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
1032 req
.hdr
.reqid
= vnn
->reqid
;
1033 req
.hdr
.destnode
= vnn
->vnn
;
1034 req
.opcode
= CTDB_CONTROL_CHECK_SRVIDS
;
1036 req
.datalen
= sizeof(uint64_t) * vnn
->num_srvids
;
1037 req
.hdr
.length
+= req
.datalen
;
1040 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1041 ctdb_packet_dump(&req
.hdr
);
1043 iov
[0].iov_base
= &req
;
1044 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
1045 iov
[1].iov_base
= vnn
->srvids
;
1046 iov
[1].iov_len
= req
.datalen
;
1048 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1049 if (nwritten
== -1) {
1050 DEBUG(10, ("write_data_iov failed: %s\n",
1058 while (num_received
< num_vnns
) {
1059 struct ctdb_req_header
*hdr
;
1060 struct ctdb_reply_control
*reply
;
1061 struct ctdb_vnn_list
*vnn
;
1063 uint8_t *reply_data
;
1066 ret
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
1068 DEBUG(10, ("ctdb_read_req failed: %s\n",
1073 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
1074 DEBUG(1, ("Received invalid reply %u\n",
1075 (unsigned)hdr
->operation
));
1078 reply
= (struct ctdb_reply_control
*)hdr
;
1080 reqid
= reply
->hdr
.reqid
;
1082 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1084 for (i
=0; i
<num_vnns
; i
++) {
1085 if (reqid
== vnns
[i
].reqid
) {
1089 if (i
== num_vnns
) {
1090 DEBUG(1, ("Received unknown reqid number %u\n",
1095 DEBUG(10, ("Found index %u\n", i
));
1099 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1100 (unsigned)vnn
->vnn
, vnn
->num_srvids
,
1101 (unsigned)reply
->datalen
));
1103 if (reply
->datalen
>= ((vnn
->num_srvids
+7)/8)) {
1107 reply_data
= reply
->data
;
1110 * Got an error reply
1112 DEBUG(5, ("Received short reply len %d, status %u, "
1114 (unsigned)reply
->datalen
,
1115 (unsigned)reply
->status
,
1116 (unsigned)reply
->errorlen
));
1117 dump_data(5, reply
->data
, reply
->errorlen
);
1120 * This will trigger everything set to false
1125 for (i
=0; i
<vnn
->num_srvids
; i
++) {
1126 int idx
= vnn
->pid_indexes
[i
];
1128 if (pids
[i
].unique_id
==
1129 SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
1130 results
[idx
] = true;
1134 (reply_data
!= NULL
) &&
1135 ((reply_data
[i
/8] & (1<<(i
%8))) != 0);
1145 cluster_fatal("serverids_exist failed");
1152 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
1153 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
1157 TDB_DATA rdata
= {0};
1158 int32_t cstatus
= 0;
1160 data
.dptr
= (uint8_t*)&db_id
;
1161 data
.dsize
= sizeof(db_id
);
1163 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1164 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
1165 mem_ctx
, &rdata
, &cstatus
);
1166 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1167 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
1171 return (char *)rdata
.dptr
;
1175 * attach to a ctdb database
1177 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
1178 const char *name
, uint32_t *db_id
, int tdb_flags
)
1183 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
1185 data
= string_term_tdb_data(name
);
1187 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1189 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1190 : CTDB_CONTROL_DB_ATTACH
,
1191 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
1192 if (!NT_STATUS_IS_OK(status
)) {
1193 DEBUG(0, (__location__
" ctdb_control for db_attach "
1194 "failed: %s\n", nt_errstr(status
)));
1198 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
1199 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
1200 return NT_STATUS_INTERNAL_ERROR
;
1203 *db_id
= *(uint32_t *)data
.dptr
;
1204 talloc_free(data
.dptr
);
1206 if (!(tdb_flags
& TDB_SEQNUM
)) {
1207 return NT_STATUS_OK
;
1210 data
.dptr
= (uint8_t *)db_id
;
1211 data
.dsize
= sizeof(*db_id
);
1213 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1214 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
1215 NULL
, NULL
, &cstatus
);
1216 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1217 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
1219 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
1223 return NT_STATUS_OK
;
1227 * force the migration of a record to this node
1229 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
1232 struct ctdb_req_call req
;
1233 struct ctdb_req_header
*hdr
;
1234 struct iovec iov
[2];
1241 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1242 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1243 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1244 req
.hdr
.operation
= CTDB_REQ_CALL
;
1245 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1246 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1247 req
.callid
= CTDB_NULL_FUNC
;
1249 req
.keylen
= key
.dsize
;
1251 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1252 ctdb_packet_dump(&req
.hdr
);
1254 iov
[0].iov_base
= &req
;
1255 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1256 iov
[1].iov_base
= key
.dptr
;
1257 iov
[1].iov_len
= key
.dsize
;
1259 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1260 if (nwritten
== -1) {
1261 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1262 cluster_fatal("cluster dispatch daemon msg write error\n");
1265 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1267 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
1268 status
= map_nt_error_from_unix(ret
);
1272 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
1273 DEBUG(0, ("received invalid reply\n"));
1274 status
= NT_STATUS_INTERNAL_ERROR
;
1278 status
= NT_STATUS_OK
;
1286 * Fetch a record and parse it
1288 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1289 TDB_DATA key
, bool local_copy
,
1290 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1291 void *private_data
),
1294 struct ctdb_req_call req
;
1295 struct ctdb_req_header
*hdr
= NULL
;
1296 struct ctdb_reply_call
*reply
;
1297 struct iovec iov
[2];
1303 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1307 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1308 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1309 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1310 req
.hdr
.operation
= CTDB_REQ_CALL
;
1311 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1313 req
.callid
= CTDB_FETCH_FUNC
;
1315 req
.keylen
= key
.dsize
;
1317 iov
[0].iov_base
= &req
;
1318 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1319 iov
[1].iov_base
= key
.dptr
;
1320 iov
[1].iov_len
= key
.dsize
;
1322 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1323 if (nwritten
== -1) {
1324 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1325 cluster_fatal("cluster dispatch daemon msg write error\n");
1328 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1330 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
1331 status
= map_nt_error_from_unix(ret
);
1335 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
1336 DEBUG(0, ("received invalid reply\n"));
1337 status
= NT_STATUS_INTERNAL_ERROR
;
1340 reply
= (struct ctdb_reply_call
*)hdr
;
1342 if (reply
->datalen
== 0) {
1344 * Treat an empty record as non-existing
1346 status
= NT_STATUS_NOT_FOUND
;
1350 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1353 status
= NT_STATUS_OK
;
1360 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1361 connection to ctdbd to avoid the hairy recursive and async problems with
1365 NTSTATUS
ctdbd_traverse(uint32_t db_id
,
1366 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1367 void *private_data
),
1370 struct ctdbd_connection
*conn
;
1374 struct ctdb_traverse_start t
;
1378 status
= ctdbd_init_connection(NULL
, &conn
);
1380 if (!NT_STATUS_IS_OK(status
)) {
1381 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1382 nt_errstr(status
)));
1387 t
.srvid
= conn
->rand_srvid
;
1388 t
.reqid
= ctdbd_next_reqid(conn
);
1390 data
.dptr
= (uint8_t *)&t
;
1391 data
.dsize
= sizeof(t
);
1393 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1394 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1395 data
, NULL
, NULL
, &cstatus
);
1397 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1399 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1402 if (NT_STATUS_IS_OK(status
)) {
1404 * We need a mapping here
1406 status
= NT_STATUS_UNSUCCESSFUL
;
1413 struct ctdb_req_header
*hdr
= NULL
;
1414 struct ctdb_req_message
*m
;
1415 struct ctdb_rec_data
*d
;
1418 ret
= ctdb_read_packet(conn
->fd
, conn
, &hdr
);
1420 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1422 cluster_fatal("ctdbd died\n");
1425 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
1426 DEBUG(0, ("Got operation %u, expected a message\n",
1427 (unsigned)hdr
->operation
));
1429 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1432 m
= (struct ctdb_req_message
*)hdr
;
1433 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1434 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1435 DEBUG(0, ("Got invalid traverse data of length %d\n",
1438 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1441 key
.dsize
= d
->keylen
;
1442 key
.dptr
= &d
->data
[0];
1443 data
.dsize
= d
->datalen
;
1444 data
.dptr
= &d
->data
[d
->keylen
];
1446 if (key
.dsize
== 0 && data
.dsize
== 0) {
1447 /* end of traverse */
1449 return NT_STATUS_OK
;
1452 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1453 DEBUG(0, ("Got invalid ltdb header length %d\n",
1456 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1458 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1459 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1462 fn(key
, data
, private_data
);
1465 return NT_STATUS_OK
;
1469 This is used to canonicalize a ctdb_sock_addr structure.
1471 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1472 struct sockaddr_storage
*out
)
1474 memcpy(out
, in
, sizeof (*out
));
1477 if (in
->ss_family
== AF_INET6
) {
1478 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1479 const struct sockaddr_in6
*in6
=
1480 (const struct sockaddr_in6
*)in
;
1481 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1482 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1483 memset(out
, 0, sizeof(*out
));
1484 #ifdef HAVE_SOCK_SIN_LEN
1485 out4
->sin_len
= sizeof(*out
);
1487 out4
->sin_family
= AF_INET
;
1488 out4
->sin_port
= in6
->sin6_port
;
1489 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1496 * Register us as a server for a particular tcp connection
1499 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1500 const struct sockaddr_storage
*_server
,
1501 const struct sockaddr_storage
*_client
,
1502 bool (*release_ip_handler
)(const char *ip_addr
,
1503 void *private_data
),
1506 struct ctdb_control_tcp_addr p
;
1507 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1509 struct sockaddr_storage client
;
1510 struct sockaddr_storage server
;
1513 * Only one connection so far
1515 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1517 smbd_ctdb_canonicalize_ip(_client
, &client
);
1518 smbd_ctdb_canonicalize_ip(_server
, &server
);
1520 switch (client
.ss_family
) {
1522 memcpy(&p
.dest
.ip
, &server
, sizeof(p
.dest
.ip
));
1523 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1526 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1527 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1530 return NT_STATUS_INTERNAL_ERROR
;
1533 conn
->release_ip_handler
= release_ip_handler
;
1534 conn
->release_ip_priv
= private_data
;
1537 * We want to be told about IP releases
1540 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
, NULL
, NULL
);
1541 if (!NT_STATUS_IS_OK(status
)) {
1546 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1547 * can send an extra ack to trigger a reset for our client, so it
1548 * immediately reconnects
1550 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1551 CTDB_CONTROL_TCP_CLIENT
, 0,
1552 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1556 call a control on the local node
1558 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1559 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1560 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1563 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1566 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1568 struct ctdb_client_notify_register reg_data
;
1573 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1575 reg_data
.notify_data
[0] = 0;
1577 struct_len
= offsetof(struct ctdb_client_notify_register
,
1578 notify_data
) + reg_data
.len
;
1580 status
= ctdbd_control_local(
1581 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1582 make_tdb_data((uint8_t *)®_data
, struct_len
),
1583 NULL
, NULL
, &cstatus
);
1584 if (!NT_STATUS_IS_OK(status
)) {
1585 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1586 nt_errstr(status
)));
1591 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1593 struct ctdb_client_notify_deregister dereg_data
;
1597 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1599 status
= ctdbd_control_local(
1600 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1601 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1602 NULL
, NULL
, &cstatus
);
1603 if (!NT_STATUS_IS_OK(status
)) {
1604 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1605 nt_errstr(status
)));
1610 NTSTATUS
ctdbd_probe(void)
1613 * Do a very early check if ctdbd is around to avoid an abort and core
1616 struct ctdbd_connection
*conn
= NULL
;
1619 status
= ctdbd_messaging_connection(talloc_tos(), &conn
);
1622 * We only care if we can connect.