s3: Initialize aio_pending_size from aio_pthread
[Samba/gebeck_regimport.git] / source3 / lib / ctdbd_conn.c
blobba511379151338334b353f1e5000415884b1ceeb
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"
25 #ifdef CLUSTER_SUPPORT
27 #include "ctdbd_conn.h"
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 reqid;
57 uint32 our_vnn;
58 uint64 rand_srvid;
59 struct ctdb_packet_context *pkt;
60 struct fd_event *fde;
62 void (*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 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 *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 cluster_fatal("ctdbd_control failed\n");
133 *vnn = (uint32_t)cstatus;
134 return status;
138 * Are we active (i.e. not banned or stopped?)
140 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
142 int32_t cstatus=-1;
143 NTSTATUS status;
144 TDB_DATA outdata;
145 struct ctdb_node_map *m;
146 uint32_t failure_flags;
147 bool ret = false;
148 int i;
150 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
151 CTDB_CONTROL_GET_NODEMAP, 0, 0,
152 tdb_null, talloc_tos(), &outdata, &cstatus);
153 if (!NT_STATUS_IS_OK(status)) {
154 cluster_fatal("ctdbd_control failed\n");
156 if ((cstatus != 0) || (outdata.dptr == NULL)) {
157 DEBUG(2, ("Received invalid ctdb data\n"));
158 return false;
161 m = (struct ctdb_node_map *)outdata.dptr;
163 for (i=0; i<m->num; i++) {
164 if (vnn == m->nodes[i].pnn) {
165 break;
169 if (i == m->num) {
170 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
171 (int)vnn));
172 goto fail;
175 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
176 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
178 if ((m->nodes[i].flags & failure_flags) != 0) {
179 DEBUG(2, ("Node has status %x, not active\n",
180 (int)m->nodes[i].flags));
181 goto fail;
184 ret = true;
185 fail:
186 TALLOC_FREE(outdata.dptr);
187 return ret;
190 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
192 return conn->our_vnn;
196 * Get us a ctdb connection
199 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
200 struct ctdb_packet_context **presult)
202 struct ctdb_packet_context *result;
203 const char *sockname = lp_ctdbd_socket();
204 struct sockaddr_un addr;
205 int fd;
206 socklen_t salen;
208 if (!sockname || !*sockname) {
209 sockname = CTDB_PATH;
212 fd = socket(AF_UNIX, SOCK_STREAM, 0);
213 if (fd == -1) {
214 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
215 return map_nt_error_from_unix(errno);
218 ZERO_STRUCT(addr);
219 addr.sun_family = AF_UNIX;
220 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
222 salen = sizeof(struct sockaddr_un);
223 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
224 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
225 strerror(errno)));
226 close(fd);
227 return map_nt_error_from_unix(errno);
230 if (!(result = ctdb_packet_init(mem_ctx, fd))) {
231 close(fd);
232 return NT_STATUS_NO_MEMORY;
235 *presult = result;
236 return NT_STATUS_OK;
240 * Do we have a complete ctdb packet in the queue?
243 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
244 size_t *length,
245 void *private_data)
247 uint32 msglen;
249 if (available < sizeof(msglen)) {
250 return False;
253 msglen = *((uint32 *)buf);
255 DEBUG(11, ("msglen = %d\n", msglen));
257 if (msglen < sizeof(struct ctdb_req_header)) {
258 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
259 "the req_header\n", (int)msglen,
260 (int)sizeof(struct ctdb_req_header)));
261 cluster_fatal("ctdbd protocol error\n");
264 if (available < msglen) {
265 return false;
268 *length = msglen;
269 return true;
273 * State necessary to defer an incoming message while we are waiting for a
274 * ctdb reply.
277 struct deferred_msg_state {
278 struct messaging_context *msg_ctx;
279 struct messaging_rec *rec;
283 * Timed event handler for the deferred message
286 static void deferred_message_dispatch(struct event_context *event_ctx,
287 struct timed_event *te,
288 struct timeval now,
289 void *private_data)
291 struct deferred_msg_state *state = talloc_get_type_abort(
292 private_data, struct deferred_msg_state);
294 messaging_dispatch_rec(state->msg_ctx, state->rec);
295 TALLOC_FREE(state);
296 TALLOC_FREE(te);
299 struct req_pull_state {
300 TALLOC_CTX *mem_ctx;
301 DATA_BLOB req;
305 * Pull a ctdb request out of the incoming ctdb_packet queue
308 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
309 void *private_data)
311 struct req_pull_state *state = (struct req_pull_state *)private_data;
313 state->req.data = talloc_move(state->mem_ctx, &buf);
314 state->req.length = length;
315 return NT_STATUS_OK;
319 * Fetch a messaging_rec from an incoming ctdb style message
322 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
323 size_t overall_length,
324 struct ctdb_req_message *msg)
326 struct messaging_rec *result;
327 DATA_BLOB blob;
328 enum ndr_err_code ndr_err;
330 if ((overall_length < offsetof(struct ctdb_req_message, data))
331 || (overall_length
332 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
334 cluster_fatal("got invalid msg length");
337 if (!(result = talloc(mem_ctx, struct messaging_rec))) {
338 DEBUG(0, ("talloc failed\n"));
339 return NULL;
342 blob = data_blob_const(msg->data, msg->datalen);
344 ndr_err = ndr_pull_struct_blob(
345 &blob, result, result,
346 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
348 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
349 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
350 ndr_errstr(ndr_err)));
351 TALLOC_FREE(result);
352 return NULL;
355 if (DEBUGLEVEL >= 11) {
356 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
357 NDR_PRINT_DEBUG(messaging_rec, result);
360 return result;
363 static NTSTATUS ctdb_packet_fd_read_sync(struct ctdb_packet_context *ctx)
365 int timeout = lp_ctdb_timeout();
367 if (timeout == 0) {
368 timeout = -1;
370 return ctdb_packet_fd_read_sync_timeout(ctx, timeout);
374 * Read a full ctdbd request. If we have a messaging context, defer incoming
375 * messages that might come in between.
378 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
379 TALLOC_CTX *mem_ctx, void *result)
381 struct ctdb_req_header *hdr;
382 struct req_pull_state state;
383 NTSTATUS status;
385 next_pkt:
386 ZERO_STRUCT(state);
387 state.mem_ctx = mem_ctx;
389 while (!ctdb_packet_handler(conn->pkt, ctdb_req_complete,
390 ctdb_req_pull, &state, &status)) {
392 * Not enough data
394 status = ctdb_packet_fd_read_sync(conn->pkt);
396 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
397 /* EAGAIN */
398 continue;
399 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
400 /* EAGAIN */
401 continue;
404 if (!NT_STATUS_IS_OK(status)) {
405 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
406 cluster_fatal("ctdbd died\n");
410 if (!NT_STATUS_IS_OK(status)) {
411 DEBUG(0, ("Could not read ctdb_packet: %s\n", nt_errstr(status)));
412 cluster_fatal("ctdbd died\n");
415 hdr = (struct ctdb_req_header *)state.req.data;
417 DEBUG(11, ("Received ctdb packet\n"));
418 ctdb_packet_dump(hdr);
420 if (hdr->operation == CTDB_REQ_MESSAGE) {
421 struct timed_event *evt;
422 struct deferred_msg_state *msg_state;
423 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
425 if (conn->msg_ctx == NULL) {
426 DEBUG(1, ("Got a message without having a msg ctx, "
427 "dropping msg %llu\n",
428 (long long unsigned)msg->srvid));
429 goto next_pkt;
432 if ((conn->release_ip_handler != NULL)
433 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
434 /* must be dispatched immediately */
435 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
436 conn->release_ip_handler((const char *)msg->data,
437 conn->release_ip_priv);
438 TALLOC_FREE(hdr);
439 goto next_pkt;
442 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
443 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
445 DEBUG(1, ("ctdb_read_req: Got %s message\n",
446 (msg->srvid == CTDB_SRVID_RECONFIGURE)
447 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
449 messaging_send(conn->msg_ctx,
450 messaging_server_id(conn->msg_ctx),
451 MSG_SMB_BRL_VALIDATE, &data_blob_null);
452 messaging_send(conn->msg_ctx,
453 messaging_server_id(conn->msg_ctx),
454 MSG_DBWRAP_G_LOCK_RETRY,
455 &data_blob_null);
456 TALLOC_FREE(hdr);
457 goto next_pkt;
460 msg_state = talloc(NULL, struct deferred_msg_state);
461 if (msg_state == NULL) {
462 DEBUG(0, ("talloc failed\n"));
463 TALLOC_FREE(hdr);
464 goto next_pkt;
467 if (!(msg_state->rec = ctdb_pull_messaging_rec(
468 msg_state, state.req.length, msg))) {
469 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
470 TALLOC_FREE(msg_state);
471 TALLOC_FREE(hdr);
472 goto next_pkt;
475 TALLOC_FREE(hdr);
477 msg_state->msg_ctx = conn->msg_ctx;
480 * We're waiting for a call reply, but an async message has
481 * crossed. Defer dispatching to the toplevel event loop.
483 evt = event_add_timed(conn->msg_ctx->event_ctx,
484 conn->msg_ctx->event_ctx,
485 timeval_zero(),
486 deferred_message_dispatch,
487 msg_state);
488 if (evt == NULL) {
489 DEBUG(0, ("event_add_timed failed\n"));
490 TALLOC_FREE(msg_state);
491 TALLOC_FREE(hdr);
492 goto next_pkt;
495 goto next_pkt;
498 if ((reqid != 0) && (hdr->reqid != reqid)) {
499 /* we got the wrong reply */
500 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
501 "been %u\n", hdr->reqid, reqid));
502 TALLOC_FREE(hdr);
503 goto next_pkt;
506 *((void **)result) = talloc_move(mem_ctx, &hdr);
508 return NT_STATUS_OK;
512 * Get us a ctdbd connection
515 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
516 struct ctdbd_connection **pconn)
518 struct ctdbd_connection *conn;
519 NTSTATUS status;
521 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
522 DEBUG(0, ("talloc failed\n"));
523 return NT_STATUS_NO_MEMORY;
526 status = ctdbd_connect(conn, &conn->pkt);
528 if (!NT_STATUS_IS_OK(status)) {
529 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
530 goto fail;
533 status = get_cluster_vnn(conn, &conn->our_vnn);
535 if (!NT_STATUS_IS_OK(status)) {
536 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
537 goto fail;
540 if (!ctdbd_working(conn, conn->our_vnn)) {
541 DEBUG(2, ("Node is not working, can not connect\n"));
542 status = NT_STATUS_INTERNAL_DB_ERROR;
543 goto fail;
546 generate_random_buffer((unsigned char *)&conn->rand_srvid,
547 sizeof(conn->rand_srvid));
549 status = register_with_ctdbd(conn, conn->rand_srvid);
551 if (!NT_STATUS_IS_OK(status)) {
552 DEBUG(5, ("Could not register random srvid: %s\n",
553 nt_errstr(status)));
554 goto fail;
557 *pconn = conn;
558 return NT_STATUS_OK;
560 fail:
561 TALLOC_FREE(conn);
562 return status;
566 * Get us a ctdbd connection and register us as a process
569 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
570 struct ctdbd_connection **pconn)
572 struct ctdbd_connection *conn;
573 NTSTATUS status;
575 status = ctdbd_init_connection(mem_ctx, &conn);
577 if (!NT_STATUS_IS_OK(status)) {
578 return status;
581 status = register_with_ctdbd(conn, (uint64_t)getpid());
582 if (!NT_STATUS_IS_OK(status)) {
583 goto fail;
586 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
587 if (!NT_STATUS_IS_OK(status)) {
588 goto fail;
591 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
592 if (!NT_STATUS_IS_OK(status)) {
593 goto fail;
596 *pconn = conn;
597 return NT_STATUS_OK;
599 fail:
600 TALLOC_FREE(conn);
601 return status;
604 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
606 return conn->msg_ctx;
609 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
611 return ctdb_packet_get_fd(conn->pkt);
615 * Packet handler to receive and handle a ctdb message
617 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
618 void *private_data)
620 struct ctdbd_connection *conn = talloc_get_type_abort(
621 private_data, struct ctdbd_connection);
622 struct ctdb_req_message *msg;
623 struct messaging_rec *msg_rec;
625 msg = (struct ctdb_req_message *)buf;
627 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
628 DEBUG(0, ("Received async msg of type %u, discarding\n",
629 msg->hdr.operation));
630 TALLOC_FREE(buf);
631 return NT_STATUS_INVALID_PARAMETER;
634 if ((conn->release_ip_handler != NULL)
635 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
636 /* must be dispatched immediately */
637 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
638 conn->release_ip_handler((const char *)msg->data,
639 conn->release_ip_priv);
640 TALLOC_FREE(buf);
641 return NT_STATUS_OK;
644 SMB_ASSERT(conn->msg_ctx != NULL);
646 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
647 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
648 DEBUG(0,("Got cluster reconfigure message\n"));
650 * when the cluster is reconfigured or someone of the
651 * family has passed away (SAMBA_NOTIFY), we need to
652 * clean the brl database
654 messaging_send(conn->msg_ctx,
655 messaging_server_id(conn->msg_ctx),
656 MSG_SMB_BRL_VALIDATE, &data_blob_null);
658 messaging_send(conn->msg_ctx,
659 messaging_server_id(conn->msg_ctx),
660 MSG_DBWRAP_G_LOCK_RETRY,
661 &data_blob_null);
663 TALLOC_FREE(buf);
664 return NT_STATUS_OK;
667 /* only messages to our pid or the broadcast are valid here */
668 if (msg->srvid != getpid() && msg->srvid != MSG_SRVID_SAMBA) {
669 DEBUG(0,("Got unexpected message with srvid=%llu\n",
670 (unsigned long long)msg->srvid));
671 TALLOC_FREE(buf);
672 return NT_STATUS_OK;
675 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
676 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
677 TALLOC_FREE(buf);
678 return NT_STATUS_NO_MEMORY;
681 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
683 TALLOC_FREE(msg_rec);
684 TALLOC_FREE(buf);
685 return NT_STATUS_OK;
689 * The ctdbd socket is readable asynchronuously
692 static void ctdbd_socket_handler(struct event_context *event_ctx,
693 struct fd_event *event,
694 uint16 flags,
695 void *private_data)
697 struct ctdbd_connection *conn = talloc_get_type_abort(
698 private_data, struct ctdbd_connection);
700 NTSTATUS status;
702 status = ctdb_packet_fd_read(conn->pkt);
704 if (!NT_STATUS_IS_OK(status)) {
705 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
706 cluster_fatal("ctdbd died\n");
709 while (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
710 ctdb_handle_message, conn, &status)) {
711 if (!NT_STATUS_IS_OK(status)) {
712 DEBUG(10, ("could not handle incoming message: %s\n",
713 nt_errstr(status)));
719 * Prepare a ctdbd connection to receive messages
722 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
723 struct messaging_context *msg_ctx)
725 SMB_ASSERT(conn->msg_ctx == NULL);
726 SMB_ASSERT(conn->fde == NULL);
728 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
729 ctdb_packet_get_fd(conn->pkt),
730 EVENT_FD_READ,
731 ctdbd_socket_handler,
732 conn))) {
733 DEBUG(0, ("event_add_fd failed\n"));
734 return NT_STATUS_NO_MEMORY;
737 conn->msg_ctx = msg_ctx;
739 return NT_STATUS_OK;
743 * Send a messaging message across a ctdbd
746 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
747 uint32 dst_vnn, uint64 dst_srvid,
748 struct messaging_rec *msg)
750 struct ctdb_req_message r;
751 TALLOC_CTX *mem_ctx;
752 DATA_BLOB blob;
753 NTSTATUS status;
754 enum ndr_err_code ndr_err;
756 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
757 DEBUG(0, ("talloc failed\n"));
758 return NT_STATUS_NO_MEMORY;
761 ndr_err = ndr_push_struct_blob(
762 &blob, mem_ctx, msg,
763 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
765 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
766 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
767 ndr_errstr(ndr_err)));
768 status = ndr_map_error2ntstatus(ndr_err);
769 goto fail;
772 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
773 r.hdr.ctdb_magic = CTDB_MAGIC;
774 r.hdr.ctdb_version = CTDB_VERSION;
775 r.hdr.generation = 1;
776 r.hdr.operation = CTDB_REQ_MESSAGE;
777 r.hdr.destnode = dst_vnn;
778 r.hdr.srcnode = conn->our_vnn;
779 r.hdr.reqid = 0;
780 r.srvid = dst_srvid;
781 r.datalen = blob.length;
783 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
784 ctdb_packet_dump(&r.hdr);
786 status = ctdb_packet_send(
787 conn->pkt, 2,
788 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
789 blob);
791 if (!NT_STATUS_IS_OK(status)) {
792 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
793 goto fail;
796 status = ctdb_packet_flush(conn->pkt);
798 if (!NT_STATUS_IS_OK(status)) {
799 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
800 cluster_fatal("cluster dispatch daemon msg write error\n");
803 status = NT_STATUS_OK;
804 fail:
805 TALLOC_FREE(mem_ctx);
806 return status;
810 * send/recv a generic ctdb control message
812 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
813 uint32_t vnn, uint32 opcode,
814 uint64_t srvid, uint32_t flags,
815 TDB_DATA data,
816 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
817 int *cstatus)
819 struct ctdb_req_control req;
820 struct ctdb_reply_control *reply = NULL;
821 struct ctdbd_connection *new_conn = NULL;
822 NTSTATUS status;
824 if (conn == NULL) {
825 status = ctdbd_init_connection(NULL, &new_conn);
827 if (!NT_STATUS_IS_OK(status)) {
828 DEBUG(10, ("Could not init temp connection: %s\n",
829 nt_errstr(status)));
830 goto fail;
833 conn = new_conn;
836 ZERO_STRUCT(req);
837 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
838 req.hdr.ctdb_magic = CTDB_MAGIC;
839 req.hdr.ctdb_version = CTDB_VERSION;
840 req.hdr.operation = CTDB_REQ_CONTROL;
841 req.hdr.reqid = ctdbd_next_reqid(conn);
842 req.hdr.destnode = vnn;
843 req.opcode = opcode;
844 req.srvid = srvid;
845 req.datalen = data.dsize;
846 req.flags = flags;
848 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
849 ctdb_packet_dump(&req.hdr);
851 status = ctdb_packet_send(
852 conn->pkt, 2,
853 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
854 data_blob_const(data.dptr, data.dsize));
856 if (!NT_STATUS_IS_OK(status)) {
857 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
858 goto fail;
861 status = ctdb_packet_flush(conn->pkt);
863 if (!NT_STATUS_IS_OK(status)) {
864 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
865 cluster_fatal("cluster dispatch daemon control write error\n");
868 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
869 TALLOC_FREE(new_conn);
870 if (cstatus) {
871 *cstatus = 0;
873 return NT_STATUS_OK;
876 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
878 if (!NT_STATUS_IS_OK(status)) {
879 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
880 goto fail;
883 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
884 DEBUG(0, ("received invalid reply\n"));
885 goto fail;
888 if (outdata) {
889 if (!(outdata->dptr = (uint8 *)talloc_memdup(
890 mem_ctx, reply->data, reply->datalen))) {
891 TALLOC_FREE(reply);
892 return NT_STATUS_NO_MEMORY;
894 outdata->dsize = reply->datalen;
896 if (cstatus) {
897 (*cstatus) = reply->status;
900 status = NT_STATUS_OK;
902 fail:
903 TALLOC_FREE(new_conn);
904 TALLOC_FREE(reply);
905 return status;
909 * see if a remote process exists
911 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
913 struct server_id id;
914 bool result;
916 id.pid = pid;
917 id.vnn = vnn;
919 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
920 DEBUG(10, ("ctdb_processes_exist failed\n"));
921 return false;
923 return result;
926 bool ctdb_processes_exist(struct ctdbd_connection *conn,
927 const struct server_id *pids, int num_pids,
928 bool *results)
930 TALLOC_CTX *frame = talloc_stackframe();
931 int i, num_received;
932 NTSTATUS status;
933 uint32_t *reqids;
934 bool result = false;
936 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
937 if (reqids == NULL) {
938 goto fail;
941 for (i=0; i<num_pids; i++) {
942 struct ctdb_req_control req;
943 pid_t pid;
945 results[i] = false;
946 reqids[i] = ctdbd_next_reqid(conn);
948 ZERO_STRUCT(req);
951 * pids[i].pid is uint64_t, scale down to pid_t which
952 * is the wire protocol towards ctdb.
954 pid = pids[i].pid;
956 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
957 (int)pids[i].vnn, (int)pid,
958 (int)reqids[i]));
960 req.hdr.length = offsetof(struct ctdb_req_control, data);
961 req.hdr.length += sizeof(pid);
962 req.hdr.ctdb_magic = CTDB_MAGIC;
963 req.hdr.ctdb_version = CTDB_VERSION;
964 req.hdr.operation = CTDB_REQ_CONTROL;
965 req.hdr.reqid = reqids[i];
966 req.hdr.destnode = pids[i].vnn;
967 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
968 req.srvid = 0;
969 req.datalen = sizeof(pid);
970 req.flags = 0;
972 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
973 ctdb_packet_dump(&req.hdr);
975 status = ctdb_packet_send(
976 conn->pkt, 2,
977 data_blob_const(
978 &req, offsetof(struct ctdb_req_control, data)),
979 data_blob_const(&pid, sizeof(pid)));
980 if (!NT_STATUS_IS_OK(status)) {
981 DEBUG(10, ("ctdb_packet_send failed: %s\n",
982 nt_errstr(status)));
983 goto fail;
987 status = ctdb_packet_flush(conn->pkt);
988 if (!NT_STATUS_IS_OK(status)) {
989 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
990 nt_errstr(status)));
991 goto fail;
994 num_received = 0;
996 while (num_received < num_pids) {
997 struct ctdb_reply_control *reply = NULL;
998 uint32_t reqid;
1000 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1001 if (!NT_STATUS_IS_OK(status)) {
1002 DEBUG(10, ("ctdb_read_req failed: %s\n",
1003 nt_errstr(status)));
1004 goto fail;
1007 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1008 DEBUG(10, ("Received invalid reply\n"));
1009 goto fail;
1012 reqid = reply->hdr.reqid;
1014 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1016 for (i=0; i<num_pids; i++) {
1017 if (reqid == reqids[i]) {
1018 break;
1021 if (i == num_pids) {
1022 DEBUG(10, ("Received unknown record number %u\n",
1023 (unsigned)reqid));
1024 goto fail;
1026 results[i] = ((reply->status) == 0);
1027 TALLOC_FREE(reply);
1028 num_received += 1;
1031 result = true;
1032 fail:
1033 TALLOC_FREE(frame);
1034 return result;
1037 struct ctdb_vnn_list {
1038 uint32_t vnn;
1039 uint32_t reqid;
1040 unsigned num_srvids;
1041 unsigned num_filled;
1042 uint64_t *srvids;
1043 unsigned *pid_indexes;
1047 * Get a list of all vnns mentioned in a list of
1048 * server_ids. vnn_indexes tells where in the vnns array we have to
1049 * place the pids.
1051 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
1052 const struct server_id *pids, unsigned num_pids,
1053 struct ctdb_vnn_list **pvnns,
1054 unsigned *pnum_vnns)
1056 struct ctdb_vnn_list *vnns = NULL;
1057 unsigned *vnn_indexes = NULL;
1058 unsigned i, num_vnns = 0;
1060 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
1061 if (vnn_indexes == NULL) {
1062 goto fail;
1065 for (i=0; i<num_pids; i++) {
1066 unsigned j;
1067 uint32_t vnn = pids[i].vnn;
1069 for (j=0; j<num_vnns; j++) {
1070 if (vnn == vnns[j].vnn) {
1071 break;
1074 vnn_indexes[i] = j;
1076 if (j < num_vnns) {
1078 * Already in the array
1080 vnns[j].num_srvids += 1;
1081 continue;
1083 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
1084 num_vnns+1);
1085 if (vnns == NULL) {
1086 goto fail;
1088 vnns[num_vnns].vnn = vnn;
1089 vnns[num_vnns].num_srvids = 1;
1090 vnns[num_vnns].num_filled = 0;
1091 num_vnns += 1;
1093 for (i=0; i<num_vnns; i++) {
1094 struct ctdb_vnn_list *vnn = &vnns[i];
1096 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
1097 if (vnn->srvids == NULL) {
1098 goto fail;
1100 vnn->pid_indexes = talloc_array(vnns, unsigned,
1101 vnn->num_srvids);
1102 if (vnn->pid_indexes == NULL) {
1103 goto fail;
1106 for (i=0; i<num_pids; i++) {
1107 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
1108 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
1109 vnn->pid_indexes[vnn->num_filled] = i;
1110 vnn->num_filled += 1;
1113 TALLOC_FREE(vnn_indexes);
1114 *pvnns = vnns;
1115 *pnum_vnns = num_vnns;
1116 return true;
1117 fail:
1118 TALLOC_FREE(vnns);
1119 TALLOC_FREE(vnn_indexes);
1120 return false;
1123 #ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1125 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
1126 const struct server_id *pids, unsigned num_pids,
1127 bool *results)
1129 unsigned i, num_received;
1130 NTSTATUS status;
1131 struct ctdb_vnn_list *vnns = NULL;
1132 unsigned num_vnns;
1133 bool result = false;
1135 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1136 &vnns, &num_vnns)) {
1137 goto fail;
1140 for (i=0; i<num_vnns; i++) {
1141 struct ctdb_vnn_list *vnn = &vnns[i];
1142 struct ctdb_req_control req;
1144 vnn->reqid = ctdbd_next_reqid(conn);
1146 ZERO_STRUCT(req);
1148 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1149 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1151 req.hdr.length = offsetof(struct ctdb_req_control, data);
1152 req.hdr.ctdb_magic = CTDB_MAGIC;
1153 req.hdr.ctdb_version = CTDB_VERSION;
1154 req.hdr.operation = CTDB_REQ_CONTROL;
1155 req.hdr.reqid = vnn->reqid;
1156 req.hdr.destnode = vnn->vnn;
1157 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1158 req.srvid = 0;
1159 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1160 req.hdr.length += req.datalen;
1161 req.flags = 0;
1163 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1164 ctdb_packet_dump(&req.hdr);
1166 status = ctdb_packet_send(
1167 conn->pkt, 2,
1168 data_blob_const(
1169 &req, offsetof(struct ctdb_req_control,
1170 data)),
1171 data_blob_const(vnn->srvids, req.datalen));
1172 if (!NT_STATUS_IS_OK(status)) {
1173 DEBUG(10, ("ctdb_packet_send failed: %s\n",
1174 nt_errstr(status)));
1175 goto fail;
1179 status = ctdb_packet_flush(conn->pkt);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
1182 nt_errstr(status)));
1183 goto fail;
1186 num_received = 0;
1188 while (num_received < num_vnns) {
1189 struct ctdb_reply_control *reply = NULL;
1190 struct ctdb_vnn_list *vnn;
1191 uint32_t reqid;
1193 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1194 if (!NT_STATUS_IS_OK(status)) {
1195 DEBUG(10, ("ctdb_read_req failed: %s\n",
1196 nt_errstr(status)));
1197 goto fail;
1200 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1201 DEBUG(10, ("Received invalid reply\n"));
1202 goto fail;
1205 reqid = reply->hdr.reqid;
1207 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1209 for (i=0; i<num_vnns; i++) {
1210 if (reqid == vnns[i].reqid) {
1211 break;
1214 if (i == num_vnns) {
1215 DEBUG(10, ("Received unknown reqid number %u\n",
1216 (unsigned)reqid));
1217 goto fail;
1220 DEBUG(10, ("Found index %u\n", i));
1222 vnn = &vnns[i];
1224 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1225 (unsigned)vnn->vnn, vnn->num_srvids,
1226 (unsigned)reply->datalen));
1228 if (reply->datalen < ((vnn->num_srvids+7)/8)) {
1229 DEBUG(10, ("Received short reply\n"));
1230 goto fail;
1233 for (i=0; i<vnn->num_srvids; i++) {
1234 int idx = vnn->pid_indexes[i];
1236 if (pids[i].unique_id ==
1237 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1238 results[idx] = true;
1239 continue;
1241 results[idx] = ((reply->data[i/8] & (1<<(i%8))) != 0);
1244 TALLOC_FREE(reply);
1245 num_received += 1;
1248 result = true;
1249 fail:
1250 TALLOC_FREE(vnns);
1251 return result;
1254 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1257 * Get a db path
1259 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1260 TALLOC_CTX *mem_ctx, uint32_t db_id)
1262 NTSTATUS status;
1263 TDB_DATA data;
1264 int32_t cstatus;
1266 data.dptr = (uint8_t*)&db_id;
1267 data.dsize = sizeof(db_id);
1269 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1270 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1271 mem_ctx, &data, &cstatus);
1272 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1273 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1274 return NULL;
1277 return (char *)data.dptr;
1281 * attach to a ctdb database
1283 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1284 const char *name, uint32_t *db_id, int tdb_flags)
1286 NTSTATUS status;
1287 TDB_DATA data;
1288 int32_t cstatus;
1289 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1291 data.dptr = (uint8_t*)name;
1292 data.dsize = strlen(name)+1;
1294 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1295 persistent
1296 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1297 : CTDB_CONTROL_DB_ATTACH,
1298 tdb_flags, 0, data, NULL, &data, &cstatus);
1299 if (!NT_STATUS_IS_OK(status)) {
1300 DEBUG(0, (__location__ " ctdb_control for db_attach "
1301 "failed: %s\n", nt_errstr(status)));
1302 return status;
1305 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1306 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1307 return NT_STATUS_INTERNAL_ERROR;
1310 *db_id = *(uint32_t *)data.dptr;
1311 talloc_free(data.dptr);
1313 if (!(tdb_flags & TDB_SEQNUM)) {
1314 return NT_STATUS_OK;
1317 data.dptr = (uint8_t *)db_id;
1318 data.dsize = sizeof(*db_id);
1320 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1321 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1322 NULL, NULL, &cstatus);
1323 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1324 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1325 "failed\n"));
1326 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1327 status;
1330 return NT_STATUS_OK;
1334 * force the migration of a record to this node
1336 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
1337 TDB_DATA key)
1339 struct ctdb_req_call req;
1340 struct ctdb_reply_call *reply;
1341 NTSTATUS status;
1343 ZERO_STRUCT(req);
1345 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1346 req.hdr.ctdb_magic = CTDB_MAGIC;
1347 req.hdr.ctdb_version = CTDB_VERSION;
1348 req.hdr.operation = CTDB_REQ_CALL;
1349 req.hdr.reqid = ctdbd_next_reqid(conn);
1350 req.flags = CTDB_IMMEDIATE_MIGRATION;
1351 req.callid = CTDB_NULL_FUNC;
1352 req.db_id = db_id;
1353 req.keylen = key.dsize;
1355 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1356 ctdb_packet_dump(&req.hdr);
1358 status = ctdb_packet_send(
1359 conn->pkt, 2,
1360 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1361 data_blob_const(key.dptr, key.dsize));
1363 if (!NT_STATUS_IS_OK(status)) {
1364 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1365 return status;
1368 status = ctdb_packet_flush(conn->pkt);
1370 if (!NT_STATUS_IS_OK(status)) {
1371 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1372 cluster_fatal("cluster dispatch daemon control write error\n");
1375 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1377 if (!NT_STATUS_IS_OK(status)) {
1378 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1379 goto fail;
1382 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1383 DEBUG(0, ("received invalid reply\n"));
1384 status = NT_STATUS_INTERNAL_ERROR;
1385 goto fail;
1388 status = NT_STATUS_OK;
1389 fail:
1391 TALLOC_FREE(reply);
1392 return status;
1396 * remotely fetch a record (read-only)
1398 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
1399 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data,
1400 bool local_copy)
1402 struct ctdb_req_call req;
1403 struct ctdb_reply_call *reply;
1404 NTSTATUS status;
1405 uint32_t flags;
1407 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1408 flags = local_copy ? CTDB_WANT_READONLY : 0;
1409 #else
1410 flags = 0;
1411 #endif
1413 ZERO_STRUCT(req);
1415 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1416 req.hdr.ctdb_magic = CTDB_MAGIC;
1417 req.hdr.ctdb_version = CTDB_VERSION;
1418 req.hdr.operation = CTDB_REQ_CALL;
1419 req.hdr.reqid = ctdbd_next_reqid(conn);
1420 req.flags = flags;
1421 req.callid = CTDB_FETCH_FUNC;
1422 req.db_id = db_id;
1423 req.keylen = key.dsize;
1425 status = ctdb_packet_send(
1426 conn->pkt, 2,
1427 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1428 data_blob_const(key.dptr, key.dsize));
1430 if (!NT_STATUS_IS_OK(status)) {
1431 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1432 return status;
1435 status = ctdb_packet_flush(conn->pkt);
1437 if (!NT_STATUS_IS_OK(status)) {
1438 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1439 cluster_fatal("cluster dispatch daemon control write error\n");
1442 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1444 if (!NT_STATUS_IS_OK(status)) {
1445 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1446 goto fail;
1449 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1450 DEBUG(0, ("received invalid reply\n"));
1451 status = NT_STATUS_INTERNAL_ERROR;
1452 goto fail;
1455 data->dsize = reply->datalen;
1456 if (data->dsize == 0) {
1457 data->dptr = NULL;
1458 goto done;
1461 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1462 reply->datalen);
1463 if (data->dptr == NULL) {
1464 DEBUG(0, ("talloc failed\n"));
1465 status = NT_STATUS_NO_MEMORY;
1466 goto fail;
1469 done:
1470 status = NT_STATUS_OK;
1471 fail:
1472 TALLOC_FREE(reply);
1473 return status;
1476 struct ctdbd_traverse_state {
1477 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1478 void *private_data;
1482 * Handle a traverse record coming in on the ctdbd connection
1485 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1486 void *private_data)
1488 struct ctdbd_traverse_state *state =
1489 (struct ctdbd_traverse_state *)private_data;
1491 struct ctdb_req_message *m;
1492 struct ctdb_rec_data *d;
1493 TDB_DATA key, data;
1495 m = (struct ctdb_req_message *)buf;
1497 if (length < sizeof(*m) || m->hdr.length != length) {
1498 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1499 TALLOC_FREE(buf);
1500 return NT_STATUS_UNEXPECTED_IO_ERROR;
1503 d = (struct ctdb_rec_data *)&m->data[0];
1504 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1505 DEBUG(0, ("Got invalid traverse data of length %d\n",
1506 (int)m->datalen));
1507 TALLOC_FREE(buf);
1508 return NT_STATUS_UNEXPECTED_IO_ERROR;
1511 key.dsize = d->keylen;
1512 key.dptr = &d->data[0];
1513 data.dsize = d->datalen;
1514 data.dptr = &d->data[d->keylen];
1516 if (key.dsize == 0 && data.dsize == 0) {
1517 /* end of traverse */
1518 return NT_STATUS_END_OF_FILE;
1521 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1522 DEBUG(0, ("Got invalid ltdb header length %d\n",
1523 (int)data.dsize));
1524 TALLOC_FREE(buf);
1525 return NT_STATUS_UNEXPECTED_IO_ERROR;
1527 data.dsize -= sizeof(struct ctdb_ltdb_header);
1528 data.dptr += sizeof(struct ctdb_ltdb_header);
1530 if (state->fn) {
1531 state->fn(key, data, state->private_data);
1534 TALLOC_FREE(buf);
1535 return NT_STATUS_OK;
1539 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1540 connection to ctdbd to avoid the hairy recursive and async problems with
1541 everything in-line.
1544 NTSTATUS ctdbd_traverse(uint32 db_id,
1545 void (*fn)(TDB_DATA key, TDB_DATA data,
1546 void *private_data),
1547 void *private_data)
1549 struct ctdbd_connection *conn;
1550 NTSTATUS status;
1552 TDB_DATA data;
1553 struct ctdb_traverse_start t;
1554 int cstatus;
1555 struct ctdbd_traverse_state state;
1557 become_root();
1558 status = ctdbd_init_connection(NULL, &conn);
1559 unbecome_root();
1560 if (!NT_STATUS_IS_OK(status)) {
1561 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1562 nt_errstr(status)));
1563 return status;
1566 t.db_id = db_id;
1567 t.srvid = conn->rand_srvid;
1568 t.reqid = ctdbd_next_reqid(conn);
1570 data.dptr = (uint8_t *)&t;
1571 data.dsize = sizeof(t);
1573 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1574 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1575 data, NULL, NULL, &cstatus);
1577 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1579 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1580 cstatus));
1582 if (NT_STATUS_IS_OK(status)) {
1584 * We need a mapping here
1586 status = NT_STATUS_UNSUCCESSFUL;
1588 goto done;
1591 state.fn = fn;
1592 state.private_data = private_data;
1594 while (True) {
1596 status = NT_STATUS_OK;
1598 if (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
1599 ctdb_traverse_handler, &state, &status)) {
1601 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1602 status = NT_STATUS_OK;
1603 break;
1607 * There might be more in the queue
1609 continue;
1612 if (!NT_STATUS_IS_OK(status)) {
1613 break;
1616 status = ctdb_packet_fd_read_sync(conn->pkt);
1618 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1620 * There might be more in the queue
1622 continue;
1625 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1626 status = NT_STATUS_OK;
1627 break;
1630 if (!NT_STATUS_IS_OK(status)) {
1631 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1632 cluster_fatal("ctdbd died\n");
1636 done:
1637 TALLOC_FREE(conn);
1638 return status;
1642 This is used to canonicalize a ctdb_sock_addr structure.
1644 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1645 struct sockaddr_storage *out)
1647 memcpy(out, in, sizeof (*out));
1649 #ifdef HAVE_IPV6
1650 if (in->ss_family == AF_INET6) {
1651 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1652 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1653 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1654 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1655 memset(out, 0, sizeof(*out));
1656 #ifdef HAVE_SOCK_SIN_LEN
1657 out4->sin_len = sizeof(*out);
1658 #endif
1659 out4->sin_family = AF_INET;
1660 out4->sin_port = in6->sin6_port;
1661 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1664 #endif
1668 * Register us as a server for a particular tcp connection
1671 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1672 const struct sockaddr_storage *_server,
1673 const struct sockaddr_storage *_client,
1674 void (*release_ip_handler)(const char *ip_addr,
1675 void *private_data),
1676 void *private_data)
1679 * we still use ctdb_control_tcp for ipv4
1680 * because we want to work against older ctdb
1681 * versions at runtime
1683 struct ctdb_control_tcp p4;
1684 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1685 struct ctdb_control_tcp_addr p;
1686 #endif
1687 TDB_DATA data;
1688 NTSTATUS status;
1689 struct sockaddr_storage client;
1690 struct sockaddr_storage server;
1693 * Only one connection so far
1695 SMB_ASSERT(conn->release_ip_handler == NULL);
1697 smbd_ctdb_canonicalize_ip(_client, &client);
1698 smbd_ctdb_canonicalize_ip(_server, &server);
1700 switch (client.ss_family) {
1701 case AF_INET:
1702 memcpy(&p4.dest, &server, sizeof(p4.dest));
1703 memcpy(&p4.src, &client, sizeof(p4.src));
1704 data.dptr = (uint8_t *)&p4;
1705 data.dsize = sizeof(p4);
1706 break;
1707 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1708 case AF_INET6:
1709 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1710 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1711 data.dptr = (uint8_t *)&p;
1712 data.dsize = sizeof(p);
1713 break;
1714 #endif
1715 default:
1716 return NT_STATUS_INTERNAL_ERROR;
1719 conn->release_ip_handler = release_ip_handler;
1720 conn->release_ip_priv = private_data;
1723 * We want to be told about IP releases
1726 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1727 if (!NT_STATUS_IS_OK(status)) {
1728 return status;
1732 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1733 * can send an extra ack to trigger a reset for our client, so it
1734 * immediately reconnects
1736 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1737 CTDB_CONTROL_TCP_CLIENT, 0,
1738 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1742 * We want to handle reconfigure events
1744 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1746 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1750 call a control on the local node
1752 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
1753 uint64_t srvid, uint32_t flags, TDB_DATA data,
1754 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1755 int *cstatus)
1757 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1760 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1762 struct ctdb_client_notify_register reg_data;
1763 size_t struct_len;
1764 NTSTATUS status;
1765 int cstatus;
1767 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1768 reg_data.len = 1;
1769 reg_data.notify_data[0] = 0;
1771 struct_len = offsetof(struct ctdb_client_notify_register,
1772 notify_data) + reg_data.len;
1774 status = ctdbd_control_local(
1775 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1776 make_tdb_data((uint8_t *)&reg_data, struct_len),
1777 NULL, NULL, &cstatus);
1778 if (!NT_STATUS_IS_OK(status)) {
1779 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1780 nt_errstr(status)));
1782 return status;
1785 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1787 struct ctdb_client_notify_deregister dereg_data;
1788 NTSTATUS status;
1789 int cstatus;
1791 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1793 status = ctdbd_control_local(
1794 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1795 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1796 NULL, NULL, &cstatus);
1797 if (!NT_STATUS_IS_OK(status)) {
1798 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1799 nt_errstr(status)));
1801 return status;
1804 #endif