add a control to permanently enable/disable a node
[metze/ctdb/wip.git] / include / ctdb_private.h
1 /* 
2    ctdb database library
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #ifndef _CTDB_PRIVATE_H
22 #define _CTDB_PRIVATE_H
23
24 #include "ctdb.h"
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27
28 /* location of daemon socket */
29 #define CTDB_PATH       "/tmp/ctdb.socket"
30
31 /* default ctdb port number */
32 #define CTDB_PORT 9001
33
34 /* we must align packets to ensure ctdb works on all architectures (eg. sparc) */
35 #define CTDB_DS_ALIGNMENT 8
36
37
38 #define CTDB_NULL_FUNC      0xFF000001
39 #define CTDB_FETCH_FUNC     0xFF000002
40
41 /* all tunable variables go in here */
42 struct ctdb_tunable {
43         uint32_t max_redirect_count;
44         uint32_t seqnum_frequency;
45         uint32_t control_timeout;
46         uint32_t traverse_timeout;
47         uint32_t keepalive_interval;
48         uint32_t keepalive_limit;
49         uint32_t max_lacount;
50         uint32_t recover_timeout;
51         uint32_t recover_interval;
52         uint32_t election_timeout;
53         uint32_t takeover_timeout;
54         uint32_t monitor_interval;
55         uint32_t script_timeout;
56 };
57
58 /*
59   an installed ctdb remote call
60 */
61 struct ctdb_registered_call {
62         struct ctdb_registered_call *next, *prev;
63         uint32_t id;
64         ctdb_fn_t fn;
65 };
66
67 /*
68   this address structure might need to be generalised later for some
69   transports
70 */
71 struct ctdb_address {
72         const char *address;
73         int port;
74 };
75
76 /*
77   check a vnn is valid
78  */
79 #define ctdb_validate_vnn(ctdb, vnn) (((uint32_t)(vnn)) < (ctdb)->num_nodes)
80
81
82 /* called from the queue code when a packet comes in. Called with data==NULL
83    on error */
84 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
85                                    void *private_data);
86
87 /* used for callbacks in ctdb_control requests */
88 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
89                                            int32_t status, TDB_DATA data, 
90                                            const char *errormsg,
91                                            void *private_data);
92
93 /*
94   structure describing a connected client in the daemon
95  */
96 struct ctdb_client {
97         struct ctdb_context *ctdb;
98         int fd;
99         struct ctdb_queue *queue;
100         uint32_t client_id;
101         struct ctdb_tcp_list *tcp_list;
102 };
103
104
105 /*
106   state associated with one node
107 */
108 struct ctdb_node {
109         struct ctdb_context *ctdb;
110         struct ctdb_address address;
111         const char *name; /* for debug messages */
112         void *private_data; /* private to transport */
113         uint32_t vnn;
114 #define NODE_FLAGS_CONNECTED            0x00000001
115 #define NODE_FLAGS_DISABLED             0x00000002
116 #define NODE_FLAGS_PERMANENTLY_DISABLED 0x00000004
117         uint32_t flags;
118
119         /* used by the dead node monitoring */
120         uint32_t dead_count;
121         uint32_t rx_cnt;
122         uint32_t tx_cnt;
123
124         /* a list of controls pending to this node, so we can time them out quickly
125            if the node becomes disconnected */
126         struct daemon_control_state *pending_controls;
127
128         /* the public address of this node, if known */
129         const char *public_address;
130         uint8_t public_netmask_bits;
131
132         /* the node number that has taken over this nodes public address, if any. 
133            If not taken over, then set to -1 */
134         int32_t takeover_vnn;
135 };
136
137 /*
138   transport specific methods
139 */
140 struct ctdb_methods {
141         int (*initialise)(struct ctdb_context *); /* initialise transport structures */ 
142         int (*start)(struct ctdb_context *); /* start protocol processing */    
143         int (*add_node)(struct ctdb_node *); /* setup a new node */     
144         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
145         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
146         void (*shutdown)(struct ctdb_context *); /* shutdown transport */
147 };
148
149 /*
150   transport calls up to the ctdb layer
151 */
152 struct ctdb_upcalls {
153         /* recv_pkt is called when a packet comes in */
154         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
155
156         /* node_dead is called when an attempt to send to a node fails */
157         void (*node_dead)(struct ctdb_node *);
158
159         /* node_connected is called when a connection to a node is established */
160         void (*node_connected)(struct ctdb_node *);
161 };
162
163 /* list of message handlers - needs to be changed to a more efficient data
164    structure so we can find a message handler given a srvid quickly */
165 struct ctdb_message_list {
166         struct ctdb_context *ctdb;
167         struct ctdb_message_list *next, *prev;
168         uint64_t srvid;
169         ctdb_message_fn_t message_handler;
170         void *message_private;
171 };
172
173 /* additional data required for the daemon mode */
174 struct ctdb_daemon_data {
175         int sd;
176         char *name;
177         struct ctdb_queue *queue;
178 };
179
180 /*
181   ctdb status information
182  */
183 struct ctdb_statistics {
184         uint32_t num_clients;
185         uint32_t frozen;
186         uint32_t recovering;
187         uint32_t client_packets_sent;
188         uint32_t client_packets_recv;
189         uint32_t node_packets_sent;
190         uint32_t node_packets_recv;
191         uint32_t keepalive_packets_sent;
192         uint32_t keepalive_packets_recv;
193         struct {
194                 uint32_t req_call;
195                 uint32_t reply_call;
196                 uint32_t req_dmaster;
197                 uint32_t reply_dmaster;
198                 uint32_t reply_error;
199                 uint32_t req_message;
200                 uint32_t req_control;
201                 uint32_t reply_control;
202         } node;
203         struct {
204                 uint32_t req_call;
205                 uint32_t req_message;
206                 uint32_t req_control;
207         } client;
208         struct {
209                 uint32_t statistics;
210                 uint32_t get_config;
211                 uint32_t ping;
212                 uint32_t attach;
213                 uint32_t set_call;
214                 uint32_t process_exists;
215                 uint32_t traverse_start;
216                 uint32_t traverse_all;
217                 uint32_t traverse_data;
218                 uint32_t update_seqnum;
219                 uint32_t enable_seqnum;
220                 uint32_t set_seqnum_frequency;
221                 uint32_t register_srvid;
222                 uint32_t deregister_srvid;
223         } controls;
224         struct {
225                 uint32_t call;
226                 uint32_t control;
227                 uint32_t traverse;
228         } timeouts;
229         uint32_t total_calls;
230         uint32_t pending_calls;
231         uint32_t lockwait_calls;
232         uint32_t pending_lockwait_calls;
233         uint32_t memory_used;
234         uint32_t __last_counter; /* hack for control_statistics_all */
235         uint32_t max_hop_count;
236         double max_call_latency;
237         double max_lockwait_latency;
238 };
239
240 /* table that contains the mapping between a hash value and lmaster
241  */
242 struct ctdb_vnn_map {
243         uint32_t generation;
244         uint32_t size;
245         uint32_t *map;
246 };
247
248 /* 
249    a wire representation of the vnn map
250  */
251 struct ctdb_vnn_map_wire {
252         uint32_t generation;
253         uint32_t size;
254         uint32_t map[1];
255 };
256
257 /* a structure that contains the elements required for the write record
258    control
259 */
260 struct ctdb_write_record {
261         uint32_t dbid;
262         uint32_t keylen;
263         uint32_t datalen;
264         unsigned char blob[1];
265 };
266
267 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
268
269 #define CTDB_MONITORING_ACTIVE          0
270 #define CTDB_MONITORING_DISABLED        1
271
272 /* information about IP takeover */
273 struct ctdb_takeover {
274         bool enabled;
275         const char *interface;
276         const char *event_script;
277         TALLOC_CTX *last_ctx;
278 };
279
280
281 /* main state of the ctdb daemon */
282 struct ctdb_context {
283         struct event_context *ev;
284         uint32_t recovery_mode;
285         uint32_t monitoring_mode;
286         TALLOC_CTX *monitor_context;
287         struct ctdb_tunable tunable;
288         enum ctdb_freeze_mode freeze_mode;
289         struct ctdb_freeze_handle *freeze_handle;
290         struct ctdb_address address;
291         const char *name;
292         const char *db_directory;
293         const char *transport;
294         const char *logfile;
295         char *node_list_file;
296         char *recovery_lock_file;
297         int recovery_lock_fd;
298         uint32_t vnn; /* our own vnn */
299         uint32_t num_nodes;
300         uint32_t num_connected;
301         unsigned flags;
302         struct idr_context *idr;
303         uint16_t idr_cnt;
304         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
305         char *err_msg;
306         const struct ctdb_methods *methods; /* transport methods */
307         const struct ctdb_upcalls *upcalls; /* transport upcalls */
308         void *private_data; /* private to transport */
309         struct ctdb_db_context *db_list;
310         struct ctdb_message_list *message_list;
311         struct ctdb_daemon_data daemon;
312         struct ctdb_statistics statistics;
313         struct ctdb_vnn_map *vnn_map;
314         uint32_t num_clients;
315         uint32_t recovery_master;
316         struct ctdb_call_state *pending_calls;
317         struct ctdb_takeover takeover;
318         struct ctdb_tcp_list *tcp_list;
319 };
320
321 struct ctdb_db_context {
322         struct ctdb_db_context *next, *prev;
323         struct ctdb_context *ctdb;
324         uint32_t db_id;
325         const char *db_name;
326         const char *db_path;
327         struct tdb_wrap *ltdb;
328         struct ctdb_registered_call *calls; /* list of registered calls */
329         uint32_t seqnum;
330         struct timed_event *te;
331 };
332
333
334 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
335           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
336           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
337           return -1; }} while (0)
338
339 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
340           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
341           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
342           }} while (0)
343
344 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
345           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
346           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
347           return NULL; }} while (0)
348
349 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
350           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
351           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
352           }} while (0)
353
354 /*
355   the extended header for records in the ltdb
356 */
357 struct ctdb_ltdb_header {
358         uint64_t rsn;
359         uint32_t dmaster;
360         uint32_t laccessor;
361         uint32_t lacount;
362 };
363
364 enum ctdb_controls {CTDB_CONTROL_PROCESS_EXISTS          = 0, 
365                     CTDB_CONTROL_STATISTICS              = 1, 
366                     /* #2 removed */
367                     CTDB_CONTROL_PING                    = 3,
368                     CTDB_CONTROL_GETDBPATH               = 4,
369                     CTDB_CONTROL_GETVNNMAP               = 5,
370                     CTDB_CONTROL_SETVNNMAP               = 6,
371                     CTDB_CONTROL_GET_DEBUG               = 7,
372                     CTDB_CONTROL_SET_DEBUG               = 8,
373                     CTDB_CONTROL_GET_DBMAP               = 9,
374                     CTDB_CONTROL_GET_NODEMAP             = 10,
375                     CTDB_CONTROL_SET_DMASTER             = 11,
376                     /* #12 removed */
377                     CTDB_CONTROL_PULL_DB                 = 13,
378                     CTDB_CONTROL_PUSH_DB                 = 14,
379                     CTDB_CONTROL_GET_RECMODE             = 15,
380                     CTDB_CONTROL_SET_RECMODE             = 16,
381                     CTDB_CONTROL_STATISTICS_RESET        = 17,
382                     CTDB_CONTROL_DB_ATTACH               = 18,
383                     CTDB_CONTROL_SET_CALL                = 19,
384                     CTDB_CONTROL_TRAVERSE_START          = 20,
385                     CTDB_CONTROL_TRAVERSE_ALL            = 21,
386                     CTDB_CONTROL_TRAVERSE_DATA           = 22,
387                     CTDB_CONTROL_REGISTER_SRVID          = 23,
388                     CTDB_CONTROL_DEREGISTER_SRVID        = 24,
389                     CTDB_CONTROL_GET_DBNAME              = 25,
390                     CTDB_CONTROL_ENABLE_SEQNUM           = 26,
391                     CTDB_CONTROL_UPDATE_SEQNUM           = 27,
392                     /* #28 removed */
393                     CTDB_CONTROL_DUMP_MEMORY             = 29,
394                     CTDB_CONTROL_GET_PID                 = 30,
395                     CTDB_CONTROL_GET_RECMASTER           = 31,
396                     CTDB_CONTROL_SET_RECMASTER           = 32,
397                     CTDB_CONTROL_FREEZE                  = 33,
398                     CTDB_CONTROL_THAW                    = 34,
399                     CTDB_CONTROL_GET_VNN                 = 35,
400                     CTDB_CONTROL_SHUTDOWN                = 36,
401                     CTDB_CONTROL_GET_MONMODE             = 37,
402                     CTDB_CONTROL_SET_MONMODE             = 38,
403                     CTDB_CONTROL_MAX_RSN                 = 39,
404                     CTDB_CONTROL_SET_RSN_NONEMPTY        = 40,
405                     CTDB_CONTROL_DELETE_LOW_RSN          = 41,
406                     CTDB_CONTROL_TAKEOVER_IP             = 42,
407                     CTDB_CONTROL_RELEASE_IP              = 43,
408                     CTDB_CONTROL_TCP_CLIENT              = 44,
409                     CTDB_CONTROL_TCP_ADD                 = 45,
410                     CTDB_CONTROL_TCP_REMOVE              = 46,
411                     CTDB_CONTROL_STARTUP                 = 47,
412                     CTDB_CONTROL_SET_TUNABLE             = 48,
413                     CTDB_CONTROL_GET_TUNABLE             = 49,
414                     CTDB_CONTROL_LIST_TUNABLES           = 50,
415                     CTDB_CONTROL_GET_PUBLIC_IPS          = 51,
416                     CTDB_CONTROL_PERMANENTLY_DISABLE     = 52,
417 };
418
419 /*
420   structure passed in ctdb_control_set_rsn_nonempty
421  */
422 struct ctdb_control_set_rsn_nonempty {
423         uint32_t db_id;
424         uint64_t rsn;
425 };
426
427 /*
428   structure passed in ctdb_control_delete_low_rsn
429  */
430 struct ctdb_control_delete_low_rsn {
431         uint32_t db_id;
432         uint64_t rsn;
433 };
434
435 /*
436   structure passed in set_call control
437  */
438 struct ctdb_control_set_call {
439         uint32_t db_id;
440         ctdb_fn_t fn;
441         uint32_t id;
442 };
443
444 /*
445   struct for tcp_client control
446  */
447 struct ctdb_control_tcp {
448         struct sockaddr_in src;
449         struct sockaddr_in dest;
450 };
451
452 /*
453   struct for tcp_add and tcp_remove controls
454  */
455 struct ctdb_control_tcp_vnn {
456         uint32_t vnn;
457         struct sockaddr_in src;
458         struct sockaddr_in dest;
459 };
460
461 /*
462   structure used for CTDB_SRVID_NODE_FLAGS_CHANGED
463  */
464 struct ctdb_node_flag_change {
465         uint32_t vnn;
466         uint32_t flags;
467 };
468
469 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
470
471 #define CTDB_LMASTER_ANY        0xffffffff
472
473 /*
474   state of a in-progress ctdb call
475 */
476 struct ctdb_call_state {
477         struct ctdb_call_state *next, *prev;
478         enum call_state state;
479         uint32_t reqid;
480         struct ctdb_req_call *c;
481         struct ctdb_db_context *ctdb_db;
482         const char *errmsg;
483         struct ctdb_call call;
484         uint32_t generation;
485         struct {
486                 void (*fn)(struct ctdb_call_state *);
487                 void *private_data;
488         } async;
489 };
490
491
492 /* used for fetch_lock */
493 struct ctdb_fetch_handle {
494         struct ctdb_db_context *ctdb_db;
495         TDB_DATA key;
496         TDB_DATA *data;
497         struct ctdb_ltdb_header header;
498 };
499
500 /*
501   operation IDs
502 */
503 enum ctdb_operation {
504         CTDB_REQ_CALL           = 0,
505         CTDB_REPLY_CALL         = 1,
506         CTDB_REQ_DMASTER        = 2,
507         CTDB_REPLY_DMASTER      = 3,
508         CTDB_REPLY_ERROR        = 4,
509         CTDB_REQ_MESSAGE        = 5,
510         /* #6 removed */
511         CTDB_REQ_CONTROL        = 7,
512         CTDB_REPLY_CONTROL      = 8,
513         CTDB_REQ_KEEPALIVE      = 9,
514 };
515
516 #define CTDB_MAGIC 0x43544442 /* CTDB */
517 #define CTDB_VERSION 1
518
519 /*
520   packet structures
521 */
522 struct ctdb_req_header {
523         uint32_t length;
524         uint32_t ctdb_magic;
525         uint32_t ctdb_version;
526         uint32_t generation;
527         uint32_t operation;
528         uint32_t destnode;
529         uint32_t srcnode;
530         uint32_t reqid;
531 };
532
533 struct ctdb_req_call {
534         struct ctdb_req_header hdr;
535         uint32_t flags;
536         uint32_t db_id;
537         uint32_t callid;
538         uint32_t hopcount;
539         uint32_t keylen;
540         uint32_t calldatalen;
541         uint8_t data[1]; /* key[] followed by calldata[] */
542 };
543
544 struct ctdb_reply_call {
545         struct ctdb_req_header hdr;
546         uint32_t status;
547         uint32_t datalen;
548         uint8_t  data[1];
549 };
550
551 struct ctdb_reply_error {
552         struct ctdb_req_header hdr;
553         uint32_t status;
554         uint32_t msglen;
555         uint8_t  msg[1];
556 };
557
558 struct ctdb_req_dmaster {
559         struct ctdb_req_header hdr;
560         uint32_t db_id;
561         uint64_t rsn;
562         uint32_t dmaster;
563         uint32_t keylen;
564         uint32_t datalen;
565         uint8_t  data[1];
566 };
567
568 struct ctdb_reply_dmaster {
569         struct ctdb_req_header hdr;
570         uint32_t db_id;
571         uint64_t rsn;
572         uint32_t keylen;
573         uint32_t datalen;
574         uint8_t  data[1];
575 };
576
577 struct ctdb_req_message {
578         struct ctdb_req_header hdr;
579         uint64_t srvid;
580         uint32_t datalen;
581         uint8_t data[1];
582 };
583
584 struct ctdb_req_getdbpath {
585         struct ctdb_req_header hdr;
586         uint32_t db_id;
587 };
588
589 struct ctdb_reply_getdbpath {
590         struct ctdb_req_header hdr;
591         uint32_t datalen;
592         uint8_t data[1];
593 };
594
595 struct ctdb_req_control {
596         struct ctdb_req_header hdr;
597         uint32_t opcode;
598         uint64_t srvid;
599         uint32_t client_id;
600 #define CTDB_CTRL_FLAG_NOREPLY   1
601         uint32_t flags;
602         uint32_t datalen;
603         uint8_t data[1];
604 };
605
606 struct ctdb_reply_control {
607         struct ctdb_req_header hdr;
608         int32_t  status;
609         uint32_t datalen;
610         uint32_t errorlen;
611         uint8_t data[1];
612 };
613
614 struct ctdb_req_keepalive {
615         struct ctdb_req_header hdr;
616 };
617
618 /* internal prototypes */
619 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
620 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
621 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
622 int ctdb_parse_address(struct ctdb_context *ctdb,
623                        TALLOC_CTX *mem_ctx, const char *str,
624                        struct ctdb_address *address);
625 uint32_t ctdb_hash(const TDB_DATA *key);
626 uint32_t ctdb_hash_string(const char *str);
627 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
628 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
629 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
630 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
631 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
632 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
633
634 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
635 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
636                     TDB_DATA key, struct ctdb_ltdb_header *header, 
637                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
638 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
639                     struct ctdb_ltdb_header *header, TDB_DATA data);
640 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
641 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
642                            TDB_DATA key, struct ctdb_req_header *hdr,
643                            void (*recv_pkt)(void *, struct ctdb_req_header *),
644                            void *recv_context, bool ignore_generation);
645 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
646                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
647                                  struct ctdb_req_header *hdr, TDB_DATA *data,
648                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
649                                  void *recv_context, bool ignore_generation);
650 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
651
652 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
653                                              struct ctdb_call *call,
654                                              struct ctdb_ltdb_header *header,
655                                              TDB_DATA *data);
656
657
658 int ctdbd_start(struct ctdb_context *ctdb);
659 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
660 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
661
662 /*
663   queue a packet for sending
664 */
665 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
666
667 /*
668   setup the fd used by the queue
669  */
670 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
671
672 /*
673   setup a packet queue on a socket
674  */
675 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
676                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
677                                     
678                                     ctdb_queue_cb_fn_t callback,
679                                     void *private_data);
680
681 /*
682   allocate a packet for use in client<->daemon communication
683  */
684 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
685                                             TALLOC_CTX *mem_ctx, 
686                                             enum ctdb_operation operation, 
687                                             size_t length, size_t slength,
688                                             const char *type);
689 #define ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, type) \
690         (type *)_ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, sizeof(type), #type)
691
692 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
693                                                  TALLOC_CTX *mem_ctx, 
694                                                  enum ctdb_operation operation, 
695                                                  size_t length, size_t slength,
696                                                  const char *type);
697 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
698         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, sizeof(type), #type)
699
700
701 /*
702   lock a record in the ltdb, given a key
703  */
704 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
705
706 /*
707   unlock a record in the ltdb, given a key
708  */
709 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
710
711
712 /*
713   make a ctdb call to the local daemon - async send. Called from client context.
714
715   This constructs a ctdb_call request and queues it for processing. 
716   This call never blocks.
717 */
718 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db, 
719                                               struct ctdb_call *call);
720
721 /*
722   make a recv call to the local ctdb daemon - called from client context
723
724   This is called when the program wants to wait for a ctdb_call to complete and get the 
725   results. This call will block unless the call has already completed.
726 */
727 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
728
729 int ctdb_daemon_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
730                              ctdb_message_fn_t handler,
731                              void *private_data);
732
733 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
734                              uint64_t srvid, TDB_DATA data);
735
736 /*
737   send a ctdb message
738 */
739 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
740                              uint64_t srvid, TDB_DATA data);
741
742
743 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
744                                       TDB_DATA key,
745                                       void (*callback)(void *), void *private_data);
746
747 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db, 
748                                               struct ctdb_call *call);
749
750 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
751
752 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
753                                                      struct ctdb_call *call, 
754                                                      struct ctdb_ltdb_header *header);
755
756 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
757                     struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx, TDB_DATA *data,
758                     uint32_t caller);
759
760 #define ctdb_reqid_find(ctdb, reqid, type)      (type *)_ctdb_reqid_find(ctdb, reqid, #type, __location__)
761
762 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
763
764 int ctdb_socket_connect(struct ctdb_context *ctdb);
765
766 void ctdb_latency(double *latency, struct timeval t);
767
768 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state);
769 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location);
770 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid);
771
772 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
773 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
774
775 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
776                              uint64_t srvid, uint32_t opcode, uint32_t client_id, uint32_t flags,
777                              TDB_DATA data,
778                              ctdb_control_callback_fn_t callback,
779                              void *private_data);
780
781 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata, 
782                                TDB_DATA *outdata);
783
784 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
785                          ctdb_fn_t fn, int id);
786
787 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid, 
788                  uint32_t opcode, uint32_t flags, TDB_DATA data, 
789                  TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
790                  struct timeval *timeout, char **errormsg);
791
792
793
794
795 #define CHECK_CONTROL_DATA_SIZE(size) do { \
796  if (indata.dsize != size) { \
797          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
798                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
799          return -1; \
800  } \
801  } while (0)
802
803 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
804 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
805 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
806 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
807 int ctdb_control_writerecord(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
808
809
810 struct ctdb_traverse_start {
811         uint32_t db_id;
812         uint32_t reqid;
813         uint64_t srvid;
814 };
815
816 /*
817   structure used to pass record data between the child and parent
818  */
819 struct ctdb_rec_data {
820         uint32_t length;
821         uint32_t reqid;
822         uint32_t keylen;
823         uint32_t datalen;
824         uint8_t  data[1];
825 };
826                                    
827
828 /* structure used for pulldb control */
829 struct ctdb_control_pulldb {
830         uint32_t db_id;
831         uint32_t lmaster;
832 };
833
834 /* structure used for pulldb control */
835 struct ctdb_control_pulldb_reply {
836         uint32_t db_id;
837         uint32_t count;
838         uint8_t data[1];
839 };
840
841 /* set dmaster control structure */
842 struct ctdb_control_set_dmaster {
843         uint32_t db_id;
844         uint32_t dmaster;
845 };
846
847 /*
848   structure for setting a tunable
849  */
850 struct ctdb_control_set_tunable {
851         uint32_t value;
852         uint32_t length;
853         uint8_t  name[1];
854 };
855
856 /*
857   structure for getting a tunable
858  */
859 struct ctdb_control_get_tunable {
860         uint32_t length;
861         uint8_t  name[1];
862 };
863
864 /*
865   structure for listing tunables
866  */
867 struct ctdb_control_list_tunable {
868         uint32_t length;
869         /* returns a : separated list of tunable names */
870         uint8_t  data[1];
871 };
872
873
874 /* table that contains a list of all nodes a ctdb knows about and their 
875    status
876  */
877 struct ctdb_node_and_flags {
878         uint32_t vnn;
879         uint32_t flags;
880         struct sockaddr_in sin;
881
882 };
883
884 struct ctdb_node_map {
885         uint32_t num;
886         struct ctdb_node_and_flags nodes[1];
887 };
888
889 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA indata, 
890                                     TDB_DATA *outdata, uint32_t srcnode);
891 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
892 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
893
894 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data);
895
896 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
897 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
898 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
899
900 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
901 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint32_t srcnode);
902 int32_t ctdb_ltdb_set_seqnum_frequency(struct ctdb_context *ctdb, uint32_t frequency);
903
904 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, TDB_DATA key, TDB_DATA data);
905
906 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
907 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
908 int32_t ctdb_control_set_dmaster(struct ctdb_context *ctdb, TDB_DATA indata);
909
910 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
911                                  struct ctdb_req_control *c,
912                                  TDB_DATA indata, bool *async_reply,
913                                  const char **errormsg);
914 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
915                                 TDB_DATA *outdata, int32_t status, const char *errormsg);
916
917 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
918 int32_t ctdb_control_thaw(struct ctdb_context *ctdb);
919
920 int ctdb_start_recoverd(struct ctdb_context *ctdb);
921
922 uint32_t ctdb_get_num_enabled_nodes(struct ctdb_context *ctdb);
923
924 void ctdb_stop_monitoring(struct ctdb_context *ctdb);
925 void ctdb_start_monitoring(struct ctdb_context *ctdb);
926 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
927
928 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node);
929 void ctdb_call_resend_all(struct ctdb_context *ctdb);
930 void ctdb_node_dead(struct ctdb_node *node);
931 void ctdb_node_connected(struct ctdb_node *node);
932 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
933 int32_t ctdb_control_max_rsn(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
934 int32_t ctdb_control_set_rsn_nonempty(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
935 int32_t ctdb_control_delete_low_rsn(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
936 int ctdb_ctrl_get_max_rsn(struct ctdb_context *ctdb, struct timeval timeout, 
937                           uint32_t destnode, uint32_t db_id, uint64_t *max_rsn);
938 int ctdb_ctrl_set_rsn_nonempty(struct ctdb_context *ctdb, struct timeval timeout, 
939                                uint32_t destnode, uint32_t db_id, uint64_t rsn);
940 int ctdb_ctrl_delete_low_rsn(struct ctdb_context *ctdb, struct timeval timeout, 
941                              uint32_t destnode, uint32_t db_id, uint64_t rsn);
942 void ctdb_set_realtime(bool enable);
943 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, 
944                                  struct ctdb_req_control *c,
945                                  TDB_DATA indata, 
946                                  bool *async_reply);
947 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, 
948                                  struct ctdb_req_control *c,
949                                  TDB_DATA indata, 
950                                  bool *async_reply);
951
952 struct ctdb_public_ip {
953         uint32_t vnn;
954         uint32_t takeover_vnn;
955         struct sockaddr_in sin;
956 };
957 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout, 
958                           uint32_t destnode, struct ctdb_public_ip *ip);
959 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout, 
960                          uint32_t destnode, struct ctdb_public_ip *ip);
961
962 struct ctdb_all_public_ips {
963         uint32_t num;
964         struct ctdb_public_ip ips[1];
965 };
966 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
967 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb, 
968                         struct timeval timeout, uint32_t destnode, 
969                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
970
971
972 /* from takeover/system.c */
973 int ctdb_sys_send_arp(const struct sockaddr_in *saddr, const char *iface);
974 bool ctdb_sys_have_ip(const char *ip);
975 int ctdb_sys_send_ack(const struct sockaddr_in *dest, 
976                       const struct sockaddr_in *src);
977
978 int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist);
979 int ctdb_set_event_script(struct ctdb_context *ctdb, const char *script);
980 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap);
981
982 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id, 
983                                 uint32_t srcnode, TDB_DATA indata);
984 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata);
985 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
986 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
987
988 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
989 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
990 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
991                                struct timeval timeout,
992                                TALLOC_CTX *mem_ctx,
993                                void (*callback)(struct ctdb_context *, int, void *),
994                                void *private_data,
995                                const char *fmt, ...) PRINTF_ATTRIBUTE(6,7);
996 void ctdb_release_all_ips(struct ctdb_context *ctdb);
997
998 void set_nonblocking(int fd);
999 void set_close_on_exec(int fd);
1000
1001 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep);
1002
1003 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
1004
1005 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
1006                                  TDB_DATA *outdata);
1007 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata);
1008 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata);
1009
1010 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb);
1011
1012 #endif