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