e1237b3f97b09faae29a964e586dc964c8bf8dc5
[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_client.h"
24 #include <sys/socket.h>
25
26 /*
27   recovery daemon memdump reply address
28  */
29 struct rd_memdump_reply {
30         uint32_t pnn;
31         uint64_t srvid;
32 };
33
34 /*
35   description for a TAKEOVER_RUN message reply address
36  */
37 struct takeover_run_reply {
38         uint32_t pnn;
39         uint64_t srvid;
40 };
41
42 /*
43  * pid of the ctdbd daemon
44  */
45 extern pid_t ctdbd_pid;
46
47 /*
48   a tcp connection description
49   also used by tcp_add and tcp_remove controls
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 recover_timeout;
86         uint32_t recover_interval;
87         uint32_t election_timeout;
88         uint32_t takeover_timeout;
89         uint32_t monitor_interval;
90         uint32_t tickle_update_interval;
91         uint32_t script_timeout;
92         uint32_t script_timeout_count; /* allow dodgy scripts to hang this many times in a row before we mark the node unhealthy */
93         uint32_t script_unhealthy_on_timeout; /* obsolete */
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 reclock_ping_period;
102         uint32_t no_ip_failback;
103         uint32_t disable_ip_failover;
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         uint32_t vacuum_default_interval;
112         uint32_t vacuum_max_run_time;
113         uint32_t repack_limit;
114         uint32_t vacuum_limit;
115         uint32_t vacuum_min_interval;
116         uint32_t vacuum_max_interval;
117         uint32_t max_queue_depth_drop_msg;
118         uint32_t use_status_events_for_monitoring;
119         uint32_t allow_unhealthy_db_read;
120         uint32_t stat_history_interval;
121         uint32_t deferred_attach_timeout;
122         uint32_t vacuum_fast_path_count;
123         uint32_t lcp2_public_ip_assignment;
124         uint32_t allow_client_db_attach;
125 };
126
127 /*
128   an installed ctdb remote call
129 */
130 struct ctdb_registered_call {
131         struct ctdb_registered_call *next, *prev;
132         uint32_t id;
133         ctdb_fn_t fn;
134 };
135
136 /*
137   this address structure might need to be generalised later for some
138   transports
139 */
140 struct ctdb_address {
141         const char *address;
142         int port;
143 };
144
145 /*
146   check that a pnn is valid
147  */
148 #define ctdb_validate_pnn(ctdb, pnn) (((uint32_t)(pnn)) < (ctdb)->num_nodes)
149
150
151 /* called from the queue code when a packet comes in. Called with data==NULL
152    on error */
153 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
154                                    void *private_data);
155
156 /* used for callbacks in ctdb_control requests */
157 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
158                                            int32_t status, TDB_DATA data, 
159                                            const char *errormsg,
160                                            void *private_data);
161 /*
162   structure describing a connected client in the daemon
163  */
164 struct ctdb_client {
165         struct ctdb_context *ctdb;
166         int fd;
167         struct ctdb_queue *queue;
168         uint32_t client_id;
169         pid_t pid;
170         struct ctdb_tcp_list *tcp_list;
171         uint32_t db_id;
172         uint32_t num_persistent_updates;
173         struct ctdb_client_notify_list *notify;
174 };
175
176 struct ctdb_iface;
177
178 /* state associated with a public ip address */
179 struct ctdb_vnn {
180         struct ctdb_vnn *prev, *next;
181
182         struct ctdb_iface *iface;
183         const char **ifaces;
184         ctdb_sock_addr public_address;
185         uint8_t public_netmask_bits;
186
187         /* the node number that is serving this public address, if any. 
188            If no node serves this ip it is set to -1 */
189         int32_t pnn;
190
191         /* List of clients to tickle for this public address */
192         struct ctdb_tcp_array *tcp_array;
193
194         /* whether we need to update the other nodes with changes to our list
195            of connected clients */
196         bool tcp_update_needed;
197
198         /* a context to hang sending gratious arp events off */
199         TALLOC_CTX *takeover_ctx;
200
201         struct ctdb_kill_tcp *killtcp;
202 };
203
204 /*
205   state associated with one node
206 */
207 struct ctdb_node {
208         struct ctdb_context *ctdb;
209         struct ctdb_address address;
210         const char *name; /* for debug messages */
211         void *private_data; /* private to transport */
212         uint32_t pnn;
213         uint32_t flags;
214
215         /* used by the dead node monitoring */
216         uint32_t dead_count;
217         uint32_t rx_cnt;
218         uint32_t tx_cnt;
219
220         /* used to track node capabilities, is only valid/tracked inside the
221            recovery daemon.
222         */
223         uint32_t capabilities;
224
225         /* a list of controls pending to this node, so we can time them out quickly
226            if the node becomes disconnected */
227         struct daemon_control_state *pending_controls;
228
229         /* used by the recovery daemon when distributing ip addresses 
230            across the nodes.  it needs to know which public ip's can be handled
231            by each node.
232         */
233         struct ctdb_all_public_ips *known_public_ips;
234         struct ctdb_all_public_ips *available_public_ips;
235         /* used by the recovery dameon to track when a node should be banned */
236         struct ctdb_banning_state *ban_state; 
237 };
238
239 /*
240   transport specific methods
241 */
242 struct ctdb_methods {
243         int (*initialise)(struct ctdb_context *); /* initialise transport structures */ 
244         int (*start)(struct ctdb_context *); /* start the transport */
245         int (*add_node)(struct ctdb_node *); /* setup a new node */     
246         int (*connect_node)(struct ctdb_node *); /* connect to node */
247         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
248         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
249         void (*shutdown)(struct ctdb_context *); /* shutdown transport */
250         void (*restart)(struct ctdb_node *); /* stop and restart the connection */
251 };
252
253 /*
254   transport calls up to the ctdb layer
255 */
256 struct ctdb_upcalls {
257         /* recv_pkt is called when a packet comes in */
258         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
259
260         /* node_dead is called when an attempt to send to a node fails */
261         void (*node_dead)(struct ctdb_node *);
262
263         /* node_connected is called when a connection to a node is established */
264         void (*node_connected)(struct ctdb_node *);
265 };
266
267 /* list of message handlers - needs to be changed to a more efficient data
268    structure so we can find a message handler given a srvid quickly */
269 struct ctdb_message_list {
270         struct ctdb_context *ctdb;
271         struct ctdb_message_list *next, *prev;
272         uint64_t srvid;
273         ctdb_msg_fn_t message_handler;
274         void *message_private;
275 };
276
277 /* additional data required for the daemon mode */
278 struct ctdb_daemon_data {
279         int sd;
280         char *name;
281         struct ctdb_queue *queue;
282 };
283
284
285 #define CTDB_UPDATE_STAT(ctdb, counter, value) \
286         {                                                                               \
287                 if (value > ctdb->statistics.counter) {                                 \
288                         ctdb->statistics.counter = c->hopcount;                         \
289                 }                                                                       \
290                 if (value > ctdb->statistics_current.counter) {                         \
291                         ctdb->statistics_current.counter = c->hopcount;                 \
292                 }                                                                       \
293         }
294
295 #define CTDB_INCREMENT_STAT(ctdb, counter) \
296         {                                                                               \
297                 ctdb->statistics.counter++;                                             \
298                 ctdb->statistics_current.counter++;                                     \
299         }
300
301 #define CTDB_DECREMENT_STAT(ctdb, counter) \
302         {                                                                               \
303                 if (ctdb->statistics.counter > 0)                                       \
304                         ctdb->statistics.counter--;                                     \
305                 if (ctdb->statistics_current.counter > 0)                               \
306                         ctdb->statistics_current.counter--;                             \
307         }
308
309 #define CTDB_UPDATE_RECLOCK_LATENCY(ctdb, name, counter, value) \
310         {                                                                               \
311                 if (value > ctdb->statistics.counter.max)                                       \
312                         ctdb->statistics.counter.max = value;                           \
313                 if (value > ctdb->statistics_current.counter.max)                               \
314                         ctdb->statistics_current.counter.max = value;                   \
315                                                                                         \
316                 if (ctdb->statistics.counter.num == 0 || value < ctdb->statistics.counter.min)  \
317                         ctdb->statistics.counter.min = value;                           \
318                 if (ctdb->statistics_current.counter.num == 0 || value < ctdb->statistics_current.counter.min)  \
319                         ctdb->statistics_current.counter.min = value;                   \
320                                                                                         \
321                 ctdb->statistics.counter.total += value;                                        \
322                 ctdb->statistics_current.counter.total += value;                                \
323                                                                                         \
324                 ctdb->statistics.counter.num++;                                         \
325                 ctdb->statistics_current.counter.num++;                                 \
326                                                                                         \
327                 if (ctdb->tunable.reclock_latency_ms != 0) {                            \
328                         if (value*1000 > ctdb->tunable.reclock_latency_ms) {            \
329                                 DEBUG(DEBUG_ERR, ("High RECLOCK latency %fs for operation %s\n", value, name)); \
330                         }                                                               \
331                 }                                                                       \
332         }
333
334
335 #define CTDB_UPDATE_LATENCY(ctdb, db, operation, counter, t) \
336         {                                                                               \
337                 double l = timeval_elapsed(&t);                                         \
338                                                                                         \
339                 if (l > ctdb->statistics.counter.max)                                   \
340                         ctdb->statistics.counter.max = l;                               \
341                 if (l > ctdb->statistics_current.counter.max)                           \
342                         ctdb->statistics_current.counter.max = l;                       \
343                                                                                         \
344                 if (ctdb->statistics.counter.num == 0 || l < ctdb->statistics.counter.min)      \
345                         ctdb->statistics.counter.min = l;                               \
346                 if (ctdb->statistics_current.counter.num == 0 || l < ctdb->statistics_current.counter.min)      \
347                         ctdb->statistics_current.counter.min = l;                       \
348                                                                                         \
349                 ctdb->statistics.counter.total += l;                                    \
350                 ctdb->statistics_current.counter.total += l;                            \
351                                                                                         \
352                 ctdb->statistics.counter.num++;                                         \
353                 ctdb->statistics_current.counter.num++;                                 \
354                                                                                         \
355                 if (ctdb->tunable.log_latency_ms !=0) {                                 \
356                         if (l*1000 > ctdb->tunable.log_latency_ms) {                    \
357                                 DEBUG(DEBUG_WARNING, ("High latency %.6fs for operation %s on database %s\n", l, operation, db->db_name));\
358                         }                                                               \
359                 }                                                                       \
360         }
361
362
363
364
365
366 #define INVALID_GENERATION 1
367 /* table that contains the mapping between a hash value and lmaster
368  */
369 struct ctdb_vnn_map {
370         uint32_t generation;
371         uint32_t size;
372         uint32_t *map;
373 };
374
375 /* 
376    a wire representation of the vnn map
377  */
378 struct ctdb_vnn_map_wire {
379         uint32_t generation;
380         uint32_t size;
381         uint32_t map[1];
382 };
383
384 /* a structure that contains the elements required for the write record
385    control
386 */
387 struct ctdb_write_record {
388         uint32_t dbid;
389         uint32_t keylen;
390         uint32_t datalen;
391         unsigned char blob[1];
392 };
393
394 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
395
396 #define CTDB_MONITORING_ACTIVE          0
397 #define CTDB_MONITORING_DISABLED        1
398
399 /* The different capabilities of the ctdb daemon. */
400 #define CTDB_CAP_RECMASTER              0x00000001
401 #define CTDB_CAP_LMASTER                0x00000002
402 /* This capability is set if CTDB_LVS_PUBLIC_IP is set */
403 #define CTDB_CAP_LVS                    0x00000004
404 /* This capability is set if NATGW is enabled */
405 #define CTDB_CAP_NATGW                  0x00000008
406
407 #define NUM_DB_PRIORITIES 3
408 /* main state of the ctdb daemon */
409 struct ctdb_context {
410         struct tevent_context *ev;
411         struct timeval ctdbd_start_time;
412         struct timeval last_recovery_started;
413         struct timeval last_recovery_finished;
414         uint32_t recovery_mode;
415         TALLOC_CTX *tickle_update_context;
416         TALLOC_CTX *keepalive_ctx;
417         struct ctdb_tunable tunable;
418         enum ctdb_freeze_mode freeze_mode[NUM_DB_PRIORITIES+1];
419         struct ctdb_freeze_handle *freeze_handles[NUM_DB_PRIORITIES+1];
420         bool freeze_transaction_started;
421         uint32_t freeze_transaction_id;
422         struct ctdb_address address;
423         const char *name;
424         const char *db_directory;
425         const char *db_directory_persistent;
426         const char *db_directory_state;
427         struct tdb_wrap *db_persistent_health;
428         uint32_t db_persistent_startup_generation;
429         uint64_t db_persistent_check_errors;
430         uint64_t max_persistent_check_errors;
431         const char *transport;
432         char *recovery_lock_file;
433         int recovery_lock_fd;
434         uint32_t pnn; /* our own pnn */
435         uint32_t num_nodes;
436         uint32_t num_connected;
437         unsigned flags;
438         uint32_t capabilities;
439         struct idr_context *idr;
440         int lastid;
441         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
442         struct ctdb_vnn *vnn; /* list of public ip addresses and interfaces */
443         struct ctdb_vnn *single_ip_vnn; /* a structure for the single ip */
444         struct ctdb_iface *ifaces; /* list of local interfaces */
445         char *err_msg;
446         const struct ctdb_methods *methods; /* transport methods */
447         const struct ctdb_upcalls *upcalls; /* transport upcalls */
448         void *private_data; /* private to transport */
449         struct ctdb_db_context *db_list;
450         struct ctdb_message_list *message_list;
451         struct ctdb_daemon_data daemon;
452         struct ctdb_statistics statistics;
453         struct ctdb_statistics statistics_current;
454 #define MAX_STAT_HISTORY 100
455         struct ctdb_statistics statistics_history[MAX_STAT_HISTORY];
456         struct ctdb_vnn_map *vnn_map;
457         uint32_t num_clients;
458         uint32_t recovery_master;
459         struct ctdb_call_state *pending_calls;
460         struct ctdb_client_ip *client_ip_list;
461         struct trbt_tree *server_ids; 
462         bool do_setsched;
463         void *saved_scheduler_param;
464         const char *event_script_dir;
465         const char *notification_script;
466         const char *default_public_interface;
467         pid_t ctdbd_pid;
468         pid_t recoverd_pid;
469         pid_t syslogd_pid;
470         bool done_startup;
471         const char *node_ip;
472         struct ctdb_monitor_state *monitor;
473         struct ctdb_log_state *log;
474         int start_as_disabled;
475         int start_as_stopped;
476         bool valgrinding;
477         uint32_t event_script_timeouts; /* counting how many consecutive times an eventscript has timedout */
478         uint32_t *recd_ping_count;
479         TALLOC_CTX *release_ips_ctx; /* a context used to automatically drop all IPs if we fail to recover the node */
480
481         TALLOC_CTX *event_script_ctx;
482
483         struct ctdb_event_script_state *current_monitor;
484         struct ctdb_scripts_wire *last_status[CTDB_EVENT_MAX];
485
486         TALLOC_CTX *banning_ctx;
487
488         struct ctdb_vacuum_child_context *vacuumers;
489
490         /* mapping from pid to ctdb_client * */
491         struct ctdb_client_pid_list *client_pids;
492
493         /* used in the recovery daemon to remember the ip allocation */
494         struct trbt_tree *ip_tree;
495
496         /* Used to defer db attach requests while in recovery mode */
497         struct ctdb_deferred_attach_context *deferred_attach;
498
499         /* list of event script callback functions that are active */
500         struct event_script_callback *script_callbacks;
501 };
502
503 struct ctdb_db_context {
504         struct ctdb_db_context *next, *prev;
505         struct ctdb_context *ctdb;
506         uint32_t db_id;
507         uint32_t priority;
508         bool persistent;
509         const char *db_name;
510         const char *db_path;
511         struct tdb_wrap *ltdb;
512         struct ctdb_registered_call *calls; /* list of registered calls */
513         uint32_t seqnum;
514         struct timed_event *seqnum_update;
515         struct ctdb_traverse_local_handle *traverse;
516         bool transaction_active;
517         struct ctdb_vacuum_handle *vacuum_handle;
518         char *unhealthy_reason;
519         int pending_requests;
520         struct lockwait_handle *lockwait_active;
521         struct lockwait_handle *lockwait_overflow;
522         struct ctdb_persistent_state *persistent_state;
523         struct trbt_tree *delete_queue;
524         int (*ctdb_ltdb_store_fn)(struct ctdb_db_context *ctdb_db,
525                                   TDB_DATA key,
526                                   struct ctdb_ltdb_header *header,
527                                   TDB_DATA data);
528
529 };
530
531
532 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
533           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
534           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
535           return -1; }} while (0)
536
537 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
538           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
539           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
540           return; }} while (0)
541
542 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
543           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
544           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
545           return NULL; }} while (0)
546
547 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
548           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
549           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
550           }} while (0)
551
552 /*
553   structure passed in set_call control
554  */
555 struct ctdb_control_set_call {
556         uint32_t db_id;
557         ctdb_fn_t fn;
558         uint32_t id;
559 };
560
561 /*
562   struct for kill_tcp control
563  */
564 struct ctdb_control_killtcp {
565         ctdb_sock_addr src_addr;
566         ctdb_sock_addr dst_addr;
567 };
568
569 /*
570   struct holding a ctdb_sock_addr and an interface name,
571   used to add/remove public addresses
572  */
573 struct ctdb_control_ip_iface {
574         ctdb_sock_addr addr;
575         uint32_t mask;
576         uint32_t len;
577         char iface[1];
578 };
579
580 /*
581   struct holding a ctdb_sock_addr and an interface name,
582   used for send_gratious_arp
583  */
584 struct ctdb_control_gratious_arp {
585         ctdb_sock_addr addr;
586         uint32_t mask;
587         uint32_t len;
588         char iface[1];
589 };
590
591 /*
592   persistent store control - update this record on all other nodes
593  */
594 struct ctdb_control_persistent_store {
595         uint32_t db_id;
596         uint32_t len;
597         uint8_t  data[1];
598 };
599
600 /*
601   structure used for CTDB_SRVID_NODE_FLAGS_CHANGED
602  */
603 struct ctdb_node_flag_change {
604         uint32_t pnn;
605         uint32_t new_flags;
606         uint32_t old_flags;
607 };
608
609 /*
610   struct for admin setting a ban
611  */
612 struct ctdb_ban_info {
613         uint32_t pnn;
614         uint32_t ban_time;
615 };
616
617 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
618
619 #define CTDB_LMASTER_ANY        0xffffffff
620
621 /*
622   state of a in-progress ctdb call
623 */
624 struct ctdb_call_state {
625         struct ctdb_call_state *next, *prev;
626         enum call_state state;
627         uint32_t reqid;
628         struct ctdb_req_call *c;
629         struct ctdb_db_context *ctdb_db;
630         const char *errmsg;
631         struct ctdb_call *call;
632         uint32_t generation;
633         struct {
634                 void (*fn)(struct ctdb_call_state *);
635                 void *private_data;
636         } async;
637 };
638
639
640 /* used for fetch_lock */
641 struct ctdb_fetch_handle {
642         struct ctdb_db_context *ctdb_db;
643         TDB_DATA key;
644         TDB_DATA *data;
645         struct ctdb_ltdb_header header;
646 };
647
648 /* internal prototypes */
649 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
650 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
651 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
652 int ctdb_parse_address(struct ctdb_context *ctdb,
653                        TALLOC_CTX *mem_ctx, const char *str,
654                        struct ctdb_address *address);
655 bool ctdb_same_ip(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
656 bool ctdb_same_sockaddr(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
657 uint32_t ctdb_hash(const TDB_DATA *key);
658 uint32_t ctdb_hash_string(const char *str);
659 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
660 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
661 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
662 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
663 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
664 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
665
666 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
667 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
668                     TDB_DATA key, struct ctdb_ltdb_header *header, 
669                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
670 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
671                     struct ctdb_ltdb_header *header, TDB_DATA data);
672 int ctdb_ltdb_delete(struct ctdb_db_context *ctdb_db, TDB_DATA key);
673 int32_t ctdb_control_start_persistent_update(struct ctdb_context *ctdb, 
674                         struct ctdb_req_control *c,
675                         TDB_DATA recdata);
676 int32_t ctdb_control_cancel_persistent_update(struct ctdb_context *ctdb, 
677                         struct ctdb_req_control *c,
678                         TDB_DATA recdata);
679 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
680 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, unsigned opcode);
681 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
682                            TDB_DATA key, struct ctdb_req_header *hdr,
683                            void (*recv_pkt)(void *, struct ctdb_req_header *),
684                            void *recv_context, bool ignore_generation);
685 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
686                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
687                                  struct ctdb_req_header *hdr, TDB_DATA *data,
688                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
689                                  void *recv_context, bool ignore_generation);
690 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
691
692 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
693                                              struct ctdb_call *call,
694                                              struct ctdb_ltdb_header *header,
695                                              TDB_DATA *data);
696
697
698 int ctdbd_start(struct ctdb_context *ctdb);
699 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
700 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
701
702 /*
703   queue a packet for sending
704 */
705 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
706
707 /*
708   setup the fd used by the queue
709  */
710 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
711
712 /*
713   setup a packet queue on a socket
714  */
715 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
716                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
717                                     
718                                     ctdb_queue_cb_fn_t callback,
719                                     void *private_data, const char *fmt, ...)
720         PRINTF_ATTRIBUTE(7,8);
721
722 /*
723   allocate a packet for use in client<->daemon communication
724  */
725 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
726                                             TALLOC_CTX *mem_ctx, 
727                                             enum ctdb_operation operation, 
728                                             size_t length, size_t slength,
729                                             const char *type);
730 #define ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, type) \
731         (type *)_ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, sizeof(type), #type)
732
733 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
734                                                  TALLOC_CTX *mem_ctx, 
735                                                  enum ctdb_operation operation, 
736                                                  size_t length, size_t slength,
737                                                  const char *type);
738 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
739         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, sizeof(type), #type)
740
741 int ctdb_queue_length(struct ctdb_queue *queue);
742
743 /*
744   lock a record in the ltdb, given a key
745  */
746 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
747
748 /*
749   unlock a record in the ltdb, given a key
750  */
751 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
752
753
754 /*
755   make a ctdb call to the local daemon - async send. Called from client context.
756
757   This constructs a ctdb_call request and queues it for processing. 
758   This call never blocks.
759 */
760 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db, 
761                                               struct ctdb_call *call);
762
763 /*
764   make a recv call to the local ctdb daemon - called from client context
765
766   This is called when the program wants to wait for a ctdb_call to complete and get the 
767   results. This call will block unless the call has already completed.
768 */
769 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
770
771 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
772                              uint64_t srvid, TDB_DATA data);
773
774 /*
775   send a ctdb message
776 */
777 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
778                              uint64_t srvid, TDB_DATA data);
779
780
781 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
782                                       TDB_DATA key,
783                                       void (*callback)(void *), void *private_data);
784
785 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db, 
786                                               struct ctdb_call *call);
787
788 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
789
790 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
791                                                      struct ctdb_call *call, 
792                                                      struct ctdb_ltdb_header *header);
793
794 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
795                     struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx,
796                     TDB_DATA *data);
797
798 #define ctdb_reqid_find(ctdb, reqid, type)      (type *)_ctdb_reqid_find(ctdb, reqid, #type, __location__)
799
800 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
801
802 int ctdb_socket_connect(struct ctdb_context *ctdb);
803
804 #define CTDB_BAD_REQID ((uint32_t)-1)
805 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state);
806 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location);
807 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid);
808
809 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
810 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
811
812 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
813                              uint64_t srvid, uint32_t opcode, uint32_t client_id, uint32_t flags,
814                              TDB_DATA data,
815                              ctdb_control_callback_fn_t callback,
816                              void *private_data);
817
818 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata, 
819                                TDB_DATA *outdata, uint64_t tdb_flags,
820                                bool persistent, uint32_t client_id,
821                                struct ctdb_req_control *c,
822                                bool *async_reply);
823
824 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
825                          ctdb_fn_t fn, int id);
826
827 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid, 
828                  uint32_t opcode, uint32_t flags, TDB_DATA data, 
829                  TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
830                  struct timeval *timeout, char **errormsg);
831 int ctdb_control_recv(struct ctdb_context *ctdb, 
832                 struct ctdb_client_control_state *state, 
833                 TALLOC_CTX *mem_ctx,
834                 TDB_DATA *outdata, int32_t *status, char **errormsg);
835
836 struct ctdb_client_control_state *
837 ctdb_control_send(struct ctdb_context *ctdb, 
838                 uint32_t destnode, uint64_t srvid, 
839                 uint32_t opcode, uint32_t flags, TDB_DATA data, 
840                 TALLOC_CTX *mem_ctx,
841                 struct timeval *timeout,
842                 char **errormsg);
843
844
845
846
847 #define CHECK_CONTROL_DATA_SIZE(size) do { \
848  if (indata.dsize != size) { \
849          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
850                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
851          return -1; \
852  } \
853  } while (0)
854
855 #define CHECK_CONTROL_MIN_DATA_SIZE(size) do { \
856  if (indata.dsize < size) { \
857          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected >= %u\n", \
858                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
859          return -1; \
860  } \
861  } while (0)
862
863 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
864 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
865 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
866 int ctdb_control_getnodemapv4(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
867 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
868 int ctdb_control_writerecord(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
869
870
871 /* structure used for pulldb control */
872 struct ctdb_control_pulldb {
873         uint32_t db_id;
874         uint32_t lmaster;
875 };
876
877 /* structure used for sending lists of records */
878 struct ctdb_marshall_buffer {
879         uint32_t db_id;
880         uint32_t count;
881         uint8_t data[1];
882 };
883
884 /* set dmaster control structure */
885 struct ctdb_control_set_dmaster {
886         uint32_t db_id;
887         uint32_t dmaster;
888 };
889
890 /*
891   structure for setting a tunable
892  */
893 struct ctdb_control_set_tunable {
894         uint32_t value;
895         uint32_t length;
896         uint8_t  name[1];
897 };
898
899 /*
900   structure for getting a tunable
901  */
902 struct ctdb_control_get_tunable {
903         uint32_t length;
904         uint8_t  name[1];
905 };
906
907 /*
908   structure for listing tunables
909  */
910 struct ctdb_control_list_tunable {
911         uint32_t length;
912         /* returns a : separated list of tunable names */
913         uint8_t  data[1];
914 };
915
916
917 struct ctdb_node_and_flagsv4 {
918         uint32_t pnn;
919         uint32_t flags;
920         struct sockaddr_in sin;
921 };
922
923 struct ctdb_node_mapv4 {
924         uint32_t num;
925         struct ctdb_node_and_flagsv4 nodes[1];
926 };
927
928 struct ctdb_control_wipe_database {
929         uint32_t db_id;
930         uint32_t transaction_id;
931 };
932
933 /*
934   state of a in-progress ctdb call in client
935 */
936 struct ctdb_client_call_state {
937         enum call_state state;
938         uint32_t reqid;
939         struct ctdb_db_context *ctdb_db;
940         struct ctdb_call *call;
941         struct {
942                 void (*fn)(struct ctdb_client_call_state *);
943                 void *private_data;
944         } async;
945 };
946
947
948 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA indata, 
949                                     TDB_DATA *outdata, uint32_t srcnode, uint32_t client_id);
950 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
951 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
952 int32_t ctdb_control_traverse_kill(struct ctdb_context *ctdb, TDB_DATA indata, 
953                                     TDB_DATA *outdata, uint32_t srcnode);
954
955 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data);
956
957 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
958 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
959 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
960
961 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
962 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint32_t srcnode);
963
964 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, 
965                                            TDB_DATA key, struct ctdb_ltdb_header *, TDB_DATA data);
966
967 struct ctdb_rec_data *ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
968                                               uint32_t *reqid,
969                                               struct ctdb_ltdb_header *header,
970                                               TDB_DATA *key, TDB_DATA *data);
971
972 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
973 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
974 int32_t ctdb_control_set_dmaster(struct ctdb_context *ctdb, TDB_DATA indata);
975
976 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
977                                  struct ctdb_req_control *c,
978                                  TDB_DATA indata, bool *async_reply,
979                                  const char **errormsg);
980 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
981                                 TDB_DATA *outdata, int32_t status, const char *errormsg);
982
983 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
984 int32_t ctdb_control_thaw(struct ctdb_context *ctdb, uint32_t priority);
985
986 int ctdb_start_recoverd(struct ctdb_context *ctdb);
987 void ctdb_stop_recoverd(struct ctdb_context *ctdb);
988
989 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb);
990
991 void ctdb_disable_monitoring(struct ctdb_context *ctdb);
992 void ctdb_enable_monitoring(struct ctdb_context *ctdb);
993 void ctdb_stop_monitoring(struct ctdb_context *ctdb);
994 void ctdb_start_monitoring(struct ctdb_context *ctdb);
995 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb);
996 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
997 void ctdb_start_keepalive(struct ctdb_context *ctdb);
998 void ctdb_stop_keepalive(struct ctdb_context *ctdb);
999 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA data, bool *async_reply);
1000
1001
1002 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node);
1003 void ctdb_call_resend_all(struct ctdb_context *ctdb);
1004 void ctdb_node_dead(struct ctdb_node *node);
1005 void ctdb_node_connected(struct ctdb_node *node);
1006 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
1007 void ctdb_set_scheduler(struct ctdb_context *ctdb);
1008 void ctdb_restore_scheduler(struct ctdb_context *ctdb);
1009 pid_t ctdb_fork(struct ctdb_context *ctdb);
1010 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, 
1011                                  struct ctdb_req_control *c,
1012                                  TDB_DATA indata, 
1013                                  bool *async_reply);
1014 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb, 
1015                                  struct ctdb_req_control *c,
1016                                  TDB_DATA indata, 
1017                                  bool *async_reply);
1018 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, 
1019                                  struct ctdb_req_control *c,
1020                                  TDB_DATA indata, 
1021                                  bool *async_reply);
1022 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb, 
1023                                  struct ctdb_req_control *c,
1024                                  TDB_DATA indata, 
1025                                  bool *async_reply);
1026 int32_t ctdb_control_start_recovery(struct ctdb_context *ctdb, 
1027                                  struct ctdb_req_control *c,
1028                                  bool *async_reply);
1029 int32_t ctdb_control_end_recovery(struct ctdb_context *ctdb, 
1030                                  struct ctdb_req_control *c,
1031                                  bool *async_reply);
1032
1033 struct ctdb_public_ipv4 {
1034         uint32_t pnn;
1035         struct sockaddr_in sin;
1036 };
1037
1038 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1039                           uint32_t destnode, struct ctdb_public_ip *ip);
1040 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1041                          uint32_t destnode, struct ctdb_public_ip *ip);
1042
1043 struct ctdb_all_public_ipsv4 {
1044         uint32_t num;
1045         struct ctdb_public_ipv4 ips[1];
1046 };
1047
1048 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1049 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1050 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb, 
1051                              struct timeval timeout,
1052                              uint32_t destnode,
1053                              TALLOC_CTX *mem_ctx,
1054                              struct ctdb_all_public_ips **ips);
1055 #define CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE 0x00010000
1056 int ctdb_ctrl_get_public_ips_flags(struct ctdb_context *ctdb,
1057                                    struct timeval timeout, uint32_t destnode,
1058                                    TALLOC_CTX *mem_ctx,
1059                                    uint32_t flags,
1060                                    struct ctdb_all_public_ips **ips);
1061 int ctdb_ctrl_get_public_ipsv4(struct ctdb_context *ctdb, 
1062                         struct timeval timeout, uint32_t destnode, 
1063                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
1064
1065 #ifdef IFNAMSIZ
1066 #define CTDB_IFACE_SIZE IFNAMSIZ
1067 #else
1068 #define CTDB_IFACE_SIZE 16
1069 #endif
1070
1071 struct ctdb_control_iface_info {
1072         char name[CTDB_IFACE_SIZE+2];
1073         uint16_t link_state;
1074         uint32_t references;
1075 };
1076
1077 struct ctdb_control_public_ip_info {
1078         struct ctdb_public_ip ip;
1079         uint32_t active_idx;
1080         uint32_t num;
1081         struct ctdb_control_iface_info ifaces[1];
1082 };
1083
1084 struct ctdb_control_get_ifaces {
1085         uint32_t num;
1086         struct ctdb_control_iface_info ifaces[1];
1087 };
1088
1089 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
1090                                         struct ctdb_req_control *c,
1091                                         TDB_DATA indata,
1092                                         TDB_DATA *outdata);
1093 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
1094                                 struct ctdb_req_control *c,
1095                                 TDB_DATA *outdata);
1096 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
1097                                     struct ctdb_req_control *c,
1098                                     TDB_DATA indata);
1099 int ctdb_ctrl_get_public_ip_info(struct ctdb_context *ctdb,
1100                                  struct timeval timeout, uint32_t destnode,
1101                                  TALLOC_CTX *mem_ctx,
1102                                  const ctdb_sock_addr *addr,
1103                                  struct ctdb_control_public_ip_info **info);
1104 int ctdb_ctrl_get_ifaces(struct ctdb_context *ctdb,
1105                          struct timeval timeout, uint32_t destnode,
1106                          TALLOC_CTX *mem_ctx,
1107                          struct ctdb_control_get_ifaces **ifaces);
1108 int ctdb_ctrl_set_iface_link(struct ctdb_context *ctdb,
1109                              struct timeval timeout, uint32_t destnode,
1110                              TALLOC_CTX *mem_ctx,
1111                              const struct ctdb_control_iface_info *info);
1112
1113 /* from takeover/system.c */
1114 uint32_t uint16_checksum(uint16_t *data, size_t n);
1115 int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface);
1116 bool ctdb_sys_have_ip(ctdb_sock_addr *addr);
1117 int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, 
1118                       const ctdb_sock_addr *src,
1119                       uint32_t seq, uint32_t ack, int rst);
1120
1121 int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist);
1122 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1123                               const char *iface,
1124                               const char *ip);
1125 int ctdb_set_event_script(struct ctdb_context *ctdb, const char *script);
1126 int ctdb_set_event_script_dir(struct ctdb_context *ctdb, const char *script_dir);
1127 int ctdb_set_notification_script(struct ctdb_context *ctdb, const char *script);
1128 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap);
1129
1130 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id, 
1131                                 TDB_DATA indata);
1132 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed);
1133 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
1134 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
1135 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata);
1136 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata);
1137 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1138 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata);
1139
1140 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
1141 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call);
1142 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
1143                            const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
1144 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
1145                                TALLOC_CTX *mem_ctx,
1146                                void (*callback)(struct ctdb_context *, int, void *),
1147                                void *private_data,
1148                                bool from_user,
1149                                enum ctdb_eventscript_call call,
1150                                const char *fmt, ...) PRINTF_ATTRIBUTE(7,8);
1151 void ctdb_release_all_ips(struct ctdb_context *ctdb);
1152
1153 void set_nonblocking(int fd);
1154 void set_close_on_exec(int fd);
1155
1156 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep);
1157
1158 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
1159
1160 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
1161                                  TDB_DATA *outdata);
1162 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata);
1163 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata);
1164 int32_t ctdb_control_try_delete_records(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1165 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1166 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1167
1168 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb);
1169
1170 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata);
1171
1172 int ctdb_ctrl_get_all_tunables(struct ctdb_context *ctdb, 
1173                                struct timeval timeout, 
1174                                uint32_t destnode,
1175                                struct ctdb_tunable *tunables);
1176
1177 int ctdb_start_freeze(struct ctdb_context *ctdb, uint32_t priority);
1178
1179 bool parse_ip_mask(const char *s, const char *iface, ctdb_sock_addr *addr, unsigned *mask);
1180 bool parse_ip_port(const char *s, ctdb_sock_addr *addr);
1181 bool parse_ip(const char *s, const char *iface, unsigned port, ctdb_sock_addr *addr);
1182 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin);
1183  
1184
1185 int ctdb_sys_open_capture_socket(const char *iface, void **private_data);
1186 int ctdb_sys_close_capture_socket(void *private_data);
1187 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);
1188
1189 int ctdb_ctrl_killtcp(struct ctdb_context *ctdb, 
1190                       struct timeval timeout, 
1191                       uint32_t destnode,
1192                       struct ctdb_control_killtcp *killtcp);
1193
1194 int ctdb_ctrl_add_public_ip(struct ctdb_context *ctdb, 
1195                       struct timeval timeout, 
1196                       uint32_t destnode,
1197                       struct ctdb_control_ip_iface *pub);
1198
1199 int ctdb_ctrl_del_public_ip(struct ctdb_context *ctdb, 
1200                       struct timeval timeout, 
1201                       uint32_t destnode,
1202                       struct ctdb_control_ip_iface *pub);
1203
1204 int ctdb_ctrl_gratious_arp(struct ctdb_context *ctdb, 
1205                       struct timeval timeout, 
1206                       uint32_t destnode,
1207                       ctdb_sock_addr *addr,
1208                       const char *ifname);
1209
1210 int ctdb_ctrl_get_tcp_tickles(struct ctdb_context *ctdb, 
1211                       struct timeval timeout, 
1212                       uint32_t destnode,
1213                       TALLOC_CTX *mem_ctx,
1214                       ctdb_sock_addr *addr,
1215                       struct ctdb_control_tcp_tickle_list **list);
1216
1217
1218 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb, 
1219                       uint32_t client_id,
1220                       TDB_DATA indata);
1221 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb, 
1222                       TDB_DATA indata);
1223 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb, 
1224                       TDB_DATA indata);
1225 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb, 
1226                       TDB_DATA *outdata);
1227 int32_t ctdb_control_uptime(struct ctdb_context *ctdb, 
1228                       TDB_DATA *outdata);
1229
1230 int ctdb_attach_databases(struct ctdb_context *ctdb);
1231
1232 int32_t ctdb_control_persistent_store(struct ctdb_context *ctdb, 
1233                                       struct ctdb_req_control *c, 
1234                                       TDB_DATA recdata, bool *async_reply);
1235 int32_t ctdb_control_update_record(struct ctdb_context *ctdb, 
1236                                    struct ctdb_req_control *c, TDB_DATA recdata, 
1237                                    bool *async_reply);
1238 int32_t ctdb_control_trans2_commit(struct ctdb_context *ctdb, 
1239                                    struct ctdb_req_control *c, 
1240                                    TDB_DATA recdata, bool *async_reply);
1241
1242 int32_t ctdb_control_trans3_commit(struct ctdb_context *ctdb,
1243                                    struct ctdb_req_control *c,
1244                                    TDB_DATA recdata, bool *async_reply);
1245
1246 void ctdb_persistent_finish_trans3_commits(struct ctdb_context *ctdb);
1247
1248 int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id);
1249 int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id);
1250 int32_t ctdb_control_transaction_cancel(struct ctdb_context *ctdb);
1251 int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata);
1252 int32_t ctdb_control_db_set_healthy(struct ctdb_context *ctdb, TDB_DATA indata);
1253 int32_t ctdb_control_db_get_health(struct ctdb_context *ctdb,
1254                                    TDB_DATA indata,
1255                                    TDB_DATA *outdata);
1256
1257
1258 int ctdb_vacuum(struct ctdb_context *ctdb, int argc, const char **argv);
1259 int ctdb_repack(struct ctdb_context *ctdb, int argc, const char **argv);
1260
1261 void ctdb_block_signal(int signum);
1262 void ctdb_unblock_signal(int signum);
1263 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb);
1264 int ctdb_set_child_logging(struct ctdb_context *ctdb);
1265 void ctdb_lockdown_memory(struct ctdb_context *ctdb);
1266
1267 typedef void (*client_async_callback)(struct ctdb_context *ctdb, uint32_t node_pnn, int32_t res, TDB_DATA outdata, void *callback_data);
1268
1269 struct client_async_data {
1270         enum ctdb_controls opcode;
1271         bool dont_log_errors;
1272         uint32_t count;
1273         uint32_t fail_count;
1274         client_async_callback callback;
1275         client_async_callback fail_callback;
1276         void *callback_data;
1277 };
1278 void ctdb_client_async_add(struct client_async_data *data, struct ctdb_client_control_state *state);
1279 int ctdb_client_async_wait(struct ctdb_context *ctdb, struct client_async_data *data);
1280 int ctdb_client_async_control(struct ctdb_context *ctdb,
1281                                 enum ctdb_controls opcode,
1282                                 uint32_t *nodes,
1283                                 uint64_t srvid,
1284                                 struct timeval timeout,
1285                                 bool dont_log_errors,
1286                                 TDB_DATA data,
1287                                 client_async_callback client_callback,
1288                                 client_async_callback fail_callback,
1289                                 void *callback_data);
1290
1291 void ctdb_load_nodes_file(struct ctdb_context *ctdb);
1292
1293 int ctdb_control_reload_nodes_file(struct ctdb_context *ctdb, uint32_t opcode);
1294
1295 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata);
1296 int32_t ctdb_control_get_capabilities(struct ctdb_context *ctdb, TDB_DATA *outdata);
1297
1298 int32_t ctdb_control_trans2_finished(struct ctdb_context *ctdb, 
1299                                      struct ctdb_req_control *c);
1300 int32_t ctdb_control_trans2_error(struct ctdb_context *ctdb, 
1301                                   struct ctdb_req_control *c);
1302 int32_t ctdb_control_trans2_active(struct ctdb_context *ctdb,
1303                                    struct ctdb_req_control *c,
1304                                    uint32_t db_id);
1305
1306 char *ctdb_addr_to_str(ctdb_sock_addr *addr);
1307 unsigned ctdb_addr_to_port(ctdb_sock_addr *addr);
1308 void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip);
1309
1310 int32_t ctdb_control_recd_ping(struct ctdb_context *ctdb);
1311 int32_t ctdb_control_set_recmaster(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata);
1312
1313 extern int script_log_level;
1314 extern bool fast_start;
1315
1316 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
1317                                              uint32_t call_type,
1318                                              TDB_DATA *outdata);
1319
1320 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len);
1321 int ctdb_ctrl_report_recd_lock_latency(struct ctdb_context *ctdb, struct timeval timeout, double latency);
1322
1323 int32_t ctdb_control_stop_node(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1324 int32_t ctdb_control_continue_node(struct ctdb_context *ctdb);
1325
1326 void ctdb_stop_vacuuming(struct ctdb_context *ctdb);
1327 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db);
1328
1329 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1330 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1331
1332 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata);
1333 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata);
1334 int32_t ctdb_control_set_db_priority(struct ctdb_context *ctdb, TDB_DATA indata);
1335 void ctdb_ban_self(struct ctdb_context *ctdb);
1336
1337 int32_t ctdb_control_register_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata);
1338
1339 int32_t ctdb_control_deregister_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata);
1340
1341 int start_syslog_daemon(struct ctdb_context *ctdb);
1342
1343 /* Where to send the log messages back to */
1344 struct ctdb_get_log_addr {
1345         uint32_t pnn;
1346         uint64_t srvid;
1347         int32_t level;
1348 };
1349
1350 extern int log_ringbuf_size;
1351
1352 int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr);
1353 int32_t ctdb_control_clear_log(struct ctdb_context *ctdb);
1354
1355
1356 struct ctdb_log_state *ctdb_fork_with_logging(TALLOC_CTX *mem_ctx,
1357                                               struct ctdb_context *ctdb,
1358                                               void (*logfn)(const char *, uint16_t, void *),
1359                                               void *logfn_private, pid_t *pid);
1360
1361 int32_t ctdb_control_process_exists(struct ctdb_context *ctdb, pid_t pid);
1362 struct ctdb_client *ctdb_find_client_by_pid(struct ctdb_context *ctdb, pid_t pid);
1363
1364 int32_t ctdb_control_get_db_seqnum(struct ctdb_context *ctdb,
1365                                    TDB_DATA indata,
1366                                    TDB_DATA *outdata);
1367
1368 int ctdb_load_persistent_health(struct ctdb_context *ctdb,
1369                                 struct ctdb_db_context *ctdb_db);
1370 int ctdb_update_persistent_health(struct ctdb_context *ctdb,
1371                                   struct ctdb_db_context *ctdb_db,
1372                                   const char *reason,/* NULL means healthy */
1373                                   int num_healthy_nodes);
1374 int ctdb_recheck_persistent_health(struct ctdb_context *ctdb);
1375
1376 void ctdb_run_notification_script(struct ctdb_context *ctdb, const char *event);
1377
1378 void ctdb_fault_setup(void);
1379
1380 int verify_remote_ip_allocation(struct ctdb_context *ctdb, 
1381                                 struct ctdb_all_public_ips *ips);
1382 int update_ip_assignment_tree(struct ctdb_context *ctdb,
1383                                 struct ctdb_public_ip *ip);
1384
1385 int ctdb_init_tevent_logging(struct ctdb_context *ctdb);
1386
1387 int ctdb_statistics_init(struct ctdb_context *ctdb);
1388
1389 int32_t ctdb_control_get_stat_history(struct ctdb_context *ctdb,
1390                                       struct ctdb_req_control *c,
1391                                       TDB_DATA *outdata);
1392
1393 int ctdb_deferred_drop_all_ips(struct ctdb_context *ctdb);
1394
1395 int ctdb_process_deferred_attach(struct ctdb_context *ctdb);
1396
1397 /**
1398  * structure to pass to a schedule_for_deletion_control
1399  */
1400 struct ctdb_control_schedule_for_deletion {
1401         uint32_t db_id;
1402         struct ctdb_ltdb_header hdr;
1403         uint32_t keylen;
1404         uint8_t key[1]; /* key[] */
1405 };
1406
1407 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1408                                            TDB_DATA indata);
1409
1410
1411 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1412                                          const struct ctdb_ltdb_header *hdr,
1413                                          TDB_DATA key);
1414
1415 struct ctdb_ltdb_header *ctdb_header_from_record_handle(struct ctdb_record_handle *h);
1416
1417 /* For unit testing ctdb_transaction.c. */
1418 struct ctdb_public_ip_list {
1419         struct ctdb_public_ip_list *next;
1420         uint32_t pnn;
1421         ctdb_sock_addr addr;
1422 };
1423 uint32_t ip_distance(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2);
1424 uint32_t ip_distance_2_sum(ctdb_sock_addr *ip,
1425                            struct ctdb_public_ip_list *ips,
1426                            int pnn);
1427 uint32_t lcp2_imbalance(struct ctdb_public_ip_list * all_ips, int pnn);
1428 void lcp2_init(struct ctdb_context * tmp_ctx,
1429                struct ctdb_node_map * nodemap,
1430                uint32_t mask,
1431                struct ctdb_public_ip_list *all_ips,
1432                uint32_t **lcp2_imbalances,
1433                bool **newly_healthy);
1434 void lcp2_allocate_unassigned(struct ctdb_context *ctdb,
1435                               struct ctdb_node_map *nodemap,
1436                               uint32_t mask,
1437                               struct ctdb_public_ip_list *all_ips,
1438                               uint32_t *lcp2_imbalances);
1439 bool lcp2_failback(struct ctdb_context *ctdb,
1440                    struct ctdb_node_map *nodemap,
1441                    uint32_t mask,
1442                    struct ctdb_public_ip_list *all_ips,
1443                    uint32_t *lcp2_imbalances,
1444                    bool *newly_healthy);
1445 void lcp2_forcerebalance(struct ctdb_context *ctdb, uint32_t pnn);
1446 void ctdb_takeover_run_core(struct ctdb_context *ctdb,
1447                             struct ctdb_node_map *nodemap,
1448                             struct ctdb_public_ip_list **all_ips_p);
1449
1450
1451 #endif