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