2231d339873ecadae9ef9649cd9fa5e62e4f14d6
[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
26 /* location of daemon socket */
27 #define CTDB_PATH       "/tmp/ctdb.socket"
28
29 /* default ctdb port number */
30 #define CTDB_PORT 4379
31
32 /* we must align packets to ensure ctdb works on all architectures (eg. sparc) */
33 #define CTDB_DS_ALIGNMENT 8
34
35
36 #define CTDB_NULL_FUNC      0xFF000001
37 #define CTDB_FETCH_FUNC     0xFF000002
38
39
40 /*
41   recovery daemon memdump reply address
42  */
43 struct rd_memdump_reply {
44         uint32_t pnn;
45         uint64_t srvid;
46 };
47
48 /*
49   a tcp connection description
50  */
51 struct ctdb_tcp_connection {
52         ctdb_sock_addr src_addr;
53         ctdb_sock_addr dst_addr;
54 };
55
56 /* the wire representation for a tcp tickle array */
57 struct ctdb_tcp_wire_array {
58         uint32_t num;
59         struct ctdb_tcp_connection connections[1];
60 };      
61
62 /* the list of tcp tickles used by get/set tcp tickle list */
63 struct ctdb_control_tcp_tickle_list {
64         ctdb_sock_addr addr;
65         struct ctdb_tcp_wire_array tickles;
66 };
67
68 /*
69   array of tcp connections
70  */
71 struct ctdb_tcp_array {
72         uint32_t num;
73         struct ctdb_tcp_connection *connections;
74 };      
75
76
77 /* all tunable variables go in here */
78 struct ctdb_tunable {
79         uint32_t max_redirect_count;
80         uint32_t seqnum_interval; /* unit is ms */
81         uint32_t control_timeout;
82         uint32_t traverse_timeout;
83         uint32_t keepalive_interval;
84         uint32_t keepalive_limit;
85         uint32_t max_lacount;
86         uint32_t recover_timeout;
87         uint32_t recover_interval;
88         uint32_t election_timeout;
89         uint32_t takeover_timeout;
90         uint32_t monitor_interval;
91         uint32_t tickle_update_interval;
92         uint32_t script_timeout;
93         uint32_t script_ban_count; /* ban after this many consec timeouts*/
94         uint32_t recovery_grace_period;
95         uint32_t recovery_ban_period;
96         uint32_t database_hash_size;
97         uint32_t database_max_dead;
98         uint32_t rerecovery_timeout;
99         uint32_t enable_bans;
100         uint32_t deterministic_public_ips;
101         uint32_t disable_when_unhealthy;
102         uint32_t reclock_ping_period;
103         uint32_t no_ip_failback;
104         uint32_t verbose_memory_names;
105         uint32_t recd_ping_timeout;
106         uint32_t recd_ping_failcount;
107         uint32_t log_latency_ms;
108         uint32_t recovery_drop_all_ips;
109 };
110
111 /*
112   an installed ctdb remote call
113 */
114 struct ctdb_registered_call {
115         struct ctdb_registered_call *next, *prev;
116         uint32_t id;
117         ctdb_fn_t fn;
118 };
119
120 /*
121   this address structure might need to be generalised later for some
122   transports
123 */
124 struct ctdb_address {
125         const char *address;
126         int port;
127 };
128
129 /*
130   check that a pnn is valid
131  */
132 #define ctdb_validate_pnn(ctdb, pnn) (((uint32_t)(pnn)) < (ctdb)->num_nodes)
133
134
135 /* called from the queue code when a packet comes in. Called with data==NULL
136    on error */
137 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
138                                    void *private_data);
139
140 /* used for callbacks in ctdb_control requests */
141 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
142                                            int32_t status, TDB_DATA data, 
143                                            const char *errormsg,
144                                            void *private_data);
145
146 /*
147   structure describing a connected client in the daemon
148  */
149 struct ctdb_client {
150         struct ctdb_context *ctdb;
151         int fd;
152         struct ctdb_queue *queue;
153         uint32_t client_id;
154         pid_t pid;
155         struct ctdb_tcp_list *tcp_list;
156         uint32_t num_persistent_updates;
157 };
158
159
160 /* state associated with a public ip address */
161 struct ctdb_vnn {
162         struct ctdb_vnn *prev, *next;
163
164         const char *iface;
165         ctdb_sock_addr public_address;
166         uint8_t public_netmask_bits;
167
168         /* the node number that is serving this public address, if any. 
169            If no node serves this ip it is set to -1 */
170         int32_t pnn;
171
172         /* List of clients to tickle for this public address */
173         struct ctdb_tcp_array *tcp_array;
174
175         /* whether we need to update the other nodes with changes to our list
176            of connected clients */
177         bool tcp_update_needed;
178
179         /* a context to hang sending gratious arp events off */
180         TALLOC_CTX *takeover_ctx;
181
182         struct ctdb_kill_tcp *killtcp;
183 };
184
185 /*
186   state associated with one node
187 */
188 struct ctdb_node {
189         struct ctdb_context *ctdb;
190         struct ctdb_address address;
191         const char *name; /* for debug messages */
192         void *private_data; /* private to transport */
193         uint32_t pnn;
194 #define NODE_FLAGS_DISCONNECTED         0x00000001 /* node isn't connected */
195 #define NODE_FLAGS_UNHEALTHY            0x00000002 /* monitoring says node is unhealthy */
196 #define NODE_FLAGS_PERMANENTLY_DISABLED 0x00000004 /* administrator has disabled node */
197 #define NODE_FLAGS_BANNED               0x00000008 /* recovery daemon has banned the node */
198 #define NODE_FLAGS_DISABLED             (NODE_FLAGS_UNHEALTHY|NODE_FLAGS_PERMANENTLY_DISABLED)
199 #define NODE_FLAGS_INACTIVE             (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_BANNED)
200         uint32_t flags;
201
202         /* used by the dead node monitoring */
203         uint32_t dead_count;
204         uint32_t rx_cnt;
205         uint32_t tx_cnt;
206
207         /* used to track node capabilities, is only valid/tracked inside the
208            recovery daemon.
209         */
210         uint32_t capabilities;
211
212         /* a list of controls pending to this node, so we can time them out quickly
213            if the node becomes disconnected */
214         struct daemon_control_state *pending_controls;
215
216         /* used by the recovery daemon when distributing ip addresses 
217            across the nodes.  it needs to know which public ip's can be handled
218            by each node.
219         */
220         struct ctdb_all_public_ips *public_ips;
221 };
222
223 /*
224   transport specific methods
225 */
226 struct ctdb_methods {
227         int (*initialise)(struct ctdb_context *); /* initialise transport structures */ 
228         int (*start)(struct ctdb_context *); /* start the transport */
229         int (*add_node)(struct ctdb_node *); /* setup a new node */     
230         int (*connect_node)(struct ctdb_node *); /* connect to node */
231         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
232         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
233         void (*shutdown)(struct ctdb_context *); /* shutdown transport */
234         void (*restart)(struct ctdb_node *); /* stop and restart the connection */
235 };
236
237 /*
238   transport calls up to the ctdb layer
239 */
240 struct ctdb_upcalls {
241         /* recv_pkt is called when a packet comes in */
242         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
243
244         /* node_dead is called when an attempt to send to a node fails */
245         void (*node_dead)(struct ctdb_node *);
246
247         /* node_connected is called when a connection to a node is established */
248         void (*node_connected)(struct ctdb_node *);
249 };
250
251 /* list of message handlers - needs to be changed to a more efficient data
252    structure so we can find a message handler given a srvid quickly */
253 struct ctdb_message_list {
254         struct ctdb_context *ctdb;
255         struct ctdb_message_list *next, *prev;
256         uint64_t srvid;
257         ctdb_message_fn_t message_handler;
258         void *message_private;
259 };
260
261 /* additional data required for the daemon mode */
262 struct ctdb_daemon_data {
263         int sd;
264         char *name;
265         struct ctdb_queue *queue;
266 };
267
268 /*
269   ctdb status information
270  */
271 struct ctdb_statistics {
272         uint32_t num_clients;
273         uint32_t frozen;
274         uint32_t recovering;
275         uint32_t client_packets_sent;
276         uint32_t client_packets_recv;
277         uint32_t node_packets_sent;
278         uint32_t node_packets_recv;
279         uint32_t keepalive_packets_sent;
280         uint32_t keepalive_packets_recv;
281         struct {
282                 uint32_t req_call;
283                 uint32_t reply_call;
284                 uint32_t req_dmaster;
285                 uint32_t reply_dmaster;
286                 uint32_t reply_error;
287                 uint32_t req_message;
288                 uint32_t req_control;
289                 uint32_t reply_control;
290         } node;
291         struct {
292                 uint32_t req_call;
293                 uint32_t req_message;
294                 uint32_t req_control;
295         } client;
296         struct {
297                 uint32_t call;
298                 uint32_t control;
299                 uint32_t traverse;
300         } timeouts;
301         uint32_t total_calls;
302         uint32_t pending_calls;
303         uint32_t lockwait_calls;
304         uint32_t pending_lockwait_calls;
305         uint32_t childwrite_calls;
306         uint32_t pending_childwrite_calls;
307         uint32_t memory_used;
308         uint32_t __last_counter; /* hack for control_statistics_all */
309         uint32_t max_hop_count;
310         double max_call_latency;
311         double max_lockwait_latency;
312         double max_childwrite_latency;
313 };
314
315
316 #define INVALID_GENERATION 1
317 /* table that contains the mapping between a hash value and lmaster
318  */
319 struct ctdb_vnn_map {
320         uint32_t generation;
321         uint32_t size;
322         uint32_t *map;
323 };
324
325 /* 
326    a wire representation of the vnn map
327  */
328 struct ctdb_vnn_map_wire {
329         uint32_t generation;
330         uint32_t size;
331         uint32_t map[1];
332 };
333
334 /* a structure that contains the elements required for the write record
335    control
336 */
337 struct ctdb_write_record {
338         uint32_t dbid;
339         uint32_t keylen;
340         uint32_t datalen;
341         unsigned char blob[1];
342 };
343
344 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
345
346 #define CTDB_MONITORING_ACTIVE          0
347 #define CTDB_MONITORING_DISABLED        1
348
349 /* The different capabilities of the ctdb daemon. */
350 #define CTDB_CAP_RECMASTER              0x00000001
351 #define CTDB_CAP_LMASTER                0x00000002
352 /* This capability is set if CTDB_LVS_PUBLIC_IP is set */
353 #define CTDB_CAP_LVS                    0x00000004
354
355 /* main state of the ctdb daemon */
356 struct ctdb_context {
357         struct event_context *ev;
358         struct timeval ctdbd_start_time;
359         struct timeval last_recovery_started;
360         struct timeval last_recovery_finished;
361         uint32_t recovery_mode;
362         TALLOC_CTX *tickle_update_context;
363         TALLOC_CTX *keepalive_ctx;
364         struct ctdb_tunable tunable;
365         enum ctdb_freeze_mode freeze_mode;
366         struct ctdb_freeze_handle *freeze_handle;
367         struct ctdb_address address;
368         const char *name;
369         const char *db_directory;
370         const char *db_directory_persistent;
371         const char *transport;
372         const char *logfile;
373         char *node_list_file;
374         char *recovery_lock_file;
375         int recovery_lock_fd;
376         uint32_t pnn; /* our own pnn */
377         uint32_t num_nodes;
378         uint32_t num_connected;
379         unsigned flags;
380         uint32_t capabilities;
381         struct idr_context *idr;
382         uint16_t idr_cnt;
383         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
384         struct ctdb_vnn *vnn; /* list of public ip addresses and interfaces */
385         struct ctdb_vnn *single_ip_vnn; /* a structure for the single ip */
386         char *err_msg;
387         const struct ctdb_methods *methods; /* transport methods */
388         const struct ctdb_upcalls *upcalls; /* transport upcalls */
389         void *private_data; /* private to transport */
390         struct ctdb_db_context *db_list;
391         struct ctdb_message_list *message_list;
392         struct ctdb_daemon_data daemon;
393         struct ctdb_statistics statistics;
394         struct ctdb_vnn_map *vnn_map;
395         uint32_t num_clients;
396         uint32_t recovery_master;
397         struct ctdb_call_state *pending_calls;
398         struct ctdb_client_ip *client_ip_list;
399         bool do_setsched;
400         bool do_checkpublicip;
401         void *saved_scheduler_param;
402         struct _trbt_tree_t *server_ids;        
403         const char *event_script_dir;
404         const char *notification_script;
405         const char *default_public_interface;
406         pid_t ctdbd_pid;
407         pid_t recoverd_pid;
408         bool done_startup;
409         const char *node_ip;
410         struct ctdb_monitor_state *monitor;
411         struct ctdb_log_state *log;
412         int start_as_disabled;
413         uint32_t event_script_timeouts; /* counting how many consecutive times an eventscript has timedout */
414         TALLOC_CTX *eventscripts_ctx; /* a context to hold data for the RUN_EVENTSCRIPTS control */
415         uint32_t *recd_ping_count;
416         TALLOC_CTX *release_ips_ctx; /* a context used to automatically drop all IPs if we fail to recover the node */
417         TALLOC_CTX *script_monitoring_ctx; /* a context where we store results while running the monitor event */
418         TALLOC_CTX *last_monitoring_ctx; 
419 };
420
421 struct ctdb_db_context {
422         struct ctdb_db_context *next, *prev;
423         struct ctdb_context *ctdb;
424         uint32_t db_id;
425         bool persistent;
426         const char *db_name;
427         const char *db_path;
428         struct tdb_wrap *ltdb;
429         struct ctdb_registered_call *calls; /* list of registered calls */
430         uint32_t seqnum;
431         struct timed_event *te;
432 };
433
434
435 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
436           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
437           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
438           return -1; }} while (0)
439
440 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
441           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
442           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
443           return; }} while (0)
444
445 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
446           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
447           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
448           return NULL; }} while (0)
449
450 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
451           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
452           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
453           }} while (0)
454
455 /*
456   the extended header for records in the ltdb
457 */
458 struct ctdb_ltdb_header {
459         uint64_t rsn;
460         uint32_t dmaster;
461         uint32_t laccessor;
462         uint32_t lacount;
463 };
464
465 enum ctdb_controls {CTDB_CONTROL_PROCESS_EXISTS          = 0, 
466                     CTDB_CONTROL_STATISTICS              = 1, 
467                     /* #2 removed */
468                     CTDB_CONTROL_PING                    = 3,
469                     CTDB_CONTROL_GETDBPATH               = 4,
470                     CTDB_CONTROL_GETVNNMAP               = 5,
471                     CTDB_CONTROL_SETVNNMAP               = 6,
472                     CTDB_CONTROL_GET_DEBUG               = 7,
473                     CTDB_CONTROL_SET_DEBUG               = 8,
474                     CTDB_CONTROL_GET_DBMAP               = 9,
475                     CTDB_CONTROL_GET_NODEMAPv4           = 10, /* obsolete */
476                     CTDB_CONTROL_SET_DMASTER             = 11,
477                     /* #12 removed */
478                     CTDB_CONTROL_PULL_DB                 = 13,
479                     CTDB_CONTROL_PUSH_DB                 = 14,
480                     CTDB_CONTROL_GET_RECMODE             = 15,
481                     CTDB_CONTROL_SET_RECMODE             = 16,
482                     CTDB_CONTROL_STATISTICS_RESET        = 17,
483                     CTDB_CONTROL_DB_ATTACH               = 18,
484                     CTDB_CONTROL_SET_CALL                = 19,
485                     CTDB_CONTROL_TRAVERSE_START          = 20,
486                     CTDB_CONTROL_TRAVERSE_ALL            = 21,
487                     CTDB_CONTROL_TRAVERSE_DATA           = 22,
488                     CTDB_CONTROL_REGISTER_SRVID          = 23,
489                     CTDB_CONTROL_DEREGISTER_SRVID        = 24,
490                     CTDB_CONTROL_GET_DBNAME              = 25,
491                     CTDB_CONTROL_ENABLE_SEQNUM           = 26,
492                     CTDB_CONTROL_UPDATE_SEQNUM           = 27,
493                     /* #28 removed */
494                     CTDB_CONTROL_DUMP_MEMORY             = 29,
495                     CTDB_CONTROL_GET_PID                 = 30,
496                     CTDB_CONTROL_GET_RECMASTER           = 31,
497                     CTDB_CONTROL_SET_RECMASTER           = 32,
498                     CTDB_CONTROL_FREEZE                  = 33,
499                     CTDB_CONTROL_THAW                    = 34,
500                     CTDB_CONTROL_GET_PNN                 = 35,
501                     CTDB_CONTROL_SHUTDOWN                = 36,
502                     CTDB_CONTROL_GET_MONMODE             = 37,
503                     /* #38 removed */
504                     /* #39 removed */
505                     /* #40 removed */
506                     /* #41 removed */
507                     CTDB_CONTROL_TAKEOVER_IPv4           = 42, /* obsolete */
508                     CTDB_CONTROL_RELEASE_IPv4            = 43, /* obsolete */
509                     CTDB_CONTROL_TCP_CLIENT              = 44,
510                     CTDB_CONTROL_TCP_ADD                 = 45,
511                     CTDB_CONTROL_TCP_REMOVE              = 46,
512                     CTDB_CONTROL_STARTUP                 = 47,
513                     CTDB_CONTROL_SET_TUNABLE             = 48,
514                     CTDB_CONTROL_GET_TUNABLE             = 49,
515                     CTDB_CONTROL_LIST_TUNABLES           = 50,
516                     CTDB_CONTROL_GET_PUBLIC_IPSv4        = 51, /* obsolete */
517                     CTDB_CONTROL_MODIFY_FLAGS            = 52,
518                     CTDB_CONTROL_GET_ALL_TUNABLES        = 53,
519                     CTDB_CONTROL_KILL_TCP                = 54,
520                     CTDB_CONTROL_GET_TCP_TICKLE_LIST     = 55,
521                     CTDB_CONTROL_SET_TCP_TICKLE_LIST     = 56,
522                     CTDB_CONTROL_REGISTER_SERVER_ID      = 57,
523                     CTDB_CONTROL_UNREGISTER_SERVER_ID    = 58,
524                     CTDB_CONTROL_CHECK_SERVER_ID         = 59,
525                     CTDB_CONTROL_GET_SERVER_ID_LIST      = 60,
526                     CTDB_CONTROL_DB_ATTACH_PERSISTENT    = 61,
527                     CTDB_CONTROL_PERSISTENT_STORE        = 62,
528                     CTDB_CONTROL_UPDATE_RECORD           = 63,
529                     CTDB_CONTROL_SEND_GRATIOUS_ARP       = 64,
530                     CTDB_CONTROL_TRANSACTION_START       = 65,
531                     CTDB_CONTROL_TRANSACTION_COMMIT      = 66,
532                     CTDB_CONTROL_WIPE_DATABASE           = 67,
533                     /* #68 removed */
534                     CTDB_CONTROL_UPTIME                  = 69,
535                     CTDB_CONTROL_START_RECOVERY          = 70,
536                     CTDB_CONTROL_END_RECOVERY            = 71,
537                     CTDB_CONTROL_RELOAD_NODES_FILE       = 72,
538                     /* #73 removed */
539                     CTDB_CONTROL_TRY_DELETE_RECORDS      = 74,
540                     CTDB_CONTROL_ENABLE_MONITOR          = 75,
541                     CTDB_CONTROL_DISABLE_MONITOR         = 76,
542                     CTDB_CONTROL_ADD_PUBLIC_IP           = 77,
543                     CTDB_CONTROL_DEL_PUBLIC_IP           = 78,
544                     CTDB_CONTROL_RUN_EVENTSCRIPTS        = 79,
545                     CTDB_CONTROL_GET_CAPABILITIES        = 80,
546                     CTDB_CONTROL_START_PERSISTENT_UPDATE = 81,
547                     CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE= 82,
548                     CTDB_CONTROL_TRANS2_COMMIT           = 83,
549                     CTDB_CONTROL_TRANS2_FINISHED         = 84,
550                     CTDB_CONTROL_TRANS2_ERROR            = 85,
551                     CTDB_CONTROL_TRANS2_COMMIT_RETRY     = 86,
552                     CTDB_CONTROL_RECD_PING               = 87,
553                     CTDB_CONTROL_RELEASE_IP              = 88,
554                     CTDB_CONTROL_TAKEOVER_IP             = 89,
555                     CTDB_CONTROL_GET_PUBLIC_IPS          = 90,
556                     CTDB_CONTROL_GET_NODEMAP             = 91,
557                     CTDB_CONTROL_EVENT_SCRIPT_INIT       = 92,
558                     CTDB_CONTROL_EVENT_SCRIPT_START      = 93,
559                     CTDB_CONTROL_EVENT_SCRIPT_STOP       = 94,
560                     CTDB_CONTROL_EVENT_SCRIPT_FINISHED   = 95,
561                     CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS = 96,
562 };      
563
564 /*
565   structure passed in set_call control
566  */
567 struct ctdb_control_set_call {
568         uint32_t db_id;
569         ctdb_fn_t fn;
570         uint32_t id;
571 };
572
573 /*
574   struct for kill_tcp control
575  */
576 struct ctdb_control_killtcp {
577         ctdb_sock_addr src_addr;
578         ctdb_sock_addr dst_addr;
579 };
580
581 /*
582   struct holding a ctdb_sock_addr and an interface name,
583   used to add/remove public addresses
584  */
585 struct ctdb_control_ip_iface {
586         ctdb_sock_addr addr;
587         uint32_t mask;
588         uint32_t len;
589         char iface[1];
590 };
591
592 /*
593   struct holding a ctdb_sock_addr and an interface name,
594   used for send_gratious_arp
595  */
596 struct ctdb_control_gratious_arp {
597         ctdb_sock_addr addr;
598         uint32_t mask;
599         uint32_t len;
600         char iface[1];
601 };
602
603 /*
604   struct for tcp_add and tcp_remove controls
605  */
606 struct ctdb_control_tcp_vnn {
607         ctdb_sock_addr src;
608         ctdb_sock_addr dest;
609 };
610
611 /*
612   persistent store control - update this record on all other nodes
613  */
614 struct ctdb_control_persistent_store {
615         uint32_t db_id;
616         uint32_t len;
617         uint8_t  data[1];
618 };
619
620 /*
621   structure used for CTDB_SRVID_NODE_FLAGS_CHANGED
622  */
623 struct ctdb_node_flag_change {
624         uint32_t pnn;
625         uint32_t new_flags;
626         uint32_t old_flags;
627 };
628
629 /*
630   struct for admin setting a ban
631  */
632 struct ctdb_ban_info {
633         uint32_t pnn;
634         uint32_t ban_time;
635 };
636
637 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
638
639 #define CTDB_LMASTER_ANY        0xffffffff
640
641 /*
642   state of a in-progress ctdb call
643 */
644 struct ctdb_call_state {
645         struct ctdb_call_state *next, *prev;
646         enum call_state state;
647         uint32_t reqid;
648         struct ctdb_req_call *c;
649         struct ctdb_db_context *ctdb_db;
650         const char *errmsg;
651         struct ctdb_call *call;
652         uint32_t generation;
653         struct {
654                 void (*fn)(struct ctdb_call_state *);
655                 void *private_data;
656         } async;
657 };
658
659
660 /* used for fetch_lock */
661 struct ctdb_fetch_handle {
662         struct ctdb_db_context *ctdb_db;
663         TDB_DATA key;
664         TDB_DATA *data;
665         struct ctdb_ltdb_header header;
666 };
667
668 /*
669   operation IDs
670 */
671 enum ctdb_operation {
672         CTDB_REQ_CALL           = 0,
673         CTDB_REPLY_CALL         = 1,
674         CTDB_REQ_DMASTER        = 2,
675         CTDB_REPLY_DMASTER      = 3,
676         CTDB_REPLY_ERROR        = 4,
677         CTDB_REQ_MESSAGE        = 5,
678         /* #6 removed */
679         CTDB_REQ_CONTROL        = 7,
680         CTDB_REPLY_CONTROL      = 8,
681         CTDB_REQ_KEEPALIVE      = 9,
682 };
683
684 #define CTDB_MAGIC 0x43544442 /* CTDB */
685 #define CTDB_VERSION 1
686
687 /*
688   packet structures
689 */
690 struct ctdb_req_header {
691         uint32_t length;
692         uint32_t ctdb_magic;
693         uint32_t ctdb_version;
694         uint32_t generation;
695         uint32_t operation;
696         uint32_t destnode;
697         uint32_t srcnode;
698         uint32_t reqid;
699 };
700
701 struct ctdb_req_call {
702         struct ctdb_req_header hdr;
703         uint32_t flags;
704         uint32_t db_id;
705         uint32_t callid;
706         uint32_t hopcount;
707         uint32_t keylen;
708         uint32_t calldatalen;
709         uint8_t data[1]; /* key[] followed by calldata[] */
710 };
711
712 struct ctdb_reply_call {
713         struct ctdb_req_header hdr;
714         uint32_t status;
715         uint32_t datalen;
716         uint8_t  data[1];
717 };
718
719 struct ctdb_reply_error {
720         struct ctdb_req_header hdr;
721         uint32_t status;
722         uint32_t msglen;
723         uint8_t  msg[1];
724 };
725
726 struct ctdb_req_dmaster {
727         struct ctdb_req_header hdr;
728         uint32_t db_id;
729         uint64_t rsn;
730         uint32_t dmaster;
731         uint32_t keylen;
732         uint32_t datalen;
733         uint8_t  data[1];
734 };
735
736 struct ctdb_reply_dmaster {
737         struct ctdb_req_header hdr;
738         uint32_t db_id;
739         uint64_t rsn;
740         uint32_t keylen;
741         uint32_t datalen;
742         uint8_t  data[1];
743 };
744
745 struct ctdb_req_message {
746         struct ctdb_req_header hdr;
747         uint64_t srvid;
748         uint32_t datalen;
749         uint8_t data[1];
750 };
751
752 struct ctdb_req_getdbpath {
753         struct ctdb_req_header hdr;
754         uint32_t db_id;
755 };
756
757 struct ctdb_reply_getdbpath {
758         struct ctdb_req_header hdr;
759         uint32_t datalen;
760         uint8_t data[1];
761 };
762
763 struct ctdb_req_control {
764         struct ctdb_req_header hdr;
765         uint32_t opcode;
766         uint64_t srvid;
767         uint32_t client_id;
768 #define CTDB_CTRL_FLAG_NOREPLY   1
769         uint32_t flags;
770         uint32_t datalen;
771         uint8_t data[1];
772 };
773
774 struct ctdb_reply_control {
775         struct ctdb_req_header hdr;
776         int32_t  status;
777         uint32_t datalen;
778         uint32_t errorlen;
779         uint8_t data[1];
780 };
781
782 struct ctdb_req_keepalive {
783         struct ctdb_req_header hdr;
784 };
785
786
787 /* types of failures possible from TRANS2_COMMIT */
788 enum ctdb_trans2_commit_error {
789         CTDB_TRANS2_COMMIT_SUCCESS=0, /* all nodes committed successfully */
790         CTDB_TRANS2_COMMIT_TIMEOUT=1, /* at least one node timed out */
791         CTDB_TRANS2_COMMIT_ALLFAIL=2, /* all nodes failed the commit */
792         CTDB_TRANS2_COMMIT_SOMEFAIL=3 /* some nodes failed the commit, some allowed it */
793 };
794
795
796 /* internal prototypes */
797 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
798 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
799 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
800 int ctdb_parse_address(struct ctdb_context *ctdb,
801                        TALLOC_CTX *mem_ctx, const char *str,
802                        struct ctdb_address *address);
803 bool ctdb_same_ip(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
804 bool ctdb_same_sockaddr(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
805 uint32_t ctdb_hash(const TDB_DATA *key);
806 uint32_t ctdb_hash_string(const char *str);
807 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
808 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
809 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
810 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
811 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
812 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
813
814 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
815 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
816                     TDB_DATA key, struct ctdb_ltdb_header *header, 
817                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
818 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
819                     struct ctdb_ltdb_header *header, TDB_DATA data);
820 int32_t ctdb_control_start_persistent_update(struct ctdb_context *ctdb, 
821                         struct ctdb_req_control *c,
822                         TDB_DATA recdata);
823 int32_t ctdb_control_cancel_persistent_update(struct ctdb_context *ctdb, 
824                         struct ctdb_req_control *c,
825                         TDB_DATA recdata);
826 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
827 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, unsigned opcode);
828 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
829                            TDB_DATA key, struct ctdb_req_header *hdr,
830                            void (*recv_pkt)(void *, struct ctdb_req_header *),
831                            void *recv_context, bool ignore_generation);
832 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
833                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
834                                  struct ctdb_req_header *hdr, TDB_DATA *data,
835                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
836                                  void *recv_context, bool ignore_generation);
837 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
838
839 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
840                                              struct ctdb_call *call,
841                                              struct ctdb_ltdb_header *header,
842                                              TDB_DATA *data);
843
844
845 int ctdbd_start(struct ctdb_context *ctdb);
846 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
847 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
848
849 /*
850   queue a packet for sending
851 */
852 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
853
854 /*
855   setup the fd used by the queue
856  */
857 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
858
859 /*
860   setup a packet queue on a socket
861  */
862 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
863                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
864                                     
865                                     ctdb_queue_cb_fn_t callback,
866                                     void *private_data);
867
868 /*
869   allocate a packet for use in client<->daemon communication
870  */
871 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
872                                             TALLOC_CTX *mem_ctx, 
873                                             enum ctdb_operation operation, 
874                                             size_t length, size_t slength,
875                                             const char *type);
876 #define ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, type) \
877         (type *)_ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, sizeof(type), #type)
878
879 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
880                                                  TALLOC_CTX *mem_ctx, 
881                                                  enum ctdb_operation operation, 
882                                                  size_t length, size_t slength,
883                                                  const char *type);
884 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
885         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, sizeof(type), #type)
886
887
888 /*
889   lock a record in the ltdb, given a key
890  */
891 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
892
893 /*
894   unlock a record in the ltdb, given a key
895  */
896 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
897
898
899 /*
900   make a ctdb call to the local daemon - async send. Called from client context.
901
902   This constructs a ctdb_call request and queues it for processing. 
903   This call never blocks.
904 */
905 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db, 
906                                               struct ctdb_call *call);
907
908 /*
909   make a recv call to the local ctdb daemon - called from client context
910
911   This is called when the program wants to wait for a ctdb_call to complete and get the 
912   results. This call will block unless the call has already completed.
913 */
914 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
915
916 int ctdb_daemon_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
917                              ctdb_message_fn_t handler,
918                              void *private_data);
919
920 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
921                              uint64_t srvid, TDB_DATA data);
922
923 /*
924   send a ctdb message
925 */
926 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
927                              uint64_t srvid, TDB_DATA data);
928
929
930 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
931                                       TDB_DATA key,
932                                       void (*callback)(void *), void *private_data);
933
934 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db, 
935                                               struct ctdb_call *call);
936
937 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
938
939 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
940                                                      struct ctdb_call *call, 
941                                                      struct ctdb_ltdb_header *header);
942
943 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
944                     struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx, TDB_DATA *data,
945                     uint32_t caller);
946
947 #define ctdb_reqid_find(ctdb, reqid, type)      (type *)_ctdb_reqid_find(ctdb, reqid, #type, __location__)
948
949 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
950
951 int ctdb_socket_connect(struct ctdb_context *ctdb);
952
953 void ctdb_latency(struct ctdb_db_context *ctdb_db, const char *name, double *latency, struct timeval t);
954
955 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state);
956 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location);
957 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid);
958
959 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
960 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
961
962 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
963                              uint64_t srvid, uint32_t opcode, uint32_t client_id, uint32_t flags,
964                              TDB_DATA data,
965                              ctdb_control_callback_fn_t callback,
966                              void *private_data);
967
968 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata, 
969                                TDB_DATA *outdata, uint64_t tdb_flags, bool persistent);
970
971 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
972                          ctdb_fn_t fn, int id);
973
974 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid, 
975                  uint32_t opcode, uint32_t flags, TDB_DATA data, 
976                  TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
977                  struct timeval *timeout, char **errormsg);
978 int ctdb_control_recv(struct ctdb_context *ctdb, 
979                 struct ctdb_client_control_state *state, 
980                 TALLOC_CTX *mem_ctx,
981                 TDB_DATA *outdata, int32_t *status, char **errormsg);
982
983 struct ctdb_client_control_state *
984 ctdb_control_send(struct ctdb_context *ctdb, 
985                 uint32_t destnode, uint64_t srvid, 
986                 uint32_t opcode, uint32_t flags, TDB_DATA data, 
987                 TALLOC_CTX *mem_ctx,
988                 struct timeval *timeout,
989                 char **errormsg);
990
991
992
993
994 #define CHECK_CONTROL_DATA_SIZE(size) do { \
995  if (indata.dsize != size) { \
996          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
997                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
998          return -1; \
999  } \
1000  } while (0)
1001
1002 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1003 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1004 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1005 int ctdb_control_getnodemapv4(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1006 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1007 int ctdb_control_writerecord(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1008
1009
1010 struct ctdb_traverse_start {
1011         uint32_t db_id;
1012         uint32_t reqid;
1013         uint64_t srvid;
1014 };
1015
1016 /*
1017   structure used to pass record data between the child and parent
1018  */
1019 struct ctdb_rec_data {
1020         uint32_t length;
1021         uint32_t reqid;
1022         uint32_t keylen;
1023         uint32_t datalen;
1024         uint8_t  data[1];
1025 };
1026                                    
1027
1028 /* structure used for pulldb control */
1029 struct ctdb_control_pulldb {
1030         uint32_t db_id;
1031         uint32_t lmaster;
1032 };
1033
1034 /* structure used for sending lists of records */
1035 struct ctdb_marshall_buffer {
1036         uint32_t db_id;
1037         uint32_t count;
1038         uint8_t data[1];
1039 };
1040
1041 /* set dmaster control structure */
1042 struct ctdb_control_set_dmaster {
1043         uint32_t db_id;
1044         uint32_t dmaster;
1045 };
1046
1047 /*
1048   structure for setting a tunable
1049  */
1050 struct ctdb_control_set_tunable {
1051         uint32_t value;
1052         uint32_t length;
1053         uint8_t  name[1];
1054 };
1055
1056 /*
1057   structure for getting a tunable
1058  */
1059 struct ctdb_control_get_tunable {
1060         uint32_t length;
1061         uint8_t  name[1];
1062 };
1063
1064 /*
1065   structure for listing tunables
1066  */
1067 struct ctdb_control_list_tunable {
1068         uint32_t length;
1069         /* returns a : separated list of tunable names */
1070         uint8_t  data[1];
1071 };
1072
1073
1074 /* table that contains a list of all nodes a ctdb knows about and their 
1075    status
1076  */
1077 struct ctdb_node_and_flags {
1078         uint32_t pnn;
1079         uint32_t flags;
1080         ctdb_sock_addr addr;
1081 };
1082
1083 struct ctdb_node_map {
1084         uint32_t num;
1085         struct ctdb_node_and_flags nodes[1];
1086 };
1087
1088 struct ctdb_node_and_flagsv4 {
1089         uint32_t pnn;
1090         uint32_t flags;
1091         struct sockaddr_in sin;
1092 };
1093
1094 struct ctdb_node_mapv4 {
1095         uint32_t num;
1096         struct ctdb_node_and_flagsv4 nodes[1];
1097 };
1098
1099 struct ctdb_control_wipe_database {
1100         uint32_t db_id;
1101         uint32_t transaction_id;
1102 };
1103
1104 /*
1105   state of a in-progress ctdb call in client
1106 */
1107 struct ctdb_client_call_state {
1108         enum call_state state;
1109         uint32_t reqid;
1110         struct ctdb_db_context *ctdb_db;
1111         struct ctdb_call *call;
1112         struct {
1113                 void (*fn)(struct ctdb_client_call_state *);
1114                 void *private_data;
1115         } async;
1116 };
1117
1118
1119 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA indata, 
1120                                     TDB_DATA *outdata, uint32_t srcnode);
1121 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
1122 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
1123
1124 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data);
1125
1126 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
1127 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
1128 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
1129
1130 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
1131 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint32_t srcnode);
1132
1133 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, 
1134                                            TDB_DATA key, struct ctdb_ltdb_header *, TDB_DATA data);
1135
1136 struct ctdb_rec_data *ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
1137                                               uint32_t *reqid,
1138                                               struct ctdb_ltdb_header *header,
1139                                               TDB_DATA *key, TDB_DATA *data);
1140
1141 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1142 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
1143 int32_t ctdb_control_set_dmaster(struct ctdb_context *ctdb, TDB_DATA indata);
1144
1145 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
1146                                  struct ctdb_req_control *c,
1147                                  TDB_DATA indata, bool *async_reply,
1148                                  const char **errormsg);
1149 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
1150                                 TDB_DATA *outdata, int32_t status, const char *errormsg);
1151
1152 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1153 int32_t ctdb_control_thaw(struct ctdb_context *ctdb);
1154
1155 int ctdb_start_recoverd(struct ctdb_context *ctdb);
1156 void ctdb_stop_recoverd(struct ctdb_context *ctdb);
1157
1158 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb);
1159
1160 void ctdb_disable_monitoring(struct ctdb_context *ctdb);
1161 void ctdb_enable_monitoring(struct ctdb_context *ctdb);
1162 void ctdb_stop_monitoring(struct ctdb_context *ctdb);
1163 void ctdb_start_monitoring(struct ctdb_context *ctdb);
1164 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb);
1165 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
1166 void ctdb_start_keepalive(struct ctdb_context *ctdb);
1167 void ctdb_stop_keepalive(struct ctdb_context *ctdb);
1168 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA data, bool *async_reply);
1169
1170
1171 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node);
1172 void ctdb_call_resend_all(struct ctdb_context *ctdb);
1173 void ctdb_node_dead(struct ctdb_node *node);
1174 void ctdb_node_connected(struct ctdb_node *node);
1175 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
1176 void ctdb_set_scheduler(struct ctdb_context *ctdb);
1177 void ctdb_restore_scheduler(struct ctdb_context *ctdb);
1178 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, 
1179                                  struct ctdb_req_control *c,
1180                                  TDB_DATA indata, 
1181                                  bool *async_reply);
1182 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb, 
1183                                  struct ctdb_req_control *c,
1184                                  TDB_DATA indata, 
1185                                  bool *async_reply);
1186 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, 
1187                                  struct ctdb_req_control *c,
1188                                  TDB_DATA indata, 
1189                                  bool *async_reply);
1190 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb, 
1191                                  struct ctdb_req_control *c,
1192                                  TDB_DATA indata, 
1193                                  bool *async_reply);
1194 int32_t ctdb_control_start_recovery(struct ctdb_context *ctdb, 
1195                                  struct ctdb_req_control *c,
1196                                  bool *async_reply);
1197 int32_t ctdb_control_end_recovery(struct ctdb_context *ctdb, 
1198                                  struct ctdb_req_control *c,
1199                                  bool *async_reply);
1200
1201 struct ctdb_public_ipv4 {
1202         uint32_t pnn;
1203         struct sockaddr_in sin;
1204 };
1205
1206 struct ctdb_public_ip {
1207         uint32_t pnn;
1208         ctdb_sock_addr addr;
1209 };
1210 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1211                           uint32_t destnode, struct ctdb_public_ip *ip);
1212 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1213                          uint32_t destnode, struct ctdb_public_ip *ip);
1214
1215 struct ctdb_all_public_ipsv4 {
1216         uint32_t num;
1217         struct ctdb_public_ipv4 ips[1];
1218 };
1219
1220 struct ctdb_all_public_ips {
1221         uint32_t num;
1222         struct ctdb_public_ip ips[1];
1223 };
1224 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1225 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1226 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb, 
1227                         struct timeval timeout, uint32_t destnode, 
1228                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
1229 int ctdb_ctrl_get_public_ipsv4(struct ctdb_context *ctdb, 
1230                         struct timeval timeout, uint32_t destnode, 
1231                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
1232
1233
1234 /* from takeover/system.c */
1235 uint32_t uint16_checksum(uint16_t *data, size_t n);
1236 int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface);
1237 bool ctdb_sys_have_ip(ctdb_sock_addr *addr);
1238 int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, 
1239                       const ctdb_sock_addr *src,
1240                       uint32_t seq, uint32_t ack, int rst);
1241
1242 int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist);
1243 int ctdb_set_event_script(struct ctdb_context *ctdb, const char *script);
1244 int ctdb_set_event_script_dir(struct ctdb_context *ctdb, const char *script_dir);
1245 int ctdb_set_notification_script(struct ctdb_context *ctdb, const char *script);
1246 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap);
1247
1248 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id, 
1249                                 TDB_DATA indata);
1250 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata);
1251 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
1252 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
1253 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata);
1254 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata);
1255 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1256 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata);
1257
1258 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
1259 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1260 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
1261                                struct timeval timeout,
1262                                TALLOC_CTX *mem_ctx,
1263                                void (*callback)(struct ctdb_context *, int, void *),
1264                                void *private_data,
1265                                const char *fmt, ...) PRINTF_ATTRIBUTE(6,7);
1266 void ctdb_release_all_ips(struct ctdb_context *ctdb);
1267
1268 void set_nonblocking(int fd);
1269 void set_close_on_exec(int fd);
1270
1271 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep);
1272
1273 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
1274
1275 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
1276                                  TDB_DATA *outdata);
1277 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata);
1278 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata);
1279 int32_t ctdb_control_try_delete_records(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1280 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1281 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1282
1283 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb);
1284
1285 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata);
1286
1287 int ctdb_ctrl_get_all_tunables(struct ctdb_context *ctdb, 
1288                                struct timeval timeout, 
1289                                uint32_t destnode,
1290                                struct ctdb_tunable *tunables);
1291
1292 void ctdb_start_freeze(struct ctdb_context *ctdb);
1293
1294 bool parse_ip_mask(const char *s, const char *iface, ctdb_sock_addr *addr, unsigned *mask);
1295 bool parse_ip_port(const char *s, ctdb_sock_addr *addr);
1296 bool parse_ip(const char *s, const char *iface, unsigned port, ctdb_sock_addr *addr);
1297 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin);
1298  
1299
1300 int ctdb_sys_open_capture_socket(const char *iface, void **private_data);
1301 int ctdb_sys_close_capture_socket(void *private_data);
1302 int ctdb_sys_read_tcp_packet(int s, void *private_data, ctdb_sock_addr *src, ctdb_sock_addr *dst, uint32_t *ack_seq, uint32_t *seq);
1303
1304 int ctdb_ctrl_killtcp(struct ctdb_context *ctdb, 
1305                       struct timeval timeout, 
1306                       uint32_t destnode,
1307                       struct ctdb_control_killtcp *killtcp);
1308
1309 int ctdb_ctrl_add_public_ip(struct ctdb_context *ctdb, 
1310                       struct timeval timeout, 
1311                       uint32_t destnode,
1312                       struct ctdb_control_ip_iface *pub);
1313
1314 int ctdb_ctrl_del_public_ip(struct ctdb_context *ctdb, 
1315                       struct timeval timeout, 
1316                       uint32_t destnode,
1317                       struct ctdb_control_ip_iface *pub);
1318
1319 int ctdb_ctrl_gratious_arp(struct ctdb_context *ctdb, 
1320                       struct timeval timeout, 
1321                       uint32_t destnode,
1322                       ctdb_sock_addr *addr,
1323                       const char *ifname);
1324
1325 int ctdb_ctrl_get_tcp_tickles(struct ctdb_context *ctdb, 
1326                       struct timeval timeout, 
1327                       uint32_t destnode,
1328                       TALLOC_CTX *mem_ctx,
1329                       ctdb_sock_addr *addr,
1330                       struct ctdb_control_tcp_tickle_list **list);
1331
1332
1333 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb, 
1334                       uint32_t client_id,
1335                       TDB_DATA indata);
1336 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb, 
1337                       TDB_DATA indata);
1338 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb, 
1339                       TDB_DATA indata);
1340 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb, 
1341                       TDB_DATA *outdata);
1342 int32_t ctdb_control_uptime(struct ctdb_context *ctdb, 
1343                       TDB_DATA *outdata);
1344
1345 int ctdb_attach_persistent(struct ctdb_context *ctdb);
1346
1347 int32_t ctdb_control_persistent_store(struct ctdb_context *ctdb, 
1348                                       struct ctdb_req_control *c, 
1349                                       TDB_DATA recdata, bool *async_reply);
1350 int32_t ctdb_control_update_record(struct ctdb_context *ctdb, 
1351                                    struct ctdb_req_control *c, TDB_DATA recdata, 
1352                                    bool *async_reply);
1353 int32_t ctdb_control_trans2_commit(struct ctdb_context *ctdb, 
1354                                    struct ctdb_req_control *c, 
1355                                    TDB_DATA recdata, bool *async_reply);
1356
1357 int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id);
1358 int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id);
1359 int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata);
1360
1361
1362 int ctdb_vacuum(struct ctdb_context *ctdb, int argc, const char **argv);
1363 int ctdb_repack(struct ctdb_context *ctdb, int argc, const char **argv);
1364
1365 void ctdb_block_signal(int signum);
1366 void ctdb_unblock_signal(int signum);
1367 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb);
1368 int ctdb_set_child_logging(struct ctdb_context *ctdb);
1369
1370
1371 typedef void (*client_async_callback)(struct ctdb_context *ctdb, uint32_t node_pnn, int32_t res, TDB_DATA outdata, void *callback_data);
1372
1373 struct client_async_data {
1374         enum ctdb_controls opcode;
1375         bool dont_log_errors;
1376         uint32_t count;
1377         uint32_t fail_count;
1378         client_async_callback callback;
1379         client_async_callback fail_callback;
1380         void *callback_data;
1381 };
1382 void ctdb_client_async_add(struct client_async_data *data, struct ctdb_client_control_state *state);
1383 int ctdb_client_async_wait(struct ctdb_context *ctdb, struct client_async_data *data);
1384 int ctdb_client_async_control(struct ctdb_context *ctdb,
1385                                 enum ctdb_controls opcode,
1386                                 uint32_t *nodes,
1387                                 struct timeval timeout,
1388                                 bool dont_log_errors,
1389                                 TDB_DATA data,
1390                                 client_async_callback client_callback,
1391                                 client_async_callback fail_callback,
1392                                 void *callback_data);
1393
1394 void ctdb_load_nodes_file(struct ctdb_context *ctdb);
1395
1396 int ctdb_control_reload_nodes_file(struct ctdb_context *ctdb, uint32_t opcode);
1397
1398 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata);
1399 int32_t ctdb_control_get_capabilities(struct ctdb_context *ctdb, TDB_DATA *outdata);
1400
1401 int32_t ctdb_control_trans2_finished(struct ctdb_context *ctdb, 
1402                                      struct ctdb_req_control *c);
1403 int32_t ctdb_control_trans2_error(struct ctdb_context *ctdb, 
1404                                   struct ctdb_req_control *c);
1405
1406 char *ctdb_addr_to_str(ctdb_sock_addr *addr);
1407 unsigned ctdb_addr_to_port(ctdb_sock_addr *addr);
1408 void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip);
1409
1410 int32_t ctdb_control_recd_ping(struct ctdb_context *ctdb);
1411 int32_t ctdb_control_set_recmaster(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata);
1412
1413 extern int script_log_level;
1414
1415 int ctdb_ctrl_event_script_init(struct ctdb_context *ctdb);
1416 int ctdb_ctrl_event_script_start(struct ctdb_context *ctdb, const char *name);
1417 int ctdb_ctrl_event_script_stop(struct ctdb_context *ctdb, int32_t res);
1418 int ctdb_ctrl_event_script_finished(struct ctdb_context *ctdb);
1419
1420 int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb);
1421 int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata);
1422 int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata);
1423 int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb);
1424
1425
1426 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata);
1427
1428 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len);
1429
1430 #endif