ctdb-vacuum: rename ctdb_vacuum_db_full --> ctdb_vacuum_traverse_db
[Samba.git] / source3 / lib / ctdbd_conn.c
blob4d903248b99c4e60a8b0467223d58a58d56013a8
1 /*
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/>.
21 #include "includes.h"
22 #include "util_tdb.h"
23 #include "serverid.h"
24 #include "ctdbd_conn.h"
26 #ifdef CLUSTER_SUPPORT
28 #include "ctdb_packet.h"
29 #include "messages.h"
32 * It is not possible to include ctdb.h and tdb_compat.h (included via
33 * some other include above) without warnings. This fixes those
34 * warnings.
37 #ifdef typesafe_cb
38 #undef typesafe_cb
39 #endif
41 #ifdef typesafe_cb_preargs
42 #undef typesafe_cb_preargs
43 #endif
45 #ifdef typesafe_cb_postargs
46 #undef typesafe_cb_postargs
47 #endif
49 /* paths to these include files come from --with-ctdb= in configure */
51 #include "ctdb.h"
52 #include "ctdb_private.h"
54 struct ctdbd_connection {
55 struct messaging_context *msg_ctx;
56 uint32_t reqid;
57 uint32_t our_vnn;
58 uint64_t rand_srvid;
59 struct ctdb_packet_context *pkt;
60 struct tevent_fd *fde;
62 bool (*release_ip_handler)(const char *ip_addr, void *private_data);
63 void *release_ip_priv;
66 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
68 conn->reqid += 1;
69 if (conn->reqid == 0) {
70 conn->reqid += 1;
72 return conn->reqid;
75 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
76 uint32_t vnn, uint32_t opcode,
77 uint64_t srvid, uint32_t flags, TDB_DATA data,
78 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
79 int *cstatus);
82 * exit on fatal communications errors with the ctdbd daemon
84 static void cluster_fatal(const char *why)
86 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
87 /* we don't use smb_panic() as we don't want to delay to write
88 a core file. We need to release this process id immediately
89 so that someone else can take over without getting sharing
90 violations */
91 _exit(1);
97 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
99 if (DEBUGLEVEL < 11) {
100 return;
102 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
103 (int)hdr->length, (int)hdr->ctdb_magic,
104 (int)hdr->ctdb_version, (int)hdr->generation,
105 (int)hdr->operation, (int)hdr->reqid));
109 * Register a srvid with ctdbd
111 NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid)
114 int cstatus;
115 return ctdbd_control(conn, CTDB_CURRENT_NODE,
116 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
117 tdb_null, NULL, NULL, &cstatus);
121 * get our vnn from the cluster
123 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
125 int32_t cstatus=-1;
126 NTSTATUS status;
127 status = ctdbd_control(conn,
128 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
129 tdb_null, NULL, NULL, &cstatus);
130 if (!NT_STATUS_IS_OK(status)) {
131 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
132 return status;
134 *vnn = (uint32_t)cstatus;
135 return status;
139 * Are we active (i.e. not banned or stopped?)
141 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
143 int32_t cstatus=-1;
144 NTSTATUS status;
145 TDB_DATA outdata;
146 struct ctdb_node_map *m;
147 uint32_t failure_flags;
148 bool ret = false;
149 int i;
151 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
152 CTDB_CONTROL_GET_NODEMAP, 0, 0,
153 tdb_null, talloc_tos(), &outdata, &cstatus);
154 if (!NT_STATUS_IS_OK(status)) {
155 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
156 return false;
158 if ((cstatus != 0) || (outdata.dptr == NULL)) {
159 DEBUG(2, ("Received invalid ctdb data\n"));
160 return false;
163 m = (struct ctdb_node_map *)outdata.dptr;
165 for (i=0; i<m->num; i++) {
166 if (vnn == m->nodes[i].pnn) {
167 break;
171 if (i == m->num) {
172 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
173 (int)vnn));
174 goto fail;
177 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
178 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
180 if ((m->nodes[i].flags & failure_flags) != 0) {
181 DEBUG(2, ("Node has status %x, not active\n",
182 (int)m->nodes[i].flags));
183 goto fail;
186 ret = true;
187 fail:
188 TALLOC_FREE(outdata.dptr);
189 return ret;
192 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
194 return conn->our_vnn;
198 * Get us a ctdb connection
201 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
202 struct ctdb_packet_context **presult)
204 struct ctdb_packet_context *result;
205 const char *sockname = lp_ctdbd_socket();
206 struct sockaddr_un addr = { 0, };
207 int fd;
208 socklen_t salen;
210 fd = socket(AF_UNIX, SOCK_STREAM, 0);
211 if (fd == -1) {
212 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
213 return map_nt_error_from_unix(errno);
216 addr.sun_family = AF_UNIX;
217 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", sockname);
219 salen = sizeof(struct sockaddr_un);
220 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
221 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
222 strerror(errno)));
223 close(fd);
224 return map_nt_error_from_unix(errno);
227 if (!(result = ctdb_packet_init(mem_ctx, fd))) {
228 close(fd);
229 return NT_STATUS_NO_MEMORY;
232 *presult = result;
233 return NT_STATUS_OK;
237 * Do we have a complete ctdb packet in the queue?
240 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
241 size_t *length,
242 void *private_data)
244 uint32_t msglen;
246 if (available < sizeof(msglen)) {
247 return False;
250 msglen = *((const uint32_t *)buf);
252 DEBUG(11, ("msglen = %d\n", msglen));
254 if (msglen < sizeof(struct ctdb_req_header)) {
255 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
256 "the req_header\n", (int)msglen,
257 (int)sizeof(struct ctdb_req_header)));
258 cluster_fatal("ctdbd protocol error\n");
261 if (available < msglen) {
262 return false;
265 *length = msglen;
266 return true;
270 * State necessary to defer an incoming message while we are waiting for a
271 * ctdb reply.
274 struct deferred_msg_state {
275 struct messaging_context *msg_ctx;
276 struct messaging_rec *rec;
280 * Timed event handler for the deferred message
283 static void deferred_message_dispatch(struct tevent_context *event_ctx,
284 struct tevent_timer *te,
285 struct timeval now,
286 void *private_data)
288 struct deferred_msg_state *state = talloc_get_type_abort(
289 private_data, struct deferred_msg_state);
291 messaging_dispatch_rec(state->msg_ctx, state->rec);
292 TALLOC_FREE(state);
293 TALLOC_FREE(te);
296 struct req_pull_state {
297 TALLOC_CTX *mem_ctx;
298 DATA_BLOB req;
302 * Pull a ctdb request out of the incoming ctdb_packet queue
305 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
306 void *private_data)
308 struct req_pull_state *state = (struct req_pull_state *)private_data;
310 state->req.data = talloc_move(state->mem_ctx, &buf);
311 state->req.length = length;
312 return NT_STATUS_OK;
316 * Fetch a messaging_rec from an incoming ctdb style message
319 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
320 size_t overall_length,
321 struct ctdb_req_message *msg)
323 struct messaging_rec *result;
324 DATA_BLOB blob;
325 enum ndr_err_code ndr_err;
327 if ((overall_length < offsetof(struct ctdb_req_message, data))
328 || (overall_length
329 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
331 cluster_fatal("got invalid msg length");
334 if (!(result = talloc(mem_ctx, struct messaging_rec))) {
335 DEBUG(0, ("talloc failed\n"));
336 return NULL;
339 blob = data_blob_const(msg->data, msg->datalen);
341 ndr_err = ndr_pull_struct_blob(
342 &blob, result, result,
343 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
345 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
346 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
347 ndr_errstr(ndr_err)));
348 TALLOC_FREE(result);
349 return NULL;
352 if (DEBUGLEVEL >= 11) {
353 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
354 NDR_PRINT_DEBUG(messaging_rec, result);
357 return result;
360 static NTSTATUS ctdb_packet_fd_read_sync(struct ctdb_packet_context *ctx)
362 int timeout = lp_ctdb_timeout();
364 if (timeout == 0) {
365 timeout = -1;
367 return ctdb_packet_fd_read_sync_timeout(ctx, timeout);
371 * Read a full ctdbd request. If we have a messaging context, defer incoming
372 * messages that might come in between.
375 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
376 TALLOC_CTX *mem_ctx, void *result)
378 struct ctdb_req_header *hdr;
379 struct req_pull_state state;
380 NTSTATUS status;
382 next_pkt:
383 ZERO_STRUCT(state);
384 state.mem_ctx = mem_ctx;
386 while (!ctdb_packet_handler(conn->pkt, ctdb_req_complete,
387 ctdb_req_pull, &state, &status)) {
389 * Not enough data
391 status = ctdb_packet_fd_read_sync(conn->pkt);
393 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
394 /* EAGAIN */
395 continue;
396 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
397 /* EAGAIN */
398 continue;
401 if (!NT_STATUS_IS_OK(status)) {
402 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
403 cluster_fatal("ctdbd died\n");
407 if (!NT_STATUS_IS_OK(status)) {
408 DEBUG(0, ("Could not read ctdb_packet: %s\n", nt_errstr(status)));
409 cluster_fatal("ctdbd died\n");
412 hdr = (struct ctdb_req_header *)state.req.data;
414 DEBUG(11, ("Received ctdb packet\n"));
415 ctdb_packet_dump(hdr);
417 if (hdr->operation == CTDB_REQ_MESSAGE) {
418 struct tevent_timer *evt;
419 struct deferred_msg_state *msg_state;
420 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
422 if (conn->msg_ctx == NULL) {
423 DEBUG(1, ("Got a message without having a msg ctx, "
424 "dropping msg %llu\n",
425 (long long unsigned)msg->srvid));
426 goto next_pkt;
429 if ((conn->release_ip_handler != NULL)
430 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
431 bool ret;
433 /* must be dispatched immediately */
434 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
435 ret = conn->release_ip_handler((const char *)msg->data,
436 conn->release_ip_priv);
437 if (ret) {
439 * We need to release the ip,
440 * so return an error to the upper layers.
442 * We make sure we don't trigger this again.
444 conn->release_ip_handler = NULL;
445 conn->release_ip_priv = NULL;
446 return NT_STATUS_ADDRESS_CLOSED;
448 TALLOC_FREE(hdr);
449 goto next_pkt;
452 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
453 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
455 DEBUG(1, ("ctdb_read_req: Got %s message\n",
456 (msg->srvid == CTDB_SRVID_RECONFIGURE)
457 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
459 messaging_send(conn->msg_ctx,
460 messaging_server_id(conn->msg_ctx),
461 MSG_SMB_BRL_VALIDATE, &data_blob_null);
462 messaging_send(conn->msg_ctx,
463 messaging_server_id(conn->msg_ctx),
464 MSG_DBWRAP_G_LOCK_RETRY,
465 &data_blob_null);
466 TALLOC_FREE(hdr);
467 goto next_pkt;
470 msg_state = talloc(NULL, struct deferred_msg_state);
471 if (msg_state == NULL) {
472 DEBUG(0, ("talloc failed\n"));
473 TALLOC_FREE(hdr);
474 goto next_pkt;
477 if (!(msg_state->rec = ctdb_pull_messaging_rec(
478 msg_state, state.req.length, msg))) {
479 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
480 TALLOC_FREE(msg_state);
481 TALLOC_FREE(hdr);
482 goto next_pkt;
485 TALLOC_FREE(hdr);
487 msg_state->msg_ctx = conn->msg_ctx;
490 * We're waiting for a call reply, but an async message has
491 * crossed. Defer dispatching to the toplevel event loop.
493 evt = tevent_add_timer(conn->msg_ctx->event_ctx,
494 conn->msg_ctx->event_ctx,
495 timeval_zero(),
496 deferred_message_dispatch,
497 msg_state);
498 if (evt == NULL) {
499 DEBUG(0, ("event_add_timed failed\n"));
500 TALLOC_FREE(msg_state);
501 TALLOC_FREE(hdr);
502 goto next_pkt;
505 goto next_pkt;
508 if ((reqid != 0) && (hdr->reqid != reqid)) {
509 /* we got the wrong reply */
510 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
511 "been %u\n", hdr->reqid, reqid));
512 TALLOC_FREE(hdr);
513 goto next_pkt;
516 *((void **)result) = talloc_move(mem_ctx, &hdr);
518 return NT_STATUS_OK;
522 * Get us a ctdbd connection
525 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
526 struct ctdbd_connection **pconn)
528 struct ctdbd_connection *conn;
529 NTSTATUS status;
531 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
532 DEBUG(0, ("talloc failed\n"));
533 return NT_STATUS_NO_MEMORY;
536 status = ctdbd_connect(conn, &conn->pkt);
538 if (!NT_STATUS_IS_OK(status)) {
539 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
540 goto fail;
543 status = get_cluster_vnn(conn, &conn->our_vnn);
545 if (!NT_STATUS_IS_OK(status)) {
546 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
547 goto fail;
550 if (!ctdbd_working(conn, conn->our_vnn)) {
551 DEBUG(2, ("Node is not working, can not connect\n"));
552 status = NT_STATUS_INTERNAL_DB_ERROR;
553 goto fail;
556 generate_random_buffer((unsigned char *)&conn->rand_srvid,
557 sizeof(conn->rand_srvid));
559 status = register_with_ctdbd(conn, conn->rand_srvid);
561 if (!NT_STATUS_IS_OK(status)) {
562 DEBUG(5, ("Could not register random srvid: %s\n",
563 nt_errstr(status)));
564 goto fail;
567 *pconn = conn;
568 return NT_STATUS_OK;
570 fail:
571 TALLOC_FREE(conn);
572 return status;
576 * Get us a ctdbd connection and register us as a process
579 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
580 struct ctdbd_connection **pconn)
582 struct ctdbd_connection *conn;
583 NTSTATUS status;
585 status = ctdbd_init_connection(mem_ctx, &conn);
587 if (!NT_STATUS_IS_OK(status)) {
588 return status;
591 status = register_with_ctdbd(conn, (uint64_t)getpid());
592 if (!NT_STATUS_IS_OK(status)) {
593 goto fail;
596 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
597 if (!NT_STATUS_IS_OK(status)) {
598 goto fail;
601 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
602 if (!NT_STATUS_IS_OK(status)) {
603 goto fail;
606 *pconn = conn;
607 return NT_STATUS_OK;
609 fail:
610 TALLOC_FREE(conn);
611 return status;
614 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
616 return conn->msg_ctx;
619 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
621 return ctdb_packet_get_fd(conn->pkt);
625 * Packet handler to receive and handle a ctdb message
627 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
628 void *private_data)
630 struct ctdbd_connection *conn = talloc_get_type_abort(
631 private_data, struct ctdbd_connection);
632 struct ctdb_req_message *msg;
633 struct messaging_rec *msg_rec;
635 msg = (struct ctdb_req_message *)buf;
637 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
638 DEBUG(0, ("Received async msg of type %u, discarding\n",
639 msg->hdr.operation));
640 TALLOC_FREE(buf);
641 return NT_STATUS_INVALID_PARAMETER;
644 if ((conn->release_ip_handler != NULL)
645 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
646 bool ret;
648 /* must be dispatched immediately */
649 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
650 ret = conn->release_ip_handler((const char *)msg->data,
651 conn->release_ip_priv);
652 if (ret) {
654 * We need to release the ip.
656 * We make sure we don't trigger this again.
658 conn->release_ip_handler = NULL;
659 conn->release_ip_priv = NULL;
661 TALLOC_FREE(buf);
662 return NT_STATUS_OK;
665 SMB_ASSERT(conn->msg_ctx != NULL);
667 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
668 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
669 DEBUG(0,("Got cluster reconfigure message\n"));
671 * when the cluster is reconfigured or someone of the
672 * family has passed away (SAMBA_NOTIFY), we need to
673 * clean the brl database
675 messaging_send(conn->msg_ctx,
676 messaging_server_id(conn->msg_ctx),
677 MSG_SMB_BRL_VALIDATE, &data_blob_null);
679 messaging_send(conn->msg_ctx,
680 messaging_server_id(conn->msg_ctx),
681 MSG_DBWRAP_G_LOCK_RETRY,
682 &data_blob_null);
684 TALLOC_FREE(buf);
685 return NT_STATUS_OK;
688 /* only messages to our pid or the broadcast are valid here */
689 if (msg->srvid != getpid() && msg->srvid != MSG_SRVID_SAMBA) {
690 DEBUG(0,("Got unexpected message with srvid=%llu\n",
691 (unsigned long long)msg->srvid));
692 TALLOC_FREE(buf);
693 return NT_STATUS_OK;
696 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
697 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
698 TALLOC_FREE(buf);
699 return NT_STATUS_NO_MEMORY;
702 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
704 TALLOC_FREE(msg_rec);
705 TALLOC_FREE(buf);
706 return NT_STATUS_OK;
710 * The ctdbd socket is readable asynchronuously
713 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
714 struct tevent_fd *event,
715 uint16 flags,
716 void *private_data)
718 struct ctdbd_connection *conn = talloc_get_type_abort(
719 private_data, struct ctdbd_connection);
721 NTSTATUS status;
723 status = ctdb_packet_fd_read(conn->pkt);
725 if (!NT_STATUS_IS_OK(status)) {
726 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
727 cluster_fatal("ctdbd died\n");
730 while (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
731 ctdb_handle_message, conn, &status)) {
732 if (!NT_STATUS_IS_OK(status)) {
733 DEBUG(10, ("could not handle incoming message: %s\n",
734 nt_errstr(status)));
740 * Prepare a ctdbd connection to receive messages
743 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
744 struct messaging_context *msg_ctx)
746 SMB_ASSERT(conn->msg_ctx == NULL);
747 SMB_ASSERT(conn->fde == NULL);
749 if (!(conn->fde = tevent_add_fd(msg_ctx->event_ctx, conn,
750 ctdb_packet_get_fd(conn->pkt),
751 TEVENT_FD_READ,
752 ctdbd_socket_handler,
753 conn))) {
754 DEBUG(0, ("event_add_fd failed\n"));
755 return NT_STATUS_NO_MEMORY;
758 conn->msg_ctx = msg_ctx;
760 return NT_STATUS_OK;
764 * Send a messaging message across a ctdbd
767 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
768 uint32_t dst_vnn, uint64_t dst_srvid,
769 struct messaging_rec *msg)
771 DATA_BLOB blob;
772 NTSTATUS status;
773 enum ndr_err_code ndr_err;
775 ndr_err = ndr_push_struct_blob(
776 &blob, talloc_tos(), msg,
777 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
779 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
780 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
781 ndr_errstr(ndr_err)));
782 return ndr_map_error2ntstatus(ndr_err);
785 status = ctdbd_messaging_send_blob(conn, dst_vnn, dst_srvid,
786 blob.data, blob.length);
787 TALLOC_FREE(blob.data);
788 return status;
791 NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
792 uint32_t dst_vnn, uint64_t dst_srvid,
793 const uint8_t *buf, size_t buflen)
795 struct ctdb_req_message r;
796 NTSTATUS status;
798 r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
799 r.hdr.ctdb_magic = CTDB_MAGIC;
800 r.hdr.ctdb_version = CTDB_VERSION;
801 r.hdr.generation = 1;
802 r.hdr.operation = CTDB_REQ_MESSAGE;
803 r.hdr.destnode = dst_vnn;
804 r.hdr.srcnode = conn->our_vnn;
805 r.hdr.reqid = 0;
806 r.srvid = dst_srvid;
807 r.datalen = buflen;
809 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
810 ctdb_packet_dump(&r.hdr);
812 status = ctdb_packet_send(
813 conn->pkt, 2,
814 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
815 data_blob_const(buf, buflen));
817 if (!NT_STATUS_IS_OK(status)) {
818 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
819 return status;
822 status = ctdb_packet_flush(conn->pkt);
823 if (!NT_STATUS_IS_OK(status)) {
824 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
825 cluster_fatal("cluster dispatch daemon msg write error\n");
827 return NT_STATUS_OK;
831 * send/recv a generic ctdb control message
833 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
834 uint32_t vnn, uint32_t opcode,
835 uint64_t srvid, uint32_t flags,
836 TDB_DATA data,
837 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
838 int *cstatus)
840 struct ctdb_req_control req;
841 struct ctdb_reply_control *reply = NULL;
842 struct ctdbd_connection *new_conn = NULL;
843 NTSTATUS status;
845 if (conn == NULL) {
846 status = ctdbd_init_connection(NULL, &new_conn);
848 if (!NT_STATUS_IS_OK(status)) {
849 DEBUG(10, ("Could not init temp connection: %s\n",
850 nt_errstr(status)));
851 goto fail;
854 conn = new_conn;
857 ZERO_STRUCT(req);
858 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
859 req.hdr.ctdb_magic = CTDB_MAGIC;
860 req.hdr.ctdb_version = CTDB_VERSION;
861 req.hdr.operation = CTDB_REQ_CONTROL;
862 req.hdr.reqid = ctdbd_next_reqid(conn);
863 req.hdr.destnode = vnn;
864 req.opcode = opcode;
865 req.srvid = srvid;
866 req.datalen = data.dsize;
867 req.flags = flags;
869 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
870 ctdb_packet_dump(&req.hdr);
872 status = ctdb_packet_send(
873 conn->pkt, 2,
874 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
875 data_blob_const(data.dptr, data.dsize));
877 if (!NT_STATUS_IS_OK(status)) {
878 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
879 goto fail;
882 status = ctdb_packet_flush(conn->pkt);
884 if (!NT_STATUS_IS_OK(status)) {
885 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
886 cluster_fatal("cluster dispatch daemon control write error\n");
889 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
890 TALLOC_FREE(new_conn);
891 if (cstatus) {
892 *cstatus = 0;
894 return NT_STATUS_OK;
897 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
899 if (!NT_STATUS_IS_OK(status)) {
900 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
901 goto fail;
904 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
905 DEBUG(0, ("received invalid reply\n"));
906 goto fail;
909 if (outdata) {
910 if (!(outdata->dptr = (uint8 *)talloc_memdup(
911 mem_ctx, reply->data, reply->datalen))) {
912 TALLOC_FREE(reply);
913 return NT_STATUS_NO_MEMORY;
915 outdata->dsize = reply->datalen;
917 if (cstatus) {
918 (*cstatus) = reply->status;
921 status = NT_STATUS_OK;
923 fail:
924 TALLOC_FREE(new_conn);
925 TALLOC_FREE(reply);
926 return status;
930 * see if a remote process exists
932 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
934 struct server_id id;
935 bool result;
937 id.pid = pid;
938 id.vnn = vnn;
940 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
941 DEBUG(10, ("ctdb_processes_exist failed\n"));
942 return false;
944 return result;
947 bool ctdb_processes_exist(struct ctdbd_connection *conn,
948 const struct server_id *pids, int num_pids,
949 bool *results)
951 TALLOC_CTX *frame = talloc_stackframe();
952 int i, num_received;
953 NTSTATUS status;
954 uint32_t *reqids;
955 bool result = false;
957 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
958 if (reqids == NULL) {
959 goto fail;
962 for (i=0; i<num_pids; i++) {
963 struct ctdb_req_control req;
964 pid_t pid;
966 results[i] = false;
967 reqids[i] = ctdbd_next_reqid(conn);
969 ZERO_STRUCT(req);
972 * pids[i].pid is uint64_t, scale down to pid_t which
973 * is the wire protocol towards ctdb.
975 pid = pids[i].pid;
977 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
978 (int)pids[i].vnn, (int)pid,
979 (int)reqids[i]));
981 req.hdr.length = offsetof(struct ctdb_req_control, data);
982 req.hdr.length += sizeof(pid);
983 req.hdr.ctdb_magic = CTDB_MAGIC;
984 req.hdr.ctdb_version = CTDB_VERSION;
985 req.hdr.operation = CTDB_REQ_CONTROL;
986 req.hdr.reqid = reqids[i];
987 req.hdr.destnode = pids[i].vnn;
988 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
989 req.srvid = 0;
990 req.datalen = sizeof(pid);
991 req.flags = 0;
993 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
994 ctdb_packet_dump(&req.hdr);
996 status = ctdb_packet_send(
997 conn->pkt, 2,
998 data_blob_const(
999 &req, offsetof(struct ctdb_req_control, data)),
1000 data_blob_const(&pid, sizeof(pid)));
1001 if (!NT_STATUS_IS_OK(status)) {
1002 DEBUG(10, ("ctdb_packet_send failed: %s\n",
1003 nt_errstr(status)));
1004 goto fail;
1008 status = ctdb_packet_flush(conn->pkt);
1009 if (!NT_STATUS_IS_OK(status)) {
1010 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
1011 nt_errstr(status)));
1012 goto fail;
1015 num_received = 0;
1017 while (num_received < num_pids) {
1018 struct ctdb_reply_control *reply = NULL;
1019 uint32_t reqid;
1021 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 DEBUG(10, ("ctdb_read_req failed: %s\n",
1024 nt_errstr(status)));
1025 goto fail;
1028 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1029 DEBUG(10, ("Received invalid reply\n"));
1030 goto fail;
1033 reqid = reply->hdr.reqid;
1035 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1037 for (i=0; i<num_pids; i++) {
1038 if (reqid == reqids[i]) {
1039 break;
1042 if (i == num_pids) {
1043 DEBUG(10, ("Received unknown record number %u\n",
1044 (unsigned)reqid));
1045 goto fail;
1047 results[i] = ((reply->status) == 0);
1048 TALLOC_FREE(reply);
1049 num_received += 1;
1052 result = true;
1053 fail:
1054 TALLOC_FREE(frame);
1055 return result;
1058 struct ctdb_vnn_list {
1059 uint32_t vnn;
1060 uint32_t reqid;
1061 unsigned num_srvids;
1062 unsigned num_filled;
1063 uint64_t *srvids;
1064 unsigned *pid_indexes;
1068 * Get a list of all vnns mentioned in a list of
1069 * server_ids. vnn_indexes tells where in the vnns array we have to
1070 * place the pids.
1072 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
1073 const struct server_id *pids, unsigned num_pids,
1074 struct ctdb_vnn_list **pvnns,
1075 unsigned *pnum_vnns)
1077 struct ctdb_vnn_list *vnns = NULL;
1078 unsigned *vnn_indexes = NULL;
1079 unsigned i, num_vnns = 0;
1081 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
1082 if (vnn_indexes == NULL) {
1083 DEBUG(1, ("talloc_array failed\n"));
1084 goto fail;
1087 for (i=0; i<num_pids; i++) {
1088 unsigned j;
1089 uint32_t vnn = pids[i].vnn;
1091 for (j=0; j<num_vnns; j++) {
1092 if (vnn == vnns[j].vnn) {
1093 break;
1096 vnn_indexes[i] = j;
1098 if (j < num_vnns) {
1100 * Already in the array
1102 vnns[j].num_srvids += 1;
1103 continue;
1105 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
1106 num_vnns+1);
1107 if (vnns == NULL) {
1108 DEBUG(1, ("talloc_realloc failed\n"));
1109 goto fail;
1111 vnns[num_vnns].vnn = vnn;
1112 vnns[num_vnns].num_srvids = 1;
1113 vnns[num_vnns].num_filled = 0;
1114 num_vnns += 1;
1116 for (i=0; i<num_vnns; i++) {
1117 struct ctdb_vnn_list *vnn = &vnns[i];
1119 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
1120 if (vnn->srvids == NULL) {
1121 DEBUG(1, ("talloc_array failed\n"));
1122 goto fail;
1124 vnn->pid_indexes = talloc_array(vnns, unsigned,
1125 vnn->num_srvids);
1126 if (vnn->pid_indexes == NULL) {
1127 DEBUG(1, ("talloc_array failed\n"));
1128 goto fail;
1131 for (i=0; i<num_pids; i++) {
1132 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
1133 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
1134 vnn->pid_indexes[vnn->num_filled] = i;
1135 vnn->num_filled += 1;
1138 TALLOC_FREE(vnn_indexes);
1139 *pvnns = vnns;
1140 *pnum_vnns = num_vnns;
1141 return true;
1142 fail:
1143 TALLOC_FREE(vnns);
1144 TALLOC_FREE(vnn_indexes);
1145 return false;
1148 #ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1150 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
1151 const struct server_id *pids, unsigned num_pids,
1152 bool *results)
1154 unsigned i, num_received;
1155 NTSTATUS status;
1156 struct ctdb_vnn_list *vnns = NULL;
1157 unsigned num_vnns;
1158 bool result = false;
1160 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1161 &vnns, &num_vnns)) {
1162 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1163 goto fail;
1166 for (i=0; i<num_vnns; i++) {
1167 struct ctdb_vnn_list *vnn = &vnns[i];
1168 struct ctdb_req_control req;
1170 vnn->reqid = ctdbd_next_reqid(conn);
1172 ZERO_STRUCT(req);
1174 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1175 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1177 req.hdr.length = offsetof(struct ctdb_req_control, data);
1178 req.hdr.ctdb_magic = CTDB_MAGIC;
1179 req.hdr.ctdb_version = CTDB_VERSION;
1180 req.hdr.operation = CTDB_REQ_CONTROL;
1181 req.hdr.reqid = vnn->reqid;
1182 req.hdr.destnode = vnn->vnn;
1183 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1184 req.srvid = 0;
1185 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1186 req.hdr.length += req.datalen;
1187 req.flags = 0;
1189 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1190 ctdb_packet_dump(&req.hdr);
1192 status = ctdb_packet_send(
1193 conn->pkt, 2,
1194 data_blob_const(
1195 &req, offsetof(struct ctdb_req_control,
1196 data)),
1197 data_blob_const(vnn->srvids, req.datalen));
1198 if (!NT_STATUS_IS_OK(status)) {
1199 DEBUG(1, ("ctdb_packet_send failed: %s\n",
1200 nt_errstr(status)));
1201 goto fail;
1205 status = ctdb_packet_flush(conn->pkt);
1206 if (!NT_STATUS_IS_OK(status)) {
1207 DEBUG(1, ("ctdb_packet_flush failed: %s\n",
1208 nt_errstr(status)));
1209 goto fail;
1212 num_received = 0;
1214 while (num_received < num_vnns) {
1215 struct ctdb_reply_control *reply = NULL;
1216 struct ctdb_vnn_list *vnn;
1217 uint32_t reqid;
1218 uint8_t *reply_data;
1220 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 DEBUG(1, ("ctdb_read_req failed: %s\n",
1223 nt_errstr(status)));
1224 goto fail;
1227 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1228 DEBUG(1, ("Received invalid reply %u\n",
1229 (unsigned)reply->hdr.operation));
1230 goto fail;
1233 reqid = reply->hdr.reqid;
1235 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1237 for (i=0; i<num_vnns; i++) {
1238 if (reqid == vnns[i].reqid) {
1239 break;
1242 if (i == num_vnns) {
1243 DEBUG(1, ("Received unknown reqid number %u\n",
1244 (unsigned)reqid));
1245 goto fail;
1248 DEBUG(10, ("Found index %u\n", i));
1250 vnn = &vnns[i];
1252 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1253 (unsigned)vnn->vnn, vnn->num_srvids,
1254 (unsigned)reply->datalen));
1256 if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
1258 * Got a real reply
1260 reply_data = reply->data;
1261 } else {
1263 * Got an error reply
1265 DEBUG(5, ("Received short reply len %d, status %u, "
1266 "errorlen %u\n",
1267 (unsigned)reply->datalen,
1268 (unsigned)reply->status,
1269 (unsigned)reply->errorlen));
1270 dump_data(5, reply->data, reply->errorlen);
1273 * This will trigger everything set to false
1275 reply_data = NULL;
1278 for (i=0; i<vnn->num_srvids; i++) {
1279 int idx = vnn->pid_indexes[i];
1281 if (pids[i].unique_id ==
1282 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1283 results[idx] = true;
1284 continue;
1286 results[idx] =
1287 (reply_data != NULL) &&
1288 ((reply_data[i/8] & (1<<(i%8))) != 0);
1291 TALLOC_FREE(reply);
1292 num_received += 1;
1295 result = true;
1296 fail:
1297 TALLOC_FREE(vnns);
1298 return result;
1301 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1304 * Get a db path
1306 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1307 TALLOC_CTX *mem_ctx, uint32_t db_id)
1309 NTSTATUS status;
1310 TDB_DATA data;
1311 int32_t cstatus;
1313 data.dptr = (uint8_t*)&db_id;
1314 data.dsize = sizeof(db_id);
1316 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1317 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1318 mem_ctx, &data, &cstatus);
1319 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1320 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1321 return NULL;
1324 return (char *)data.dptr;
1328 * attach to a ctdb database
1330 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1331 const char *name, uint32_t *db_id, int tdb_flags)
1333 NTSTATUS status;
1334 TDB_DATA data;
1335 int32_t cstatus;
1336 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1338 data = string_term_tdb_data(name);
1340 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1341 persistent
1342 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1343 : CTDB_CONTROL_DB_ATTACH,
1344 tdb_flags, 0, data, NULL, &data, &cstatus);
1345 if (!NT_STATUS_IS_OK(status)) {
1346 DEBUG(0, (__location__ " ctdb_control for db_attach "
1347 "failed: %s\n", nt_errstr(status)));
1348 return status;
1351 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1352 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1353 return NT_STATUS_INTERNAL_ERROR;
1356 *db_id = *(uint32_t *)data.dptr;
1357 talloc_free(data.dptr);
1359 if (!(tdb_flags & TDB_SEQNUM)) {
1360 return NT_STATUS_OK;
1363 data.dptr = (uint8_t *)db_id;
1364 data.dsize = sizeof(*db_id);
1366 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1367 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1368 NULL, NULL, &cstatus);
1369 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1370 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1371 "failed\n"));
1372 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1373 status;
1376 return NT_STATUS_OK;
1380 * force the migration of a record to this node
1382 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
1383 TDB_DATA key)
1385 struct ctdb_req_call req;
1386 struct ctdb_reply_call *reply;
1387 NTSTATUS status;
1389 ZERO_STRUCT(req);
1391 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1392 req.hdr.ctdb_magic = CTDB_MAGIC;
1393 req.hdr.ctdb_version = CTDB_VERSION;
1394 req.hdr.operation = CTDB_REQ_CALL;
1395 req.hdr.reqid = ctdbd_next_reqid(conn);
1396 req.flags = CTDB_IMMEDIATE_MIGRATION;
1397 req.callid = CTDB_NULL_FUNC;
1398 req.db_id = db_id;
1399 req.keylen = key.dsize;
1401 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1402 ctdb_packet_dump(&req.hdr);
1404 status = ctdb_packet_send(
1405 conn->pkt, 2,
1406 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1407 data_blob_const(key.dptr, key.dsize));
1409 if (!NT_STATUS_IS_OK(status)) {
1410 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1411 return status;
1414 status = ctdb_packet_flush(conn->pkt);
1416 if (!NT_STATUS_IS_OK(status)) {
1417 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1418 cluster_fatal("cluster dispatch daemon control write error\n");
1421 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1423 if (!NT_STATUS_IS_OK(status)) {
1424 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1425 goto fail;
1428 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1429 DEBUG(0, ("received invalid reply\n"));
1430 status = NT_STATUS_INTERNAL_ERROR;
1431 goto fail;
1434 status = NT_STATUS_OK;
1435 fail:
1437 TALLOC_FREE(reply);
1438 return status;
1442 * Fetch a record and parse it
1444 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
1445 TDB_DATA key, bool local_copy,
1446 void (*parser)(TDB_DATA key, TDB_DATA data,
1447 void *private_data),
1448 void *private_data)
1450 struct ctdb_req_call req;
1451 struct ctdb_reply_call *reply;
1452 NTSTATUS status;
1453 uint32_t flags;
1455 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1456 flags = local_copy ? CTDB_WANT_READONLY : 0;
1457 #else
1458 flags = 0;
1459 #endif
1461 ZERO_STRUCT(req);
1463 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1464 req.hdr.ctdb_magic = CTDB_MAGIC;
1465 req.hdr.ctdb_version = CTDB_VERSION;
1466 req.hdr.operation = CTDB_REQ_CALL;
1467 req.hdr.reqid = ctdbd_next_reqid(conn);
1468 req.flags = flags;
1469 req.callid = CTDB_FETCH_FUNC;
1470 req.db_id = db_id;
1471 req.keylen = key.dsize;
1473 status = ctdb_packet_send(
1474 conn->pkt, 2,
1475 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1476 data_blob_const(key.dptr, key.dsize));
1478 if (!NT_STATUS_IS_OK(status)) {
1479 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1480 return status;
1483 status = ctdb_packet_flush(conn->pkt);
1485 if (!NT_STATUS_IS_OK(status)) {
1486 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1487 cluster_fatal("cluster dispatch daemon control write error\n");
1490 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1492 if (!NT_STATUS_IS_OK(status)) {
1493 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1494 goto fail;
1497 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1498 DEBUG(0, ("received invalid reply\n"));
1499 status = NT_STATUS_INTERNAL_ERROR;
1500 goto fail;
1503 if (reply->datalen == 0) {
1505 * Treat an empty record as non-existing
1507 status = NT_STATUS_NOT_FOUND;
1508 goto fail;
1511 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1512 private_data);
1514 status = NT_STATUS_OK;
1515 fail:
1516 TALLOC_FREE(reply);
1517 return status;
1520 struct ctdbd_traverse_state {
1521 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1522 void *private_data;
1526 * Handle a traverse record coming in on the ctdbd connection
1529 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1530 void *private_data)
1532 struct ctdbd_traverse_state *state =
1533 (struct ctdbd_traverse_state *)private_data;
1535 struct ctdb_req_message *m;
1536 struct ctdb_rec_data *d;
1537 TDB_DATA key, data;
1539 m = (struct ctdb_req_message *)buf;
1541 if (length < sizeof(*m) || m->hdr.length != length) {
1542 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1543 TALLOC_FREE(buf);
1544 return NT_STATUS_UNEXPECTED_IO_ERROR;
1547 d = (struct ctdb_rec_data *)&m->data[0];
1548 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1549 DEBUG(0, ("Got invalid traverse data of length %d\n",
1550 (int)m->datalen));
1551 TALLOC_FREE(buf);
1552 return NT_STATUS_UNEXPECTED_IO_ERROR;
1555 key.dsize = d->keylen;
1556 key.dptr = &d->data[0];
1557 data.dsize = d->datalen;
1558 data.dptr = &d->data[d->keylen];
1560 if (key.dsize == 0 && data.dsize == 0) {
1561 /* end of traverse */
1562 return NT_STATUS_END_OF_FILE;
1565 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1566 DEBUG(0, ("Got invalid ltdb header length %d\n",
1567 (int)data.dsize));
1568 TALLOC_FREE(buf);
1569 return NT_STATUS_UNEXPECTED_IO_ERROR;
1571 data.dsize -= sizeof(struct ctdb_ltdb_header);
1572 data.dptr += sizeof(struct ctdb_ltdb_header);
1574 if (state->fn) {
1575 state->fn(key, data, state->private_data);
1578 TALLOC_FREE(buf);
1579 return NT_STATUS_OK;
1583 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1584 connection to ctdbd to avoid the hairy recursive and async problems with
1585 everything in-line.
1588 NTSTATUS ctdbd_traverse(uint32_t db_id,
1589 void (*fn)(TDB_DATA key, TDB_DATA data,
1590 void *private_data),
1591 void *private_data)
1593 struct ctdbd_connection *conn;
1594 NTSTATUS status;
1596 TDB_DATA data;
1597 struct ctdb_traverse_start t;
1598 int cstatus;
1599 struct ctdbd_traverse_state state;
1601 become_root();
1602 status = ctdbd_init_connection(NULL, &conn);
1603 unbecome_root();
1604 if (!NT_STATUS_IS_OK(status)) {
1605 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1606 nt_errstr(status)));
1607 return status;
1610 t.db_id = db_id;
1611 t.srvid = conn->rand_srvid;
1612 t.reqid = ctdbd_next_reqid(conn);
1614 data.dptr = (uint8_t *)&t;
1615 data.dsize = sizeof(t);
1617 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1618 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1619 data, NULL, NULL, &cstatus);
1621 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1623 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1624 cstatus));
1626 if (NT_STATUS_IS_OK(status)) {
1628 * We need a mapping here
1630 status = NT_STATUS_UNSUCCESSFUL;
1632 goto done;
1635 state.fn = fn;
1636 state.private_data = private_data;
1638 while (True) {
1640 status = NT_STATUS_OK;
1642 if (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
1643 ctdb_traverse_handler, &state, &status)) {
1645 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1646 status = NT_STATUS_OK;
1647 break;
1651 * There might be more in the queue
1653 continue;
1656 if (!NT_STATUS_IS_OK(status)) {
1657 break;
1660 status = ctdb_packet_fd_read_sync(conn->pkt);
1662 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1664 * There might be more in the queue
1666 continue;
1669 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1670 status = NT_STATUS_OK;
1671 break;
1674 if (!NT_STATUS_IS_OK(status)) {
1675 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1676 cluster_fatal("ctdbd died\n");
1680 done:
1681 TALLOC_FREE(conn);
1682 return status;
1686 This is used to canonicalize a ctdb_sock_addr structure.
1688 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1689 struct sockaddr_storage *out)
1691 memcpy(out, in, sizeof (*out));
1693 #ifdef HAVE_IPV6
1694 if (in->ss_family == AF_INET6) {
1695 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1696 const struct sockaddr_in6 *in6 =
1697 (const struct sockaddr_in6 *)in;
1698 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1699 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1700 memset(out, 0, sizeof(*out));
1701 #ifdef HAVE_SOCK_SIN_LEN
1702 out4->sin_len = sizeof(*out);
1703 #endif
1704 out4->sin_family = AF_INET;
1705 out4->sin_port = in6->sin6_port;
1706 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1709 #endif
1713 * Register us as a server for a particular tcp connection
1716 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1717 const struct sockaddr_storage *_server,
1718 const struct sockaddr_storage *_client,
1719 bool (*release_ip_handler)(const char *ip_addr,
1720 void *private_data),
1721 void *private_data)
1724 * we still use ctdb_control_tcp for ipv4
1725 * because we want to work against older ctdb
1726 * versions at runtime
1728 struct ctdb_control_tcp p4;
1729 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1730 struct ctdb_control_tcp_addr p;
1731 #endif
1732 TDB_DATA data;
1733 NTSTATUS status;
1734 struct sockaddr_storage client;
1735 struct sockaddr_storage server;
1738 * Only one connection so far
1740 SMB_ASSERT(conn->release_ip_handler == NULL);
1742 smbd_ctdb_canonicalize_ip(_client, &client);
1743 smbd_ctdb_canonicalize_ip(_server, &server);
1745 switch (client.ss_family) {
1746 case AF_INET:
1747 memcpy(&p4.dest, &server, sizeof(p4.dest));
1748 memcpy(&p4.src, &client, sizeof(p4.src));
1749 data.dptr = (uint8_t *)&p4;
1750 data.dsize = sizeof(p4);
1751 break;
1752 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1753 case AF_INET6:
1754 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1755 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1756 data.dptr = (uint8_t *)&p;
1757 data.dsize = sizeof(p);
1758 break;
1759 #endif
1760 default:
1761 return NT_STATUS_INTERNAL_ERROR;
1764 conn->release_ip_handler = release_ip_handler;
1765 conn->release_ip_priv = private_data;
1768 * We want to be told about IP releases
1771 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1772 if (!NT_STATUS_IS_OK(status)) {
1773 return status;
1777 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1778 * can send an extra ack to trigger a reset for our client, so it
1779 * immediately reconnects
1781 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1782 CTDB_CONTROL_TCP_CLIENT, 0,
1783 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1787 * We want to handle reconfigure events
1789 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1791 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1795 call a control on the local node
1797 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1798 uint64_t srvid, uint32_t flags, TDB_DATA data,
1799 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1800 int *cstatus)
1802 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1805 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1807 struct ctdb_client_notify_register reg_data;
1808 size_t struct_len;
1809 NTSTATUS status;
1810 int cstatus;
1812 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1813 reg_data.len = 1;
1814 reg_data.notify_data[0] = 0;
1816 struct_len = offsetof(struct ctdb_client_notify_register,
1817 notify_data) + reg_data.len;
1819 status = ctdbd_control_local(
1820 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1821 make_tdb_data((uint8_t *)&reg_data, struct_len),
1822 NULL, NULL, &cstatus);
1823 if (!NT_STATUS_IS_OK(status)) {
1824 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1825 nt_errstr(status)));
1827 return status;
1830 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1832 struct ctdb_client_notify_deregister dereg_data;
1833 NTSTATUS status;
1834 int cstatus;
1836 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1838 status = ctdbd_control_local(
1839 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1840 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1841 NULL, NULL, &cstatus);
1842 if (!NT_STATUS_IS_OK(status)) {
1843 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1844 nt_errstr(status)));
1846 return status;
1849 NTSTATUS ctdbd_probe(void)
1852 * Do a very early check if ctdbd is around to avoid an abort and core
1853 * later
1855 struct ctdbd_connection *conn = NULL;
1856 NTSTATUS status;
1858 status = ctdbd_messaging_connection(talloc_tos(), &conn);
1861 * We only care if we can connect.
1863 TALLOC_FREE(conn);
1865 return status;
1868 #else
1870 NTSTATUS ctdbd_probe(void)
1872 return NT_STATUS_OK;
1875 NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
1876 uint32_t dst_vnn, uint64_t dst_srvid,
1877 const uint8_t *buf, size_t buflen)
1879 return NT_STATUS_NOT_IMPLEMENTED;
1882 #endif