940d477da1b31dcd6b9826cd3b15ab67a19fa25a
[mat/samba.git] / source3 / lib / ctdbd_conn.c
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 #include "util_tdb.h"
23
24 #ifdef CLUSTER_SUPPORT
25
26 #include "ctdbd_conn.h"
27 #include "ctdb_packet.h"
28 #include "messages.h"
29
30 /*
31  * It is not possible to include ctdb.h and tdb_compat.h (included via
32  * some other include above) without warnings. This fixes those
33  * warnings.
34  */
35
36 #ifdef typesafe_cb
37 #undef typesafe_cb
38 #endif
39
40 #ifdef typesafe_cb_preargs
41 #undef typesafe_cb_preargs
42 #endif
43
44 #ifdef typesafe_cb_postargs
45 #undef typesafe_cb_postargs
46 #endif
47
48 /* paths to these include files come from --with-ctdb= in configure */
49
50 #include "ctdb.h"
51 #include "ctdb_private.h"
52
53 struct ctdbd_connection {
54         struct messaging_context *msg_ctx;
55         uint32 reqid;
56         uint32 our_vnn;
57         uint64 rand_srvid;
58         struct ctdb_packet_context *pkt;
59         struct fd_event *fde;
60
61         void (*release_ip_handler)(const char *ip_addr, void *private_data);
62         void *release_ip_priv;
63 };
64
65 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
66 {
67         conn->reqid += 1;
68         if (conn->reqid == 0) {
69                 conn->reqid += 1;
70         }
71         return conn->reqid;
72 }
73
74 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
75                               uint32_t vnn, uint32 opcode, 
76                               uint64_t srvid, uint32_t flags, TDB_DATA data, 
77                               TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
78                               int *cstatus);
79
80 /*
81  * exit on fatal communications errors with the ctdbd daemon
82  */
83 static void cluster_fatal(const char *why)
84 {
85         DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
86         /* we don't use smb_panic() as we don't want to delay to write
87            a core file. We need to release this process id immediately
88            so that someone else can take over without getting sharing
89            violations */
90         _exit(1);
91 }
92
93 /*
94  *
95  */
96 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
97 {
98         if (DEBUGLEVEL < 10) {
99                 return;
100         }
101         DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
102                       (int)hdr->length, (int)hdr->ctdb_magic,
103                       (int)hdr->ctdb_version, (int)hdr->generation,
104                       (int)hdr->operation, (int)hdr->reqid));
105 }
106
107 /*
108  * Register a srvid with ctdbd
109  */
110 NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid)
111 {
112
113         int cstatus;
114         return ctdbd_control(conn, CTDB_CURRENT_NODE,
115                              CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
116                              tdb_null, NULL, NULL, &cstatus);
117 }
118
119 /*
120  * get our vnn from the cluster
121  */
122 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
123 {
124         int32_t cstatus=-1;
125         NTSTATUS status;
126         status = ctdbd_control(conn,
127                                CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
128                                tdb_null, NULL, NULL, &cstatus);
129         if (!NT_STATUS_IS_OK(status)) {
130                 cluster_fatal("ctdbd_control failed\n");
131         }
132         *vnn = (uint32_t)cstatus;
133         return status;
134 }
135
136 /*
137  * Are we active (i.e. not banned or stopped?)
138  */
139 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
140 {
141         int32_t cstatus=-1;
142         NTSTATUS status;
143         TDB_DATA outdata;
144         struct ctdb_node_map *m;
145         uint32_t failure_flags;
146         bool ret = false;
147         int i;
148
149         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
150                                CTDB_CONTROL_GET_NODEMAP, 0, 0,
151                                tdb_null, talloc_tos(), &outdata, &cstatus);
152         if (!NT_STATUS_IS_OK(status)) {
153                 cluster_fatal("ctdbd_control failed\n");
154         }
155         if ((cstatus != 0) || (outdata.dptr == NULL)) {
156                 DEBUG(2, ("Received invalid ctdb data\n"));
157                 return false;
158         }
159
160         m = (struct ctdb_node_map *)outdata.dptr;
161
162         for (i=0; i<m->num; i++) {
163                 if (vnn == m->nodes[i].pnn) {
164                         break;
165                 }
166         }
167
168         if (i == m->num) {
169                 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
170                           (int)vnn));
171                 goto fail;
172         }
173
174         failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
175                 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
176
177         if ((m->nodes[i].flags & failure_flags) != 0) {
178                 DEBUG(2, ("Node has status %x, not active\n",
179                           (int)m->nodes[i].flags));
180                 goto fail;
181         }
182
183         ret = true;
184 fail:
185         TALLOC_FREE(outdata.dptr);
186         return ret;
187 }
188
189 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
190 {
191         return conn->our_vnn;
192 }
193
194 /*
195  * Get us a ctdb connection
196  */
197
198 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
199                               struct ctdb_packet_context **presult)
200 {
201         struct ctdb_packet_context *result;
202         const char *sockname = lp_ctdbd_socket();
203         struct sockaddr_un addr;
204         int fd;
205
206         if (!sockname || !*sockname) {
207                 sockname = CTDB_PATH;
208         }
209
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);
214         }
215
216         ZERO_STRUCT(addr);
217         addr.sun_family = AF_UNIX;
218         strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
219
220         if (sys_connect(fd, (struct sockaddr *)(void *)&addr) == -1) {
221                 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
222                           strerror(errno)));
223                 close(fd);
224                 return map_nt_error_from_unix(errno);
225         }
226
227         if (!(result = ctdb_packet_init(mem_ctx, fd))) {
228                 close(fd);
229                 return NT_STATUS_NO_MEMORY;
230         }
231
232         *presult = result;
233         return NT_STATUS_OK;
234 }
235
236 /*
237  * Do we have a complete ctdb packet in the queue?
238  */
239
240 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
241                               size_t *length,
242                               void *private_data)
243 {
244         uint32 msglen;
245
246         if (available < sizeof(msglen)) {
247                 return False;
248         }
249
250         msglen = *((uint32 *)buf);
251
252         DEBUG(10, ("msglen = %d\n", msglen));
253
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");
259         }
260
261         if (available < msglen) {
262                 return false;
263         }
264
265         *length = msglen;
266         return true;
267 }
268
269 /*
270  * State necessary to defer an incoming message while we are waiting for a
271  * ctdb reply.
272  */
273
274 struct deferred_msg_state {
275         struct messaging_context *msg_ctx;
276         struct messaging_rec *rec;
277 };
278
279 /*
280  * Timed event handler for the deferred message
281  */
282
283 static void deferred_message_dispatch(struct event_context *event_ctx,
284                                       struct timed_event *te,
285                                       struct timeval now,
286                                       void *private_data)
287 {
288         struct deferred_msg_state *state = talloc_get_type_abort(
289                 private_data, struct deferred_msg_state);
290
291         messaging_dispatch_rec(state->msg_ctx, state->rec);
292         TALLOC_FREE(state);
293         TALLOC_FREE(te);
294 }
295
296 struct req_pull_state {
297         TALLOC_CTX *mem_ctx;
298         DATA_BLOB req;
299 };
300
301 /*
302  * Pull a ctdb request out of the incoming ctdb_packet queue
303  */
304
305 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
306                               void *private_data)
307 {
308         struct req_pull_state *state = (struct req_pull_state *)private_data;
309
310         state->req.data = talloc_move(state->mem_ctx, &buf);
311         state->req.length = length;
312         return NT_STATUS_OK;
313 }
314
315 /*
316  * Fetch a messaging_rec from an incoming ctdb style message
317  */
318
319 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
320                                                      size_t overall_length,
321                                                      struct ctdb_req_message *msg)
322 {
323         struct messaging_rec *result;
324         DATA_BLOB blob;
325         enum ndr_err_code ndr_err;
326
327         if ((overall_length < offsetof(struct ctdb_req_message, data))
328             || (overall_length
329                 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
330
331                 cluster_fatal("got invalid msg length");
332         }
333
334         if (!(result = talloc(mem_ctx, struct messaging_rec))) {
335                 DEBUG(0, ("talloc failed\n"));
336                 return NULL;
337         }
338
339         blob = data_blob_const(msg->data, msg->datalen);
340
341         ndr_err = ndr_pull_struct_blob(
342                 &blob, result, result,
343                 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
344
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;
350         }
351
352         if (DEBUGLEVEL >= 10) {
353                 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
354                 NDR_PRINT_DEBUG(messaging_rec, result);
355         }
356
357         return result;
358 }
359
360 static NTSTATUS ctdb_packet_fd_read_sync(struct ctdb_packet_context *ctx)
361 {
362         int timeout = lp_ctdb_timeout();
363
364         if (timeout == 0) {
365                 timeout = -1;
366         }
367         return ctdb_packet_fd_read_sync_timeout(ctx, timeout);
368 }
369
370 /*
371  * Read a full ctdbd request. If we have a messaging context, defer incoming
372  * messages that might come in between.
373  */
374
375 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
376                               TALLOC_CTX *mem_ctx, void *result)
377 {
378         struct ctdb_req_header *hdr;
379         struct req_pull_state state;
380         NTSTATUS status;
381
382  next_pkt:
383         ZERO_STRUCT(state);
384         state.mem_ctx = mem_ctx;
385
386         while (!ctdb_packet_handler(conn->pkt, ctdb_req_complete,
387                                     ctdb_req_pull, &state, &status)) {
388                 /*
389                  * Not enough data
390                  */
391                 status = ctdb_packet_fd_read_sync(conn->pkt);
392
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;
399                 }
400
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");
404                 }
405         }
406
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");
410         }
411
412         hdr = (struct ctdb_req_header *)state.req.data;
413
414         DEBUG(10, ("Received ctdb packet\n"));
415         ctdb_packet_dump(hdr);
416
417         if (hdr->operation == CTDB_REQ_MESSAGE) {
418                 struct timed_event *evt;
419                 struct deferred_msg_state *msg_state;
420                 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
421
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;
427                 }
428
429                 if ((conn->release_ip_handler != NULL)
430                     && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
431                         /* must be dispatched immediately */
432                         DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
433                         conn->release_ip_handler((const char *)msg->data,
434                                                  conn->release_ip_priv);
435                         TALLOC_FREE(hdr);
436                         goto next_pkt;
437                 }
438
439                 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
440                     || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
441
442                         DEBUG(1, ("ctdb_read_req: Got %s message\n",
443                                   (msg->srvid == CTDB_SRVID_RECONFIGURE)
444                                   ? "cluster reconfigure" : "SAMBA_NOTIFY"));
445
446                         messaging_send(conn->msg_ctx,
447                                        messaging_server_id(conn->msg_ctx),
448                                        MSG_SMB_BRL_VALIDATE, &data_blob_null);
449                         messaging_send(conn->msg_ctx,
450                                        messaging_server_id(conn->msg_ctx),
451                                        MSG_DBWRAP_G_LOCK_RETRY,
452                                        &data_blob_null);
453                         TALLOC_FREE(hdr);
454                         goto next_pkt;
455                 }
456
457                 msg_state = talloc(NULL, struct deferred_msg_state);
458                 if (msg_state == NULL) {
459                         DEBUG(0, ("talloc failed\n"));
460                         TALLOC_FREE(hdr);
461                         goto next_pkt;
462                 }
463
464                 if (!(msg_state->rec = ctdb_pull_messaging_rec(
465                               msg_state, state.req.length, msg))) {
466                         DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
467                         TALLOC_FREE(msg_state);
468                         TALLOC_FREE(hdr);
469                         goto next_pkt;
470                 }
471
472                 TALLOC_FREE(hdr);
473
474                 msg_state->msg_ctx = conn->msg_ctx;
475
476                 /*
477                  * We're waiting for a call reply, but an async message has
478                  * crossed. Defer dispatching to the toplevel event loop.
479                  */
480                 evt = event_add_timed(conn->msg_ctx->event_ctx,
481                                       conn->msg_ctx->event_ctx,
482                                       timeval_zero(),
483                                       deferred_message_dispatch,
484                                       msg_state);
485                 if (evt == NULL) {
486                         DEBUG(0, ("event_add_timed failed\n"));
487                         TALLOC_FREE(msg_state);
488                         TALLOC_FREE(hdr);
489                         goto next_pkt;
490                 }
491
492                 goto next_pkt;
493         }
494
495         if ((reqid != 0) && (hdr->reqid != reqid)) {
496                 /* we got the wrong reply */
497                 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
498                          "been %u\n", hdr->reqid, reqid));
499                 TALLOC_FREE(hdr);
500                 goto next_pkt;
501         }
502
503         *((void **)result) = talloc_move(mem_ctx, &hdr);
504
505         return NT_STATUS_OK;
506 }
507
508 /*
509  * Get us a ctdbd connection
510  */
511
512 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
513                                       struct ctdbd_connection **pconn)
514 {
515         struct ctdbd_connection *conn;
516         NTSTATUS status;
517
518         if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
519                 DEBUG(0, ("talloc failed\n"));
520                 return NT_STATUS_NO_MEMORY;
521         }
522
523         status = ctdbd_connect(conn, &conn->pkt);
524
525         if (!NT_STATUS_IS_OK(status)) {
526                 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
527                 goto fail;
528         }
529
530         status = get_cluster_vnn(conn, &conn->our_vnn);
531
532         if (!NT_STATUS_IS_OK(status)) {
533                 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
534                 goto fail;
535         }
536
537         if (!ctdbd_working(conn, conn->our_vnn)) {
538                 DEBUG(2, ("Node is not working, can not connect\n"));
539                 status = NT_STATUS_INTERNAL_DB_ERROR;
540                 goto fail;
541         }
542
543         generate_random_buffer((unsigned char *)&conn->rand_srvid,
544                                sizeof(conn->rand_srvid));
545
546         status = register_with_ctdbd(conn, conn->rand_srvid);
547
548         if (!NT_STATUS_IS_OK(status)) {
549                 DEBUG(5, ("Could not register random srvid: %s\n",
550                           nt_errstr(status)));
551                 goto fail;
552         }
553
554         *pconn = conn;
555         return NT_STATUS_OK;
556
557  fail:
558         TALLOC_FREE(conn);
559         return status;
560 }
561
562 /*
563  * Get us a ctdbd connection and register us as a process
564  */
565
566 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
567                                     struct ctdbd_connection **pconn)
568 {
569         struct ctdbd_connection *conn;
570         NTSTATUS status;
571
572         status = ctdbd_init_connection(mem_ctx, &conn);
573
574         if (!NT_STATUS_IS_OK(status)) {
575                 return status;
576         }
577
578         status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
579         if (!NT_STATUS_IS_OK(status)) {
580                 goto fail;
581         }
582
583         status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
584         if (!NT_STATUS_IS_OK(status)) {
585                 goto fail;
586         }
587
588         status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
589         if (!NT_STATUS_IS_OK(status)) {
590                 goto fail;
591         }
592
593         *pconn = conn;
594         return NT_STATUS_OK;
595
596  fail:
597         TALLOC_FREE(conn);
598         return status;
599 }
600
601 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
602 {
603         return conn->msg_ctx;
604 }
605
606 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
607 {
608         return ctdb_packet_get_fd(conn->pkt);
609 }
610
611 /*
612  * Packet handler to receive and handle a ctdb message
613  */
614 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
615                                     void *private_data)
616 {
617         struct ctdbd_connection *conn = talloc_get_type_abort(
618                 private_data, struct ctdbd_connection);
619         struct ctdb_req_message *msg;
620         struct messaging_rec *msg_rec;
621
622         msg = (struct ctdb_req_message *)buf;
623
624         if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
625                 DEBUG(0, ("Received async msg of type %u, discarding\n",
626                           msg->hdr.operation));
627                 TALLOC_FREE(buf);
628                 return NT_STATUS_INVALID_PARAMETER;
629         }
630
631         if ((conn->release_ip_handler != NULL)
632             && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
633                 /* must be dispatched immediately */
634                 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
635                 conn->release_ip_handler((const char *)msg->data,
636                                          conn->release_ip_priv);
637                 TALLOC_FREE(buf);
638                 return NT_STATUS_OK;
639         }
640
641         SMB_ASSERT(conn->msg_ctx != NULL);
642
643         if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
644             || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
645                 DEBUG(0,("Got cluster reconfigure message\n"));
646                 /*
647                  * when the cluster is reconfigured or someone of the
648                  * family has passed away (SAMBA_NOTIFY), we need to
649                  * clean the brl database
650                  */
651                 messaging_send(conn->msg_ctx,
652                                messaging_server_id(conn->msg_ctx),
653                                MSG_SMB_BRL_VALIDATE, &data_blob_null);
654
655                 messaging_send(conn->msg_ctx,
656                                messaging_server_id(conn->msg_ctx),
657                                MSG_DBWRAP_G_LOCK_RETRY,
658                                &data_blob_null);
659
660                 TALLOC_FREE(buf);
661                 return NT_STATUS_OK;
662         }
663
664         /* only messages to our pid or the broadcast are valid here */
665         if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
666                 DEBUG(0,("Got unexpected message with srvid=%llu\n", 
667                          (unsigned long long)msg->srvid));
668                 TALLOC_FREE(buf);
669                 return NT_STATUS_OK;
670         }
671
672         if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
673                 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
674                 TALLOC_FREE(buf);
675                 return NT_STATUS_NO_MEMORY;
676         }
677
678         messaging_dispatch_rec(conn->msg_ctx, msg_rec);
679
680         TALLOC_FREE(msg_rec);
681         TALLOC_FREE(buf);
682         return NT_STATUS_OK;
683 }
684
685 /*
686  * The ctdbd socket is readable asynchronuously
687  */
688
689 static void ctdbd_socket_handler(struct event_context *event_ctx,
690                                  struct fd_event *event,
691                                  uint16 flags,
692                                  void *private_data)
693 {
694         struct ctdbd_connection *conn = talloc_get_type_abort(
695                 private_data, struct ctdbd_connection);
696
697         NTSTATUS status;
698
699         status = ctdb_packet_fd_read(conn->pkt);
700
701         if (!NT_STATUS_IS_OK(status)) {
702                 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
703                 cluster_fatal("ctdbd died\n");
704         }
705
706         while (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
707                               ctdb_handle_message, conn, &status)) {
708                 if (!NT_STATUS_IS_OK(status)) {
709                         DEBUG(10, ("could not handle incoming message: %s\n",
710                                    nt_errstr(status)));
711                 }
712         }
713 }
714
715 /*
716  * Prepare a ctdbd connection to receive messages
717  */
718
719 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
720                                 struct messaging_context *msg_ctx)
721 {
722         SMB_ASSERT(conn->msg_ctx == NULL);
723         SMB_ASSERT(conn->fde == NULL);
724
725         if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
726                                        ctdb_packet_get_fd(conn->pkt),
727                                        EVENT_FD_READ,
728                                        ctdbd_socket_handler,
729                                        conn))) {
730                 DEBUG(0, ("event_add_fd failed\n"));
731                 return NT_STATUS_NO_MEMORY;
732         }
733
734         conn->msg_ctx = msg_ctx;
735
736         return NT_STATUS_OK;
737 }
738
739 /*
740  * Send a messaging message across a ctdbd
741  */
742
743 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
744                               uint32 dst_vnn, uint64 dst_srvid,
745                               struct messaging_rec *msg)
746 {
747         struct ctdb_req_message r;
748         TALLOC_CTX *mem_ctx;
749         DATA_BLOB blob;
750         NTSTATUS status;
751         enum ndr_err_code ndr_err;
752
753         if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
754                 DEBUG(0, ("talloc failed\n"));
755                 return NT_STATUS_NO_MEMORY;
756         }
757
758         ndr_err = ndr_push_struct_blob(
759                 &blob, mem_ctx, msg,
760                 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
761
762         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
763                 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
764                           ndr_errstr(ndr_err)));
765                 status = ndr_map_error2ntstatus(ndr_err);
766                 goto fail;
767         }
768
769         r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
770         r.hdr.ctdb_magic = CTDB_MAGIC;
771         r.hdr.ctdb_version = CTDB_VERSION;
772         r.hdr.generation = 1;
773         r.hdr.operation  = CTDB_REQ_MESSAGE;
774         r.hdr.destnode   = dst_vnn;
775         r.hdr.srcnode    = conn->our_vnn;
776         r.hdr.reqid      = 0;
777         r.srvid          = dst_srvid;
778         r.datalen        = blob.length;
779
780         DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
781         ctdb_packet_dump(&r.hdr);
782
783         status = ctdb_packet_send(
784                 conn->pkt, 2,
785                 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
786                 blob);
787
788         if (!NT_STATUS_IS_OK(status)) {
789                 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
790                 goto fail;
791         }
792
793         status = ctdb_packet_flush(conn->pkt);
794
795         if (!NT_STATUS_IS_OK(status)) {
796                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
797                 cluster_fatal("cluster dispatch daemon msg write error\n");
798         }
799
800         status = NT_STATUS_OK;
801  fail:
802         TALLOC_FREE(mem_ctx);
803         return status;
804 }
805
806 /*
807  * send/recv a generic ctdb control message
808  */
809 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
810                               uint32_t vnn, uint32 opcode, 
811                               uint64_t srvid, uint32_t flags, 
812                               TDB_DATA data, 
813                               TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
814                               int *cstatus)
815 {
816         struct ctdb_req_control req;
817         struct ctdb_reply_control *reply = NULL;
818         struct ctdbd_connection *new_conn = NULL;
819         NTSTATUS status;
820
821         if (conn == NULL) {
822                 status = ctdbd_init_connection(NULL, &new_conn);
823
824                 if (!NT_STATUS_IS_OK(status)) {
825                         DEBUG(10, ("Could not init temp connection: %s\n",
826                                    nt_errstr(status)));
827                         goto fail;
828                 }
829
830                 conn = new_conn;
831         }
832
833         ZERO_STRUCT(req);
834         req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
835         req.hdr.ctdb_magic   = CTDB_MAGIC;
836         req.hdr.ctdb_version = CTDB_VERSION;
837         req.hdr.operation    = CTDB_REQ_CONTROL;
838         req.hdr.reqid        = ctdbd_next_reqid(conn);
839         req.hdr.destnode     = vnn;
840         req.opcode           = opcode;
841         req.srvid            = srvid;
842         req.datalen          = data.dsize;
843         req.flags            = flags;
844
845         DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
846         ctdb_packet_dump(&req.hdr);
847
848         status = ctdb_packet_send(
849                 conn->pkt, 2,
850                 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
851                 data_blob_const(data.dptr, data.dsize));
852
853         if (!NT_STATUS_IS_OK(status)) {
854                 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
855                 goto fail;
856         }
857
858         status = ctdb_packet_flush(conn->pkt);
859
860         if (!NT_STATUS_IS_OK(status)) {
861                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
862                 cluster_fatal("cluster dispatch daemon control write error\n");
863         }
864
865         if (flags & CTDB_CTRL_FLAG_NOREPLY) {
866                 TALLOC_FREE(new_conn);
867                 if (cstatus) {
868                         *cstatus = 0;
869                 }
870                 return NT_STATUS_OK;
871         }
872
873         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
874
875         if (!NT_STATUS_IS_OK(status)) {
876                 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
877                 goto fail;
878         }
879
880         if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
881                 DEBUG(0, ("received invalid reply\n"));
882                 goto fail;
883         }
884
885         if (outdata) {
886                 if (!(outdata->dptr = (uint8 *)talloc_memdup(
887                               mem_ctx, reply->data, reply->datalen))) {
888                         TALLOC_FREE(reply);
889                         return NT_STATUS_NO_MEMORY;
890                 }
891                 outdata->dsize = reply->datalen;
892         }
893         if (cstatus) {
894                 (*cstatus) = reply->status;
895         }
896
897         status = NT_STATUS_OK;
898
899  fail:
900         TALLOC_FREE(new_conn);
901         TALLOC_FREE(reply);
902         return status;
903 }
904
905 /*
906  * see if a remote process exists
907  */
908 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
909 {
910         struct server_id id;
911         bool result;
912
913         id.pid = pid;
914         id.vnn = vnn;
915
916         if (!ctdb_processes_exist(conn, &id, 1, &result)) {
917                 DEBUG(10, ("ctdb_processes_exist failed\n"));
918                 return false;
919         }
920         return result;
921 }
922
923 bool ctdb_processes_exist(struct ctdbd_connection *conn,
924                           const struct server_id *pids, int num_pids,
925                           bool *results)
926 {
927         TALLOC_CTX *frame = talloc_stackframe();
928         int i, num_received;
929         NTSTATUS status;
930         uint32_t *reqids;
931         bool result = false;
932
933         reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
934         if (reqids == NULL) {
935                 goto fail;
936         }
937
938         for (i=0; i<num_pids; i++) {
939                 struct ctdb_req_control req;
940                 pid_t pid;
941
942                 results[i] = false;
943                 reqids[i] = ctdbd_next_reqid(conn);
944
945                 ZERO_STRUCT(req);
946
947                 /*
948                  * pids[i].pid is uint64_t, scale down to pid_t which
949                  * is the wire protocol towards ctdb.
950                  */
951                 pid = pids[i].pid;
952
953                 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
954                            (int)pids[i].vnn, (int)pid,
955                            (int)reqids[i]));
956
957                 req.hdr.length = offsetof(struct ctdb_req_control, data);
958                 req.hdr.length += sizeof(pid);
959                 req.hdr.ctdb_magic   = CTDB_MAGIC;
960                 req.hdr.ctdb_version = CTDB_VERSION;
961                 req.hdr.operation    = CTDB_REQ_CONTROL;
962                 req.hdr.reqid        = reqids[i];
963                 req.hdr.destnode     = pids[i].vnn;
964                 req.opcode           = CTDB_CONTROL_PROCESS_EXISTS;
965                 req.srvid            = 0;
966                 req.datalen          = sizeof(pid);
967                 req.flags            = 0;
968
969                 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
970                 ctdb_packet_dump(&req.hdr);
971
972                 status = ctdb_packet_send(
973                         conn->pkt, 2,
974                         data_blob_const(
975                                 &req, offsetof(struct ctdb_req_control, data)),
976                         data_blob_const(&pid, sizeof(pid)));
977                 if (!NT_STATUS_IS_OK(status)) {
978                         DEBUG(10, ("ctdb_packet_send failed: %s\n",
979                                    nt_errstr(status)));
980                         goto fail;
981                 }
982         }
983
984         status = ctdb_packet_flush(conn->pkt);
985         if (!NT_STATUS_IS_OK(status)) {
986                 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
987                            nt_errstr(status)));
988                 goto fail;
989         }
990
991         num_received = 0;
992
993         while (num_received < num_pids) {
994                 struct ctdb_reply_control *reply = NULL;
995                 uint32_t reqid;
996
997                 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
998                 if (!NT_STATUS_IS_OK(status)) {
999                         DEBUG(10, ("ctdb_read_req failed: %s\n",
1000                                    nt_errstr(status)));
1001                         goto fail;
1002                 }
1003
1004                 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1005                         DEBUG(10, ("Received invalid reply\n"));
1006                         goto fail;
1007                 }
1008
1009                 reqid = reply->hdr.reqid;
1010
1011                 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1012
1013                 for (i=0; i<num_pids; i++) {
1014                         if (reqid == reqids[i]) {
1015                                 break;
1016                         }
1017                 }
1018                 if (i == num_pids) {
1019                         DEBUG(10, ("Received unknown record number %u\n",
1020                                    (unsigned)reqid));
1021                         goto fail;
1022                 }
1023                 results[i] = ((reply->status) == 0);
1024                 TALLOC_FREE(reply);
1025                 num_received += 1;
1026         }
1027
1028         result = true;
1029 fail:
1030         TALLOC_FREE(frame);
1031         return result;
1032 }
1033
1034 struct ctdb_vnn_list {
1035         uint32_t vnn;
1036         uint32_t reqid;
1037         unsigned num_srvids;
1038         unsigned num_filled;
1039         uint64_t *srvids;
1040         unsigned *pid_indexes;
1041 };
1042
1043 /*
1044  * Get a list of all vnns mentioned in a list of
1045  * server_ids. vnn_indexes tells where in the vnns array we have to
1046  * place the pids.
1047  */
1048 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
1049                               const struct server_id *pids, unsigned num_pids,
1050                               struct ctdb_vnn_list **pvnns,
1051                               unsigned *pnum_vnns)
1052 {
1053         struct ctdb_vnn_list *vnns = NULL;
1054         unsigned *vnn_indexes = NULL;
1055         unsigned i, num_vnns = 0;
1056
1057         vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
1058         if (vnn_indexes == NULL) {
1059                 goto fail;
1060         }
1061
1062         for (i=0; i<num_pids; i++) {
1063                 unsigned j;
1064                 uint32_t vnn = pids[i].vnn;
1065
1066                 for (j=0; j<num_vnns; j++) {
1067                         if (vnn == vnns[j].vnn) {
1068                                 break;
1069                         }
1070                 }
1071                 vnn_indexes[i] = j;
1072
1073                 if (j < num_vnns) {
1074                         /*
1075                          * Already in the array
1076                          */
1077                         vnns[j].num_srvids += 1;
1078                         continue;
1079                 }
1080                 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
1081                                       num_vnns+1);
1082                 if (vnns == NULL) {
1083                         goto fail;
1084                 }
1085                 vnns[num_vnns].vnn = vnn;
1086                 vnns[num_vnns].num_srvids = 1;
1087                 vnns[num_vnns].num_filled = 0;
1088                 num_vnns += 1;
1089         }
1090         for (i=0; i<num_vnns; i++) {
1091                 struct ctdb_vnn_list *vnn = &vnns[i];
1092
1093                 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
1094                 if (vnn->srvids == NULL) {
1095                         goto fail;
1096                 }
1097                 vnn->pid_indexes = talloc_array(vnns, unsigned,
1098                                                 vnn->num_srvids);
1099                 if (vnn->pid_indexes == NULL) {
1100                         goto fail;
1101                 }
1102         }
1103         for (i=0; i<num_pids; i++) {
1104                 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
1105                 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
1106                 vnn->pid_indexes[vnn->num_filled] = i;
1107                 vnn->num_filled += 1;
1108         }
1109
1110         TALLOC_FREE(vnn_indexes);
1111         *pvnns = vnns;
1112         *pnum_vnns = num_vnns;
1113         return true;
1114 fail:
1115         TALLOC_FREE(vnns);
1116         TALLOC_FREE(vnn_indexes);
1117         return false;
1118 }
1119
1120 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
1121                           const struct server_id *pids, unsigned num_pids,
1122                           bool *results)
1123 {
1124         unsigned i, num_received;
1125         NTSTATUS status;
1126         struct ctdb_vnn_list *vnns = NULL;
1127         unsigned num_vnns;
1128         bool result = false;
1129
1130         if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1131                                &vnns, &num_vnns)) {
1132                 goto fail;
1133         }
1134
1135         for (i=0; i<num_vnns; i++) {
1136                 struct ctdb_vnn_list *vnn = &vnns[i];
1137                 struct ctdb_req_control req;
1138
1139                 vnn->reqid = ctdbd_next_reqid(conn);
1140
1141                 ZERO_STRUCT(req);
1142
1143                 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1144                            (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1145
1146                 req.hdr.length = offsetof(struct ctdb_req_control, data);
1147                 req.hdr.ctdb_magic   = CTDB_MAGIC;
1148                 req.hdr.ctdb_version = CTDB_VERSION;
1149                 req.hdr.operation    = CTDB_REQ_CONTROL;
1150                 req.hdr.reqid        = vnn->reqid;
1151                 req.hdr.destnode     = vnn->vnn;
1152                 req.opcode           = CTDB_CONTROL_CHECK_SRVIDS;
1153                 req.srvid            = 0;
1154                 req.datalen          = sizeof(uint64_t) * vnn->num_srvids;
1155                 req.hdr.length      += req.datalen;
1156                 req.flags            = 0;
1157
1158                 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1159                 ctdb_packet_dump(&req.hdr);
1160
1161                 status = ctdb_packet_send(
1162                         conn->pkt, 2,
1163                         data_blob_const(
1164                                 &req, offsetof(struct ctdb_req_control,
1165                                                data)),
1166                         data_blob_const(vnn->srvids, req.datalen));
1167                 if (!NT_STATUS_IS_OK(status)) {
1168                         DEBUG(10, ("ctdb_packet_send failed: %s\n",
1169                                    nt_errstr(status)));
1170                         goto fail;
1171                 }
1172         }
1173
1174         status = ctdb_packet_flush(conn->pkt);
1175         if (!NT_STATUS_IS_OK(status)) {
1176                 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
1177                            nt_errstr(status)));
1178                 goto fail;
1179         }
1180
1181         num_received = 0;
1182
1183         while (num_received < num_vnns) {
1184                 struct ctdb_reply_control *reply = NULL;
1185                 struct ctdb_vnn_list *vnn;
1186                 uint32_t reqid;
1187
1188                 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1189                 if (!NT_STATUS_IS_OK(status)) {
1190                         DEBUG(10, ("ctdb_read_req failed: %s\n",
1191                                    nt_errstr(status)));
1192                         goto fail;
1193                 }
1194
1195                 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1196                         DEBUG(10, ("Received invalid reply\n"));
1197                         goto fail;
1198                 }
1199
1200                 reqid = reply->hdr.reqid;
1201
1202                 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1203
1204                 for (i=0; i<num_vnns; i++) {
1205                         if (reqid == vnns[i].reqid) {
1206                                 break;
1207                         }
1208                 }
1209                 if (i == num_vnns) {
1210                         DEBUG(10, ("Received unknown reqid number %u\n",
1211                                    (unsigned)reqid));
1212                         goto fail;
1213                 }
1214
1215                 DEBUG(10, ("Found index %u\n", i));
1216
1217                 vnn = &vnns[i];
1218
1219                 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1220                            (unsigned)vnn->vnn, vnn->num_srvids,
1221                            (unsigned)reply->datalen));
1222
1223                 if (reply->datalen < ((vnn->num_srvids+7)/8)) {
1224                         DEBUG(10, ("Received short reply\n"));
1225                         goto fail;
1226                 }
1227
1228                 for (i=0; i<vnn->num_srvids; i++) {
1229                         results[vnn->pid_indexes[i]] =
1230                                 ((reply->data[i/8] & (1<<(i%8))) != 0);
1231                 }
1232
1233                 TALLOC_FREE(reply);
1234                 num_received += 1;
1235         }
1236
1237         result = true;
1238 fail:
1239         TALLOC_FREE(vnns);
1240         return result;
1241 }
1242
1243 /*
1244  * Get a db path
1245  */
1246 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1247                    TALLOC_CTX *mem_ctx, uint32_t db_id)
1248 {
1249         NTSTATUS status;
1250         TDB_DATA data;
1251         int32_t cstatus;
1252
1253         data.dptr = (uint8_t*)&db_id;
1254         data.dsize = sizeof(db_id);
1255
1256         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1257                                CTDB_CONTROL_GETDBPATH, 0, 0, data, 
1258                                mem_ctx, &data, &cstatus);
1259         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1260                 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1261                 return NULL;
1262         }
1263
1264         return (char *)data.dptr;
1265 }
1266
1267 /*
1268  * attach to a ctdb database
1269  */
1270 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1271                          const char *name, uint32_t *db_id, int tdb_flags)
1272 {
1273         NTSTATUS status;
1274         TDB_DATA data;
1275         int32_t cstatus;
1276         bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1277
1278         data.dptr = (uint8_t*)name;
1279         data.dsize = strlen(name)+1;
1280
1281         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1282                                persistent
1283                                ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1284                                : CTDB_CONTROL_DB_ATTACH,
1285                                tdb_flags, 0, data, NULL, &data, &cstatus);
1286         if (!NT_STATUS_IS_OK(status)) {
1287                 DEBUG(0, (__location__ " ctdb_control for db_attach "
1288                           "failed: %s\n", nt_errstr(status)));
1289                 return status;
1290         }
1291
1292         if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1293                 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1294                 return NT_STATUS_INTERNAL_ERROR;
1295         }
1296
1297         *db_id = *(uint32_t *)data.dptr;
1298         talloc_free(data.dptr);
1299
1300         if (!(tdb_flags & TDB_SEQNUM)) {
1301                 return NT_STATUS_OK;
1302         }
1303
1304         data.dptr = (uint8_t *)db_id;
1305         data.dsize = sizeof(*db_id);
1306
1307         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1308                                CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data, 
1309                                NULL, NULL, &cstatus);
1310         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1311                 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1312                          "failed\n"));
1313                 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1314                         status;
1315         }
1316
1317         return NT_STATUS_OK;
1318 }
1319
1320 /*
1321  * force the migration of a record to this node
1322  */
1323 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
1324                        TDB_DATA key)
1325 {
1326         struct ctdb_req_call req;
1327         struct ctdb_reply_call *reply;
1328         NTSTATUS status;
1329
1330         ZERO_STRUCT(req);
1331
1332         req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1333         req.hdr.ctdb_magic   = CTDB_MAGIC;
1334         req.hdr.ctdb_version = CTDB_VERSION;
1335         req.hdr.operation    = CTDB_REQ_CALL;
1336         req.hdr.reqid        = ctdbd_next_reqid(conn);
1337         req.flags            = CTDB_IMMEDIATE_MIGRATION;
1338         req.callid           = CTDB_NULL_FUNC;
1339         req.db_id            = db_id;
1340         req.keylen           = key.dsize;
1341
1342         DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1343         ctdb_packet_dump(&req.hdr);
1344
1345         status = ctdb_packet_send(
1346                 conn->pkt, 2,
1347                 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1348                 data_blob_const(key.dptr, key.dsize));
1349
1350         if (!NT_STATUS_IS_OK(status)) {
1351                 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1352                 return status;
1353         }
1354
1355         status = ctdb_packet_flush(conn->pkt);
1356
1357         if (!NT_STATUS_IS_OK(status)) {
1358                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1359                 cluster_fatal("cluster dispatch daemon control write error\n");
1360         }
1361
1362         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1363
1364         if (!NT_STATUS_IS_OK(status)) {
1365                 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1366                 goto fail;
1367         }
1368
1369         if (reply->hdr.operation != CTDB_REPLY_CALL) {
1370                 DEBUG(0, ("received invalid reply\n"));
1371                 status = NT_STATUS_INTERNAL_ERROR;
1372                 goto fail;
1373         }
1374
1375         status = NT_STATUS_OK;
1376  fail:
1377
1378         TALLOC_FREE(reply);
1379         return status;
1380 }
1381
1382 /*
1383  * remotely fetch a record without locking it or forcing a migration
1384  */
1385 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
1386                      TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
1387 {
1388         struct ctdb_req_call req;
1389         struct ctdb_reply_call *reply;
1390         NTSTATUS status;
1391
1392         ZERO_STRUCT(req);
1393
1394         req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1395         req.hdr.ctdb_magic   = CTDB_MAGIC;
1396         req.hdr.ctdb_version = CTDB_VERSION;
1397         req.hdr.operation    = CTDB_REQ_CALL;
1398         req.hdr.reqid        = ctdbd_next_reqid(conn);
1399         req.flags            = 0;
1400         req.callid           = CTDB_FETCH_FUNC;
1401         req.db_id            = db_id;
1402         req.keylen           = key.dsize;
1403
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));
1408
1409         if (!NT_STATUS_IS_OK(status)) {
1410                 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1411                 return status;
1412         }
1413
1414         status = ctdb_packet_flush(conn->pkt);
1415
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");
1419         }
1420
1421         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1422
1423         if (!NT_STATUS_IS_OK(status)) {
1424                 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1425                 goto fail;
1426         }
1427
1428         if (reply->hdr.operation != CTDB_REPLY_CALL) {
1429                 DEBUG(0, ("received invalid reply\n"));
1430                 status = NT_STATUS_INTERNAL_ERROR;
1431                 goto fail;
1432         }
1433
1434         data->dsize = reply->datalen;
1435         if (data->dsize == 0) {
1436                 data->dptr = NULL;
1437                 goto done;
1438         }
1439
1440         data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1441                                             reply->datalen);
1442         if (data->dptr == NULL) {
1443                 DEBUG(0, ("talloc failed\n"));
1444                 status = NT_STATUS_NO_MEMORY;
1445                 goto fail;
1446         }
1447
1448  done:
1449         status = NT_STATUS_OK;
1450  fail:
1451         TALLOC_FREE(reply);
1452         return status;
1453 }
1454
1455 struct ctdbd_traverse_state {
1456         void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1457         void *private_data;
1458 };
1459
1460 /*
1461  * Handle a traverse record coming in on the ctdbd connection
1462  */
1463
1464 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1465                                       void *private_data)
1466 {
1467         struct ctdbd_traverse_state *state =
1468                 (struct ctdbd_traverse_state *)private_data;
1469
1470         struct ctdb_req_message *m;
1471         struct ctdb_rec_data *d;
1472         TDB_DATA key, data;
1473
1474         m = (struct ctdb_req_message *)buf;
1475
1476         if (length < sizeof(*m) || m->hdr.length != length) {
1477                 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1478                 TALLOC_FREE(buf);
1479                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1480         }
1481
1482         d = (struct ctdb_rec_data *)&m->data[0];
1483         if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1484                 DEBUG(0, ("Got invalid traverse data of length %d\n",
1485                           (int)m->datalen));
1486                 TALLOC_FREE(buf);
1487                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1488         }
1489
1490         key.dsize = d->keylen;
1491         key.dptr  = &d->data[0];
1492         data.dsize = d->datalen;
1493         data.dptr = &d->data[d->keylen];                
1494
1495         if (key.dsize == 0 && data.dsize == 0) {
1496                 /* end of traverse */
1497                 return NT_STATUS_END_OF_FILE;
1498         }
1499
1500         if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1501                 DEBUG(0, ("Got invalid ltdb header length %d\n",
1502                           (int)data.dsize));
1503                 TALLOC_FREE(buf);
1504                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1505         }
1506         data.dsize -= sizeof(struct ctdb_ltdb_header);
1507         data.dptr += sizeof(struct ctdb_ltdb_header);
1508
1509         if (state->fn) {
1510                 state->fn(key, data, state->private_data);
1511         }
1512
1513         TALLOC_FREE(buf);
1514         return NT_STATUS_OK;
1515 }
1516
1517 /*
1518   Traverse a ctdb database. This uses a kind-of hackish way to open a second
1519   connection to ctdbd to avoid the hairy recursive and async problems with
1520   everything in-line.
1521 */
1522
1523 NTSTATUS ctdbd_traverse(uint32 db_id,
1524                         void (*fn)(TDB_DATA key, TDB_DATA data,
1525                                    void *private_data),
1526                         void *private_data)
1527 {
1528         struct ctdbd_connection *conn;
1529         NTSTATUS status;
1530
1531         TDB_DATA data;
1532         struct ctdb_traverse_start t;
1533         int cstatus;
1534         struct ctdbd_traverse_state state;
1535
1536         status = ctdbd_init_connection(NULL, &conn);
1537         if (!NT_STATUS_IS_OK(status)) {
1538                 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1539                           nt_errstr(status)));
1540                 return status;
1541         }
1542
1543         t.db_id = db_id;
1544         t.srvid = conn->rand_srvid;
1545         t.reqid = ctdbd_next_reqid(conn);
1546
1547         data.dptr = (uint8_t *)&t;
1548         data.dsize = sizeof(t);
1549
1550         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1551                                CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1552                                data, NULL, NULL, &cstatus);
1553
1554         if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1555
1556                 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1557                          cstatus));
1558
1559                 if (NT_STATUS_IS_OK(status)) {
1560                         /*
1561                          * We need a mapping here
1562                          */
1563                         status = NT_STATUS_UNSUCCESSFUL;
1564                 }
1565                 goto done;
1566         }
1567
1568         state.fn = fn;
1569         state.private_data = private_data;
1570
1571         while (True) {
1572
1573                 status = NT_STATUS_OK;
1574
1575                 if (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
1576                                    ctdb_traverse_handler, &state, &status)) {
1577
1578                         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1579                                 status = NT_STATUS_OK;
1580                                 break;
1581                         }
1582
1583                         /*
1584                          * There might be more in the queue
1585                          */
1586                         continue;
1587                 }
1588
1589                 if (!NT_STATUS_IS_OK(status)) {
1590                         break;
1591                 }
1592
1593                 status = ctdb_packet_fd_read_sync(conn->pkt);
1594
1595                 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1596                         /*
1597                          * There might be more in the queue
1598                          */
1599                         continue;
1600                 }
1601
1602                 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1603                         status = NT_STATUS_OK;
1604                         break;
1605                 }
1606
1607                 if (!NT_STATUS_IS_OK(status)) {
1608                         DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1609                         cluster_fatal("ctdbd died\n");
1610                 }
1611         }
1612
1613  done:
1614         TALLOC_FREE(conn);
1615         return status;
1616 }
1617
1618 /*
1619    This is used to canonicalize a ctdb_sock_addr structure.
1620 */
1621 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1622                                       struct sockaddr_storage *out)
1623 {
1624         memcpy(out, in, sizeof (*out));
1625
1626 #ifdef HAVE_IPV6
1627         if (in->ss_family == AF_INET6) {
1628                 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1629                 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1630                 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1631                 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1632                         memset(out, 0, sizeof(*out));
1633 #ifdef HAVE_SOCK_SIN_LEN
1634                         out4->sin_len = sizeof(*out);
1635 #endif
1636                         out4->sin_family = AF_INET;
1637                         out4->sin_port   = in6->sin6_port;
1638                         memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
1639                 }
1640         }
1641 #endif
1642 }
1643
1644 /*
1645  * Register us as a server for a particular tcp connection
1646  */
1647
1648 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1649                             const struct sockaddr_storage *_server,
1650                             const struct sockaddr_storage *_client,
1651                             void (*release_ip_handler)(const char *ip_addr,
1652                                                        void *private_data),
1653                             void *private_data)
1654 {
1655         /*
1656          * we still use ctdb_control_tcp for ipv4
1657          * because we want to work against older ctdb
1658          * versions at runtime
1659          */
1660         struct ctdb_control_tcp p4;
1661 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1662         struct ctdb_control_tcp_addr p;
1663 #endif
1664         TDB_DATA data;
1665         NTSTATUS status;
1666         struct sockaddr_storage client;
1667         struct sockaddr_storage server;
1668
1669         /*
1670          * Only one connection so far
1671          */
1672         SMB_ASSERT(conn->release_ip_handler == NULL);
1673
1674         smbd_ctdb_canonicalize_ip(_client, &client);
1675         smbd_ctdb_canonicalize_ip(_server, &server);
1676
1677         switch (client.ss_family) {
1678         case AF_INET:
1679                 memcpy(&p4.dest, &server, sizeof(p4.dest));
1680                 memcpy(&p4.src, &client, sizeof(p4.src));
1681                 data.dptr = (uint8_t *)&p4;
1682                 data.dsize = sizeof(p4);
1683                 break;
1684 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1685         case AF_INET6:
1686                 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1687                 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1688                 data.dptr = (uint8_t *)&p;
1689                 data.dsize = sizeof(p);
1690                 break;
1691 #endif
1692         default:
1693                 return NT_STATUS_INTERNAL_ERROR;
1694         }
1695
1696         conn->release_ip_handler = release_ip_handler;
1697         conn->release_ip_priv = private_data;
1698
1699         /*
1700          * We want to be told about IP releases
1701          */
1702
1703         status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1704         if (!NT_STATUS_IS_OK(status)) {
1705                 return status;
1706         }
1707
1708         /*
1709          * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1710          * can send an extra ack to trigger a reset for our client, so it
1711          * immediately reconnects
1712          */
1713         return ctdbd_control(conn, CTDB_CURRENT_NODE, 
1714                              CTDB_CONTROL_TCP_CLIENT, 0,
1715                              CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1716 }
1717
1718 /*
1719  * We want to handle reconfigure events
1720  */
1721 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1722 {
1723         return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1724 }
1725
1726 /*
1727   call a control on the local node
1728  */
1729 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode, 
1730                              uint64_t srvid, uint32_t flags, TDB_DATA data, 
1731                              TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1732                              int *cstatus)
1733 {
1734         return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1735 }
1736
1737 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1738 {
1739         struct ctdb_client_notify_register reg_data;
1740         size_t struct_len;
1741         NTSTATUS status;
1742         int cstatus;
1743
1744         reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1745         reg_data.len = 1;
1746         reg_data.notify_data[0] = 0;
1747
1748         struct_len = offsetof(struct ctdb_client_notify_register,
1749                               notify_data) + reg_data.len;
1750
1751         status = ctdbd_control_local(
1752                 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1753                 make_tdb_data((uint8_t *)&reg_data, struct_len),
1754                 NULL, NULL, &cstatus);
1755         if (!NT_STATUS_IS_OK(status)) {
1756                 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1757                           nt_errstr(status)));
1758         }
1759         return status;
1760 }
1761
1762 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1763 {
1764         struct ctdb_client_notify_deregister dereg_data;
1765         NTSTATUS status;
1766         int cstatus;
1767
1768         dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1769
1770         status = ctdbd_control_local(
1771                 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1772                 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1773                 NULL, NULL, &cstatus);
1774         if (!NT_STATUS_IS_OK(status)) {
1775                 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1776                           nt_errstr(status)));
1777         }
1778         return status;
1779 }
1780
1781 #endif