source: branches/samba-3.3.x/source/lib/ctdbd_conn.c

Last change on this file was 274, checked in by Herwig Bauernfeind, 16 years ago

Update 3.3 branch to 3.3.5

File size: 32.1 KB
Line 
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
6
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.
11
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.
16
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/>.
19*/
20
21#include "includes.h"
22
23#ifdef CLUSTER_SUPPORT
24
25#include "librpc/gen_ndr/messaging.h"
26#include "librpc/gen_ndr/ndr_messaging.h"
27
28/* paths to these include files come from --with-ctdb= in configure */
29#include "ctdb.h"
30#include "ctdb_private.h"
31
32struct ctdbd_connection {
33 struct messaging_context *msg_ctx;
34 uint32 reqid;
35 uint32 our_vnn;
36 uint64 rand_srvid;
37 struct packet_context *pkt;
38 struct fd_event *fde;
39
40 void (*release_ip_handler)(const char *ip_addr, void *private_data);
41 void *release_ip_priv;
42};
43
44static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
45 uint32_t vnn, uint32 opcode,
46 uint64_t srvid, uint32_t flags, TDB_DATA data,
47 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
48 int *cstatus);
49
50/*
51 * exit on fatal communications errors with the ctdbd daemon
52 */
53static void cluster_fatal(const char *why)
54{
55 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
56 /* we don't use smb_panic() as we don't want to delay to write
57 a core file. We need to release this process id immediately
58 so that someone else can take over without getting sharing
59 violations */
60 _exit(0);
61}
62
63/*
64 *
65 */
66static void ctdb_packet_dump(struct ctdb_req_header *hdr)
67{
68 if (DEBUGLEVEL < 10) {
69 return;
70 }
71 DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
72 (int)hdr->length, (int)hdr->ctdb_magic,
73 (int)hdr->ctdb_version, (int)hdr->generation,
74 (int)hdr->operation, (int)hdr->reqid));
75}
76
77/*
78 * Register a srvid with ctdbd
79 */
80static NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn,
81 uint64_t srvid)
82{
83
84 int cstatus;
85 return ctdbd_control(conn, CTDB_CURRENT_NODE,
86 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
87 tdb_null, NULL, NULL, &cstatus);
88}
89
90/*
91 * get our vnn from the cluster
92 */
93static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
94{
95 int32_t cstatus=-1;
96 NTSTATUS status;
97 status = ctdbd_control(conn,
98 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
99 tdb_null, NULL, NULL, &cstatus);
100 if (!NT_STATUS_IS_OK(status)) {
101 cluster_fatal("ctdbd_control failed\n");
102 }
103 *vnn = (uint32_t)cstatus;
104 return status;
105}
106
107uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
108{
109 return conn->our_vnn;
110}
111
112/*
113 * Get us a ctdb connection
114 */
115
116static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
117 struct packet_context **presult)
118{
119 struct packet_context *result;
120 const char *sockname = lp_ctdbd_socket();
121 struct sockaddr_un addr;
122 int fd;
123
124 if (!sockname || !*sockname) {
125 sockname = CTDB_PATH;
126 }
127
128 fd = socket(AF_UNIX, SOCK_STREAM, 0);
129 if (fd == -1) {
130 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
131 return map_nt_error_from_unix(errno);
132 }
133
134 ZERO_STRUCT(addr);
135 addr.sun_family = AF_UNIX;
136 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
137
138 if (sys_connect(fd, (struct sockaddr *)&addr) == -1) {
139 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
140 strerror(errno)));
141 close(fd);
142 return map_nt_error_from_unix(errno);
143 }
144
145 if (!(result = packet_init(mem_ctx, fd))) {
146 close(fd);
147 return NT_STATUS_NO_MEMORY;
148 }
149
150 *presult = result;
151 return NT_STATUS_OK;
152}
153
154/*
155 * Do we have a complete ctdb packet in the queue?
156 */
157
158static bool ctdb_req_complete(const struct data_blob *data,
159 size_t *length,
160 void *private_data)
161{
162 uint32 msglen;
163
164 if (data->length < sizeof(msglen)) {
165 return False;
166 }
167
168 msglen = *((uint32 *)data->data);
169
170 DEBUG(10, ("msglen = %d\n", msglen));
171
172 if (msglen < sizeof(struct ctdb_req_header)) {
173 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
174 "the req_header\n", (int)msglen,
175 (int)sizeof(struct ctdb_req_header)));
176 cluster_fatal("ctdbd protocol error\n");
177 }
178
179 if (data->length >= msglen) {
180 *length = msglen;
181 return True;
182 }
183
184 return False;
185}
186
187/*
188 * State necessary to defer an incoming message while we are waiting for a
189 * ctdb reply.
190 */
191
192struct deferred_msg_state {
193 struct messaging_context *msg_ctx;
194 struct messaging_rec *rec;
195};
196
197/*
198 * Timed event handler for the deferred message
199 */
200
201static void deferred_message_dispatch(struct event_context *event_ctx,
202 struct timed_event *te,
203 struct timeval now,
204 void *private_data)
205{
206 struct deferred_msg_state *state = talloc_get_type_abort(
207 private_data, struct deferred_msg_state);
208
209 messaging_dispatch_rec(state->msg_ctx, state->rec);
210 TALLOC_FREE(state);
211 TALLOC_FREE(te);
212}
213
214struct req_pull_state {
215 TALLOC_CTX *mem_ctx;
216 DATA_BLOB req;
217};
218
219/*
220 * Pull a ctdb request out of the incoming packet queue
221 */
222
223static NTSTATUS ctdb_req_pull(const struct data_blob *data,
224 void *private_data)
225{
226 struct req_pull_state *state = (struct req_pull_state *)private_data;
227
228 state->req = data_blob_talloc(state->mem_ctx, data->data,
229 data->length);
230 if (state->req.data == NULL) {
231 return NT_STATUS_NO_MEMORY;
232 }
233 return NT_STATUS_OK;
234}
235
236/*
237 * Fetch a messaging_rec from an incoming ctdb style message
238 */
239
240static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
241 size_t overall_length,
242 struct ctdb_req_message *msg)
243{
244 struct messaging_rec *result;
245 DATA_BLOB blob;
246 enum ndr_err_code ndr_err;
247
248 if ((overall_length < offsetof(struct ctdb_req_message, data))
249 || (overall_length
250 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
251
252 cluster_fatal("got invalid msg length");
253 }
254
255 if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
256 DEBUG(0, ("talloc failed\n"));
257 return NULL;
258 }
259
260 blob = data_blob_const(msg->data, msg->datalen);
261
262 ndr_err = ndr_pull_struct_blob(
263 &blob, result, result,
264 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
265
266 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
267 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
268 ndr_errstr(ndr_err)));
269 TALLOC_FREE(result);
270 return NULL;
271 }
272
273 if (DEBUGLEVEL >= 10) {
274 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
275 NDR_PRINT_DEBUG(messaging_rec, result);
276 }
277
278 return result;
279}
280
281/*
282 * Read a full ctdbd request. If we have a messaging context, defer incoming
283 * messages that might come in between.
284 */
285
286static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
287 TALLOC_CTX *mem_ctx, void *result)
288{
289 struct ctdb_req_header *hdr;
290 struct req_pull_state state;
291 NTSTATUS status;
292
293 again:
294
295 status = packet_fd_read_sync(conn->pkt);
296
297 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
298 /* EAGAIN */
299 goto again;
300 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
301 /* EAGAIN */
302 goto again;
303 }
304
305 if (!NT_STATUS_IS_OK(status)) {
306 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
307 cluster_fatal("ctdbd died\n");
308 }
309
310 next_pkt:
311
312 ZERO_STRUCT(state);
313 state.mem_ctx = mem_ctx;
314
315 if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
316 &state, &status)) {
317 /*
318 * Not enough data
319 */
320 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
321 goto again;
322 }
323
324 if (!NT_STATUS_IS_OK(status)) {
325 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
326 cluster_fatal("ctdbd died\n");
327 }
328
329 hdr = (struct ctdb_req_header *)state.req.data;
330
331 DEBUG(10, ("Received ctdb packet\n"));
332 ctdb_packet_dump(hdr);
333
334 if (hdr->operation == CTDB_REQ_MESSAGE) {
335 struct timed_event *evt;
336 struct deferred_msg_state *msg_state;
337 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
338
339 if (conn->msg_ctx == NULL) {
340 DEBUG(1, ("Got a message without having a msg ctx, "
341 "dropping msg %llu\n",
342 (long long unsigned)msg->srvid));
343 goto next_pkt;
344 }
345
346 if ((conn->release_ip_handler != NULL)
347 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
348 /* must be dispatched immediately */
349 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
350 conn->release_ip_handler((const char *)msg->data,
351 conn->release_ip_priv);
352 TALLOC_FREE(hdr);
353 goto next_pkt;
354 }
355
356 if (msg->srvid == CTDB_SRVID_RECONFIGURE) {
357 DEBUG(0,("Got cluster reconfigure message in ctdb_read_req\n"));
358 messaging_send(conn->msg_ctx, procid_self(),
359 MSG_SMB_BRL_VALIDATE, &data_blob_null);
360 TALLOC_FREE(hdr);
361 goto next_pkt;
362 }
363
364 if (!(msg_state = TALLOC_P(NULL, struct deferred_msg_state))) {
365 DEBUG(0, ("talloc failed\n"));
366 TALLOC_FREE(hdr);
367 goto next_pkt;
368 }
369
370 if (!(msg_state->rec = ctdb_pull_messaging_rec(
371 msg_state, state.req.length, msg))) {
372 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
373 TALLOC_FREE(msg_state);
374 TALLOC_FREE(hdr);
375 goto next_pkt;
376 }
377
378 TALLOC_FREE(hdr);
379
380 msg_state->msg_ctx = conn->msg_ctx;
381
382 /*
383 * We're waiting for a call reply, but an async message has
384 * crossed. Defer dispatching to the toplevel event loop.
385 */
386 evt = event_add_timed(conn->msg_ctx->event_ctx,
387 conn->msg_ctx->event_ctx,
388 timeval_zero(),
389 deferred_message_dispatch,
390 msg_state);
391 if (evt == NULL) {
392 DEBUG(0, ("event_add_timed failed\n"));
393 TALLOC_FREE(msg_state);
394 TALLOC_FREE(hdr);
395 goto next_pkt;
396 }
397
398 goto next_pkt;
399 }
400
401 if (hdr->reqid != reqid) {
402 /* we got the wrong reply */
403 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
404 "been %u\n", hdr->reqid, reqid));
405 TALLOC_FREE(hdr);
406 goto again;
407 }
408
409 *((void **)result) = talloc_move(mem_ctx, &hdr);
410
411 return NT_STATUS_OK;
412}
413
414/*
415 * Get us a ctdbd connection
416 */
417
418NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
419 struct ctdbd_connection **pconn)
420{
421 struct ctdbd_connection *conn;
422 NTSTATUS status;
423
424 if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
425 DEBUG(0, ("talloc failed\n"));
426 return NT_STATUS_NO_MEMORY;
427 }
428
429 status = ctdbd_connect(conn, &conn->pkt);
430
431 if (!NT_STATUS_IS_OK(status)) {
432 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
433 goto fail;
434 }
435
436 status = get_cluster_vnn(conn, &conn->our_vnn);
437
438 if (!NT_STATUS_IS_OK(status)) {
439 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
440 goto fail;
441 }
442
443 generate_random_buffer((unsigned char *)&conn->rand_srvid,
444 sizeof(conn->rand_srvid));
445
446 status = register_with_ctdbd(conn, conn->rand_srvid);
447
448 if (!NT_STATUS_IS_OK(status)) {
449 DEBUG(5, ("Could not register random srvid: %s\n",
450 nt_errstr(status)));
451 goto fail;
452 }
453
454 *pconn = conn;
455 return NT_STATUS_OK;
456
457 fail:
458 TALLOC_FREE(conn);
459 return status;
460}
461
462/*
463 * Get us a ctdbd connection and register us as a process
464 */
465
466NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
467 struct ctdbd_connection **pconn)
468{
469 struct ctdbd_connection *conn;
470 NTSTATUS status;
471
472 status = ctdbd_init_connection(mem_ctx, &conn);
473
474 if (!NT_STATUS_IS_OK(status)) {
475 return status;
476 }
477
478 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
479 if (!NT_STATUS_IS_OK(status)) {
480 goto fail;
481 }
482
483 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
484 if (!NT_STATUS_IS_OK(status)) {
485 goto fail;
486 }
487
488 *pconn = conn;
489 return NT_STATUS_OK;
490
491 fail:
492 TALLOC_FREE(conn);
493 return status;
494}
495
496/*
497 * Packet handler to receive and handle a ctdb message
498 */
499static NTSTATUS ctdb_handle_message(const struct data_blob *data,
500 void *private_data)
501{
502 struct ctdbd_connection *conn = talloc_get_type_abort(
503 private_data, struct ctdbd_connection);
504 struct ctdb_req_message *msg;
505 struct messaging_rec *msg_rec;
506
507 msg = (struct ctdb_req_message *)data->data;
508
509 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
510 DEBUG(0, ("Received async msg of type %u, discarding\n",
511 msg->hdr.operation));
512 return NT_STATUS_INVALID_PARAMETER;
513 }
514
515 if ((conn->release_ip_handler != NULL)
516 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
517 /* must be dispatched immediately */
518 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
519 conn->release_ip_handler((const char *)msg->data,
520 conn->release_ip_priv);
521 return NT_STATUS_OK;
522 }
523
524 SMB_ASSERT(conn->msg_ctx != NULL);
525
526 if (msg->srvid == CTDB_SRVID_RECONFIGURE) {
527 DEBUG(0,("Got cluster reconfigure message\n"));
528 /*
529 * when the cluster is reconfigured, we need to clean the brl
530 * database
531 */
532 messaging_send(conn->msg_ctx, procid_self(),
533 MSG_SMB_BRL_VALIDATE, &data_blob_null);
534
535 /*
536 * it's possible that we have just rejoined the cluster after
537 * an outage. In that case our pending locks could have been
538 * removed from the lockdb, so retry them once more
539 */
540 message_send_all(conn->msg_ctx, MSG_SMB_UNLOCK, NULL, 0, NULL);
541
542 return NT_STATUS_OK;
543
544 }
545
546 /* only messages to our pid or the broadcast are valid here */
547 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
548 DEBUG(0,("Got unexpected message with srvid=%llu\n",
549 (unsigned long long)msg->srvid));
550 return NT_STATUS_OK;
551 }
552
553 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, data->length, msg))) {
554 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
555 return NT_STATUS_NO_MEMORY;
556 }
557
558 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
559
560 TALLOC_FREE(msg_rec);
561 return NT_STATUS_OK;
562}
563
564/*
565 * The ctdbd socket is readable asynchronuously
566 */
567
568static void ctdbd_socket_handler(struct event_context *event_ctx,
569 struct fd_event *event,
570 uint16 flags,
571 void *private_data)
572{
573 struct ctdbd_connection *conn = talloc_get_type_abort(
574 private_data, struct ctdbd_connection);
575
576 NTSTATUS status;
577
578 status = packet_fd_read(conn->pkt);
579
580 if (!NT_STATUS_IS_OK(status)) {
581 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
582 cluster_fatal("ctdbd died\n");
583 }
584
585 while (packet_handler(conn->pkt, ctdb_req_complete,
586 ctdb_handle_message, conn, &status)) {
587 if (!NT_STATUS_IS_OK(status)) {
588 DEBUG(10, ("could not handle incoming message: %s\n",
589 nt_errstr(status)));
590 }
591 }
592}
593
594/*
595 * Prepare a ctdbd connection to receive messages
596 */
597
598NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
599 struct messaging_context *msg_ctx)
600{
601 SMB_ASSERT(conn->msg_ctx == NULL);
602 SMB_ASSERT(conn->fde == NULL);
603
604 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
605 packet_get_fd(conn->pkt),
606 EVENT_FD_READ,
607 ctdbd_socket_handler,
608 conn))) {
609 DEBUG(0, ("event_add_fd failed\n"));
610 return NT_STATUS_NO_MEMORY;
611 }
612
613 conn->msg_ctx = msg_ctx;
614
615 return NT_STATUS_OK;
616}
617
618/*
619 * Send a messaging message across a ctdbd
620 */
621
622NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
623 uint32 dst_vnn, uint64 dst_srvid,
624 struct messaging_rec *msg)
625{
626 struct ctdb_req_message r;
627 TALLOC_CTX *mem_ctx;
628 DATA_BLOB blob;
629 NTSTATUS status;
630 enum ndr_err_code ndr_err;
631
632 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
633 DEBUG(0, ("talloc failed\n"));
634 return NT_STATUS_NO_MEMORY;
635 }
636
637 ndr_err = ndr_push_struct_blob(
638 &blob, mem_ctx, msg,
639 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
640
641 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
642 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
643 ndr_errstr(ndr_err)));
644 status = ndr_map_error2ntstatus(ndr_err);
645 goto fail;
646 }
647
648 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
649 r.hdr.ctdb_magic = CTDB_MAGIC;
650 r.hdr.ctdb_version = CTDB_VERSION;
651 r.hdr.generation = 1;
652 r.hdr.operation = CTDB_REQ_MESSAGE;
653 r.hdr.destnode = dst_vnn;
654 r.hdr.srcnode = conn->our_vnn;
655 r.hdr.reqid = 0;
656 r.srvid = dst_srvid;
657 r.datalen = blob.length;
658
659 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
660 ctdb_packet_dump(&r.hdr);
661
662 status = packet_send(
663 conn->pkt, 2,
664 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
665 blob);
666
667 if (!NT_STATUS_IS_OK(status)) {
668 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
669 goto fail;
670 }
671
672 status = packet_flush(conn->pkt);
673
674 if (!NT_STATUS_IS_OK(status)) {
675 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
676 cluster_fatal("cluster dispatch daemon msg write error\n");
677 }
678
679 status = NT_STATUS_OK;
680 fail:
681 TALLOC_FREE(mem_ctx);
682 return status;
683}
684
685/*
686 * send/recv a generic ctdb control message
687 */
688static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
689 uint32_t vnn, uint32 opcode,
690 uint64_t srvid, uint32_t flags,
691 TDB_DATA data,
692 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
693 int *cstatus)
694{
695 struct ctdb_req_control req;
696 struct ctdb_reply_control *reply = NULL;
697 struct ctdbd_connection *new_conn = NULL;
698 NTSTATUS status;
699
700 /* the samba3 ctdb code can't handle NOREPLY yet */
701 flags &= ~CTDB_CTRL_FLAG_NOREPLY;
702
703 if (conn == NULL) {
704 status = ctdbd_init_connection(NULL, &new_conn);
705
706 if (!NT_STATUS_IS_OK(status)) {
707 DEBUG(10, ("Could not init temp connection: %s\n",
708 nt_errstr(status)));
709 goto fail;
710 }
711
712 conn = new_conn;
713 }
714
715 ZERO_STRUCT(req);
716 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
717 req.hdr.ctdb_magic = CTDB_MAGIC;
718 req.hdr.ctdb_version = CTDB_VERSION;
719 req.hdr.operation = CTDB_REQ_CONTROL;
720 req.hdr.reqid = ++conn->reqid;
721 req.hdr.destnode = vnn;
722 req.opcode = opcode;
723 req.srvid = srvid;
724 req.datalen = data.dsize;
725
726 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
727 ctdb_packet_dump(&req.hdr);
728
729 status = packet_send(
730 conn->pkt, 2,
731 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
732 data_blob_const(data.dptr, data.dsize));
733
734 if (!NT_STATUS_IS_OK(status)) {
735 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
736 goto fail;
737 }
738
739 status = packet_flush(conn->pkt);
740
741 if (!NT_STATUS_IS_OK(status)) {
742 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
743 cluster_fatal("cluster dispatch daemon control write error\n");
744 }
745
746 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
747 TALLOC_FREE(new_conn);
748 return NT_STATUS_OK;
749 }
750
751 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
752
753 if (!NT_STATUS_IS_OK(status)) {
754 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
755 goto fail;
756 }
757
758 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
759 DEBUG(0, ("received invalid reply\n"));
760 goto fail;
761 }
762
763 if (outdata) {
764 if (!(outdata->dptr = (uint8 *)talloc_memdup(
765 mem_ctx, reply->data, reply->datalen))) {
766 TALLOC_FREE(reply);
767 return NT_STATUS_NO_MEMORY;
768 }
769 outdata->dsize = reply->datalen;
770 }
771 if (cstatus) {
772 (*cstatus) = reply->status;
773 }
774
775 status = NT_STATUS_OK;
776
777 fail:
778 TALLOC_FREE(new_conn);
779 TALLOC_FREE(reply);
780 return status;
781}
782
783/*
784 * see if a remote process exists
785 */
786bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
787{
788 NTSTATUS status;
789 TDB_DATA data;
790 int32_t cstatus;
791
792 data.dptr = (uint8_t*)&pid;
793 data.dsize = sizeof(pid);
794
795 status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
796 data, NULL, NULL, &cstatus);
797 if (!NT_STATUS_IS_OK(status)) {
798 DEBUG(0, (__location__ " ctdb_control for process_exists "
799 "failed\n"));
800 return False;
801 }
802
803 return cstatus == 0;
804}
805
806/*
807 * Get a db path
808 */
809char *ctdbd_dbpath(struct ctdbd_connection *conn,
810 TALLOC_CTX *mem_ctx, uint32_t db_id)
811{
812 NTSTATUS status;
813 TDB_DATA data;
814 int32_t cstatus;
815
816 data.dptr = (uint8_t*)&db_id;
817 data.dsize = sizeof(db_id);
818
819 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
820 CTDB_CONTROL_GETDBPATH, 0, 0, data,
821 mem_ctx, &data, &cstatus);
822 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
823 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
824 return NULL;
825 }
826
827 return (char *)data.dptr;
828}
829
830/*
831 * attach to a ctdb database
832 */
833NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
834 const char *name, uint32_t *db_id, int tdb_flags)
835{
836 NTSTATUS status;
837 TDB_DATA data;
838 int32_t cstatus;
839 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
840
841 data.dptr = (uint8_t*)name;
842 data.dsize = strlen(name)+1;
843
844 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
845 persistent
846 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
847 : CTDB_CONTROL_DB_ATTACH,
848 0, 0, data, NULL, &data, &cstatus);
849 if (!NT_STATUS_IS_OK(status)) {
850 DEBUG(0, (__location__ " ctdb_control for db_attach "
851 "failed: %s\n", nt_errstr(status)));
852 return status;
853 }
854
855 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
856 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
857 return NT_STATUS_INTERNAL_ERROR;
858 }
859
860 *db_id = *(uint32_t *)data.dptr;
861 talloc_free(data.dptr);
862
863 if (!(tdb_flags & TDB_SEQNUM)) {
864 return NT_STATUS_OK;
865 }
866
867 data.dptr = (uint8_t *)db_id;
868 data.dsize = sizeof(*db_id);
869
870 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
871 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
872 NULL, NULL, &cstatus);
873 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
874 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
875 "failed\n"));
876 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
877 status;
878 }
879
880 return NT_STATUS_OK;
881}
882
883/*
884 * force the migration of a record to this node
885 */
886NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
887 TDB_DATA key)
888{
889 struct ctdb_req_call req;
890 struct ctdb_reply_call *reply;
891 NTSTATUS status;
892
893 ZERO_STRUCT(req);
894
895 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
896 req.hdr.ctdb_magic = CTDB_MAGIC;
897 req.hdr.ctdb_version = CTDB_VERSION;
898 req.hdr.operation = CTDB_REQ_CALL;
899 req.hdr.reqid = ++conn->reqid;
900 req.flags = CTDB_IMMEDIATE_MIGRATION;
901 req.callid = CTDB_NULL_FUNC;
902 req.db_id = db_id;
903 req.keylen = key.dsize;
904
905 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
906 ctdb_packet_dump(&req.hdr);
907
908 status = packet_send(
909 conn->pkt, 2,
910 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
911 data_blob_const(key.dptr, key.dsize));
912
913 if (!NT_STATUS_IS_OK(status)) {
914 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
915 return status;
916 }
917
918 status = packet_flush(conn->pkt);
919
920 if (!NT_STATUS_IS_OK(status)) {
921 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
922 cluster_fatal("cluster dispatch daemon control write error\n");
923 }
924
925 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
926
927 if (!NT_STATUS_IS_OK(status)) {
928 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
929 goto fail;
930 }
931
932 if (reply->hdr.operation != CTDB_REPLY_CALL) {
933 DEBUG(0, ("received invalid reply\n"));
934 status = NT_STATUS_INTERNAL_ERROR;
935 goto fail;
936 }
937
938 status = NT_STATUS_OK;
939 fail:
940
941 TALLOC_FREE(reply);
942 return status;
943}
944
945/*
946 * remotely fetch a record without locking it or forcing a migration
947 */
948NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
949 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
950{
951 struct ctdb_req_call req;
952 struct ctdb_reply_call *reply;
953 NTSTATUS status;
954
955 ZERO_STRUCT(req);
956
957 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
958 req.hdr.ctdb_magic = CTDB_MAGIC;
959 req.hdr.ctdb_version = CTDB_VERSION;
960 req.hdr.operation = CTDB_REQ_CALL;
961 req.hdr.reqid = ++conn->reqid;
962 req.flags = 0;
963 req.callid = CTDB_FETCH_FUNC;
964 req.db_id = db_id;
965 req.keylen = key.dsize;
966
967 status = packet_send(
968 conn->pkt, 2,
969 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
970 data_blob_const(key.dptr, key.dsize));
971
972 if (!NT_STATUS_IS_OK(status)) {
973 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
974 return status;
975 }
976
977 status = packet_flush(conn->pkt);
978
979 if (!NT_STATUS_IS_OK(status)) {
980 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
981 cluster_fatal("cluster dispatch daemon control write error\n");
982 }
983
984 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
985
986 if (!NT_STATUS_IS_OK(status)) {
987 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
988 goto fail;
989 }
990
991 if (reply->hdr.operation != CTDB_REPLY_CALL) {
992 DEBUG(0, ("received invalid reply\n"));
993 status = NT_STATUS_INTERNAL_ERROR;
994 goto fail;
995 }
996
997 data->dsize = reply->datalen;
998 if (data->dsize == 0) {
999 data->dptr = NULL;
1000 goto done;
1001 }
1002
1003 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1004 reply->datalen);
1005 if (data->dptr == NULL) {
1006 DEBUG(0, ("talloc failed\n"));
1007 status = NT_STATUS_NO_MEMORY;
1008 goto fail;
1009 }
1010
1011 done:
1012 status = NT_STATUS_OK;
1013 fail:
1014 TALLOC_FREE(reply);
1015 return status;
1016}
1017
1018struct ctdbd_traverse_state {
1019 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1020 void *private_data;
1021};
1022
1023/*
1024 * Handle a traverse record coming in on the ctdbd connection
1025 */
1026
1027static NTSTATUS ctdb_traverse_handler(const struct data_blob *blob,
1028 void *private_data)
1029{
1030 struct ctdbd_traverse_state *state =
1031 (struct ctdbd_traverse_state *)private_data;
1032
1033 struct ctdb_req_message *m;
1034 struct ctdb_rec_data *d;
1035 TDB_DATA key, data;
1036
1037 m = (struct ctdb_req_message *)blob->data;
1038
1039 if (blob->length < sizeof(*m) || m->hdr.length != blob->length) {
1040 DEBUG(0, ("Got invalid message of length %d\n",
1041 (int)blob->length));
1042 return NT_STATUS_UNEXPECTED_IO_ERROR;
1043 }
1044
1045 d = (struct ctdb_rec_data *)&m->data[0];
1046 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1047 DEBUG(0, ("Got invalid traverse data of length %d\n",
1048 (int)m->datalen));
1049 return NT_STATUS_UNEXPECTED_IO_ERROR;
1050 }
1051
1052 key.dsize = d->keylen;
1053 key.dptr = &d->data[0];
1054 data.dsize = d->datalen;
1055 data.dptr = &d->data[d->keylen];
1056
1057 if (key.dsize == 0 && data.dsize == 0) {
1058 /* end of traverse */
1059 return NT_STATUS_END_OF_FILE;
1060 }
1061
1062 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1063 DEBUG(0, ("Got invalid ltdb header length %d\n",
1064 (int)data.dsize));
1065 return NT_STATUS_UNEXPECTED_IO_ERROR;
1066 }
1067 data.dsize -= sizeof(struct ctdb_ltdb_header);
1068 data.dptr += sizeof(struct ctdb_ltdb_header);
1069
1070 if (state->fn) {
1071 state->fn(key, data, state->private_data);
1072 }
1073
1074 return NT_STATUS_OK;
1075}
1076
1077/*
1078 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1079 connection to ctdbd to avoid the hairy recursive and async problems with
1080 everything in-line.
1081*/
1082
1083NTSTATUS ctdbd_traverse(uint32 db_id,
1084 void (*fn)(TDB_DATA key, TDB_DATA data,
1085 void *private_data),
1086 void *private_data)
1087{
1088 struct ctdbd_connection *conn;
1089 NTSTATUS status;
1090
1091 TDB_DATA data;
1092 struct ctdb_traverse_start t;
1093 int cstatus;
1094 struct ctdbd_traverse_state state;
1095
1096 status = ctdbd_init_connection(NULL, &conn);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1099 nt_errstr(status)));
1100 return status;
1101 }
1102
1103 t.db_id = db_id;
1104 t.srvid = conn->rand_srvid;
1105 t.reqid = ++conn->reqid;
1106
1107 data.dptr = (uint8_t *)&t;
1108 data.dsize = sizeof(t);
1109
1110 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1111 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1112 data, NULL, NULL, &cstatus);
1113
1114 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1115
1116 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1117 cstatus));
1118
1119 if (NT_STATUS_IS_OK(status)) {
1120 /*
1121 * We need a mapping here
1122 */
1123 status = NT_STATUS_UNSUCCESSFUL;
1124 }
1125 goto done;
1126 }
1127
1128 state.fn = fn;
1129 state.private_data = private_data;
1130
1131 while (True) {
1132
1133 status = NT_STATUS_OK;
1134
1135 if (packet_handler(conn->pkt, ctdb_req_complete,
1136 ctdb_traverse_handler, &state, &status)) {
1137
1138 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1139 status = NT_STATUS_OK;
1140 break;
1141 }
1142
1143 /*
1144 * There might be more in the queue
1145 */
1146 continue;
1147 }
1148
1149 if (!NT_STATUS_IS_OK(status)) {
1150 break;
1151 }
1152
1153 status = packet_fd_read_sync(conn->pkt);
1154
1155 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1156 /*
1157 * There might be more in the queue
1158 */
1159 continue;
1160 }
1161
1162 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1163 status = NT_STATUS_OK;
1164 }
1165
1166 if (!NT_STATUS_IS_OK(status)) {
1167 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1168 cluster_fatal("ctdbd died\n");
1169 }
1170 }
1171
1172 done:
1173 TALLOC_FREE(conn);
1174 return status;
1175}
1176
1177/*
1178 This is used to canonicalize a ctdb_sock_addr structure.
1179*/
1180static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1181 struct sockaddr_storage *out)
1182{
1183 memcpy(out, in, sizeof (*out));
1184
1185#ifdef HAVE_IPV6
1186 if (in->ss_family == AF_INET6) {
1187 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1188 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1189 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1190 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1191 memset(out, 0, sizeof(*out));
1192#ifdef HAVE_SOCK_SIN_LEN
1193 out4->sin_len = sizeof(*out);
1194#endif
1195 out4->sin_family = AF_INET;
1196 out4->sin_port = in6->sin6_port;
1197 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
1198 }
1199 }
1200#endif
1201}
1202
1203/*
1204 * Register us as a server for a particular tcp connection
1205 */
1206
1207NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1208 const struct sockaddr_storage *_server,
1209 const struct sockaddr_storage *_client,
1210 void (*release_ip_handler)(const char *ip_addr,
1211 void *private_data),
1212 void *private_data)
1213{
1214 /*
1215 * we still use ctdb_control_tcp for ipv4
1216 * because we want to work against older ctdb
1217 * versions at runtime
1218 */
1219 struct ctdb_control_tcp p4;
1220#ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1221 struct ctdb_control_tcp_addr p;
1222#endif
1223 TDB_DATA data;
1224 NTSTATUS status;
1225 struct sockaddr_storage client;
1226 struct sockaddr_storage server;
1227
1228 /*
1229 * Only one connection so far
1230 */
1231 SMB_ASSERT(conn->release_ip_handler == NULL);
1232
1233 smbd_ctdb_canonicalize_ip(_client, &client);
1234 smbd_ctdb_canonicalize_ip(_server, &server);
1235
1236 switch (client.ss_family) {
1237 case AF_INET:
1238 p4.dest = *(struct sockaddr_in *)&server;
1239 p4.src = *(struct sockaddr_in *)&client;
1240 data.dptr = (uint8_t *)&p4;
1241 data.dsize = sizeof(p4);
1242 break;
1243#ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1244 case AF_INET6:
1245 p.dest.ip6 = *(struct sockaddr_in6 *)&server;
1246 p.src.ip6 = *(struct sockaddr_in6 *)&client;
1247 data.dptr = (uint8_t *)&p;
1248 data.dsize = sizeof(p);
1249 break;
1250#endif
1251 default:
1252 return NT_STATUS_INTERNAL_ERROR;
1253 }
1254
1255 conn->release_ip_handler = release_ip_handler;
1256
1257 /*
1258 * We want to be told about IP releases
1259 */
1260
1261 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1262 if (!NT_STATUS_IS_OK(status)) {
1263 return status;
1264 }
1265
1266 /*
1267 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1268 * can send an extra ack to trigger a reset for our client, so it
1269 * immediately reconnects
1270 */
1271 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1272 CTDB_CONTROL_TCP_CLIENT, 0,
1273 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1274}
1275
1276/*
1277 * We want to handle reconfigure events
1278 */
1279NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1280{
1281 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1282}
1283
1284/*
1285 call a control on the local node
1286 */
1287NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
1288 uint64_t srvid, uint32_t flags, TDB_DATA data,
1289 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1290 int *cstatus)
1291{
1292 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1293}
1294
1295#else
1296
1297NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1298 struct ctdbd_connection **pconn)
1299{
1300 return NT_STATUS_NOT_IMPLEMENTED;
1301}
1302
1303#endif
Note: See TracBrowser for help on using the repository browser.