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/>.
25 #include "ctdbd_conn.h"
26 #include "system/select.h"
27 #include "lib/util/util_net.h"
28 #include "lib/util/sys_rw_data.h"
29 #include "lib/util/iov_buf.h"
30 #include "lib/util/select.h"
31 #include "lib/util/debug.h"
32 #include "lib/util/talloc_stack.h"
33 #include "lib/util/genrand.h"
34 #include "lib/util/fault.h"
35 #include "lib/util/dlinklist.h"
36 #include "lib/util/tevent_unix.h"
37 #include "lib/util/sys_rw.h"
38 #include "lib/util/blocking.h"
39 #include "ctdb/include/ctdb_protocol.h"
40 #include "lib/async_req/async_sock.h"
41 #include "lib/dbwrap/dbwrap.h"
42 #include "lib/dbwrap/dbwrap_rbt.h"
44 /* paths to these include files come from --with-ctdb= in configure */
46 struct ctdbd_srvid_cb
{
48 int (*cb
)(struct tevent_context
*ev
,
49 uint32_t src_vnn
, uint32_t dst_vnn
,
51 const uint8_t *msg
, size_t msglen
,
56 struct ctdbd_connection
{
60 struct ctdbd_srvid_cb
*callbacks
;
65 * Outgoing queue for writev_send of asynchronous ctdb requests
67 struct tevent_queue
*outgoing
;
68 struct tevent_req
**pending
;
69 struct tevent_req
*read_req
;
72 static bool ctdbd_conn_has_async_reqs(struct ctdbd_connection
*conn
)
74 size_t len
= talloc_array_length(conn
->pending
);
78 static uint32_t ctdbd_next_reqid(struct ctdbd_connection
*conn
)
81 if (conn
->reqid
== 0) {
87 static int ctdbd_control(struct ctdbd_connection
*conn
,
88 uint32_t vnn
, uint32_t opcode
,
89 uint64_t srvid
, uint32_t flags
,
91 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
95 * exit on fatal communications errors with the ctdbd daemon
97 static void cluster_fatal(const char *why
)
99 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
100 /* we don't use smb_panic() as we don't want to delay to write
101 a core file. We need to release this process id immediately
102 so that someone else can take over without getting sharing
110 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
112 if (DEBUGLEVEL
< 11) {
115 DEBUGADD(11, ("len=%"PRIu32
", magic=%"PRIu32
", vers=%"PRIu32
", "
116 "gen=%"PRIu32
", op=%"PRIu32
", reqid=%"PRIu32
"\n",
126 * Register a srvid with ctdbd
128 int register_with_ctdbd(struct ctdbd_connection
*conn
, uint64_t srvid
,
129 int (*cb
)(struct tevent_context
*ev
,
130 uint32_t src_vnn
, uint32_t dst_vnn
,
132 const uint8_t *msg
, size_t msglen
,
136 size_t num_callbacks
= talloc_array_length(conn
->callbacks
);
137 struct ctdbd_srvid_cb
*tmp
;
138 bool need_register
= true;
141 for (i
= 0; i
< num_callbacks
; i
++) {
142 struct ctdbd_srvid_cb
*c
= &conn
->callbacks
[i
];
144 if (c
->srvid
== srvid
) {
145 need_register
= false;
154 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_REGISTER_SRVID
,
155 srvid
, 0, tdb_null
, NULL
, NULL
,
163 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
168 conn
->callbacks
= tmp
;
170 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
171 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
177 void deregister_from_ctdbd(struct ctdbd_connection
*conn
,
179 int (*cb
)(struct tevent_context
*ev
,
188 struct ctdbd_srvid_cb
*cbs
= conn
->callbacks
;
189 size_t i
, num_callbacks
= talloc_array_length(cbs
);
190 bool need_deregister
= false;
191 bool keep_registration
= false;
193 if (num_callbacks
== 0) {
197 for (i
= 0; i
< num_callbacks
;) {
198 struct ctdbd_srvid_cb
*c
= &cbs
[i
];
200 if (c
->srvid
!= srvid
) {
205 if ((c
->cb
== cb
) && (c
->private_data
== private_data
)) {
206 need_deregister
= true;
207 ARRAY_DEL_ELEMENT(cbs
, i
, num_callbacks
);
212 keep_registration
= true;
216 conn
->callbacks
= talloc_realloc(conn
,
218 struct ctdbd_srvid_cb
,
221 if (keep_registration
) {
222 need_deregister
= false;
225 if (need_deregister
) {
229 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_DEREGISTER_SRVID
,
230 srvid
, 0, tdb_null
, NULL
, NULL
,
234 * If CTDB_CONTROL_DEREGISTER_SRVID fails we may still
235 * get messages later, but we don't have a callback
236 * anymore, we just ignore these.
244 static int ctdbd_msg_call_back(struct tevent_context
*ev
,
245 struct ctdbd_connection
*conn
,
246 struct ctdb_req_message_old
*msg
)
249 size_t i
, num_callbacks
;
251 msg_len
= msg
->hdr
.length
;
252 if (msg_len
< offsetof(struct ctdb_req_message_old
, data
)) {
253 DBG_DEBUG("len %"PRIu32
" too small\n", msg_len
);
256 msg_len
-= offsetof(struct ctdb_req_message_old
, data
);
258 if (msg_len
< msg
->datalen
) {
259 DBG_DEBUG("msg_len=%"PRIu32
" < msg->datalen=%"PRIu32
"\n",
260 msg_len
, msg
->datalen
);
264 num_callbacks
= talloc_array_length(conn
->callbacks
);
266 for (i
=0; i
<num_callbacks
; i
++) {
267 struct ctdbd_srvid_cb
*cb
= &conn
->callbacks
[i
];
269 if ((cb
->srvid
== msg
->srvid
) && (cb
->cb
!= NULL
)) {
273 msg
->hdr
.srcnode
, msg
->hdr
.destnode
,
274 msg
->srvid
, msg
->data
, msg
->datalen
,
285 * get our vnn from the cluster
287 static int get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
291 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GET_PNN
, 0, 0,
292 tdb_null
, NULL
, NULL
, &cstatus
);
294 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret
)));
297 *vnn
= (uint32_t)cstatus
;
301 static int ctdbd_control_get_nodemap(struct ctdbd_connection
*conn
,
303 struct ctdb_node_map_old
**_nodemap
)
306 TDB_DATA outdata
= {0};
309 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GET_NODEMAP
, 0, 0,
310 tdb_null
, mem_ctx
, &outdata
, &cstatus
);
312 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret
)));
315 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
316 DEBUG(2, ("Received invalid ctdb data\n"));
320 *_nodemap
= (struct ctdb_node_map_old
*)outdata
.dptr
;
325 * Are we active (i.e. not banned or stopped?)
327 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
329 struct ctdb_node_map_old
*m
= NULL
;
334 ret
= ctdbd_control_get_nodemap(conn
, talloc_tos(), &m
);
336 DEBUG(1, ("ctdbd_control_get_nodemap() failed: %s\n", strerror(ret
)));
340 for (i
=0; i
<m
->num
; i
++) {
341 if (vnn
== m
->nodes
[i
].pnn
) {
347 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
352 if ((m
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) != 0) {
353 DEBUG(2, ("Node has status %x, not active\n",
354 (int)m
->nodes
[i
].flags
));
364 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
366 return conn
->our_vnn
;
370 * Get us a ctdb connection
373 static int ctdbd_connect(const char *sockname
, int *pfd
)
375 struct samba_sockaddr addr
= {
376 .sa_socklen
= sizeof(struct sockaddr_un
),
379 .sun_family
= AF_UNIX
,
387 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
390 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
394 namelen
= strlcpy(addr
.u
.un
.sun_path
,
396 sizeof(addr
.u
.un
.sun_path
));
397 if (namelen
>= sizeof(addr
.u
.un
.sun_path
)) {
398 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
404 ret
= connect(fd
, &addr
.u
.sa
, addr
.sa_socklen
);
407 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
417 static int ctdb_read_packet(int fd
, int timeout
, TALLOC_CTX
*mem_ctx
,
418 struct ctdb_req_header
**result
)
420 struct ctdb_req_header
*req
;
425 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
428 ret
= sys_poll_intr(&pfd
, 1, timeout
);
440 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
448 if (msglen
< sizeof(struct ctdb_req_header
)) {
452 req
= talloc_size(mem_ctx
, msglen
);
456 talloc_set_name_const(req
, "struct ctdb_req_header");
458 req
->length
= msglen
;
460 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
461 msglen
- sizeof(msglen
));
476 * Read a full ctdbd request. If we have a messaging context, defer incoming
477 * messages that might come in between.
480 static int ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
481 TALLOC_CTX
*mem_ctx
, struct ctdb_req_header
**result
)
483 struct ctdb_req_header
*hdr
= NULL
;
488 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, mem_ctx
, &hdr
);
490 DBG_ERR("ctdb_read_packet failed: %s\n", strerror(ret
));
491 cluster_fatal("failed to read data from ctdbd\n");
494 SMB_ASSERT(hdr
!= NULL
);
496 DEBUG(11, ("Received ctdb packet\n"));
497 ctdb_packet_dump(hdr
);
499 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
500 struct ctdb_req_message_old
*msg
= (struct ctdb_req_message_old
*)hdr
;
502 ret
= ctdbd_msg_call_back(NULL
, conn
, msg
);
512 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
513 /* we got the wrong reply */
514 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
515 "been %u\n", hdr
->reqid
, reqid
));
520 *result
= talloc_move(mem_ctx
, &hdr
);
525 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
);
528 * Get us a ctdbd connection
531 static int ctdbd_init_connection_internal(TALLOC_CTX
*mem_ctx
,
532 const char *sockname
, int timeout
,
533 struct ctdbd_connection
*conn
)
537 conn
->timeout
= timeout
;
538 if (conn
->timeout
== 0) {
542 ret
= ctdbd_connect(sockname
, &conn
->fd
);
544 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret
)));
547 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
549 ret
= get_cluster_vnn(conn
, &conn
->our_vnn
);
551 DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret
)));
555 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
556 DEBUG(2, ("Node is not working, can not connect\n"));
560 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
561 sizeof(conn
->rand_srvid
));
563 ret
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
565 DEBUG(5, ("Could not register random srvid: %s\n",
573 int ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
574 const char *sockname
, int timeout
,
575 struct ctdbd_connection
**pconn
)
577 struct ctdbd_connection
*conn
;
580 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
581 DEBUG(0, ("talloc failed\n"));
585 ret
= ctdbd_init_connection_internal(mem_ctx
,
590 DBG_ERR("ctdbd_init_connection_internal failed (%s)\n",
603 int ctdbd_reinit_connection(TALLOC_CTX
*mem_ctx
,
604 const char *sockname
, int timeout
,
605 struct ctdbd_connection
*conn
)
609 ret
= ctdbd_connection_destructor(conn
);
611 DBG_ERR("ctdbd_connection_destructor failed\n");
615 ret
= ctdbd_init_connection_internal(mem_ctx
,
620 DBG_ERR("ctdbd_init_connection_internal failed (%s)\n",
628 int ctdbd_init_async_connection(
630 const char *sockname
,
632 struct ctdbd_connection
**pconn
)
634 struct ctdbd_connection
*conn
= NULL
;
639 ret
= ctdbd_init_connection(mem_ctx
, sockname
, timeout
, &conn
);
644 ret
= set_blocking(conn
->fd
, false);
647 SMB_ASSERT(err
!= 0);
652 conn
->outgoing
= tevent_queue_create(conn
, "ctdb async outgoing");
653 if (conn
->outgoing
== NULL
) {
662 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
668 * Packet handler to receive and handle a ctdb message
670 static int ctdb_handle_message(struct tevent_context
*ev
,
671 struct ctdbd_connection
*conn
,
672 struct ctdb_req_header
*hdr
)
674 struct ctdb_req_message_old
*msg
;
676 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
677 DEBUG(0, ("Received async msg of type %u, discarding\n",
682 msg
= (struct ctdb_req_message_old
*)hdr
;
684 ctdbd_msg_call_back(ev
, conn
, msg
);
689 void ctdbd_socket_readable(struct tevent_context
*ev
,
690 struct ctdbd_connection
*conn
)
692 struct ctdb_req_header
*hdr
= NULL
;
695 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, talloc_tos(), &hdr
);
697 DBG_ERR("ctdb_read_packet failed: %s\n", strerror(ret
));
698 cluster_fatal("failed to read data from ctdbd\n");
700 SMB_ASSERT(hdr
!= NULL
);
702 ret
= ctdb_handle_message(ev
, conn
, hdr
);
707 DEBUG(10, ("could not handle incoming message: %s\n",
712 int ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
713 uint32_t dst_vnn
, uint64_t dst_srvid
,
714 const struct iovec
*iov
, int iovlen
)
716 struct ctdb_req_message_old r
;
717 struct iovec iov2
[iovlen
+1];
718 size_t buflen
= iov_buflen(iov
, iovlen
);
721 r
.hdr
.length
= offsetof(struct ctdb_req_message_old
, data
) + buflen
;
722 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
723 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
724 r
.hdr
.generation
= 1;
725 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
726 r
.hdr
.destnode
= dst_vnn
;
727 r
.hdr
.srcnode
= conn
->our_vnn
;
732 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
733 ctdb_packet_dump(&r
.hdr
);
735 iov2
[0].iov_base
= &r
;
736 iov2
[0].iov_len
= offsetof(struct ctdb_req_message_old
, data
);
737 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
739 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
740 if (nwritten
== -1) {
741 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
742 cluster_fatal("cluster dispatch daemon msg write error\n");
749 * send/recv a generic ctdb control message
751 static int ctdbd_control(struct ctdbd_connection
*conn
,
752 uint32_t vnn
, uint32_t opcode
,
753 uint64_t srvid
, uint32_t flags
,
755 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
758 struct ctdb_req_control_old req
;
759 struct ctdb_req_header
*hdr
;
760 struct ctdb_reply_control_old
*reply
= NULL
;
765 if (ctdbd_conn_has_async_reqs(conn
)) {
767 * Can't use sync call while an async call is in flight. Adding
768 * this check as a safety net. We'll be using different
769 * connections for sync and async requests, so this shouldn't
770 * happen, but who knows...
772 DBG_ERR("Async ctdb req on sync connection\n");
777 req
.hdr
.length
= offsetof(struct ctdb_req_control_old
, data
) + data
.dsize
;
778 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
779 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
780 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
781 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
782 req
.hdr
.destnode
= vnn
;
785 req
.datalen
= data
.dsize
;
788 DBG_DEBUG("Sending ctdb packet reqid=%"PRIu32
", vnn=%"PRIu32
", "
789 "opcode=%"PRIu32
", srvid=%"PRIu64
"\n", req
.hdr
.reqid
,
790 req
.hdr
.destnode
, req
.opcode
, req
.srvid
);
791 ctdb_packet_dump(&req
.hdr
);
793 iov
[0].iov_base
= &req
;
794 iov
[0].iov_len
= offsetof(struct ctdb_req_control_old
, data
);
795 iov
[1].iov_base
= data
.dptr
;
796 iov
[1].iov_len
= data
.dsize
;
798 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
799 if (nwritten
== -1) {
800 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
801 cluster_fatal("cluster dispatch daemon msg write error\n");
804 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
811 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
813 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
817 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
818 DEBUG(0, ("received invalid reply\n"));
822 reply
= (struct ctdb_reply_control_old
*)hdr
;
825 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
826 mem_ctx
, reply
->data
, reply
->datalen
))) {
830 outdata
->dsize
= reply
->datalen
;
833 (*cstatus
) = reply
->status
;
841 * see if a remote process exists
843 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
,
844 pid_t pid
, uint64_t unique_id
)
846 uint8_t buf
[sizeof(pid
)+sizeof(unique_id
)];
850 if (unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
851 ret
= ctdbd_control(conn
, vnn
, CTDB_CONTROL_PROCESS_EXISTS
,
853 (TDB_DATA
) { .dptr
= (uint8_t *)&pid
,
854 .dsize
= sizeof(pid
) },
855 NULL
, NULL
, &cstatus
);
859 return (cstatus
== 0);
862 memcpy(buf
, &pid
, sizeof(pid
));
863 memcpy(buf
+sizeof(pid
), &unique_id
, sizeof(unique_id
));
865 ret
= ctdbd_control(conn
, vnn
, CTDB_CONTROL_CHECK_PID_SRVID
, 0, 0,
866 (TDB_DATA
) { .dptr
= buf
, .dsize
= sizeof(buf
) },
867 NULL
, NULL
, &cstatus
);
871 return (cstatus
== 0);
877 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
878 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
882 TDB_DATA rdata
= {0};
885 data
.dptr
= (uint8_t*)&db_id
;
886 data
.dsize
= sizeof(db_id
);
888 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
889 mem_ctx
, &rdata
, &cstatus
);
890 if ((ret
!= 0) || cstatus
!= 0) {
891 DEBUG(0, (__location__
" ctdb_control for getdbpath failed: %s\n",
893 TALLOC_FREE(rdata
.dptr
);
896 return (char *)rdata
.dptr
;
900 * attach to a ctdb database
902 int ctdbd_db_attach(struct ctdbd_connection
*conn
,
903 const char *name
, uint32_t *db_id
, bool persistent
)
909 data
= string_term_tdb_data(name
);
911 ret
= ctdbd_control_local(conn
,
913 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
914 : CTDB_CONTROL_DB_ATTACH
,
915 0, 0, data
, NULL
, &data
, &cstatus
);
917 DEBUG(0, (__location__
" ctdb_control for db_attach "
918 "failed: %s\n", strerror(ret
)));
922 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
923 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
924 TALLOC_FREE(data
.dptr
);
928 *db_id
= *(uint32_t *)data
.dptr
;
929 talloc_free(data
.dptr
);
935 * force the migration of a record to this node
937 int ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
, TDB_DATA key
)
939 struct ctdb_req_call_old req
;
940 struct ctdb_req_header
*hdr
= NULL
;
945 if (ctdbd_conn_has_async_reqs(conn
)) {
947 * Can't use sync call while an async call is in flight. Adding
948 * this check as a safety net. We'll be using different
949 * connections for sync and async requests, so this shouldn't
950 * happen, but who knows...
952 DBG_ERR("Async ctdb req on sync connection\n");
958 req
.hdr
.length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
959 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
960 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
961 req
.hdr
.operation
= CTDB_REQ_CALL
;
962 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
963 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
964 req
.callid
= CTDB_NULL_FUNC
;
966 req
.keylen
= key
.dsize
;
968 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
969 ctdb_packet_dump(&req
.hdr
);
971 iov
[0].iov_base
= &req
;
972 iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
973 iov
[1].iov_base
= key
.dptr
;
974 iov
[1].iov_len
= key
.dsize
;
976 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
977 if (nwritten
== -1) {
978 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
979 cluster_fatal("cluster dispatch daemon msg write error\n");
982 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
984 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
988 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
989 if (hdr
->operation
== CTDB_REPLY_ERROR
) {
990 DBG_ERR("received error from ctdb\n");
992 DBG_ERR("received invalid reply\n");
1005 * Fetch a record and parse it
1007 int ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1008 TDB_DATA key
, bool local_copy
,
1009 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1010 void *private_data
),
1013 struct ctdb_req_call_old req
;
1014 struct ctdb_req_header
*hdr
= NULL
;
1015 struct ctdb_reply_call_old
*reply
;
1016 struct iovec iov
[2];
1021 if (ctdbd_conn_has_async_reqs(conn
)) {
1023 * Can't use sync call while an async call is in flight. Adding
1024 * this check as a safety net. We'll be using different
1025 * connections for sync and async requests, so this shouldn't
1026 * happen, but who knows...
1028 DBG_ERR("Async ctdb req on sync connection\n");
1032 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1036 req
.hdr
.length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
1037 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1038 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1039 req
.hdr
.operation
= CTDB_REQ_CALL
;
1040 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1042 req
.callid
= CTDB_FETCH_FUNC
;
1044 req
.keylen
= key
.dsize
;
1046 iov
[0].iov_base
= &req
;
1047 iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
1048 iov
[1].iov_base
= key
.dptr
;
1049 iov
[1].iov_len
= key
.dsize
;
1051 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1052 if (nwritten
== -1) {
1053 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1054 cluster_fatal("cluster dispatch daemon msg write error\n");
1057 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1059 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
1063 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
1064 DEBUG(0, ("received invalid reply\n"));
1068 reply
= (struct ctdb_reply_call_old
*)hdr
;
1070 if (reply
->datalen
== 0) {
1072 * Treat an empty record as non-existing
1078 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1088 Traverse a ctdb database. "conn" must be an otherwise unused
1089 ctdb_connection where no other messages but the traverse ones are
1093 int ctdbd_traverse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1094 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1095 void *private_data
),
1100 struct ctdb_traverse_start t
;
1101 int32_t cstatus
= 0;
1103 if (ctdbd_conn_has_async_reqs(conn
)) {
1105 * Can't use sync call while an async call is in flight. Adding
1106 * this check as a safety net. We'll be using different
1107 * connections for sync and async requests, so this shouldn't
1108 * happen, but who knows...
1110 DBG_ERR("Async ctdb req on sync connection\n");
1115 t
.srvid
= conn
->rand_srvid
;
1116 t
.reqid
= ctdbd_next_reqid(conn
);
1118 data
.dptr
= (uint8_t *)&t
;
1119 data
.dsize
= sizeof(t
);
1121 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_TRAVERSE_START
,
1123 0, data
, NULL
, NULL
, &cstatus
);
1125 if ((ret
!= 0) || (cstatus
!= 0)) {
1126 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret
),
1131 * We need a mapping here
1139 struct ctdb_req_header
*hdr
= NULL
;
1140 struct ctdb_req_message_old
*m
;
1141 struct ctdb_rec_data_old
*d
;
1143 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, conn
, &hdr
);
1145 DBG_ERR("ctdb_read_packet failed: %s\n", strerror(ret
));
1146 cluster_fatal("failed to read data from ctdbd\n");
1148 SMB_ASSERT(hdr
!= NULL
);
1150 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
1151 DEBUG(0, ("Got operation %u, expected a message\n",
1152 (unsigned)hdr
->operation
));
1156 m
= (struct ctdb_req_message_old
*)hdr
;
1157 d
= (struct ctdb_rec_data_old
*)&m
->data
[0];
1158 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1159 DEBUG(0, ("Got invalid traverse data of length %d\n",
1164 key
.dsize
= d
->keylen
;
1165 key
.dptr
= &d
->data
[0];
1166 data
.dsize
= d
->datalen
;
1167 data
.dptr
= &d
->data
[d
->keylen
];
1169 if (key
.dsize
== 0 && data
.dsize
== 0) {
1170 /* end of traverse */
1174 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1175 DEBUG(0, ("Got invalid ltdb header length %d\n",
1179 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1180 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1183 fn(key
, data
, private_data
);
1190 This is used to canonicalize a ctdb_sock_addr structure.
1192 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1193 struct sockaddr_storage
*out
)
1195 memcpy(out
, in
, sizeof (*out
));
1198 if (in
->ss_family
== AF_INET6
) {
1199 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1200 const struct sockaddr_in6
*in6
=
1201 (const struct sockaddr_in6
*)in
;
1202 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1203 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1204 memset(out
, 0, sizeof(*out
));
1205 #ifdef HAVE_SOCK_SIN_LEN
1206 out4
->sin_len
= sizeof(*out
);
1208 out4
->sin_family
= AF_INET
;
1209 out4
->sin_port
= in6
->sin6_port
;
1210 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1217 * Register us as a server for a particular tcp connection
1220 int ctdbd_register_ips(struct ctdbd_connection
*conn
,
1221 const struct sockaddr_storage
*_server
,
1222 const struct sockaddr_storage
*_client
,
1223 int (*cb
)(struct tevent_context
*ev
,
1224 uint32_t src_vnn
, uint32_t dst_vnn
,
1226 const uint8_t *msg
, size_t msglen
,
1227 void *private_data
),
1230 struct ctdb_connection p
;
1231 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1233 struct sockaddr_storage client
;
1234 struct sockaddr_storage server
;
1237 * Only one connection so far
1240 smbd_ctdb_canonicalize_ip(_client
, &client
);
1241 smbd_ctdb_canonicalize_ip(_server
, &server
);
1244 switch (client
.ss_family
) {
1246 memcpy(&p
.dst
.ip
, &server
, sizeof(p
.dst
.ip
));
1247 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1250 memcpy(&p
.dst
.ip6
, &server
, sizeof(p
.dst
.ip6
));
1251 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1258 * We want to be told about IP releases
1261 ret
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1268 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1269 * can send an extra ack to trigger a reset for our client, so it
1270 * immediately reconnects
1272 ret
= ctdbd_control_local(conn
,
1273 CTDB_CONTROL_TCP_CLIENT
, 0,
1274 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
,
1282 void ctdbd_unregister_ips(struct ctdbd_connection
*conn
,
1283 const struct sockaddr_storage
*_server
,
1284 const struct sockaddr_storage
*_client
,
1285 int (*cb
)(struct tevent_context
*ev
,
1291 void *private_data
),
1294 struct ctdb_connection p
= {};
1295 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1297 struct sockaddr_storage client
;
1298 struct sockaddr_storage server
;
1301 * Only one connection so far
1304 smbd_ctdb_canonicalize_ip(_client
, &client
);
1305 smbd_ctdb_canonicalize_ip(_server
, &server
);
1307 switch (client
.ss_family
) {
1309 memcpy(&p
.dst
.ip
, &server
, sizeof(p
.dst
.ip
));
1310 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1313 memcpy(&p
.dst
.ip6
, &server
, sizeof(p
.dst
.ip6
));
1314 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1321 * We no longer want to be told about IP releases
1322 * for the given callback/private_data combination
1324 deregister_from_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1328 * inform ctdb of our tcp connection is no longer active
1330 ret
= ctdbd_control_local(conn
,
1331 CTDB_CONTROL_TCP_CLIENT_DISCONNECTED
, 0,
1332 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
,
1336 * We ignore errors here, as we'll just
1337 * no longer have a callback handler
1338 * registered and messages may just be ignored
1345 void ctdbd_passed_ips(struct ctdbd_connection
*conn
,
1346 const struct sockaddr_storage
*_server
,
1347 const struct sockaddr_storage
*_client
,
1348 int (*cb
)(struct tevent_context
*ev
,
1354 void *private_data
),
1357 struct ctdb_connection p
;
1358 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1360 struct sockaddr_storage client
;
1361 struct sockaddr_storage server
;
1364 * Only one connection so far
1367 smbd_ctdb_canonicalize_ip(_client
, &client
);
1368 smbd_ctdb_canonicalize_ip(_server
, &server
);
1371 switch (client
.ss_family
) {
1373 memcpy(&p
.dst
.ip
, &server
, sizeof(p
.dst
.ip
));
1374 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1377 memcpy(&p
.dst
.ip6
, &server
, sizeof(p
.dst
.ip6
));
1378 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1385 * We no longer want to be told about IP releases
1386 * for the given callback/private_data combination
1388 deregister_from_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1392 * inform ctdb of our tcp connection is now passed to
1395 ret
= ctdbd_control_local(conn
,
1396 CTDB_CONTROL_TCP_CLIENT_PASSED
, 0,
1397 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
,
1401 * We ignore errors here, as we'll just
1402 * no longer have a callback handler
1403 * registered and messages may just be ignored
1410 static int ctdbd_control_get_public_ips(struct ctdbd_connection
*conn
,
1413 TALLOC_CTX
*mem_ctx
,
1414 struct ctdb_public_ip_list_old
**_ips
)
1416 struct ctdb_public_ip_list_old
*ips
= NULL
;
1418 int32_t cstatus
= -1;
1425 ret
= ctdbd_control(conn
,
1427 CTDB_CONTROL_GET_PUBLIC_IPS
,
1430 tdb_null
, /* indata */
1434 if (ret
!= 0 || cstatus
!= 0) {
1435 DBG_ERR("ctdb_control for getpublicips failed ret:%d cstatus:%d\n",
1440 min_dsize
= offsetof(struct ctdb_public_ip_list_old
, ips
);
1441 if (outdata
.dsize
< min_dsize
) {
1442 DBG_ERR("outdata.dsize=%zu < min_dsize=%zu\n",
1443 outdata
.dsize
, min_dsize
);
1446 max_ips
= (outdata
.dsize
- min_dsize
)/sizeof(struct ctdb_public_ip
);
1447 ips
= (struct ctdb_public_ip_list_old
*)outdata
.dptr
;
1448 if ((size_t)ips
->num
> max_ips
) {
1449 DBG_ERR("ips->num=%zu > max_ips=%zu\n",
1450 (size_t)ips
->num
, max_ips
);
1458 static struct samba_sockaddr
ctdbd_sock_addr_to_samba(const ctdb_sock_addr
*c
)
1460 struct samba_sockaddr s
= {};
1462 switch (c
->sa
.sa_family
) {
1468 * ctdb always requires HAVE_IPV6,
1469 * so we don't need an ifdef here.
1475 * ctdb_sock_addr only supports ipv4 and ipv6
1477 smb_panic(__location__
);
1484 int ctdbd_public_ip_foreach(struct ctdbd_connection
*conn
,
1485 int (*cb
)(uint32_t total_ip_count
,
1486 const struct sockaddr_storage
*ip
,
1488 void *private_data
),
1492 struct ctdb_public_ip_list_old
*ips
= NULL
;
1494 TALLOC_CTX
*frame
= talloc_stackframe();
1496 ret
= ctdbd_control_get_public_ips(conn
, CTDB_CURRENT_NODE
, 0, frame
, &ips
);
1502 for (i
=0; i
< ips
->num
; i
++) {
1503 const ctdb_sock_addr
*addr
= &ips
->ips
[i
].addr
;
1504 struct samba_sockaddr tmp
= ctdbd_sock_addr_to_samba(addr
);
1508 true, /* all ctdb public ips are movable */
1521 static int count_ips(struct db_record
*rec
, void *private_data
)
1526 static int collect_ips(struct db_record
*rec
, void *private_data
)
1528 struct ctdb_public_ip_list_old
*ips
= talloc_get_type_abort(
1529 private_data
, struct ctdb_public_ip_list_old
);
1530 struct ctdb_public_ip
*ip
;
1531 TDB_DATA val
= dbwrap_record_get_value(rec
);
1533 SMB_ASSERT(val
.dsize
== sizeof(*ip
));
1535 ip
= (struct ctdb_public_ip
*)val
.dptr
;
1536 ips
->ips
[ips
->num
] = *ip
;
1542 static int ctdbd_control_get_all_public_ips(struct ctdbd_connection
*conn
,
1543 const struct ctdb_node_map_old
*nodemap
,
1544 TALLOC_CTX
*mem_ctx
,
1545 struct ctdb_public_ip_list_old
**_ips
)
1547 TALLOC_CTX
*frame
= talloc_stackframe();
1549 struct ctdb_public_ip_list_old
*ips
= NULL
;
1550 struct db_context
*rbt
= NULL
;
1556 rbt
= db_open_rbt(frame
);
1558 DBG_WARNING("db_open_rbt() failed\n");
1563 for (ni
=0; ni
< nodemap
->num
; ni
++) {
1564 const struct ctdb_node_and_flags
*n
= &nodemap
->nodes
[ni
];
1567 if (n
->flags
& NODE_FLAGS_INACTIVE
) {
1571 ret
= ctdbd_control_get_public_ips(conn
,
1581 for (j
=0; j
<ips
->num
; j
++) {
1582 struct ctdb_public_ip ip
;
1586 ip
.pnn
= ips
->ips
[j
].pnn
;
1587 ip
.addr
= ips
->ips
[j
].addr
;
1589 key
= make_tdb_data((uint8_t *)&ip
.addr
, sizeof(ip
.addr
));
1590 val
= make_tdb_data((uint8_t *)&ip
, sizeof(ip
));
1592 if (n
->pnn
== ip
.pnn
) {
1594 * Node claims IP is hosted on it, so
1595 * save that information
1597 status
= dbwrap_store(rbt
, key
, val
,
1599 if (!NT_STATUS_IS_OK(status
)) {
1605 * Node thinks IP is hosted elsewhere,
1606 * so overwrite with CTDB_UNKNOWN_PNN
1607 * if there's no existing entry
1609 bool exists
= dbwrap_exists(rbt
, key
);
1611 ip
.pnn
= CTDB_UNKNOWN_PNN
;
1612 status
= dbwrap_store(rbt
, key
, val
,
1614 if (!NT_STATUS_IS_OK(status
)) {
1625 status
= dbwrap_traverse_read(rbt
, count_ips
, NULL
, &count
);
1626 if (!NT_STATUS_IS_OK(status
)) {
1631 len
= offsetof(struct ctdb_public_ip_list_old
, ips
) +
1632 count
*sizeof(struct ctdb_public_ip
);
1633 ips
= talloc_zero_size(mem_ctx
, len
);
1638 talloc_set_type(ips
, struct ctdb_public_ip_list_old
);
1639 talloc_reparent(mem_ctx
, frame
, ips
);
1641 status
= dbwrap_traverse_read(rbt
, collect_ips
, ips
, &count
);
1642 if (!NT_STATUS_IS_OK(status
)) {
1647 if ((unsigned int)count
!= ips
->num
) {
1652 *_ips
= talloc_move(mem_ctx
, &ips
);
1658 * This includes all node and/or public ips
1659 * of the whole cluster.
1662 * - a valid pinned_pnn value.
1663 * - current_pnn is valid if the node is healthy
1666 * - pinned_pnn as CTDB_UNKNOWN_PNN
1667 * - current_pnn is valid if a node healthy and hosting this ip.
1669 int ctdbd_all_ip_foreach(struct ctdbd_connection
*conn
,
1670 bool include_node_ips
,
1671 bool include_public_ips
,
1672 int (*cb
)(uint32_t total_ip_count
,
1673 const struct sockaddr_storage
*ip
,
1674 uint32_t pinned_pnn
,
1675 uint32_t current_pnn
,
1676 void *private_data
),
1679 TALLOC_CTX
*frame
= talloc_stackframe();
1680 struct ctdb_node_map_old
*nodemap
= NULL
;
1681 struct ctdb_public_ip_list_old
*ips
= NULL
;
1683 uint32_t total_ip_count
= 0;
1686 ret
= ctdbd_control_get_nodemap(conn
, frame
, &nodemap
);
1688 DBG_WARNING("ctdbd_control_get_nodemap() failed: %s\n", strerror(ret
));
1693 for (i
=0; include_node_ips
&& i
< nodemap
->num
; i
++) {
1694 const struct ctdb_node_and_flags
*n
= &nodemap
->nodes
[i
];
1696 if (n
->flags
& NODE_FLAGS_DELETED
) {
1700 total_ip_count
+= 1;
1703 if (include_public_ips
) {
1704 ret
= ctdbd_control_get_all_public_ips(conn
, nodemap
,
1711 total_ip_count
+= ips
->num
;
1714 for (i
=0; include_node_ips
&& i
< nodemap
->num
; i
++) {
1715 const struct ctdb_node_and_flags
*n
= &nodemap
->nodes
[i
];
1716 struct samba_sockaddr tmp
= ctdbd_sock_addr_to_samba(&n
->addr
);
1717 uint32_t pinned_pnn
= n
->pnn
;
1718 uint32_t current_pnn
= n
->pnn
;
1720 if (n
->flags
& NODE_FLAGS_DELETED
) {
1724 if (n
->flags
& (NODE_FLAGS_INACTIVE
|NODE_FLAGS_DISABLED
)) {
1726 * The ip address is not available
1727 * unless the node is up and
1730 current_pnn
= CTDB_UNKNOWN_PNN
;
1733 ret
= cb(total_ip_count
,
1743 for (i
=0; include_public_ips
&& i
< ips
->num
; i
++) {
1744 const ctdb_sock_addr
*addr
= &ips
->ips
[i
].addr
;
1745 struct samba_sockaddr tmp
= ctdbd_sock_addr_to_samba(addr
);
1746 /* all ctdb public ips are movable and not pinned */
1747 uint32_t pinned_pnn
= CTDB_UNKNOWN_PNN
;
1748 uint32_t current_pnn
= ips
->ips
[i
].pnn
;
1751 for (ni
=0; ni
< nodemap
->num
; ni
++) {
1752 const struct ctdb_node_and_flags
*n
= &nodemap
->nodes
[ni
];
1754 if (n
->pnn
!= current_pnn
) {
1758 if (n
->flags
& (NODE_FLAGS_INACTIVE
|NODE_FLAGS_DISABLED
)) {
1759 current_pnn
= CTDB_UNKNOWN_PNN
;
1764 ret
= cb(total_ip_count
,
1781 call a control on the local node
1783 int ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1784 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1785 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1788 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
,
1789 mem_ctx
, outdata
, cstatus
);
1792 int ctdb_watch_us(struct ctdbd_connection
*conn
)
1794 struct ctdb_notify_data_old reg_data
;
1799 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1801 reg_data
.notify_data
[0] = 0;
1803 struct_len
= offsetof(struct ctdb_notify_data_old
,
1804 notify_data
) + reg_data
.len
;
1806 ret
= ctdbd_control_local(
1807 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1808 make_tdb_data((uint8_t *)®_data
, struct_len
),
1809 NULL
, NULL
, &cstatus
);
1811 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1817 int ctdb_unwatch(struct ctdbd_connection
*conn
)
1819 uint64_t srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1823 ret
= ctdbd_control_local(
1824 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1825 make_tdb_data((uint8_t *)&srvid
, sizeof(srvid
)),
1826 NULL
, NULL
, &cstatus
);
1828 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1834 int ctdbd_probe(const char *sockname
, int timeout
)
1837 * Do a very early check if ctdbd is around to avoid an abort and core
1840 struct ctdbd_connection
*conn
= NULL
;
1843 ret
= ctdbd_init_connection(talloc_tos(), sockname
, timeout
,
1847 * We only care if we can connect.
1854 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
)
1863 void ctdbd_prep_hdr_next_reqid(
1864 struct ctdbd_connection
*conn
, struct ctdb_req_header
*hdr
)
1866 *hdr
= (struct ctdb_req_header
) {
1867 .ctdb_magic
= CTDB_MAGIC
,
1868 .ctdb_version
= CTDB_PROTOCOL
,
1869 .reqid
= ctdbd_next_reqid(conn
),
1870 .destnode
= CTDB_CURRENT_NODE
,
1874 struct ctdbd_pkt_read_state
{
1878 static ssize_t
ctdbd_pkt_read_more(
1879 uint8_t *buf
, size_t buflen
, void *private_data
);
1880 static void ctdbd_pkt_read_done(struct tevent_req
*subreq
);
1882 static struct tevent_req
*ctdbd_pkt_read_send(
1883 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
, int fd
)
1885 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1886 struct ctdbd_pkt_read_state
*state
= NULL
;
1888 req
= tevent_req_create(mem_ctx
, &state
, struct ctdbd_pkt_read_state
);
1892 subreq
= read_packet_send(state
, ev
, fd
, 4, ctdbd_pkt_read_more
, NULL
);
1893 if (tevent_req_nomem(subreq
, req
)) {
1894 return tevent_req_post(req
, ev
);
1896 tevent_req_set_callback(subreq
, ctdbd_pkt_read_done
, req
);
1900 static ssize_t
ctdbd_pkt_read_more(
1901 uint8_t *buf
, size_t buflen
, void *private_data
)
1908 return 0; /* Been here, done */
1910 memcpy(&msglen
, buf
, 4);
1912 if (msglen
< sizeof(struct ctdb_req_header
)) {
1915 return msglen
- sizeof(msglen
);
1918 static void ctdbd_pkt_read_done(struct tevent_req
*subreq
)
1920 struct tevent_req
*req
= tevent_req_callback_data(
1921 subreq
, struct tevent_req
);
1922 struct ctdbd_pkt_read_state
*state
= tevent_req_data(
1923 req
, struct ctdbd_pkt_read_state
);
1927 nread
= read_packet_recv(subreq
, state
, &state
->pkt
, &err
);
1928 TALLOC_FREE(subreq
);
1930 tevent_req_error(req
, err
);
1933 tevent_req_done(req
);
1936 static int ctdbd_pkt_read_recv(
1937 struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
, uint8_t **pkt
)
1939 struct ctdbd_pkt_read_state
*state
= tevent_req_data(
1940 req
, struct ctdbd_pkt_read_state
);
1943 if (tevent_req_is_unix_error(req
, &err
)) {
1946 *pkt
= talloc_move(mem_ctx
, &state
->pkt
);
1947 tevent_req_received(req
);
1951 static bool ctdbd_conn_receive_next(struct ctdbd_connection
*conn
);
1952 static void ctdbd_conn_received(struct tevent_req
*subreq
);
1954 struct ctdbd_req_state
{
1955 struct ctdbd_connection
*conn
;
1956 struct tevent_context
*ev
;
1958 struct ctdb_req_header
*reply
;
1961 static void ctdbd_req_unset_pending(struct tevent_req
*req
)
1963 struct ctdbd_req_state
*state
= tevent_req_data(
1964 req
, struct ctdbd_req_state
);
1965 struct ctdbd_connection
*conn
= state
->conn
;
1966 size_t num_pending
= talloc_array_length(conn
->pending
);
1967 size_t i
, num_after
;
1969 tevent_req_set_cleanup_fn(req
, NULL
);
1971 if (num_pending
== 1) {
1973 * conn->read_req is a child of conn->pending
1975 TALLOC_FREE(conn
->pending
);
1976 conn
->read_req
= NULL
;
1980 for (i
=0; i
<num_pending
; i
++) {
1981 if (req
== conn
->pending
[i
]) {
1985 if (i
== num_pending
) {
1987 * Something's seriously broken. Just returning here is the
1988 * right thing nevertheless, the point of this routine is to
1989 * remove ourselves from conn->pending.
1994 num_after
= num_pending
- i
- 1;
1995 if (num_after
> 0) {
1996 memmove(&conn
->pending
[i
],
1997 &conn
->pending
[i
] + 1,
1998 sizeof(*conn
->pending
) * num_after
);
2000 conn
->pending
= talloc_realloc(
2001 NULL
, conn
->pending
, struct tevent_req
*, num_pending
- 1);
2004 static void ctdbd_req_cleanup(
2005 struct tevent_req
*req
, enum tevent_req_state req_state
)
2007 ctdbd_req_unset_pending(req
);
2010 static bool ctdbd_req_set_pending(struct tevent_req
*req
)
2012 struct ctdbd_req_state
*state
= tevent_req_data(
2013 req
, struct ctdbd_req_state
);
2014 struct ctdbd_connection
*conn
= state
->conn
;
2015 struct tevent_req
**pending
= NULL
;
2016 size_t num_pending
= talloc_array_length(conn
->pending
);
2019 pending
= talloc_realloc(
2020 conn
, conn
->pending
, struct tevent_req
*, num_pending
+ 1);
2021 if (pending
== NULL
) {
2024 pending
[num_pending
] = req
;
2025 conn
->pending
= pending
;
2027 tevent_req_set_cleanup_fn(req
, ctdbd_req_cleanup
);
2029 ok
= ctdbd_conn_receive_next(conn
);
2031 ctdbd_req_unset_pending(req
);
2038 static bool ctdbd_conn_receive_next(struct ctdbd_connection
*conn
)
2040 size_t num_pending
= talloc_array_length(conn
->pending
);
2041 struct tevent_req
*req
= NULL
;
2042 struct ctdbd_req_state
*state
= NULL
;
2044 if (conn
->read_req
!= NULL
) {
2047 if (num_pending
== 0) {
2054 req
= conn
->pending
[0];
2055 state
= tevent_req_data(req
, struct ctdbd_req_state
);
2057 conn
->read_req
= ctdbd_pkt_read_send(
2058 conn
->pending
, state
->ev
, conn
->fd
);
2059 if (conn
->read_req
== NULL
) {
2062 tevent_req_set_callback(conn
->read_req
, ctdbd_conn_received
, conn
);
2066 static void ctdbd_conn_received(struct tevent_req
*subreq
)
2068 struct ctdbd_connection
*conn
= tevent_req_callback_data(
2069 subreq
, struct ctdbd_connection
);
2070 TALLOC_CTX
*frame
= talloc_stackframe();
2071 uint8_t *pkt
= NULL
;
2073 struct ctdb_req_header
*hdr
= NULL
;
2075 struct tevent_req
*req
= NULL
;
2076 struct ctdbd_req_state
*state
= NULL
;
2077 size_t i
, num_pending
;
2080 SMB_ASSERT(subreq
== conn
->read_req
);
2081 conn
->read_req
= NULL
;
2083 ret
= ctdbd_pkt_read_recv(subreq
, frame
, &pkt
);
2084 TALLOC_FREE(subreq
);
2086 cluster_fatal("ctdbd_pkt_read failed\n");
2089 hdr
= (struct ctdb_req_header
*)pkt
;
2091 num_pending
= talloc_array_length(conn
->pending
);
2093 for (i
=0; i
<num_pending
; i
++) {
2094 req
= conn
->pending
[i
];
2095 state
= tevent_req_data(req
, struct ctdbd_req_state
);
2096 if (state
->reqid
== reqid
) {
2101 if (i
== num_pending
) {
2107 state
->reply
= talloc_move(state
, &hdr
);
2108 tevent_req_defer_callback(req
, state
->ev
);
2109 tevent_req_done(req
);
2113 ok
= ctdbd_conn_receive_next(conn
);
2115 cluster_fatal("ctdbd_conn_receive_next failed\n");
2119 static void ctdbd_req_written(struct tevent_req
*subreq
);
2121 struct tevent_req
*ctdbd_req_send(
2122 TALLOC_CTX
*mem_ctx
,
2123 struct tevent_context
*ev
,
2124 struct ctdbd_connection
*conn
,
2128 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2129 struct ctdbd_req_state
*state
= NULL
;
2130 struct ctdb_req_header
*hdr
= NULL
;
2133 req
= tevent_req_create(mem_ctx
, &state
, struct ctdbd_req_state
);
2140 if ((num_iov
== 0) ||
2141 (iov
[0].iov_len
< sizeof(struct ctdb_req_header
))) {
2142 tevent_req_error(req
, EINVAL
);
2143 return tevent_req_post(req
, ev
);
2145 hdr
= iov
[0].iov_base
;
2146 state
->reqid
= hdr
->reqid
;
2148 ok
= ctdbd_req_set_pending(req
);
2150 tevent_req_oom(req
);
2151 return tevent_req_post(req
, ev
);
2154 subreq
= writev_send(
2155 state
, ev
, conn
->outgoing
, conn
->fd
, false, iov
, num_iov
);
2156 if (tevent_req_nomem(subreq
, req
)) {
2157 return tevent_req_post(req
, ev
);
2159 tevent_req_set_callback(subreq
, ctdbd_req_written
, req
);
2164 static void ctdbd_req_written(struct tevent_req
*subreq
)
2166 struct tevent_req
*req
= tevent_req_callback_data(
2167 subreq
, struct tevent_req
);
2171 nwritten
= writev_recv(subreq
, &err
);
2172 TALLOC_FREE(subreq
);
2173 if (nwritten
== -1) {
2174 tevent_req_error(req
, err
);
2180 struct tevent_req
*req
,
2181 TALLOC_CTX
*mem_ctx
,
2182 struct ctdb_req_header
**reply
)
2184 struct ctdbd_req_state
*state
= tevent_req_data(
2185 req
, struct ctdbd_req_state
);
2188 if (tevent_req_is_unix_error(req
, &err
)) {
2191 *reply
= talloc_move(mem_ctx
, &state
->reply
);
2192 tevent_req_received(req
);
2196 struct ctdbd_parse_state
{
2197 struct tevent_context
*ev
;
2198 struct ctdbd_connection
*conn
;
2201 uint8_t _keybuf
[64];
2202 struct ctdb_req_call_old ctdb_req
;
2203 struct iovec iov
[2];
2204 void (*parser
)(TDB_DATA key
,
2206 void *private_data
);
2210 static void ctdbd_parse_done(struct tevent_req
*subreq
);
2212 struct tevent_req
*ctdbd_parse_send(TALLOC_CTX
*mem_ctx
,
2213 struct tevent_context
*ev
,
2214 struct ctdbd_connection
*conn
,
2218 void (*parser
)(TDB_DATA key
,
2220 void *private_data
),
2222 enum dbwrap_req_state
*req_state
)
2224 struct tevent_req
*req
= NULL
;
2225 struct ctdbd_parse_state
*state
= NULL
;
2227 uint32_t packet_length
;
2228 struct tevent_req
*subreq
= NULL
;
2230 req
= tevent_req_create(mem_ctx
, &state
, struct ctdbd_parse_state
);
2232 *req_state
= DBWRAP_REQ_ERROR
;
2236 *req_state
= DBWRAP_REQ_DISPATCHED
;
2238 *state
= (struct ctdbd_parse_state
) {
2241 .reqid
= ctdbd_next_reqid(conn
),
2243 .private_data
= private_data
,
2246 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
2247 packet_length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
2250 * Copy the key into our state, as ctdb_pkt_send_cleanup() requires that
2251 * all passed iov elements have a lifetime longer that the tevent_req
2252 * returned by ctdb_pkt_send_send(). This is required continue sending a
2253 * the low level request into the ctdb socket, if a higher level
2254 * ('this') request is canceled (or talloc free'd) by the application
2255 * layer, without sending invalid packets to ctdb.
2257 if (key
.dsize
> sizeof(state
->_keybuf
)) {
2258 state
->key
.dptr
= talloc_memdup(state
, key
.dptr
, key
.dsize
);
2259 if (tevent_req_nomem(state
->key
.dptr
, req
)) {
2260 return tevent_req_post(req
, ev
);
2263 memcpy(state
->_keybuf
, key
.dptr
, key
.dsize
);
2264 state
->key
.dptr
= state
->_keybuf
;
2266 state
->key
.dsize
= key
.dsize
;
2268 state
->ctdb_req
.hdr
.length
= packet_length
;
2269 state
->ctdb_req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
2270 state
->ctdb_req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
2271 state
->ctdb_req
.hdr
.operation
= CTDB_REQ_CALL
;
2272 state
->ctdb_req
.hdr
.reqid
= state
->reqid
;
2273 state
->ctdb_req
.flags
= flags
;
2274 state
->ctdb_req
.callid
= CTDB_FETCH_FUNC
;
2275 state
->ctdb_req
.db_id
= db_id
;
2276 state
->ctdb_req
.keylen
= state
->key
.dsize
;
2278 state
->iov
[0].iov_base
= &state
->ctdb_req
;
2279 state
->iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
2280 state
->iov
[1].iov_base
= state
->key
.dptr
;
2281 state
->iov
[1].iov_len
= state
->key
.dsize
;
2283 subreq
= ctdbd_req_send(
2284 state
, ev
, conn
, state
->iov
, ARRAY_SIZE(state
->iov
));
2285 if (tevent_req_nomem(subreq
, req
)) {
2286 *req_state
= DBWRAP_REQ_ERROR
;
2287 return tevent_req_post(req
, ev
);
2289 tevent_req_set_callback(subreq
, ctdbd_parse_done
, req
);
2294 static void ctdbd_parse_done(struct tevent_req
*subreq
)
2296 struct tevent_req
*req
= tevent_req_callback_data(
2297 subreq
, struct tevent_req
);
2298 struct ctdbd_parse_state
*state
= tevent_req_data(
2299 req
, struct ctdbd_parse_state
);
2300 struct ctdb_req_header
*hdr
= NULL
;
2301 struct ctdb_reply_call_old
*reply
= NULL
;
2304 ret
= ctdbd_req_recv(subreq
, state
, &hdr
);
2305 TALLOC_FREE(subreq
);
2306 if (tevent_req_error(req
, ret
)) {
2307 DBG_DEBUG("ctdb_req_recv failed %s\n", strerror(ret
));
2310 SMB_ASSERT(hdr
!= NULL
);
2312 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
2313 DBG_ERR("received invalid reply\n");
2314 ctdb_packet_dump(hdr
);
2315 tevent_req_error(req
, EIO
);
2319 reply
= (struct ctdb_reply_call_old
*)hdr
;
2321 if (reply
->datalen
== 0) {
2323 * Treat an empty record as non-existing
2325 tevent_req_error(req
, ENOENT
);
2329 state
->parser(state
->key
,
2330 make_tdb_data(&reply
->data
[0], reply
->datalen
),
2331 state
->private_data
);
2333 tevent_req_done(req
);
2337 int ctdbd_parse_recv(struct tevent_req
*req
)
2339 return tevent_req_simple_recv_unix(req
);