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 const char *sockname
; /* Needed in ctdbd_traverse */
47 struct messaging_context
*msg_ctx
;
51 struct ctdbd_srvid_cb
*callbacks
;
53 struct tevent_fd
*fde
;
57 static uint32_t ctdbd_next_reqid(struct ctdbd_connection
*conn
)
60 if (conn
->reqid
== 0) {
66 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
67 uint32_t vnn
, uint32_t opcode
,
68 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
69 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
71 static int ctdbd_control_unix(struct ctdbd_connection
*conn
,
72 uint32_t vnn
, uint32_t opcode
,
73 uint64_t srvid
, uint32_t flags
,
75 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
79 * exit on fatal communications errors with the ctdbd daemon
81 static void cluster_fatal(const char *why
)
83 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
84 /* we don't use smb_panic() as we don't want to delay to write
85 a core file. We need to release this process id immediately
86 so that someone else can take over without getting sharing
94 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
96 if (DEBUGLEVEL
< 11) {
99 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
100 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
101 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
102 (int)hdr
->operation
, (int)hdr
->reqid
));
106 * Register a srvid with ctdbd
108 NTSTATUS
register_with_ctdbd(struct ctdbd_connection
*conn
, uint64_t srvid
,
109 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
111 const uint8_t *msg
, size_t msglen
,
117 size_t num_callbacks
;
118 struct ctdbd_srvid_cb
*tmp
;
120 ret
= ctdbd_control_unix(conn
, CTDB_CURRENT_NODE
,
121 CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
122 tdb_null
, NULL
, NULL
, &cstatus
);
124 return map_nt_error_from_unix(ret
);
127 num_callbacks
= talloc_array_length(conn
->callbacks
);
129 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
132 return NT_STATUS_NO_MEMORY
;
134 conn
->callbacks
= tmp
;
136 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
137 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
143 static int ctdbd_msg_call_back(struct ctdbd_connection
*conn
,
144 struct ctdb_req_message
*msg
)
147 size_t i
, num_callbacks
;
149 msg_len
= msg
->hdr
.length
;
150 if (msg_len
< offsetof(struct ctdb_req_message
, data
)) {
151 DEBUG(10, ("%s: len %u too small\n", __func__
,
155 msg_len
-= offsetof(struct ctdb_req_message
, data
);
157 if (msg_len
< msg
->datalen
) {
158 DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__
,
159 (unsigned)msg_len
, (unsigned)msg
->datalen
));
163 num_callbacks
= talloc_array_length(conn
->callbacks
);
165 for (i
=0; i
<num_callbacks
; i
++) {
166 struct ctdbd_srvid_cb
*cb
= &conn
->callbacks
[i
];
168 if ((cb
->srvid
== msg
->srvid
) && (cb
->cb
!= NULL
)) {
171 ret
= cb
->cb(msg
->hdr
.srcnode
, msg
->hdr
.destnode
,
172 msg
->srvid
, msg
->data
, msg
->datalen
,
183 * get our vnn from the cluster
185 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
189 ret
= ctdbd_control_unix(conn
,
190 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_PNN
, 0, 0,
191 tdb_null
, NULL
, NULL
, &cstatus
);
193 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret
)));
194 return map_nt_error_from_unix(ret
);
196 *vnn
= (uint32_t)cstatus
;
201 * Are we active (i.e. not banned or stopped?)
203 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
207 struct ctdb_node_map
*m
;
208 uint32_t failure_flags
;
212 ret
= ctdbd_control_unix(conn
, CTDB_CURRENT_NODE
,
213 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
214 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
216 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret
)));
219 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
220 DEBUG(2, ("Received invalid ctdb data\n"));
224 m
= (struct ctdb_node_map
*)outdata
.dptr
;
226 for (i
=0; i
<m
->num
; i
++) {
227 if (vnn
== m
->nodes
[i
].pnn
) {
233 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
238 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
239 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
241 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
242 DEBUG(2, ("Node has status %x, not active\n",
243 (int)m
->nodes
[i
].flags
));
249 TALLOC_FREE(outdata
.dptr
);
253 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
255 return conn
->our_vnn
;
259 * Get us a ctdb connection
262 static int ctdbd_connect(const char *sockname
, int *pfd
)
264 struct sockaddr_un addr
= { 0, };
269 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
272 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
276 addr
.sun_family
= AF_UNIX
;
278 namelen
= strlcpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
279 if (namelen
>= sizeof(addr
.sun_path
)) {
280 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
286 salen
= sizeof(struct sockaddr_un
);
288 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
290 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
300 static int ctdb_read_packet(int fd
, int timeout
, TALLOC_CTX
*mem_ctx
,
301 struct ctdb_req_header
**result
)
303 struct ctdb_req_header
*req
;
309 ret
= poll_one_fd(fd
, POLLIN
, timeout
, &revents
);
321 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
329 if (msglen
< sizeof(struct ctdb_req_header
)) {
333 req
= talloc_size(mem_ctx
, msglen
);
337 talloc_set_name_const(req
, "struct ctdb_req_header");
339 req
->length
= msglen
;
341 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
342 msglen
- sizeof(msglen
));
357 * Read a full ctdbd request. If we have a messaging context, defer incoming
358 * messages that might come in between.
361 static int ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
362 TALLOC_CTX
*mem_ctx
, struct ctdb_req_header
**result
)
364 struct ctdb_req_header
*hdr
;
369 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, mem_ctx
, &hdr
);
371 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
372 cluster_fatal("ctdbd died\n");
375 DEBUG(11, ("Received ctdb packet\n"));
376 ctdb_packet_dump(hdr
);
378 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
379 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
381 if (conn
->msg_ctx
== NULL
) {
382 DEBUG(1, ("Got a message without having a msg ctx, "
383 "dropping msg %llu\n",
384 (long long unsigned)msg
->srvid
));
389 ret
= ctdbd_msg_call_back(conn
, msg
);
399 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
400 /* we got the wrong reply */
401 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
402 "been %u\n", hdr
->reqid
, reqid
));
407 *result
= talloc_move(mem_ctx
, &hdr
);
412 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
)
422 * Get us a ctdbd connection
425 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
426 const char *sockname
, int timeout
,
427 struct ctdbd_connection
**pconn
)
429 struct ctdbd_connection
*conn
;
433 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
434 DEBUG(0, ("talloc failed\n"));
435 return NT_STATUS_NO_MEMORY
;
438 conn
->sockname
= talloc_strdup(conn
, sockname
);
439 if (conn
->sockname
== NULL
) {
440 DBG_ERR("%s: talloc failed\n", __func__
);
441 status
= NT_STATUS_NO_MEMORY
;
445 conn
->timeout
= timeout
;
447 if (conn
->timeout
== 0) {
451 ret
= ctdbd_connect(conn
->sockname
, &conn
->fd
);
453 status
= map_nt_error_from_unix(ret
);
454 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret
)));
457 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
459 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
461 if (!NT_STATUS_IS_OK(status
)) {
462 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
466 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
467 DEBUG(2, ("Node is not working, can not connect\n"));
468 status
= NT_STATUS_INTERNAL_DB_ERROR
;
472 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
473 sizeof(conn
->rand_srvid
));
475 status
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
477 if (!NT_STATUS_IS_OK(status
)) {
478 DEBUG(5, ("Could not register random srvid: %s\n",
492 * Get us a ctdbd connection and register us as a process
495 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
496 const char *sockname
, int timeout
,
497 struct ctdbd_connection
**pconn
)
499 struct ctdbd_connection
*conn
;
502 status
= ctdbd_init_connection(mem_ctx
, sockname
, timeout
, &conn
);
504 if (!NT_STATUS_IS_OK(status
)) {
508 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
, NULL
, NULL
);
509 if (!NT_STATUS_IS_OK(status
)) {
521 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
523 return conn
->msg_ctx
;
526 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
532 * Packet handler to receive and handle a ctdb message
534 static int ctdb_handle_message(struct ctdbd_connection
*conn
,
535 struct ctdb_req_header
*hdr
)
537 struct ctdb_req_message
*msg
;
539 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
540 DEBUG(0, ("Received async msg of type %u, discarding\n",
545 msg
= (struct ctdb_req_message
*)hdr
;
547 ctdbd_msg_call_back(conn
, msg
);
553 * The ctdbd socket is readable asynchronuously
556 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
557 struct tevent_fd
*event
,
561 struct ctdbd_connection
*conn
= talloc_get_type_abort(
562 private_data
, struct ctdbd_connection
);
563 struct ctdb_req_header
*hdr
= NULL
;
566 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, talloc_tos(), &hdr
);
568 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
569 cluster_fatal("ctdbd died\n");
572 ret
= ctdb_handle_message(conn
, hdr
);
577 DEBUG(10, ("could not handle incoming message: %s\n",
583 * Prepare a ctdbd connection to receive messages
586 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
587 struct messaging_context
*msg_ctx
)
589 SMB_ASSERT(conn
->msg_ctx
== NULL
);
590 SMB_ASSERT(conn
->fde
== NULL
);
592 if (!(conn
->fde
= tevent_add_fd(messaging_tevent_context(msg_ctx
),
596 ctdbd_socket_handler
,
598 DEBUG(0, ("event_add_fd failed\n"));
599 return NT_STATUS_NO_MEMORY
;
602 conn
->msg_ctx
= msg_ctx
;
607 NTSTATUS
ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
608 uint32_t dst_vnn
, uint64_t dst_srvid
,
609 const struct iovec
*iov
, int iovlen
)
611 struct ctdb_req_message r
;
612 struct iovec iov2
[iovlen
+1];
613 size_t buflen
= iov_buflen(iov
, iovlen
);
616 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + buflen
;
617 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
618 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
619 r
.hdr
.generation
= 1;
620 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
621 r
.hdr
.destnode
= dst_vnn
;
622 r
.hdr
.srcnode
= conn
->our_vnn
;
627 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
628 ctdb_packet_dump(&r
.hdr
);
630 iov2
[0].iov_base
= &r
;
631 iov2
[0].iov_len
= offsetof(struct ctdb_req_message
, data
);
632 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
634 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
635 if (nwritten
== -1) {
636 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
637 cluster_fatal("cluster dispatch daemon msg write error\n");
644 * send/recv a generic ctdb control message
646 static int ctdbd_control_unix(struct ctdbd_connection
*conn
,
647 uint32_t vnn
, uint32_t opcode
,
648 uint64_t srvid
, uint32_t flags
,
650 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
653 struct ctdb_req_control req
;
654 struct ctdb_req_header
*hdr
;
655 struct ctdb_reply_control
*reply
= NULL
;
661 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
662 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
663 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
664 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
665 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
666 req
.hdr
.destnode
= vnn
;
669 req
.datalen
= data
.dsize
;
672 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
673 ctdb_packet_dump(&req
.hdr
);
675 iov
[0].iov_base
= &req
;
676 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
677 iov
[1].iov_base
= data
.dptr
;
678 iov
[1].iov_len
= data
.dsize
;
680 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
681 if (nwritten
== -1) {
682 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
683 cluster_fatal("cluster dispatch daemon msg write error\n");
686 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
693 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
695 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
699 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
700 DEBUG(0, ("received invalid reply\n"));
704 reply
= (struct ctdb_reply_control
*)hdr
;
707 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
708 mem_ctx
, reply
->data
, reply
->datalen
))) {
712 outdata
->dsize
= reply
->datalen
;
715 (*cstatus
) = reply
->status
;
722 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
723 uint32_t vnn
, uint32_t opcode
,
724 uint64_t srvid
, uint32_t flags
,
726 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
731 ret
= ctdbd_control_unix(conn
, vnn
, opcode
, srvid
, flags
, data
,
732 mem_ctx
, outdata
, cstatus
);
734 return map_nt_error_from_unix(ret
);
740 * see if a remote process exists
742 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
750 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
751 DEBUG(10, ("ctdb_processes_exist failed\n"));
757 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
758 const struct server_id
*pids
, int num_pids
,
761 TALLOC_CTX
*frame
= talloc_stackframe();
766 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
767 if (reqids
== NULL
) {
771 for (i
=0; i
<num_pids
; i
++) {
772 struct ctdb_req_control req
;
778 reqids
[i
] = ctdbd_next_reqid(conn
);
783 * pids[i].pid is uint64_t, scale down to pid_t which
784 * is the wire protocol towards ctdb.
788 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
789 (int)pids
[i
].vnn
, (int)pid
,
792 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
793 req
.hdr
.length
+= sizeof(pid
);
794 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
795 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
796 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
797 req
.hdr
.reqid
= reqids
[i
];
798 req
.hdr
.destnode
= pids
[i
].vnn
;
799 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
801 req
.datalen
= sizeof(pid
);
804 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
805 ctdb_packet_dump(&req
.hdr
);
807 iov
[0].iov_base
= &req
;
808 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
809 iov
[1].iov_base
= &pid
;
810 iov
[1].iov_len
= sizeof(pid
);
812 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
813 if (nwritten
== -1) {
814 DEBUG(10, ("write_data_iov failed: %s\n",
822 while (num_received
< num_pids
) {
823 struct ctdb_req_header
*hdr
;
824 struct ctdb_reply_control
*reply
;
828 ret
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
830 DEBUG(10, ("ctdb_read_req failed: %s\n",
835 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
836 DEBUG(10, ("Received invalid reply\n"));
839 reply
= (struct ctdb_reply_control
*)hdr
;
841 reqid
= reply
->hdr
.reqid
;
843 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
845 for (i
=0; i
<num_pids
; i
++) {
846 if (reqid
== reqids
[i
]) {
851 DEBUG(10, ("Received unknown record number %u\n",
855 results
[i
] = ((reply
->status
) == 0);
869 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
870 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
874 TDB_DATA rdata
= {0};
877 data
.dptr
= (uint8_t*)&db_id
;
878 data
.dsize
= sizeof(db_id
);
880 ret
= ctdbd_control_unix(conn
, CTDB_CURRENT_NODE
,
881 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
882 mem_ctx
, &rdata
, &cstatus
);
883 if ((ret
!= 0) || cstatus
!= 0) {
884 DEBUG(0, (__location__
" ctdb_control for getdbpath failed: %s\n",
889 return (char *)rdata
.dptr
;
893 * attach to a ctdb database
895 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
896 const char *name
, uint32_t *db_id
, int tdb_flags
)
901 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
903 data
= string_term_tdb_data(name
);
905 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
907 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
908 : CTDB_CONTROL_DB_ATTACH
,
909 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
910 if (!NT_STATUS_IS_OK(status
)) {
911 DEBUG(0, (__location__
" ctdb_control for db_attach "
912 "failed: %s\n", nt_errstr(status
)));
916 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
917 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
918 return NT_STATUS_INTERNAL_ERROR
;
921 *db_id
= *(uint32_t *)data
.dptr
;
922 talloc_free(data
.dptr
);
924 if (!(tdb_flags
& TDB_SEQNUM
)) {
928 data
.dptr
= (uint8_t *)db_id
;
929 data
.dsize
= sizeof(*db_id
);
931 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
932 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
933 NULL
, NULL
, &cstatus
);
934 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
935 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
937 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
945 * force the migration of a record to this node
947 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
950 struct ctdb_req_call req
;
951 struct ctdb_req_header
*hdr
;
959 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
960 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
961 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
962 req
.hdr
.operation
= CTDB_REQ_CALL
;
963 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
964 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
965 req
.callid
= CTDB_NULL_FUNC
;
967 req
.keylen
= key
.dsize
;
969 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
970 ctdb_packet_dump(&req
.hdr
);
972 iov
[0].iov_base
= &req
;
973 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
974 iov
[1].iov_base
= key
.dptr
;
975 iov
[1].iov_len
= key
.dsize
;
977 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
978 if (nwritten
== -1) {
979 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
980 cluster_fatal("cluster dispatch daemon msg write error\n");
983 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
985 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
986 status
= map_nt_error_from_unix(ret
);
990 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
991 DEBUG(0, ("received invalid reply\n"));
992 status
= NT_STATUS_INTERNAL_ERROR
;
996 status
= NT_STATUS_OK
;
1004 * Fetch a record and parse it
1006 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1007 TDB_DATA key
, bool local_copy
,
1008 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1009 void *private_data
),
1012 struct ctdb_req_call req
;
1013 struct ctdb_req_header
*hdr
= NULL
;
1014 struct ctdb_reply_call
*reply
;
1015 struct iovec iov
[2];
1021 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1025 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1026 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1027 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1028 req
.hdr
.operation
= CTDB_REQ_CALL
;
1029 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1031 req
.callid
= CTDB_FETCH_FUNC
;
1033 req
.keylen
= key
.dsize
;
1035 iov
[0].iov_base
= &req
;
1036 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1037 iov
[1].iov_base
= key
.dptr
;
1038 iov
[1].iov_len
= key
.dsize
;
1040 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1041 if (nwritten
== -1) {
1042 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1043 cluster_fatal("cluster dispatch daemon msg write error\n");
1046 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1048 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
1049 status
= map_nt_error_from_unix(ret
);
1053 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
1054 DEBUG(0, ("received invalid reply\n"));
1055 status
= NT_STATUS_INTERNAL_ERROR
;
1058 reply
= (struct ctdb_reply_call
*)hdr
;
1060 if (reply
->datalen
== 0) {
1062 * Treat an empty record as non-existing
1064 status
= NT_STATUS_NOT_FOUND
;
1068 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1071 status
= NT_STATUS_OK
;
1078 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1079 connection to ctdbd to avoid the hairy recursive and async problems with
1083 NTSTATUS
ctdbd_traverse(struct ctdbd_connection
*master
, uint32_t db_id
,
1084 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1085 void *private_data
),
1088 struct ctdbd_connection
*conn
;
1092 struct ctdb_traverse_start t
;
1096 status
= ctdbd_init_connection(NULL
, master
->sockname
, master
->timeout
,
1099 if (!NT_STATUS_IS_OK(status
)) {
1100 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1101 nt_errstr(status
)));
1106 t
.srvid
= conn
->rand_srvid
;
1107 t
.reqid
= ctdbd_next_reqid(conn
);
1109 data
.dptr
= (uint8_t *)&t
;
1110 data
.dsize
= sizeof(t
);
1112 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1113 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1114 data
, NULL
, NULL
, &cstatus
);
1116 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1118 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1121 if (NT_STATUS_IS_OK(status
)) {
1123 * We need a mapping here
1125 status
= NT_STATUS_UNSUCCESSFUL
;
1132 struct ctdb_req_header
*hdr
= NULL
;
1133 struct ctdb_req_message
*m
;
1134 struct ctdb_rec_data
*d
;
1137 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, conn
, &hdr
);
1139 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1141 cluster_fatal("ctdbd died\n");
1144 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
1145 DEBUG(0, ("Got operation %u, expected a message\n",
1146 (unsigned)hdr
->operation
));
1148 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1151 m
= (struct ctdb_req_message
*)hdr
;
1152 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1153 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1154 DEBUG(0, ("Got invalid traverse data of length %d\n",
1157 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1160 key
.dsize
= d
->keylen
;
1161 key
.dptr
= &d
->data
[0];
1162 data
.dsize
= d
->datalen
;
1163 data
.dptr
= &d
->data
[d
->keylen
];
1165 if (key
.dsize
== 0 && data
.dsize
== 0) {
1166 /* end of traverse */
1168 return NT_STATUS_OK
;
1171 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1172 DEBUG(0, ("Got invalid ltdb header length %d\n",
1175 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1177 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1178 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1181 fn(key
, data
, private_data
);
1184 return NT_STATUS_OK
;
1188 This is used to canonicalize a ctdb_sock_addr structure.
1190 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1191 struct sockaddr_storage
*out
)
1193 memcpy(out
, in
, sizeof (*out
));
1196 if (in
->ss_family
== AF_INET6
) {
1197 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1198 const struct sockaddr_in6
*in6
=
1199 (const struct sockaddr_in6
*)in
;
1200 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1201 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1202 memset(out
, 0, sizeof(*out
));
1203 #ifdef HAVE_SOCK_SIN_LEN
1204 out4
->sin_len
= sizeof(*out
);
1206 out4
->sin_family
= AF_INET
;
1207 out4
->sin_port
= in6
->sin6_port
;
1208 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1215 * Register us as a server for a particular tcp connection
1218 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1219 const struct sockaddr_storage
*_server
,
1220 const struct sockaddr_storage
*_client
,
1221 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
1223 const uint8_t *msg
, size_t msglen
,
1224 void *private_data
),
1227 struct ctdb_control_tcp_addr p
;
1228 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1230 struct sockaddr_storage client
;
1231 struct sockaddr_storage server
;
1234 * Only one connection so far
1237 smbd_ctdb_canonicalize_ip(_client
, &client
);
1238 smbd_ctdb_canonicalize_ip(_server
, &server
);
1240 switch (client
.ss_family
) {
1242 memcpy(&p
.dest
.ip
, &server
, sizeof(p
.dest
.ip
));
1243 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1246 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1247 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1250 return NT_STATUS_INTERNAL_ERROR
;
1254 * We want to be told about IP releases
1257 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1259 if (!NT_STATUS_IS_OK(status
)) {
1264 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1265 * can send an extra ack to trigger a reset for our client, so it
1266 * immediately reconnects
1268 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1269 CTDB_CONTROL_TCP_CLIENT
, 0,
1270 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1274 call a control on the local node
1276 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1277 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1278 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1281 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1284 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1286 struct ctdb_client_notify_register reg_data
;
1291 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1293 reg_data
.notify_data
[0] = 0;
1295 struct_len
= offsetof(struct ctdb_client_notify_register
,
1296 notify_data
) + reg_data
.len
;
1298 status
= ctdbd_control_local(
1299 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1300 make_tdb_data((uint8_t *)®_data
, struct_len
),
1301 NULL
, NULL
, &cstatus
);
1302 if (!NT_STATUS_IS_OK(status
)) {
1303 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1304 nt_errstr(status
)));
1309 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1311 struct ctdb_client_notify_deregister dereg_data
;
1315 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1317 status
= ctdbd_control_local(
1318 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1319 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1320 NULL
, NULL
, &cstatus
);
1321 if (!NT_STATUS_IS_OK(status
)) {
1322 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1323 nt_errstr(status
)));
1328 NTSTATUS
ctdbd_probe(const char *sockname
, int timeout
)
1331 * Do a very early check if ctdbd is around to avoid an abort and core
1334 struct ctdbd_connection
*conn
= NULL
;
1337 status
= ctdbd_messaging_connection(talloc_tos(), sockname
, timeout
,
1341 * We only care if we can connect.