2 Unix SMB/CIFS implementation.
3 Samba internal messaging functions
4 Copyright (C) 2007 by Volker Lendecke
5 Copyright (C) 2007 by Andrew Tridgell
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "ctdbd_conn.h"
25 #include "system/select.h"
26 #include "lib/sys_rw_data.h"
27 #include "lib/util/iov_buf.h"
31 /* paths to these include files come from --with-ctdb= in configure */
34 #include "ctdb_private.h"
36 struct ctdbd_srvid_cb
{
38 void (*cb
)(struct ctdb_req_message
*msg
, void *private_data
);
42 struct ctdbd_connection
{
43 struct messaging_context
*msg_ctx
;
47 struct ctdbd_srvid_cb
*callbacks
;
49 struct tevent_fd
*fde
;
51 bool (*release_ip_handler
)(const char *ip_addr
, void *private_data
);
52 void *release_ip_priv
;
55 static uint32_t ctdbd_next_reqid(struct ctdbd_connection
*conn
)
58 if (conn
->reqid
== 0) {
64 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
65 uint32_t vnn
, uint32_t opcode
,
66 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
67 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
71 * exit on fatal communications errors with the ctdbd daemon
73 static void cluster_fatal(const char *why
)
75 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
76 /* we don't use smb_panic() as we don't want to delay to write
77 a core file. We need to release this process id immediately
78 so that someone else can take over without getting sharing
86 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
88 if (DEBUGLEVEL
< 11) {
91 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
92 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
93 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
94 (int)hdr
->operation
, (int)hdr
->reqid
));
98 * Register a srvid with ctdbd
100 NTSTATUS
register_with_ctdbd(struct ctdbd_connection
*conn
, uint64_t srvid
,
101 void (*cb
)(struct ctdb_req_message
*msg
,
108 size_t num_callbacks
;
109 struct ctdbd_srvid_cb
*tmp
;
111 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
112 CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
113 tdb_null
, NULL
, NULL
, &cstatus
);
114 if (!NT_STATUS_IS_OK(status
)) {
118 num_callbacks
= talloc_array_length(conn
->callbacks
);
120 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
123 return NT_STATUS_NO_MEMORY
;
125 conn
->callbacks
= tmp
;
127 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
128 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
134 static void ctdbd_msg_call_back(struct ctdbd_connection
*conn
,
135 struct ctdb_req_message
*msg
)
137 size_t i
, num_callbacks
;
139 num_callbacks
= talloc_array_length(conn
->callbacks
);
141 for (i
=0; i
<num_callbacks
; i
++) {
142 struct ctdbd_srvid_cb
*cb
= &conn
->callbacks
[i
];
144 if ((cb
->srvid
== msg
->srvid
) && (cb
->cb
!= NULL
)) {
145 cb
->cb(msg
, cb
->private_data
);
151 * get our vnn from the cluster
153 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
157 status
= ctdbd_control(conn
,
158 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_PNN
, 0, 0,
159 tdb_null
, NULL
, NULL
, &cstatus
);
160 if (!NT_STATUS_IS_OK(status
)) {
161 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
164 *vnn
= (uint32_t)cstatus
;
169 * Are we active (i.e. not banned or stopped?)
171 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
176 struct ctdb_node_map
*m
;
177 uint32_t failure_flags
;
181 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
182 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
183 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
184 if (!NT_STATUS_IS_OK(status
)) {
185 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
188 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
189 DEBUG(2, ("Received invalid ctdb data\n"));
193 m
= (struct ctdb_node_map
*)outdata
.dptr
;
195 for (i
=0; i
<m
->num
; i
++) {
196 if (vnn
== m
->nodes
[i
].pnn
) {
202 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
207 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
208 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
210 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
211 DEBUG(2, ("Node has status %x, not active\n",
212 (int)m
->nodes
[i
].flags
));
218 TALLOC_FREE(outdata
.dptr
);
222 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
224 return conn
->our_vnn
;
227 const char *lp_ctdbd_socket(void)
231 ret
= lp__ctdbd_socket();
232 if (ret
!= NULL
&& strlen(ret
) > 0) {
240 * Get us a ctdb connection
243 static int ctdbd_connect(int *pfd
)
245 const char *sockname
= lp_ctdbd_socket();
246 struct sockaddr_un addr
= { 0, };
251 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
254 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
258 addr
.sun_family
= AF_UNIX
;
260 namelen
= strlcpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
261 if (namelen
>= sizeof(addr
.sun_path
)) {
262 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
268 salen
= sizeof(struct sockaddr_un
);
270 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
272 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
282 static NTSTATUS
ctdb_read_packet(int fd
, TALLOC_CTX
*mem_ctx
,
283 struct ctdb_req_header
**result
)
285 int timeout
= lp_ctdb_timeout();
286 struct ctdb_req_header
*req
;
296 ret
= poll_one_fd(fd
, POLLIN
, timeout
, &revents
);
298 return map_nt_error_from_unix(errno
);
301 return NT_STATUS_IO_TIMEOUT
;
304 return NT_STATUS_UNEXPECTED_IO_ERROR
;
308 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
310 return map_nt_error_from_unix(errno
);
313 return NT_STATUS_UNEXPECTED_IO_ERROR
;
316 if (msglen
< sizeof(struct ctdb_req_header
)) {
317 return NT_STATUS_UNEXPECTED_IO_ERROR
;
320 req
= talloc_size(mem_ctx
, msglen
);
322 return NT_STATUS_NO_MEMORY
;
324 talloc_set_name_const(req
, "struct ctdb_req_header");
326 req
->length
= msglen
;
328 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
329 msglen
- sizeof(msglen
));
331 return map_nt_error_from_unix(errno
);
334 return NT_STATUS_UNEXPECTED_IO_ERROR
;
342 * Read a full ctdbd request. If we have a messaging context, defer incoming
343 * messages that might come in between.
346 static NTSTATUS
ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
348 struct ctdb_req_header
**result
)
350 struct ctdb_req_header
*hdr
;
355 status
= ctdb_read_packet(conn
->fd
, mem_ctx
, &hdr
);
356 if (!NT_STATUS_IS_OK(status
)) {
357 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status
)));
358 cluster_fatal("ctdbd died\n");
361 DEBUG(11, ("Received ctdb packet\n"));
362 ctdb_packet_dump(hdr
);
364 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
365 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
367 if (conn
->msg_ctx
== NULL
) {
368 DEBUG(1, ("Got a message without having a msg ctx, "
369 "dropping msg %llu\n",
370 (long long unsigned)msg
->srvid
));
374 if ((conn
->release_ip_handler
!= NULL
)
375 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
378 /* must be dispatched immediately */
379 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
380 ret
= conn
->release_ip_handler((const char *)msg
->data
,
381 conn
->release_ip_priv
);
386 * We need to release the ip,
387 * so return an error to the upper layers.
389 * We make sure we don't trigger this again.
391 conn
->release_ip_handler
= NULL
;
392 conn
->release_ip_priv
= NULL
;
393 return NT_STATUS_ADDRESS_CLOSED
;
398 ctdbd_msg_call_back(conn
, msg
);
403 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
404 /* we got the wrong reply */
405 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
406 "been %u\n", hdr
->reqid
, reqid
));
411 *result
= talloc_move(mem_ctx
, &hdr
);
416 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 struct ctdbd_connection
**pconn
)
428 struct ctdbd_connection
*conn
;
432 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
433 DEBUG(0, ("talloc failed\n"));
434 return NT_STATUS_NO_MEMORY
;
437 ret
= ctdbd_connect(&conn
->fd
);
439 status
= map_nt_error_from_unix(errno
);
440 DEBUG(10, ("ctdbd_connect failed: %s\n", strerror(errno
)));
443 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
445 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
447 if (!NT_STATUS_IS_OK(status
)) {
448 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
452 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
453 DEBUG(2, ("Node is not working, can not connect\n"));
454 status
= NT_STATUS_INTERNAL_DB_ERROR
;
458 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
459 sizeof(conn
->rand_srvid
));
461 status
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
463 if (!NT_STATUS_IS_OK(status
)) {
464 DEBUG(5, ("Could not register random srvid: %s\n",
478 * Get us a ctdbd connection and register us as a process
481 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
482 struct ctdbd_connection
**pconn
)
484 struct ctdbd_connection
*conn
;
487 status
= ctdbd_init_connection(mem_ctx
, &conn
);
489 if (!NT_STATUS_IS_OK(status
)) {
493 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
, NULL
, NULL
);
494 if (!NT_STATUS_IS_OK(status
)) {
506 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
508 return conn
->msg_ctx
;
511 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
517 * Packet handler to receive and handle a ctdb message
519 static NTSTATUS
ctdb_handle_message(struct ctdbd_connection
*conn
,
520 struct ctdb_req_header
*hdr
)
522 struct ctdb_req_message
*msg
;
524 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
525 DEBUG(0, ("Received async msg of type %u, discarding\n",
527 return NT_STATUS_INVALID_PARAMETER
;
530 msg
= (struct ctdb_req_message
*)hdr
;
532 if ((conn
->release_ip_handler
!= NULL
)
533 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
536 /* must be dispatched immediately */
537 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
538 ret
= conn
->release_ip_handler((const char *)msg
->data
,
539 conn
->release_ip_priv
);
542 * We need to release the ip.
544 * We make sure we don't trigger this again.
546 conn
->release_ip_handler
= NULL
;
547 conn
->release_ip_priv
= NULL
;
552 ctdbd_msg_call_back(conn
, msg
);
558 * The ctdbd socket is readable asynchronuously
561 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
562 struct tevent_fd
*event
,
566 struct ctdbd_connection
*conn
= talloc_get_type_abort(
567 private_data
, struct ctdbd_connection
);
568 struct ctdb_req_header
*hdr
= NULL
;
571 status
= ctdb_read_packet(conn
->fd
, talloc_tos(), &hdr
);
572 if (!NT_STATUS_IS_OK(status
)) {
573 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status
)));
574 cluster_fatal("ctdbd died\n");
577 status
= ctdb_handle_message(conn
, hdr
);
578 if (!NT_STATUS_IS_OK(status
)) {
579 DEBUG(10, ("could not handle incoming message: %s\n",
585 * Prepare a ctdbd connection to receive messages
588 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
589 struct messaging_context
*msg_ctx
)
591 SMB_ASSERT(conn
->msg_ctx
== NULL
);
592 SMB_ASSERT(conn
->fde
== NULL
);
594 if (!(conn
->fde
= tevent_add_fd(messaging_tevent_context(msg_ctx
),
598 ctdbd_socket_handler
,
600 DEBUG(0, ("event_add_fd failed\n"));
601 return NT_STATUS_NO_MEMORY
;
604 conn
->msg_ctx
= msg_ctx
;
609 NTSTATUS
ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
610 uint32_t dst_vnn
, uint64_t dst_srvid
,
611 const struct iovec
*iov
, int iovlen
)
613 struct ctdb_req_message r
;
614 struct iovec iov2
[iovlen
+1];
615 size_t buflen
= iov_buflen(iov
, iovlen
);
618 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + buflen
;
619 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
620 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
621 r
.hdr
.generation
= 1;
622 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
623 r
.hdr
.destnode
= dst_vnn
;
624 r
.hdr
.srcnode
= conn
->our_vnn
;
629 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
630 ctdb_packet_dump(&r
.hdr
);
632 iov2
[0].iov_base
= &r
;
633 iov2
[0].iov_len
= offsetof(struct ctdb_req_message
, data
);
634 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
636 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
637 if (nwritten
== -1) {
638 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
639 cluster_fatal("cluster dispatch daemon msg write error\n");
646 * send/recv a generic ctdb control message
648 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
649 uint32_t vnn
, uint32_t opcode
,
650 uint64_t srvid
, uint32_t flags
,
652 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
655 struct ctdb_req_control req
;
656 struct ctdb_req_header
*hdr
;
657 struct ctdb_reply_control
*reply
= NULL
;
658 struct ctdbd_connection
*new_conn
= NULL
;
664 status
= ctdbd_init_connection(NULL
, &new_conn
);
666 if (!NT_STATUS_IS_OK(status
)) {
667 DEBUG(10, ("Could not init temp connection: %s\n",
676 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
677 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
678 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
679 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
680 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
681 req
.hdr
.destnode
= vnn
;
684 req
.datalen
= data
.dsize
;
687 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
688 ctdb_packet_dump(&req
.hdr
);
690 iov
[0].iov_base
= &req
;
691 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
692 iov
[1].iov_base
= data
.dptr
;
693 iov
[1].iov_len
= data
.dsize
;
695 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
696 if (nwritten
== -1) {
697 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
698 cluster_fatal("cluster dispatch daemon msg write error\n");
701 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
702 TALLOC_FREE(new_conn
);
709 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
711 if (!NT_STATUS_IS_OK(status
)) {
712 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
716 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
717 DEBUG(0, ("received invalid reply\n"));
720 reply
= (struct ctdb_reply_control
*)hdr
;
723 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
724 mem_ctx
, reply
->data
, reply
->datalen
))) {
726 return NT_STATUS_NO_MEMORY
;
728 outdata
->dsize
= reply
->datalen
;
731 (*cstatus
) = reply
->status
;
734 status
= NT_STATUS_OK
;
737 TALLOC_FREE(new_conn
);
743 * see if a remote process exists
745 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
753 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
754 DEBUG(10, ("ctdb_processes_exist failed\n"));
760 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
761 const struct server_id
*pids
, int num_pids
,
764 TALLOC_CTX
*frame
= talloc_stackframe();
770 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
771 if (reqids
== NULL
) {
775 for (i
=0; i
<num_pids
; i
++) {
776 struct ctdb_req_control req
;
782 reqids
[i
] = ctdbd_next_reqid(conn
);
787 * pids[i].pid is uint64_t, scale down to pid_t which
788 * is the wire protocol towards ctdb.
792 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
793 (int)pids
[i
].vnn
, (int)pid
,
796 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
797 req
.hdr
.length
+= sizeof(pid
);
798 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
799 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
800 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
801 req
.hdr
.reqid
= reqids
[i
];
802 req
.hdr
.destnode
= pids
[i
].vnn
;
803 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
805 req
.datalen
= sizeof(pid
);
808 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
809 ctdb_packet_dump(&req
.hdr
);
811 iov
[0].iov_base
= &req
;
812 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
813 iov
[1].iov_base
= &pid
;
814 iov
[1].iov_len
= sizeof(pid
);
816 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
817 if (nwritten
== -1) {
818 status
= map_nt_error_from_unix(errno
);
819 DEBUG(10, ("write_data_iov failed: %s\n",
827 while (num_received
< num_pids
) {
828 struct ctdb_req_header
*hdr
;
829 struct ctdb_reply_control
*reply
;
832 status
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
833 if (!NT_STATUS_IS_OK(status
)) {
834 DEBUG(10, ("ctdb_read_req failed: %s\n",
839 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
840 DEBUG(10, ("Received invalid reply\n"));
843 reply
= (struct ctdb_reply_control
*)hdr
;
845 reqid
= reply
->hdr
.reqid
;
847 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
849 for (i
=0; i
<num_pids
; i
++) {
850 if (reqid
== reqids
[i
]) {
855 DEBUG(10, ("Received unknown record number %u\n",
859 results
[i
] = ((reply
->status
) == 0);
870 struct ctdb_vnn_list
{
876 unsigned *pid_indexes
;
880 * Get a list of all vnns mentioned in a list of
881 * server_ids. vnn_indexes tells where in the vnns array we have to
884 static bool ctdb_collect_vnns(TALLOC_CTX
*mem_ctx
,
885 const struct server_id
*pids
, unsigned num_pids
,
886 struct ctdb_vnn_list
**pvnns
,
889 struct ctdb_vnn_list
*vnns
= NULL
;
890 unsigned *vnn_indexes
= NULL
;
891 unsigned i
, num_vnns
= 0;
893 vnn_indexes
= talloc_array(mem_ctx
, unsigned, num_pids
);
894 if (vnn_indexes
== NULL
) {
895 DEBUG(1, ("talloc_array failed\n"));
899 for (i
=0; i
<num_pids
; i
++) {
901 uint32_t vnn
= pids
[i
].vnn
;
903 for (j
=0; j
<num_vnns
; j
++) {
904 if (vnn
== vnns
[j
].vnn
) {
912 * Already in the array
914 vnns
[j
].num_srvids
+= 1;
917 vnns
= talloc_realloc(mem_ctx
, vnns
, struct ctdb_vnn_list
,
920 DEBUG(1, ("talloc_realloc failed\n"));
923 vnns
[num_vnns
].vnn
= vnn
;
924 vnns
[num_vnns
].num_srvids
= 1;
925 vnns
[num_vnns
].num_filled
= 0;
928 for (i
=0; i
<num_vnns
; i
++) {
929 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
931 vnn
->srvids
= talloc_array(vnns
, uint64_t, vnn
->num_srvids
);
932 if (vnn
->srvids
== NULL
) {
933 DEBUG(1, ("talloc_array failed\n"));
936 vnn
->pid_indexes
= talloc_array(vnns
, unsigned,
938 if (vnn
->pid_indexes
== NULL
) {
939 DEBUG(1, ("talloc_array failed\n"));
943 for (i
=0; i
<num_pids
; i
++) {
944 struct ctdb_vnn_list
*vnn
= &vnns
[vnn_indexes
[i
]];
945 vnn
->srvids
[vnn
->num_filled
] = pids
[i
].unique_id
;
946 vnn
->pid_indexes
[vnn
->num_filled
] = i
;
947 vnn
->num_filled
+= 1;
950 TALLOC_FREE(vnn_indexes
);
952 *pnum_vnns
= num_vnns
;
956 TALLOC_FREE(vnn_indexes
);
960 bool ctdb_serverids_exist_supported(struct ctdbd_connection
*conn
)
965 bool ctdb_serverids_exist(struct ctdbd_connection
*conn
,
966 const struct server_id
*pids
, unsigned num_pids
,
969 unsigned i
, num_received
;
971 struct ctdb_vnn_list
*vnns
= NULL
;
974 if (!ctdb_collect_vnns(talloc_tos(), pids
, num_pids
,
976 DEBUG(1, ("ctdb_collect_vnns failed\n"));
980 for (i
=0; i
<num_vnns
; i
++) {
981 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
982 struct ctdb_req_control req
;
986 vnn
->reqid
= ctdbd_next_reqid(conn
);
990 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
991 (int)vnn
->vnn
, (int)vnn
->reqid
, vnn
->num_srvids
));
993 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
994 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
995 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
996 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
997 req
.hdr
.reqid
= vnn
->reqid
;
998 req
.hdr
.destnode
= vnn
->vnn
;
999 req
.opcode
= CTDB_CONTROL_CHECK_SRVIDS
;
1001 req
.datalen
= sizeof(uint64_t) * vnn
->num_srvids
;
1002 req
.hdr
.length
+= req
.datalen
;
1005 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1006 ctdb_packet_dump(&req
.hdr
);
1008 iov
[0].iov_base
= &req
;
1009 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
1010 iov
[1].iov_base
= vnn
->srvids
;
1011 iov
[1].iov_len
= req
.datalen
;
1013 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1014 if (nwritten
== -1) {
1015 status
= map_nt_error_from_unix(errno
);
1016 DEBUG(10, ("write_data_iov failed: %s\n",
1024 while (num_received
< num_vnns
) {
1025 struct ctdb_req_header
*hdr
;
1026 struct ctdb_reply_control
*reply
;
1027 struct ctdb_vnn_list
*vnn
;
1029 uint8_t *reply_data
;
1031 status
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
1032 if (!NT_STATUS_IS_OK(status
)) {
1033 DEBUG(1, ("ctdb_read_req failed: %s\n",
1034 nt_errstr(status
)));
1038 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
1039 DEBUG(1, ("Received invalid reply %u\n",
1040 (unsigned)hdr
->operation
));
1043 reply
= (struct ctdb_reply_control
*)hdr
;
1045 reqid
= reply
->hdr
.reqid
;
1047 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1049 for (i
=0; i
<num_vnns
; i
++) {
1050 if (reqid
== vnns
[i
].reqid
) {
1054 if (i
== num_vnns
) {
1055 DEBUG(1, ("Received unknown reqid number %u\n",
1060 DEBUG(10, ("Found index %u\n", i
));
1064 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1065 (unsigned)vnn
->vnn
, vnn
->num_srvids
,
1066 (unsigned)reply
->datalen
));
1068 if (reply
->datalen
>= ((vnn
->num_srvids
+7)/8)) {
1072 reply_data
= reply
->data
;
1075 * Got an error reply
1077 DEBUG(5, ("Received short reply len %d, status %u, "
1079 (unsigned)reply
->datalen
,
1080 (unsigned)reply
->status
,
1081 (unsigned)reply
->errorlen
));
1082 dump_data(5, reply
->data
, reply
->errorlen
);
1085 * This will trigger everything set to false
1090 for (i
=0; i
<vnn
->num_srvids
; i
++) {
1091 int idx
= vnn
->pid_indexes
[i
];
1093 if (pids
[i
].unique_id
==
1094 SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
1095 results
[idx
] = true;
1099 (reply_data
!= NULL
) &&
1100 ((reply_data
[i
/8] & (1<<(i
%8))) != 0);
1110 cluster_fatal("serverids_exist failed");
1117 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
1118 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
1122 TDB_DATA rdata
= {0};
1123 int32_t cstatus
= 0;
1125 data
.dptr
= (uint8_t*)&db_id
;
1126 data
.dsize
= sizeof(db_id
);
1128 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1129 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
1130 mem_ctx
, &rdata
, &cstatus
);
1131 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1132 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
1136 return (char *)rdata
.dptr
;
1140 * attach to a ctdb database
1142 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
1143 const char *name
, uint32_t *db_id
, int tdb_flags
)
1148 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
1150 data
= string_term_tdb_data(name
);
1152 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1154 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1155 : CTDB_CONTROL_DB_ATTACH
,
1156 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
1157 if (!NT_STATUS_IS_OK(status
)) {
1158 DEBUG(0, (__location__
" ctdb_control for db_attach "
1159 "failed: %s\n", nt_errstr(status
)));
1163 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
1164 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
1165 return NT_STATUS_INTERNAL_ERROR
;
1168 *db_id
= *(uint32_t *)data
.dptr
;
1169 talloc_free(data
.dptr
);
1171 if (!(tdb_flags
& TDB_SEQNUM
)) {
1172 return NT_STATUS_OK
;
1175 data
.dptr
= (uint8_t *)db_id
;
1176 data
.dsize
= sizeof(*db_id
);
1178 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1179 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
1180 NULL
, NULL
, &cstatus
);
1181 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1182 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
1184 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
1188 return NT_STATUS_OK
;
1192 * force the migration of a record to this node
1194 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
1197 struct ctdb_req_call req
;
1198 struct ctdb_req_header
*hdr
;
1199 struct iovec iov
[2];
1205 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1206 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1207 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1208 req
.hdr
.operation
= CTDB_REQ_CALL
;
1209 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1210 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1211 req
.callid
= CTDB_NULL_FUNC
;
1213 req
.keylen
= key
.dsize
;
1215 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1216 ctdb_packet_dump(&req
.hdr
);
1218 iov
[0].iov_base
= &req
;
1219 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1220 iov
[1].iov_base
= key
.dptr
;
1221 iov
[1].iov_len
= key
.dsize
;
1223 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1224 if (nwritten
== -1) {
1225 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1226 cluster_fatal("cluster dispatch daemon msg write error\n");
1229 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1231 if (!NT_STATUS_IS_OK(status
)) {
1232 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1236 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
1237 DEBUG(0, ("received invalid reply\n"));
1238 status
= NT_STATUS_INTERNAL_ERROR
;
1242 status
= NT_STATUS_OK
;
1250 * Fetch a record and parse it
1252 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1253 TDB_DATA key
, bool local_copy
,
1254 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1255 void *private_data
),
1258 struct ctdb_req_call req
;
1259 struct ctdb_req_header
*hdr
= NULL
;
1260 struct ctdb_reply_call
*reply
;
1261 struct iovec iov
[2];
1266 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1270 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1271 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1272 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1273 req
.hdr
.operation
= CTDB_REQ_CALL
;
1274 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1276 req
.callid
= CTDB_FETCH_FUNC
;
1278 req
.keylen
= key
.dsize
;
1280 iov
[0].iov_base
= &req
;
1281 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1282 iov
[1].iov_base
= key
.dptr
;
1283 iov
[1].iov_len
= key
.dsize
;
1285 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1286 if (nwritten
== -1) {
1287 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1288 cluster_fatal("cluster dispatch daemon msg write error\n");
1291 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1293 if (!NT_STATUS_IS_OK(status
)) {
1294 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1298 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
1299 DEBUG(0, ("received invalid reply\n"));
1300 status
= NT_STATUS_INTERNAL_ERROR
;
1303 reply
= (struct ctdb_reply_call
*)hdr
;
1305 if (reply
->datalen
== 0) {
1307 * Treat an empty record as non-existing
1309 status
= NT_STATUS_NOT_FOUND
;
1313 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1316 status
= NT_STATUS_OK
;
1323 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1324 connection to ctdbd to avoid the hairy recursive and async problems with
1328 NTSTATUS
ctdbd_traverse(uint32_t db_id
,
1329 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1330 void *private_data
),
1333 struct ctdbd_connection
*conn
;
1337 struct ctdb_traverse_start t
;
1341 status
= ctdbd_init_connection(NULL
, &conn
);
1343 if (!NT_STATUS_IS_OK(status
)) {
1344 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1345 nt_errstr(status
)));
1350 t
.srvid
= conn
->rand_srvid
;
1351 t
.reqid
= ctdbd_next_reqid(conn
);
1353 data
.dptr
= (uint8_t *)&t
;
1354 data
.dsize
= sizeof(t
);
1356 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1357 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1358 data
, NULL
, NULL
, &cstatus
);
1360 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1362 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1365 if (NT_STATUS_IS_OK(status
)) {
1367 * We need a mapping here
1369 status
= NT_STATUS_UNSUCCESSFUL
;
1376 struct ctdb_req_header
*hdr
= NULL
;
1377 struct ctdb_req_message
*m
;
1378 struct ctdb_rec_data
*d
;
1380 status
= ctdb_read_packet(conn
->fd
, conn
, &hdr
);
1381 if (!NT_STATUS_IS_OK(status
)) {
1382 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1383 nt_errstr(status
)));
1384 cluster_fatal("ctdbd died\n");
1387 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
1388 DEBUG(0, ("Got operation %u, expected a message\n",
1389 (unsigned)hdr
->operation
));
1391 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1394 m
= (struct ctdb_req_message
*)hdr
;
1395 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1396 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1397 DEBUG(0, ("Got invalid traverse data of length %d\n",
1400 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1403 key
.dsize
= d
->keylen
;
1404 key
.dptr
= &d
->data
[0];
1405 data
.dsize
= d
->datalen
;
1406 data
.dptr
= &d
->data
[d
->keylen
];
1408 if (key
.dsize
== 0 && data
.dsize
== 0) {
1409 /* end of traverse */
1411 return NT_STATUS_OK
;
1414 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1415 DEBUG(0, ("Got invalid ltdb header length %d\n",
1418 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1420 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1421 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1424 fn(key
, data
, private_data
);
1427 return NT_STATUS_OK
;
1431 This is used to canonicalize a ctdb_sock_addr structure.
1433 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1434 struct sockaddr_storage
*out
)
1436 memcpy(out
, in
, sizeof (*out
));
1439 if (in
->ss_family
== AF_INET6
) {
1440 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1441 const struct sockaddr_in6
*in6
=
1442 (const struct sockaddr_in6
*)in
;
1443 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1444 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1445 memset(out
, 0, sizeof(*out
));
1446 #ifdef HAVE_SOCK_SIN_LEN
1447 out4
->sin_len
= sizeof(*out
);
1449 out4
->sin_family
= AF_INET
;
1450 out4
->sin_port
= in6
->sin6_port
;
1451 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1458 * Register us as a server for a particular tcp connection
1461 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1462 const struct sockaddr_storage
*_server
,
1463 const struct sockaddr_storage
*_client
,
1464 bool (*release_ip_handler
)(const char *ip_addr
,
1465 void *private_data
),
1468 struct ctdb_control_tcp_addr p
;
1469 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1471 struct sockaddr_storage client
;
1472 struct sockaddr_storage server
;
1475 * Only one connection so far
1477 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1479 smbd_ctdb_canonicalize_ip(_client
, &client
);
1480 smbd_ctdb_canonicalize_ip(_server
, &server
);
1482 switch (client
.ss_family
) {
1484 memcpy(&p
.dest
.ip
, &server
, sizeof(p
.dest
.ip
));
1485 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1488 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1489 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1492 return NT_STATUS_INTERNAL_ERROR
;
1495 conn
->release_ip_handler
= release_ip_handler
;
1496 conn
->release_ip_priv
= private_data
;
1499 * We want to be told about IP releases
1502 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
, NULL
, NULL
);
1503 if (!NT_STATUS_IS_OK(status
)) {
1508 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1509 * can send an extra ack to trigger a reset for our client, so it
1510 * immediately reconnects
1512 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1513 CTDB_CONTROL_TCP_CLIENT
, 0,
1514 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1518 call a control on the local node
1520 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1521 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1522 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1525 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1528 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1530 struct ctdb_client_notify_register reg_data
;
1535 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1537 reg_data
.notify_data
[0] = 0;
1539 struct_len
= offsetof(struct ctdb_client_notify_register
,
1540 notify_data
) + reg_data
.len
;
1542 status
= ctdbd_control_local(
1543 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1544 make_tdb_data((uint8_t *)®_data
, struct_len
),
1545 NULL
, NULL
, &cstatus
);
1546 if (!NT_STATUS_IS_OK(status
)) {
1547 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1548 nt_errstr(status
)));
1553 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1555 struct ctdb_client_notify_deregister dereg_data
;
1559 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1561 status
= ctdbd_control_local(
1562 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1563 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1564 NULL
, NULL
, &cstatus
);
1565 if (!NT_STATUS_IS_OK(status
)) {
1566 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1567 nt_errstr(status
)));
1572 NTSTATUS
ctdbd_probe(void)
1575 * Do a very early check if ctdbd is around to avoid an abort and core
1578 struct ctdbd_connection
*conn
= NULL
;
1581 status
= ctdbd_messaging_connection(talloc_tos(), &conn
);
1584 * We only care if we can connect.