first cut at adding full transactions for ctdb to samba3
[amitay/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
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
32 struct 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
44 static 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  */
53 static 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  */
66 static 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  */
80 static 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  */
93 static 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
107 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
108 {
109         return conn->our_vnn;
110 }
111
112 /*
113  * Get us a ctdb connection
114  */
115
116 static 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
158 static 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
192 struct 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
201 static void deferred_message_dispatch(struct event_context *event_ctx,
202                                       struct timed_event *te,
203                                       const 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
214 struct 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
223 static 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
240 static 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
286 static 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_state = TALLOC_P(NULL, struct deferred_msg_state))) {
357                         DEBUG(0, ("talloc failed\n"));
358                         TALLOC_FREE(hdr);
359                         goto next_pkt;
360                 }
361
362                 if (!(msg_state->rec = ctdb_pull_messaging_rec(
363                               msg_state, state.req.length, msg))) {
364                         DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
365                         TALLOC_FREE(msg_state);
366                         TALLOC_FREE(hdr);
367                         goto next_pkt;
368                 }
369
370                 TALLOC_FREE(hdr);
371
372                 msg_state->msg_ctx = conn->msg_ctx;
373                 
374                 /*
375                  * We're waiting for a call reply, but an async message has
376                  * crossed. Defer dispatching to the toplevel event loop.
377                  */
378                 evt = event_add_timed(conn->msg_ctx->event_ctx,
379                                       conn->msg_ctx->event_ctx,
380                                       timeval_zero(),
381                                       "deferred_message_dispatch",
382                                       deferred_message_dispatch,
383                                       msg_state);
384                 if (evt == NULL) {
385                         DEBUG(0, ("event_add_timed failed\n"));
386                         TALLOC_FREE(msg_state);
387                         TALLOC_FREE(hdr);
388                         goto next_pkt;
389                 }
390                 
391                 goto next_pkt;
392         }
393
394         if (hdr->reqid != reqid) {
395                 /* we got the wrong reply */
396                 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
397                          "been %u\n", hdr->reqid, reqid));
398                 TALLOC_FREE(hdr);
399                 goto again;
400         }
401
402         *((void **)result) = talloc_move(mem_ctx, &hdr);
403
404         return NT_STATUS_OK;
405 }
406
407 /*
408  * Get us a ctdbd connection
409  */
410
411 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
412                                struct ctdbd_connection **pconn)
413 {
414         struct ctdbd_connection *conn;
415         NTSTATUS status;
416
417         if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
418                 DEBUG(0, ("talloc failed\n"));
419                 return NT_STATUS_NO_MEMORY;
420         }
421
422         status = ctdbd_connect(conn, &conn->pkt);
423
424         if (!NT_STATUS_IS_OK(status)) {
425                 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
426                 goto fail;
427         }
428
429         status = get_cluster_vnn(conn, &conn->our_vnn);
430
431         if (!NT_STATUS_IS_OK(status)) {
432                 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
433                 goto fail;
434         }
435
436         generate_random_buffer((unsigned char *)&conn->rand_srvid,
437                                sizeof(conn->rand_srvid));
438
439         status = register_with_ctdbd(conn, conn->rand_srvid);
440
441         if (!NT_STATUS_IS_OK(status)) {
442                 DEBUG(5, ("Could not register random srvid: %s\n",
443                           nt_errstr(status)));
444                 goto fail;
445         }
446
447         *pconn = conn;
448         return NT_STATUS_OK;
449
450  fail:
451         TALLOC_FREE(conn);
452         return status;
453 }
454
455 /*
456  * Get us a ctdbd connection and register us as a process
457  */
458
459 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
460                                     struct ctdbd_connection **pconn)
461 {
462         struct ctdbd_connection *conn;
463         NTSTATUS status;
464
465         status = ctdbd_init_connection(mem_ctx, &conn);
466
467         if (!NT_STATUS_IS_OK(status)) {
468                 return status;
469         }
470
471         status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
472         if (!NT_STATUS_IS_OK(status)) {
473                 goto fail;
474         }
475
476         status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
477         if (!NT_STATUS_IS_OK(status)) {
478                 goto fail;
479         }
480
481         *pconn = conn;
482         return NT_STATUS_OK;
483
484  fail:
485         TALLOC_FREE(conn);
486         return status;
487 }
488
489 /*
490  * Packet handler to receive and handle a ctdb message
491  */
492 static NTSTATUS ctdb_handle_message(const struct data_blob *data,
493                                     void *private_data)
494 {
495         struct ctdbd_connection *conn = talloc_get_type_abort(
496                 private_data, struct ctdbd_connection);
497         struct ctdb_req_message *msg;
498         struct messaging_rec *msg_rec;
499
500         msg = (struct ctdb_req_message *)data->data;
501
502         if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
503                 DEBUG(0, ("Received async msg of type %u, discarding\n",
504                           msg->hdr.operation));
505                 return NT_STATUS_INVALID_PARAMETER;
506         }
507
508         if ((conn->release_ip_handler != NULL)
509             && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
510                 /* must be dispatched immediately */
511                 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
512                 conn->release_ip_handler((const char *)msg->data,
513                                          conn->release_ip_priv);
514                 return NT_STATUS_OK;
515         }
516
517         SMB_ASSERT(conn->msg_ctx != NULL);
518
519         if (msg->srvid == CTDB_SRVID_RECONFIGURE) {
520                 DEBUG(0,("Got cluster reconfigure message\n"));
521                 /*
522                  * when the cluster is reconfigured, we need to clean the brl
523                  * database
524                  */
525                 messaging_send(conn->msg_ctx, procid_self(),
526                                MSG_SMB_BRL_VALIDATE, &data_blob_null);
527
528                 /*
529                  * it's possible that we have just rejoined the cluster after
530                  * an outage. In that case our pending locks could have been
531                  * removed from the lockdb, so retry them once more
532                  */
533                 message_send_all(conn->msg_ctx, MSG_SMB_UNLOCK, NULL, 0, NULL);
534
535                 return NT_STATUS_OK;
536                 
537         }
538
539         /* only messages to our pid or the broadcast are valid here */
540         if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
541                 DEBUG(0,("Got unexpected message with srvid=%llu\n", 
542                          (unsigned long long)msg->srvid));
543                 return NT_STATUS_OK;
544         }
545
546         if (!(msg_rec = ctdb_pull_messaging_rec(NULL, data->length, msg))) {
547                 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
548                 return NT_STATUS_NO_MEMORY;
549         }
550
551         messaging_dispatch_rec(conn->msg_ctx, msg_rec);
552
553         TALLOC_FREE(msg_rec);
554         return NT_STATUS_OK;
555 }
556
557 /*
558  * The ctdbd socket is readable asynchronuously
559  */
560
561 static void ctdbd_socket_handler(struct event_context *event_ctx,
562                                  struct fd_event *event,
563                                  uint16 flags,
564                                  void *private_data)
565 {
566         struct ctdbd_connection *conn = talloc_get_type_abort(
567                 private_data, struct ctdbd_connection);
568
569         NTSTATUS status;
570
571         status = packet_fd_read(conn->pkt);
572
573         if (!NT_STATUS_IS_OK(status)) {
574                 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
575                 cluster_fatal("ctdbd died\n");
576         }
577
578         while (packet_handler(conn->pkt, ctdb_req_complete,
579                               ctdb_handle_message, conn, &status)) {
580                 if (!NT_STATUS_IS_OK(status)) {
581                         DEBUG(10, ("could not handle incoming message: %s\n",
582                                    nt_errstr(status)));
583                 }
584         }
585 }
586
587 /*
588  * Prepare a ctdbd connection to receive messages
589  */
590
591 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
592                                 struct messaging_context *msg_ctx)
593 {
594         SMB_ASSERT(conn->msg_ctx == NULL);
595         SMB_ASSERT(conn->fde == NULL);
596
597         if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
598                                        packet_get_fd(conn->pkt),
599                                        EVENT_FD_READ,
600                                        ctdbd_socket_handler,
601                                        conn))) {
602                 DEBUG(0, ("event_add_fd failed\n"));
603                 return NT_STATUS_NO_MEMORY;
604         }
605
606         conn->msg_ctx = msg_ctx;
607
608         return NT_STATUS_OK;
609 }
610
611 /*
612  * Send a messaging message across a ctdbd
613  */
614
615 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
616                               uint32 dst_vnn, uint64 dst_srvid,
617                               struct messaging_rec *msg)
618 {
619         struct ctdb_req_message r;
620         TALLOC_CTX *mem_ctx;
621         DATA_BLOB blob;
622         NTSTATUS status;
623         enum ndr_err_code ndr_err;
624
625         if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
626                 DEBUG(0, ("talloc failed\n"));
627                 return NT_STATUS_NO_MEMORY;
628         }
629
630         ndr_err = ndr_push_struct_blob(
631                 &blob, mem_ctx, msg,
632                 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
633
634         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
635                 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
636                           ndr_errstr(ndr_err)));
637                 status = ndr_map_error2ntstatus(ndr_err);
638                 goto fail;
639         }
640
641         r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
642         r.hdr.ctdb_magic = CTDB_MAGIC;
643         r.hdr.ctdb_version = CTDB_VERSION;
644         r.hdr.generation = 1;
645         r.hdr.operation  = CTDB_REQ_MESSAGE;
646         r.hdr.destnode   = dst_vnn;
647         r.hdr.srcnode    = conn->our_vnn;
648         r.hdr.reqid      = 0;
649         r.srvid          = dst_srvid;
650         r.datalen        = blob.length;
651
652         DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
653         ctdb_packet_dump(&r.hdr);
654
655         status = packet_send(
656                 conn->pkt, 2,
657                 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
658                 blob);
659
660         if (!NT_STATUS_IS_OK(status)) {
661                 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
662                 goto fail;
663         }
664
665         status = packet_flush(conn->pkt);
666
667         if (!NT_STATUS_IS_OK(status)) {
668                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
669                 cluster_fatal("cluster dispatch daemon msg write error\n");
670         }
671
672         status = NT_STATUS_OK;
673  fail:
674         TALLOC_FREE(mem_ctx);
675         return status;
676 }
677
678 /*
679  * send/recv a generic ctdb control message
680  */
681 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
682                               uint32_t vnn, uint32 opcode, 
683                               uint64_t srvid, uint32_t flags, 
684                               TDB_DATA data, 
685                               TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
686                               int *cstatus)
687 {
688         struct ctdb_req_control req;
689         struct ctdb_reply_control *reply = NULL;
690         struct ctdbd_connection *new_conn = NULL;
691         NTSTATUS status;
692
693         if (conn == NULL) {
694                 status = ctdbd_init_connection(NULL, &new_conn);
695
696                 if (!NT_STATUS_IS_OK(status)) {
697                         DEBUG(10, ("Could not init temp connection: %s\n",
698                                    nt_errstr(status)));
699                         goto fail;
700                 }
701
702                 conn = new_conn;
703         }
704
705         ZERO_STRUCT(req);
706         req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
707         req.hdr.ctdb_magic   = CTDB_MAGIC;
708         req.hdr.ctdb_version = CTDB_VERSION;
709         req.hdr.operation    = CTDB_REQ_CONTROL;
710         req.hdr.reqid        = ++conn->reqid;
711         req.hdr.destnode     = vnn;
712         req.opcode           = opcode;
713         req.srvid            = srvid;
714         req.datalen          = data.dsize;
715
716         DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
717         ctdb_packet_dump(&req.hdr);
718
719         status = packet_send(
720                 conn->pkt, 2,
721                 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
722                 data_blob_const(data.dptr, data.dsize));
723
724         if (!NT_STATUS_IS_OK(status)) {
725                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
726                 goto fail;
727         }
728
729         status = packet_flush(conn->pkt);
730
731         if (!NT_STATUS_IS_OK(status)) {
732                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
733                 cluster_fatal("cluster dispatch daemon control write error\n");
734         }
735
736         if (flags & CTDB_CTRL_FLAG_NOREPLY) {
737                 TALLOC_FREE(new_conn);
738                 return NT_STATUS_OK;
739         }
740
741         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
742
743         if (!NT_STATUS_IS_OK(status)) {
744                 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
745                 goto fail;
746         }
747
748         if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
749                 DEBUG(0, ("received invalid reply\n"));
750                 goto fail;
751         }
752
753         if (outdata) {
754                 if (!(outdata->dptr = (uint8 *)talloc_memdup(
755                               mem_ctx, reply->data, reply->datalen))) {
756                         TALLOC_FREE(reply);
757                         return NT_STATUS_NO_MEMORY;
758                 }
759                 outdata->dsize = reply->datalen;
760         }
761         if (cstatus) {
762                 (*cstatus) = reply->status;
763         }
764
765         status = NT_STATUS_OK;
766
767  fail:
768         TALLOC_FREE(new_conn);
769         TALLOC_FREE(reply);
770         return status;
771 }
772
773 /*
774  * see if a remote process exists
775  */
776 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
777 {
778         NTSTATUS status;
779         TDB_DATA data;
780         int32_t cstatus;
781
782         data.dptr = (uint8_t*)&pid;
783         data.dsize = sizeof(pid);
784
785         status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
786                                data, NULL, NULL, &cstatus);
787         if (!NT_STATUS_IS_OK(status)) {
788                 DEBUG(0, (__location__ " ctdb_control for process_exists "
789                           "failed\n"));
790                 return False;
791         }
792
793         return cstatus == 0;
794 }
795
796 /*
797  * Get a db path
798  */
799 char *ctdbd_dbpath(struct ctdbd_connection *conn,
800                    TALLOC_CTX *mem_ctx, uint32_t db_id)
801 {
802         NTSTATUS status;
803         TDB_DATA data;
804         int32_t cstatus;
805
806         data.dptr = (uint8_t*)&db_id;
807         data.dsize = sizeof(db_id);
808
809         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
810                                CTDB_CONTROL_GETDBPATH, 0, 0, data, 
811                                mem_ctx, &data, &cstatus);
812         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
813                 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
814                 return NULL;
815         }
816
817         return (char *)data.dptr;
818 }
819
820 /*
821  * attach to a ctdb database
822  */
823 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
824                          const char *name, uint32_t *db_id, int tdb_flags)
825 {
826         NTSTATUS status;
827         TDB_DATA data;
828         int32_t cstatus;
829         bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
830
831         data.dptr = (uint8_t*)name;
832         data.dsize = strlen(name)+1;
833
834         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
835                                persistent
836                                ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
837                                : CTDB_CONTROL_DB_ATTACH,
838                                0, 0, data, NULL, &data, &cstatus);
839         if (!NT_STATUS_IS_OK(status)) {
840                 DEBUG(0, (__location__ " ctdb_control for db_attach "
841                           "failed: %s\n", nt_errstr(status)));
842                 return status;
843         }
844
845         if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
846                 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
847                 return NT_STATUS_INTERNAL_ERROR;
848         }
849
850         *db_id = *(uint32_t *)data.dptr;
851         talloc_free(data.dptr);
852
853         if (!(tdb_flags & TDB_SEQNUM)) {
854                 return NT_STATUS_OK;
855         }
856
857         data.dptr = (uint8_t *)db_id;
858         data.dsize = sizeof(*db_id);
859
860         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
861                                CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data, 
862                                NULL, NULL, &cstatus);
863         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
864                 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
865                          "failed\n"));
866                 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
867                         status;
868         }
869
870         return NT_STATUS_OK;
871 }
872
873 /*
874  * force the migration of a record to this node
875  */
876 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
877                        TDB_DATA key)
878 {
879         struct ctdb_req_call req;
880         struct ctdb_reply_call *reply;
881         NTSTATUS status;
882
883         ZERO_STRUCT(req);
884         
885         req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
886         req.hdr.ctdb_magic   = CTDB_MAGIC;
887         req.hdr.ctdb_version = CTDB_VERSION;
888         req.hdr.operation    = CTDB_REQ_CALL;
889         req.hdr.reqid        = ++conn->reqid;
890         req.flags            = CTDB_IMMEDIATE_MIGRATION;
891         req.callid           = CTDB_NULL_FUNC;
892         req.db_id            = db_id;
893         req.keylen           = key.dsize;
894
895         DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
896         ctdb_packet_dump(&req.hdr);
897
898         status = packet_send(
899                 conn->pkt, 2,
900                 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
901                 data_blob_const(key.dptr, key.dsize));
902
903         if (!NT_STATUS_IS_OK(status)) {
904                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
905                 return status;
906         }
907
908         status = packet_flush(conn->pkt);
909
910         if (!NT_STATUS_IS_OK(status)) {
911                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
912                 cluster_fatal("cluster dispatch daemon control write error\n");
913         }
914
915         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
916
917         if (!NT_STATUS_IS_OK(status)) {
918                 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
919                 goto fail;
920         }
921
922         if (reply->hdr.operation != CTDB_REPLY_CALL) {
923                 DEBUG(0, ("received invalid reply\n"));
924                 status = NT_STATUS_INTERNAL_ERROR;
925                 goto fail;
926         }
927
928         status = NT_STATUS_OK;
929  fail:
930
931         TALLOC_FREE(reply);
932         return status;
933 }
934
935 /*
936  * remotely fetch a record without locking it or forcing a migration
937  */
938 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
939                      TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
940 {
941         struct ctdb_req_call req;
942         struct ctdb_reply_call *reply;
943         NTSTATUS status;
944
945         ZERO_STRUCT(req);
946         
947         req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
948         req.hdr.ctdb_magic   = CTDB_MAGIC;
949         req.hdr.ctdb_version = CTDB_VERSION;
950         req.hdr.operation    = CTDB_REQ_CALL;
951         req.hdr.reqid        = ++conn->reqid;
952         req.flags            = 0;
953         req.callid           = CTDB_FETCH_FUNC;
954         req.db_id            = db_id;
955         req.keylen           = key.dsize;
956
957         status = packet_send(
958                 conn->pkt, 2,
959                 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
960                 data_blob_const(key.dptr, key.dsize));
961
962         if (!NT_STATUS_IS_OK(status)) {
963                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
964                 return status;
965         }
966
967         status = packet_flush(conn->pkt);
968
969         if (!NT_STATUS_IS_OK(status)) {
970                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
971                 cluster_fatal("cluster dispatch daemon control write error\n");
972         }
973
974         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
975
976         if (!NT_STATUS_IS_OK(status)) {
977                 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
978                 goto fail;
979         }
980
981         if (reply->hdr.operation != CTDB_REPLY_CALL) {
982                 DEBUG(0, ("received invalid reply\n"));
983                 status = NT_STATUS_INTERNAL_ERROR;
984                 goto fail;
985         }
986
987         data->dsize = reply->datalen;
988         if (data->dsize == 0) {
989                 data->dptr = NULL;
990                 goto done;
991         }
992
993         data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
994                                             reply->datalen);
995         if (data->dptr == NULL) {
996                 DEBUG(0, ("talloc failed\n"));
997                 status = NT_STATUS_NO_MEMORY;
998                 goto fail;
999         }
1000
1001  done:
1002         status = NT_STATUS_OK;
1003  fail:
1004         TALLOC_FREE(reply);
1005         return status;
1006 }
1007
1008 struct ctdbd_traverse_state {
1009         void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1010         void *private_data;
1011 };
1012
1013 /*
1014  * Handle a traverse record coming in on the ctdbd connection
1015  */
1016
1017 static NTSTATUS ctdb_traverse_handler(const struct data_blob *blob,
1018                                       void *private_data)
1019 {
1020         struct ctdbd_traverse_state *state =
1021                 (struct ctdbd_traverse_state *)private_data;
1022
1023         struct ctdb_req_message *m;
1024         struct ctdb_rec_data *d;
1025         TDB_DATA key, data;
1026
1027         m = (struct ctdb_req_message *)blob->data;
1028
1029         if (blob->length < sizeof(*m) || m->hdr.length != blob->length) {
1030                 DEBUG(0, ("Got invalid message of length %d\n",
1031                           (int)blob->length));
1032                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1033         }
1034
1035         d = (struct ctdb_rec_data *)&m->data[0];
1036         if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1037                 DEBUG(0, ("Got invalid traverse data of length %d\n",
1038                           (int)m->datalen));
1039                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1040         }
1041
1042         key.dsize = d->keylen;
1043         key.dptr  = &d->data[0];
1044         data.dsize = d->datalen;
1045         data.dptr = &d->data[d->keylen];                
1046
1047         if (key.dsize == 0 && data.dsize == 0) {
1048                 /* end of traverse */
1049                 return NT_STATUS_END_OF_FILE;
1050         }
1051
1052         if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1053                 DEBUG(0, ("Got invalid ltdb header length %d\n",
1054                           (int)data.dsize));
1055                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1056         }
1057         data.dsize -= sizeof(struct ctdb_ltdb_header);
1058         data.dptr += sizeof(struct ctdb_ltdb_header);
1059
1060         if (state->fn) {
1061                 state->fn(key, data, state->private_data);
1062         }
1063
1064         return NT_STATUS_OK;
1065 }
1066
1067 /*
1068   Traverse a ctdb database. This uses a kind-of hackish way to open a second
1069   connection to ctdbd to avoid the hairy recursive and async problems with
1070   everything in-line.
1071 */
1072
1073 NTSTATUS ctdbd_traverse(uint32 db_id,
1074                         void (*fn)(TDB_DATA key, TDB_DATA data,
1075                                    void *private_data),
1076                         void *private_data)
1077 {
1078         struct ctdbd_connection *conn;
1079         NTSTATUS status;
1080
1081         TDB_DATA data;
1082         struct ctdb_traverse_start t;
1083         int cstatus;
1084         struct ctdbd_traverse_state state;
1085
1086         status = ctdbd_init_connection(NULL, &conn);
1087
1088         t.db_id = db_id;
1089         t.srvid = conn->rand_srvid;
1090         t.reqid = ++conn->reqid;
1091
1092         data.dptr = (uint8_t *)&t;
1093         data.dsize = sizeof(t);
1094
1095         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1096                                CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1097                                data, NULL, NULL, &cstatus);
1098
1099         if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1100
1101                 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1102                          cstatus));
1103
1104                 if (NT_STATUS_IS_OK(status)) {
1105                         /*
1106                          * We need a mapping here
1107                          */
1108                         status = NT_STATUS_UNSUCCESSFUL;
1109                 }
1110                 goto done;
1111         }
1112
1113         state.fn = fn;
1114         state.private_data = private_data;
1115
1116         while (True) {
1117
1118                 status = NT_STATUS_OK;
1119
1120                 if (packet_handler(conn->pkt, ctdb_req_complete,
1121                                    ctdb_traverse_handler, &state, &status)) {
1122
1123                         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1124                                 status = NT_STATUS_OK;
1125                                 break;
1126                         }
1127
1128                         /*
1129                          * There might be more in the queue
1130                          */
1131                         continue;
1132                 }
1133
1134                 if (!NT_STATUS_IS_OK(status)) {
1135                         break;
1136                 }
1137
1138                 status = packet_fd_read_sync(conn->pkt);
1139
1140                 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1141                         /*
1142                          * There might be more in the queue
1143                          */
1144                         continue;
1145                 }
1146
1147                 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1148                         status = NT_STATUS_OK;
1149                 }
1150
1151                 if (!NT_STATUS_IS_OK(status)) {
1152                         DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1153                         cluster_fatal("ctdbd died\n");
1154                 }
1155         }
1156
1157  done:
1158         TALLOC_FREE(conn);
1159         return status;
1160 }
1161
1162 /*
1163  * Register us as a server for a particular tcp connection
1164  */
1165
1166 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1167                             const struct sockaddr_in *server,
1168                             const struct sockaddr_in *client,
1169                             void (*release_ip_handler)(const char *ip_addr,
1170                                                        void *private_data),
1171                             void *private_data)
1172 {
1173         struct ctdb_control_tcp p;
1174         TDB_DATA data;
1175         NTSTATUS status;
1176
1177         /*
1178          * Only one connection so far
1179          */
1180         SMB_ASSERT(conn->release_ip_handler == NULL);
1181
1182         /*
1183          * We want to be told about IP releases
1184          */
1185
1186         status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1187         if (!NT_STATUS_IS_OK(status)) {
1188                 return status;
1189         }
1190
1191         p.dest = *server;
1192         p.src = *client;
1193
1194         /*
1195          * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1196          * can send an extra ack to trigger a reset for our client, so it
1197          * immediately reconnects
1198          */
1199         data.dptr = (uint8_t *)&p;
1200         data.dsize = sizeof(p);
1201
1202         return ctdbd_control(conn, CTDB_CURRENT_NODE, 
1203                              CTDB_CONTROL_TCP_CLIENT, 0,
1204                              CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1205 }
1206
1207 /*
1208  * We want to handle reconfigure events
1209  */
1210 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1211 {
1212         return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1213 }
1214
1215 /*
1216   persistent call. Used to start, store or cancel persistent updates.
1217  */
1218 static NTSTATUS ctdbd_persistent_call(struct ctdbd_connection *conn, uint32_t opcode, uint32_t db_id, TDB_DATA key, TDB_DATA data)
1219 {
1220        int cstatus=0;
1221        struct ctdb_rec_data *rec;
1222        TDB_DATA recdata;
1223        size_t length;
1224        NTSTATUS status;
1225
1226        length = offsetof(struct ctdb_rec_data, data) + key.dsize + data.dsize;
1227
1228        rec = (struct ctdb_rec_data *)talloc_size(conn, length);
1229        NT_STATUS_HAVE_NO_MEMORY(rec);
1230
1231        rec->length = length;
1232        rec->reqid  = db_id;
1233        rec->keylen = key.dsize;
1234        rec->datalen= data.dsize;
1235        memcpy(&rec->data[0], key.dptr, key.dsize);
1236        memcpy(&rec->data[key.dsize], data.dptr, data.dsize);
1237
1238        recdata.dptr  = (uint8_t *)rec;
1239        recdata.dsize = length;
1240
1241        status = ctdbd_control(conn, CTDB_CURRENT_NODE, opcode,
1242                               0, 0, recdata, NULL, NULL, &cstatus);
1243        if (cstatus != 0) {
1244                return NT_STATUS_INTERNAL_DB_CORRUPTION;
1245        }
1246        return status;
1247 }
1248
1249 /*
1250   persistent store. Used when we update a record in a persistent database
1251  */
1252 NTSTATUS ctdbd_persistent_store(struct ctdbd_connection *conn, uint32_t db_id, TDB_DATA key, TDB_DATA data)
1253 {
1254         return ctdbd_persistent_call(conn,
1255                                 CTDB_CONTROL_PERSISTENT_STORE, 
1256                                 db_id, key, data);
1257 }
1258
1259 /*
1260   tell the ctdb daemon that we are starting a persistent update operation.
1261   If we terminate/disconnect from the daemon without first performing
1262   either a persistent_store or a cancel ctdbd will perform recovery.
1263  */
1264 NTSTATUS ctdbd_start_persistent_update(struct ctdbd_connection *conn, uint32_t db_id, TDB_DATA key, TDB_DATA data)
1265 {
1266         return ctdbd_persistent_call(conn,
1267                                 CTDB_CONTROL_START_PERSISTENT_UPDATE, 
1268                                 db_id, key, data);
1269                               
1270 }
1271
1272 /*
1273   Cancel a persistent update operation. This is used if we have started a
1274   persistent update  but we want to abort it before we have made changes to
1275   the tdb database.
1276  */
1277 NTSTATUS ctdbd_cancel_persistent_update(struct ctdbd_connection *conn, uint32_t db_id, TDB_DATA key, TDB_DATA data)
1278 {
1279         return ctdbd_persistent_call(conn,
1280                                 CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE, 
1281                                 db_id, key, data);
1282 }
1283
1284 /*
1285   call a control on the local node
1286  */
1287 NTSTATUS 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
1296
1297
1298 #else
1299
1300 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1301                                struct ctdbd_connection **pconn)
1302 {
1303         return NT_STATUS_NOT_IMPLEMENTED;
1304 }
1305
1306 #endif