ctdb-daemon: Add controls for transactions on a single database
[vlendec/samba-autobuild/.git] / ctdb / 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  * Structures to support SRVID requests and replies
28  */
29 struct srvid_request {
30         uint32_t pnn;
31         uint64_t srvid;
32 };
33
34 struct srvid_request_data {
35         uint32_t pnn;
36         uint64_t srvid;
37         uint32_t data;
38 };
39
40 /*
41   a tcp connection description
42   also used by tcp_add and tcp_remove controls
43  */
44 struct ctdb_tcp_connection {
45         ctdb_sock_addr src_addr;
46         ctdb_sock_addr dst_addr;
47 };
48
49 /* the wire representation for a tcp tickle array */
50 struct ctdb_tcp_wire_array {
51         uint32_t num;
52         struct ctdb_tcp_connection connections[1];
53 };      
54
55 /* the list of tcp tickles used by get/set tcp tickle list */
56 struct ctdb_control_tcp_tickle_list {
57         ctdb_sock_addr addr;
58         struct ctdb_tcp_wire_array tickles;
59 };
60
61 /*
62   array of tcp connections
63  */
64 struct ctdb_tcp_array {
65         uint32_t num;
66         struct ctdb_tcp_connection *connections;
67 };      
68
69
70 /* all tunable variables go in here */
71 struct ctdb_tunable {
72         uint32_t max_redirect_count;
73         uint32_t seqnum_interval; /* unit is ms */
74         uint32_t control_timeout;
75         uint32_t traverse_timeout;
76         uint32_t keepalive_interval;
77         uint32_t keepalive_limit;
78         uint32_t recover_timeout;
79         uint32_t recover_interval;
80         uint32_t election_timeout;
81         uint32_t takeover_timeout;
82         uint32_t monitor_interval;
83         uint32_t tickle_update_interval;
84         uint32_t script_timeout;
85         uint32_t script_timeout_count; /* allow dodgy scripts to hang this many times in a row before we mark the node unhealthy */
86         uint32_t script_unhealthy_on_timeout; /* obsolete */
87         uint32_t recovery_grace_period;
88         uint32_t recovery_ban_period;
89         uint32_t database_hash_size;
90         uint32_t database_max_dead;
91         uint32_t rerecovery_timeout;
92         uint32_t enable_bans;
93         uint32_t deterministic_public_ips;
94         uint32_t reclock_ping_period;
95         uint32_t no_ip_failback;
96         uint32_t disable_ip_failover;
97         uint32_t verbose_memory_names;
98         uint32_t recd_ping_timeout;
99         uint32_t recd_ping_failcount;
100         uint32_t log_latency_ms;
101         uint32_t reclock_latency_ms;
102         uint32_t recovery_drop_all_ips;
103         uint32_t verify_recovery_lock;
104         uint32_t vacuum_interval;
105         uint32_t vacuum_max_run_time;
106         uint32_t repack_limit;
107         uint32_t vacuum_limit;
108         uint32_t max_queue_depth_drop_msg;
109         uint32_t allow_unhealthy_db_read;
110         uint32_t stat_history_interval;
111         uint32_t deferred_attach_timeout;
112         uint32_t vacuum_fast_path_count;
113         uint32_t lcp2_public_ip_assignment;
114         uint32_t allow_client_db_attach;
115         uint32_t recover_pdb_by_seqnum;
116         uint32_t deferred_rebalance_on_node_add;
117         uint32_t fetch_collapse;
118         uint32_t hopcount_make_sticky;
119         uint32_t sticky_duration;
120         uint32_t sticky_pindown;
121         uint32_t no_ip_takeover;
122         uint32_t db_record_count_warn;
123         uint32_t db_record_size_warn;
124         uint32_t db_size_warn;
125         uint32_t pulldb_preallocation_size;
126         uint32_t no_ip_host_on_all_disabled;
127         uint32_t samba3_hack;
128         uint32_t mutex_enabled;
129         uint32_t lock_processes_per_db;
130 };
131
132 /*
133   an installed ctdb remote call
134 */
135 struct ctdb_registered_call {
136         struct ctdb_registered_call *next, *prev;
137         uint32_t id;
138         ctdb_fn_t fn;
139 };
140
141 /*
142   check that a pnn is valid
143  */
144 #define ctdb_validate_pnn(ctdb, pnn) (((uint32_t)(pnn)) < (ctdb)->num_nodes)
145
146
147 /* called from the queue code when a packet comes in. Called with data==NULL
148    on error */
149 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
150                                    void *private_data);
151
152 /* used for callbacks in ctdb_control requests */
153 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
154                                            int32_t status, TDB_DATA data, 
155                                            const char *errormsg,
156                                            void *private_data);
157 /*
158   structure describing a connected client in the daemon
159  */
160 struct ctdb_client {
161         struct ctdb_context *ctdb;
162         int fd;
163         struct ctdb_queue *queue;
164         uint32_t client_id;
165         pid_t pid;
166         struct ctdb_tcp_list *tcp_list;
167         uint32_t db_id;
168         uint32_t num_persistent_updates;
169         struct ctdb_client_notify_list *notify;
170 };
171
172 struct ctdb_iface;
173
174 /* state associated with a public ip address */
175 struct ctdb_vnn {
176         struct ctdb_vnn *prev, *next;
177
178         struct ctdb_iface *iface;
179         const char **ifaces;
180         ctdb_sock_addr public_address;
181         uint8_t public_netmask_bits;
182
183         /* the node number that is serving this public address, if any. 
184            If no node serves this ip it is set to -1 */
185         int32_t pnn;
186
187         /* List of clients to tickle for this public address */
188         struct ctdb_tcp_array *tcp_array;
189
190         /* whether we need to update the other nodes with changes to our list
191            of connected clients */
192         bool tcp_update_needed;
193
194         /* a context to hang sending gratious arp events off */
195         TALLOC_CTX *takeover_ctx;
196
197         struct ctdb_kill_tcp *killtcp;
198
199         /* Set to true any time an update to this VNN is in flight.
200            This helps to avoid races. */
201         bool update_in_flight;
202
203         /* If CTDB_CONTROL_DEL_PUBLIC_IP is received for this IP
204          * address then this flag is set.  It will be deleted in the
205          * release IP callback. */
206         bool delete_pending;
207 };
208
209 /*
210   state associated with one node
211 */
212 struct ctdb_node {
213         struct ctdb_context *ctdb;
214         ctdb_sock_addr address;
215         const char *name; /* for debug messages */
216         void *private_data; /* private to transport */
217         uint32_t pnn;
218         uint32_t flags;
219
220         /* used by the dead node monitoring */
221         uint32_t dead_count;
222         uint32_t rx_cnt;
223         uint32_t tx_cnt;
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_header {
270         struct ctdb_message_list_header *next, *prev;
271         struct ctdb_context *ctdb;
272         uint64_t srvid;
273         struct ctdb_message_list *m;
274 };
275 struct ctdb_message_list {
276         struct ctdb_message_list *next, *prev;
277         struct ctdb_message_list_header *h;
278         ctdb_msg_fn_t message_handler;
279         void *message_private;
280 };
281
282 /* additional data required for the daemon mode */
283 struct ctdb_daemon_data {
284         int sd;
285         char *name;
286         struct ctdb_queue *queue;
287 };
288
289
290 #define CTDB_UPDATE_STAT(ctdb, counter, value) \
291         {                                                                               \
292                 if (value > ctdb->statistics.counter) {                                 \
293                         ctdb->statistics.counter = c->hopcount;                         \
294                 }                                                                       \
295                 if (value > ctdb->statistics_current.counter) {                         \
296                         ctdb->statistics_current.counter = c->hopcount;                 \
297                 }                                                                       \
298         }
299
300 #define CTDB_INCREMENT_STAT(ctdb, counter) \
301         {                                                                               \
302                 ctdb->statistics.counter++;                                             \
303                 ctdb->statistics_current.counter++;                                     \
304         }
305
306 #define CTDB_DECREMENT_STAT(ctdb, counter) \
307         {                                                                               \
308                 if (ctdb->statistics.counter > 0)                                       \
309                         ctdb->statistics.counter--;                                     \
310                 if (ctdb->statistics_current.counter > 0)                               \
311                         ctdb->statistics_current.counter--;                             \
312         }
313
314 #define CTDB_INCREMENT_DB_STAT(ctdb_db, counter) \
315         {                                                                               \
316                 ctdb_db->statistics.counter++;                                          \
317         }
318
319 #define CTDB_DECREMENT_DB_STAT(ctdb_db, counter) \
320         {                                                                               \
321                 if (ctdb_db->statistics.counter > 0)                                    \
322                         ctdb_db->statistics.counter--;                                  \
323         }
324
325 #define CTDB_UPDATE_RECLOCK_LATENCY(ctdb, name, counter, value) \
326         {                                                                               \
327                 if (value > ctdb->statistics.counter.max)                               \
328                         ctdb->statistics.counter.max = value;                           \
329                 if (value > ctdb->statistics_current.counter.max)                       \
330                         ctdb->statistics_current.counter.max = value;                   \
331                                                                                         \
332                 if (ctdb->statistics.counter.num == 0 ||                                \
333                     value < ctdb->statistics.counter.min)                               \
334                         ctdb->statistics.counter.min = value;                           \
335                 if (ctdb->statistics_current.counter.num == 0 ||                        \
336                     value < ctdb->statistics_current.counter.min)                       \
337                         ctdb->statistics_current.counter.min = value;                   \
338                                                                                         \
339                 ctdb->statistics.counter.total += value;                                \
340                 ctdb->statistics_current.counter.total += value;                        \
341                                                                                         \
342                 ctdb->statistics.counter.num++;                                         \
343                 ctdb->statistics_current.counter.num++;                                 \
344                                                                                         \
345                 if (ctdb->tunable.reclock_latency_ms != 0) {                            \
346                         if (value*1000 > ctdb->tunable.reclock_latency_ms) {            \
347                                 DEBUG(DEBUG_ERR,                                        \
348                                       ("High RECLOCK latency %fs for operation %s\n",   \
349                                        value, name));                                   \
350                         }                                                               \
351                 }                                                                       \
352         }
353
354 #define CTDB_UPDATE_DB_LATENCY(ctdb_db, operation, counter, value)                      \
355         {                                                                               \
356                 if (value > ctdb_db->statistics.counter.max)                            \
357                         ctdb_db->statistics.counter.max = value;                        \
358                 if (ctdb_db->statistics.counter.num == 0 ||                             \
359                     value < ctdb_db->statistics.counter.min)                            \
360                         ctdb_db->statistics.counter.min = value;                        \
361                                                                                         \
362                 ctdb_db->statistics.counter.total += value;                             \
363                 ctdb_db->statistics.counter.num++;                                      \
364                                                                                         \
365                 if (ctdb_db->ctdb->tunable.log_latency_ms != 0) {                       \
366                         if (value*1000 > ctdb_db->ctdb->tunable.log_latency_ms) {       \
367                                 DEBUG(DEBUG_ERR,                                        \
368                                       ("High latency %.6fs for operation %s on database %s\n",\
369                                        value, operation, ctdb_db->db_name));            \
370                         }                                                               \
371                 }                                                                       \
372         }
373
374 #define CTDB_UPDATE_LATENCY(ctdb, db, operation, counter, t) \
375         {                                                                               \
376                 double l = timeval_elapsed(&t);                                         \
377                                                                                         \
378                 if (l > ctdb->statistics.counter.max)                                   \
379                         ctdb->statistics.counter.max = l;                               \
380                 if (l > ctdb->statistics_current.counter.max)                           \
381                         ctdb->statistics_current.counter.max = l;                       \
382                                                                                         \
383                 if (ctdb->statistics.counter.num == 0 ||                                \
384                     l < ctdb->statistics.counter.min)                                   \
385                         ctdb->statistics.counter.min = l;                               \
386                 if (ctdb->statistics_current.counter.num == 0 ||                        \
387                     l < ctdb->statistics_current.counter.min)                           \
388                         ctdb->statistics_current.counter.min = l;                       \
389                                                                                         \
390                 ctdb->statistics.counter.total += l;                                    \
391                 ctdb->statistics_current.counter.total += l;                            \
392                                                                                         \
393                 ctdb->statistics.counter.num++;                                         \
394                 ctdb->statistics_current.counter.num++;                                 \
395                                                                                         \
396                 if (ctdb->tunable.log_latency_ms != 0) {                                \
397                         if (l*1000 > ctdb->tunable.log_latency_ms) {                    \
398                                 DEBUG(DEBUG_WARNING,                                    \
399                                       ("High latency %.6fs for operation %s on database %s\n",\
400                                        l, operation, db->db_name));                     \
401                         }                                                               \
402                 }                                                                       \
403         }
404
405
406
407 /* a structure that contains the elements required for the write record
408    control
409 */
410 struct ctdb_write_record {
411         uint32_t dbid;
412         uint32_t keylen;
413         uint32_t datalen;
414         unsigned char blob[1];
415 };
416
417 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
418
419 enum ctdb_runstate {
420         CTDB_RUNSTATE_UNKNOWN,
421         CTDB_RUNSTATE_INIT,
422         CTDB_RUNSTATE_SETUP,
423         CTDB_RUNSTATE_FIRST_RECOVERY,
424         CTDB_RUNSTATE_STARTUP,
425         CTDB_RUNSTATE_RUNNING,
426         CTDB_RUNSTATE_SHUTDOWN,
427 };
428
429 const char *runstate_to_string(enum ctdb_runstate runstate);
430 enum ctdb_runstate runstate_from_string(const char *label);
431 void ctdb_set_runstate(struct ctdb_context *ctdb, enum ctdb_runstate runstate);
432
433 void ctdb_shutdown_sequence(struct ctdb_context *ctdb, int exit_code);
434
435 #define CTDB_MONITORING_ACTIVE          0
436 #define CTDB_MONITORING_DISABLED        1
437
438 #define NUM_DB_PRIORITIES 3
439 /* main state of the ctdb daemon */
440 struct ctdb_context {
441         struct tevent_context *ev;
442         struct timeval ctdbd_start_time;
443         struct timeval last_recovery_started;
444         struct timeval last_recovery_finished;
445         uint32_t recovery_mode;
446         TALLOC_CTX *tickle_update_context;
447         TALLOC_CTX *keepalive_ctx;
448         TALLOC_CTX *check_public_ifaces_ctx;
449         struct ctdb_tunable tunable;
450         enum ctdb_freeze_mode freeze_mode[NUM_DB_PRIORITIES+1];
451         struct ctdb_freeze_handle *freeze_handles[NUM_DB_PRIORITIES+1];
452         bool freeze_transaction_started;
453         uint32_t freeze_transaction_id;
454         ctdb_sock_addr *address;
455         const char *name;
456         const char *db_directory;
457         const char *db_directory_persistent;
458         const char *db_directory_state;
459         struct tdb_wrap *db_persistent_health;
460         uint32_t db_persistent_startup_generation;
461         uint64_t db_persistent_check_errors;
462         uint64_t max_persistent_check_errors;
463         const char *transport;
464         char *recovery_lock_file;
465         int recovery_lock_fd;
466         uint32_t pnn; /* our own pnn */
467         uint32_t num_nodes;
468         uint32_t num_connected;
469         unsigned flags;
470         uint32_t capabilities;
471         struct idr_context *idr;
472         int lastid;
473         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
474         struct ctdb_vnn *vnn; /* list of public ip addresses and interfaces */
475         struct ctdb_vnn *single_ip_vnn; /* a structure for the single ip */
476         struct ctdb_iface *ifaces; /* list of local interfaces */
477         char *err_msg;
478         const struct ctdb_methods *methods; /* transport methods */
479         const struct ctdb_upcalls *upcalls; /* transport upcalls */
480         void *private_data; /* private to transport */
481         struct ctdb_db_context *db_list;
482         struct ctdb_message_list_header *message_list_header;
483         struct tdb_context *message_list_indexdb;
484         struct ctdb_daemon_data daemon;
485         struct ctdb_statistics statistics;
486         struct ctdb_statistics statistics_current;
487 #define MAX_STAT_HISTORY 100
488         struct ctdb_statistics statistics_history[MAX_STAT_HISTORY];
489         struct ctdb_vnn_map *vnn_map;
490         uint32_t num_clients;
491         uint32_t recovery_master;
492         struct ctdb_client_ip *client_ip_list;
493         bool do_checkpublicip;
494         struct trbt_tree *server_ids; 
495         bool do_setsched;
496         const char *event_script_dir;
497         const char *notification_script;
498         const char *default_public_interface;
499         pid_t ctdbd_pid;
500         pid_t recoverd_pid;
501         enum ctdb_runstate runstate;
502         struct ctdb_monitor_state *monitor;
503         int start_as_disabled;
504         int start_as_stopped;
505         bool valgrinding;
506         uint32_t event_script_timeouts; /* counting how many consecutive times an eventscript has timedout */
507         uint32_t *recd_ping_count;
508         TALLOC_CTX *recd_ctx; /* a context used to track recoverd monitoring events */
509         TALLOC_CTX *release_ips_ctx; /* a context used to automatically drop all IPs if we fail to recover the node */
510
511         TALLOC_CTX *event_script_ctx;
512         int active_events;
513
514         struct ctdb_event_script_state *current_monitor;
515         struct ctdb_scripts_wire *last_status[CTDB_EVENT_MAX];
516
517         TALLOC_CTX *banning_ctx;
518
519         struct ctdb_vacuum_child_context *vacuumers;
520
521         /* mapping from pid to ctdb_client * */
522         struct ctdb_client_pid_list *client_pids;
523
524         /* used in the recovery daemon to remember the ip allocation */
525         struct trbt_tree *ip_tree;
526
527         /* Used to defer db attach requests while in recovery mode */
528         struct ctdb_deferred_attach_context *deferred_attach;
529
530         /* if we are a child process, do we have a domain socket to send controls on */
531         bool can_send_controls;
532
533         /* list of event script callback functions that are active */
534         struct event_script_callback *script_callbacks;
535
536         struct ctdb_reloadips_handle *reload_ips;
537
538         const char *nodes_file;
539         const char *public_addresses_file;
540         struct trbt_tree *child_processes; 
541
542         /* Used for locking record/db/alldb */
543         struct lock_context *lock_current;
544         struct lock_context *lock_pending;
545 };
546
547 struct ctdb_db_context {
548         struct ctdb_db_context *next, *prev;
549         struct ctdb_context *ctdb;
550         uint32_t db_id;
551         uint32_t priority;
552         bool persistent;
553         bool readonly; /* Do we support read-only delegations ? */
554         bool sticky; /* Do we support sticky records ? */
555         const char *db_name;
556         const char *db_path;
557         struct tdb_wrap *ltdb;
558         struct tdb_context *rottdb; /* ReadOnly tracking TDB */
559         struct ctdb_registered_call *calls; /* list of registered calls */
560         uint32_t seqnum;
561         struct timed_event *seqnum_update;
562         struct ctdb_traverse_local_handle *traverse;
563         struct ctdb_vacuum_handle *vacuum_handle;
564         char *unhealthy_reason;
565         int pending_requests;
566         struct revokechild_handle *revokechild_active;
567         struct ctdb_persistent_state *persistent_state;
568         struct trbt_tree *delete_queue;
569         struct trbt_tree *sticky_records; 
570         int (*ctdb_ltdb_store_fn)(struct ctdb_db_context *ctdb_db,
571                                   TDB_DATA key,
572                                   struct ctdb_ltdb_header *header,
573                                   TDB_DATA data);
574
575         /* used to track which records we are currently fetching
576            so we can avoid sending duplicate fetch requests
577         */
578         struct trbt_tree *deferred_fetch;
579         struct trbt_tree *defer_dmaster;
580
581         struct ctdb_db_statistics statistics;
582
583         struct lock_context *lock_current;
584         struct lock_context *lock_pending;
585         int lock_num_current;
586
587         struct ctdb_call_state *pending_calls;
588
589         enum ctdb_freeze_mode freeze_mode;
590         struct ctdb_db_freeze_handle *freeze_handle;
591         bool freeze_transaction_started;
592         uint32_t freeze_transaction_id;
593 };
594
595
596 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
597           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
598           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
599           return -1; }} while (0)
600
601 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
602           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
603           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
604           return; }} while (0)
605
606 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
607           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
608           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
609           return NULL; }} while (0)
610
611 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
612           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
613           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
614           }} while (0)
615
616 /*
617   struct holding a ctdb_sock_addr and an interface name,
618   used to add/remove public addresses
619  */
620 struct ctdb_control_ip_iface {
621         ctdb_sock_addr addr;
622         uint32_t mask;
623         uint32_t len;
624         char iface[1];
625 };
626
627 /*
628   struct holding a ctdb_sock_addr and an interface name,
629   used for send_gratious_arp
630  */
631 struct ctdb_control_gratious_arp {
632         ctdb_sock_addr addr;
633         uint32_t mask;
634         uint32_t len;
635         char iface[1];
636 };
637
638 /*
639   persistent store control - update this record on all other nodes
640  */
641 struct ctdb_control_persistent_store {
642         uint32_t db_id;
643         uint32_t len;
644         uint8_t  data[1];
645 };
646
647 /*
648   structure used for CTDB_SRVID_NODE_FLAGS_CHANGED
649  */
650 struct ctdb_node_flag_change {
651         uint32_t pnn;
652         uint32_t new_flags;
653         uint32_t old_flags;
654 };
655
656 /*
657   struct for admin setting a ban
658  */
659 struct ctdb_ban_info {
660         uint32_t pnn;
661         uint32_t ban_time;
662 };
663
664 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
665
666 #define CTDB_LMASTER_ANY        0xffffffff
667
668 /*
669   state of a in-progress ctdb call
670 */
671 struct ctdb_call_state {
672         struct ctdb_call_state *next, *prev;
673         enum call_state state;
674         uint32_t reqid;
675         struct ctdb_req_call *c;
676         struct ctdb_db_context *ctdb_db;
677         const char *errmsg;
678         struct ctdb_call *call;
679         uint32_t generation;
680         struct {
681                 void (*fn)(struct ctdb_call_state *);
682                 void *private_data;
683         } async;
684 };
685
686
687 /* used for fetch_lock */
688 struct ctdb_fetch_handle {
689         struct ctdb_db_context *ctdb_db;
690         TDB_DATA key;
691         TDB_DATA *data;
692         struct ctdb_ltdb_header header;
693 };
694
695 /* internal prototypes */
696 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
697 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
698 void ctdb_die(struct ctdb_context *ctdb, const char *msg);
699 bool ctdb_set_helper(const char *type, char *helper, size_t size,
700                      const char *envvar, const char *dir, const char *file);
701 void ctdb_external_trace(void);
702 bool ctdb_same_address(ctdb_sock_addr *a1, ctdb_sock_addr *a2);
703 int ctdb_parse_address(TALLOC_CTX *mem_ctx, const char *str,
704                        ctdb_sock_addr *address);
705 bool ctdb_same_ip(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
706 bool ctdb_same_sockaddr(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
707 uint32_t ctdb_hash(const TDB_DATA *key);
708 uint32_t *ctdb_key_to_idkey(TALLOC_CTX *mem_ctx, TDB_DATA key);
709
710 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
711 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
712 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
713 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
714 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
715 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
716
717 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
718 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
719                     TDB_DATA key, struct ctdb_ltdb_header *header, 
720                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
721 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
722                     struct ctdb_ltdb_header *header, TDB_DATA data);
723 int ctdb_ltdb_delete(struct ctdb_db_context *ctdb_db, TDB_DATA key);
724 int ctdb_ltdb_fetch_with_header(struct ctdb_db_context *ctdb_db, 
725                     TDB_DATA key, struct ctdb_ltdb_header *header, 
726                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
727 int32_t ctdb_control_start_persistent_update(struct ctdb_context *ctdb, 
728                         struct ctdb_req_control *c,
729                         TDB_DATA recdata);
730 int32_t ctdb_control_cancel_persistent_update(struct ctdb_context *ctdb, 
731                         struct ctdb_req_control *c,
732                         TDB_DATA recdata);
733 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
734 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, unsigned opcode);
735 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
736                            TDB_DATA key, struct ctdb_req_header *hdr,
737                            void (*recv_pkt)(void *, struct ctdb_req_header *),
738                            void *recv_context, bool ignore_generation);
739 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
740                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
741                                  struct ctdb_req_header *hdr, TDB_DATA *data,
742                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
743                                  void *recv_context, bool ignore_generation);
744 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
745
746 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
747                                              struct ctdb_call *call,
748                                              struct ctdb_ltdb_header *header,
749                                              TDB_DATA *data);
750
751
752 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork);
753
754 /*
755   queue a packet for sending
756 */
757 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
758
759 /*
760   setup the fd used by the queue
761  */
762 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
763
764 /*
765   setup a packet queue on a socket
766  */
767 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
768                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
769                                     
770                                     ctdb_queue_cb_fn_t callback,
771                                     void *private_data, const char *fmt, ...)
772         PRINTF_ATTRIBUTE(7,8);
773
774 /*
775   allocate a packet for use in client<->daemon communication
776  */
777 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
778                                             TALLOC_CTX *mem_ctx, 
779                                             enum ctdb_operation operation, 
780                                             size_t length, size_t slength,
781                                             const char *type);
782 #define ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, type) \
783         (type *)_ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, sizeof(type), #type)
784
785 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
786                                                  TALLOC_CTX *mem_ctx, 
787                                                  enum ctdb_operation operation, 
788                                                  size_t length, size_t slength,
789                                                  const char *type);
790 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
791         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, sizeof(type), #type)
792
793 int ctdb_queue_length(struct ctdb_queue *queue);
794
795 /*
796   lock a record in the ltdb, given a key
797  */
798 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
799
800 /*
801   unlock a record in the ltdb, given a key
802  */
803 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
804
805
806 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
807                              uint64_t srvid, TDB_DATA data);
808
809 /*
810   send a ctdb message
811 */
812 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
813                              uint64_t srvid, TDB_DATA data);
814
815
816 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
817
818 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
819                                                      struct ctdb_call *call, 
820                                                      struct ctdb_ltdb_header *header);
821
822 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
823                     struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx,
824                     TDB_DATA *data, bool updatetdb);
825
826 #define ctdb_reqid_find(ctdb, reqid, type)      (type *)_ctdb_reqid_find(ctdb, reqid, #type, __location__)
827
828 int ctdb_socket_connect(struct ctdb_context *ctdb);
829 void ctdb_client_read_cb(uint8_t *data, size_t cnt, void *args);
830
831 #define CTDB_BAD_REQID ((uint32_t)-1)
832 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state);
833 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location);
834 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid);
835
836 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
837 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
838
839 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
840                              uint64_t srvid, uint32_t opcode, uint32_t client_id, uint32_t flags,
841                              TDB_DATA data,
842                              ctdb_control_callback_fn_t callback,
843                              void *private_data);
844
845 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata, 
846                                TDB_DATA *outdata, uint64_t tdb_flags,
847                                bool persistent, uint32_t client_id,
848                                struct ctdb_req_control *c,
849                                bool *async_reply);
850 int32_t ctdb_control_db_detach(struct ctdb_context *ctdb, TDB_DATA indata,
851                                uint32_t client_id);
852
853 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
854                          ctdb_fn_t fn, int id);
855
856 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid, 
857                  uint32_t opcode, uint32_t flags, TDB_DATA data, 
858                  TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
859                  struct timeval *timeout, char **errormsg);
860 int ctdb_control_recv(struct ctdb_context *ctdb, 
861                 struct ctdb_client_control_state *state, 
862                 TALLOC_CTX *mem_ctx,
863                 TDB_DATA *outdata, int32_t *status, char **errormsg);
864
865 struct ctdb_client_control_state *
866 ctdb_control_send(struct ctdb_context *ctdb, 
867                 uint32_t destnode, uint64_t srvid, 
868                 uint32_t opcode, uint32_t flags, TDB_DATA data, 
869                 TALLOC_CTX *mem_ctx,
870                 struct timeval *timeout,
871                 char **errormsg);
872
873
874
875
876 #define CHECK_CONTROL_DATA_SIZE(size) do { \
877  if (indata.dsize != size) { \
878          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
879                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
880          return -1; \
881  } \
882  } while (0)
883
884 #define CHECK_CONTROL_MIN_DATA_SIZE(size) do { \
885  if (indata.dsize < size) { \
886          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected >= %u\n", \
887                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
888          return -1; \
889  } \
890  } while (0)
891
892 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
893 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
894 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
895 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
896 int ctdb_control_getnodesfile(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
897
898
899 /* structure used for pulldb control */
900 struct ctdb_control_pulldb {
901         uint32_t db_id;
902         uint32_t lmaster;
903 };
904
905 /* structure used for sending lists of records */
906 struct ctdb_marshall_buffer {
907         uint32_t db_id;
908         uint32_t count;
909         uint8_t data[1];
910 };
911
912 /*
913   structure for setting a tunable
914  */
915 struct ctdb_control_set_tunable {
916         uint32_t value;
917         uint32_t length;
918         uint8_t  name[1];
919 };
920
921 /*
922   structure for getting a tunable
923  */
924 struct ctdb_control_get_tunable {
925         uint32_t length;
926         uint8_t  name[1];
927 };
928
929 /*
930   structure for listing tunables
931  */
932 struct ctdb_control_list_tunable {
933         uint32_t length;
934         /* returns a : separated list of tunable names */
935         uint8_t  data[1];
936 };
937
938
939 struct ctdb_control_transdb {
940         uint32_t db_id;
941         uint32_t transaction_id;
942 };
943
944 /*
945   state of a in-progress ctdb call in client
946 */
947 struct ctdb_client_call_state {
948         enum call_state state;
949         uint32_t reqid;
950         struct ctdb_db_context *ctdb_db;
951         struct ctdb_call *call;
952         struct {
953                 void (*fn)(struct ctdb_client_call_state *);
954                 void *private_data;
955         } async;
956 };
957
958
959 int32_t ctdb_control_traverse_start_ext(struct ctdb_context *ctdb,
960                                         TDB_DATA indata,
961                                         TDB_DATA *outdata,
962                                         uint32_t srcnode,
963                                         uint32_t client_id);
964 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA indata, 
965                                     TDB_DATA *outdata, uint32_t srcnode, uint32_t client_id);
966 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
967 int32_t ctdb_control_traverse_all_ext(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
968 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
969 int32_t ctdb_control_traverse_kill(struct ctdb_context *ctdb, TDB_DATA indata, 
970                                     TDB_DATA *outdata, uint32_t srcnode);
971
972 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data);
973 bool ctdb_check_message_handler(struct ctdb_context *ctdb, uint64_t srvid);
974
975 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
976 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
977 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
978 int daemon_check_srvids(struct ctdb_context *ctdb, TDB_DATA indata,
979                         TDB_DATA *outdata);
980
981 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
982 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint32_t srcnode);
983
984 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, 
985                                            TDB_DATA key, struct ctdb_ltdb_header *, TDB_DATA data);
986
987 struct ctdb_rec_data *ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
988                                               uint32_t *reqid,
989                                               struct ctdb_ltdb_header *header,
990                                               TDB_DATA *key, TDB_DATA *data);
991
992 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
993 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
994
995 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
996                                  struct ctdb_req_control *c,
997                                  TDB_DATA indata, bool *async_reply,
998                                  const char **errormsg);
999 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
1000                                 TDB_DATA *outdata, int32_t status, const char *errormsg);
1001
1002 int32_t ctdb_control_db_freeze(struct ctdb_context *ctdb,
1003                                struct ctdb_req_control *c,
1004                                uint32_t db_id, bool *async_reply);
1005 int32_t ctdb_control_db_thaw(struct ctdb_context *ctdb, uint32_t db_id);
1006
1007 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1008 int32_t ctdb_control_thaw(struct ctdb_context *ctdb, uint32_t priority,
1009                           bool check_recmode);
1010 bool ctdb_db_frozen(struct ctdb_db_context *ctdb_db);
1011 bool ctdb_db_prio_frozen(struct ctdb_context *ctdb, uint32_t priority);
1012 bool ctdb_db_all_frozen(struct ctdb_context *ctdb);
1013
1014 int ctdb_start_recoverd(struct ctdb_context *ctdb);
1015 void ctdb_stop_recoverd(struct ctdb_context *ctdb);
1016
1017 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb);
1018
1019 void ctdb_disable_monitoring(struct ctdb_context *ctdb);
1020 void ctdb_enable_monitoring(struct ctdb_context *ctdb);
1021 void ctdb_stop_monitoring(struct ctdb_context *ctdb);
1022 void ctdb_wait_for_first_recovery(struct ctdb_context *ctdb);
1023 int ctdb_tcp_init(struct ctdb_context *ctdb);
1024 int ctdb_ibw_init(struct ctdb_context *ctdb);
1025 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb);
1026 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
1027 void ctdb_start_keepalive(struct ctdb_context *ctdb);
1028 void ctdb_stop_keepalive(struct ctdb_context *ctdb);
1029 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA data, bool *async_reply);
1030
1031
1032 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node);
1033 void ctdb_call_resend_db(struct ctdb_db_context *ctdb);
1034 void ctdb_call_resend_all(struct ctdb_context *ctdb);
1035 void ctdb_node_dead(struct ctdb_node *node);
1036 void ctdb_node_connected(struct ctdb_node *node);
1037 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
1038 bool set_scheduler(void);
1039 void reset_scheduler(void);
1040
1041 struct tevent_signal *ctdb_init_sigchld(struct ctdb_context *ctdb);
1042 void ctdb_track_child(struct ctdb_context *ctdb, pid_t pid);
1043 pid_t ctdb_fork(struct ctdb_context *ctdb);
1044 void ctdb_set_child_info(TALLOC_CTX *mem_ctx, const char *child_name_fmt, ...);
1045 int ctdb_kill(struct ctdb_context *ctdb, pid_t pid, int signum);
1046
1047 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, 
1048                                  struct ctdb_req_control *c,
1049                                  TDB_DATA indata, 
1050                                  bool *async_reply);
1051 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, 
1052                                  struct ctdb_req_control *c,
1053                                  TDB_DATA indata, 
1054                                  bool *async_reply);
1055 int32_t ctdb_control_ipreallocated(struct ctdb_context *ctdb, 
1056                                  struct ctdb_req_control *c,
1057                                  bool *async_reply);
1058 int32_t ctdb_control_start_recovery(struct ctdb_context *ctdb, 
1059                                  struct ctdb_req_control *c,
1060                                  bool *async_reply);
1061 int32_t ctdb_control_end_recovery(struct ctdb_context *ctdb, 
1062                                  struct ctdb_req_control *c,
1063                                  bool *async_reply);
1064
1065 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1066                           uint32_t destnode, struct ctdb_public_ip *ip);
1067 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1068                          uint32_t destnode, struct ctdb_public_ip *ip);
1069
1070 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
1071                                     struct ctdb_req_control *c,
1072                                     TDB_DATA *outdata);
1073 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb,
1074                              struct timeval timeout,
1075                              uint32_t destnode,
1076                              TALLOC_CTX *mem_ctx,
1077                              struct ctdb_all_public_ips **ips);
1078 #define CTDB_PUBLIC_IP_FLAGS_ONLY_AVAILABLE 0x00010000
1079 int ctdb_ctrl_get_public_ips_flags(struct ctdb_context *ctdb,
1080                                    struct timeval timeout, uint32_t destnode,
1081                                    TALLOC_CTX *mem_ctx,
1082                                    uint32_t flags,
1083                                    struct ctdb_all_public_ips **ips);
1084
1085 struct ctdb_control_iface_info {
1086         char name[CTDB_IFACE_SIZE+2];
1087         uint16_t link_state;
1088         uint32_t references;
1089 };
1090
1091 struct ctdb_control_public_ip_info {
1092         struct ctdb_public_ip ip;
1093         uint32_t active_idx;
1094         uint32_t num;
1095         struct ctdb_control_iface_info ifaces[1];
1096 };
1097
1098 struct ctdb_control_get_ifaces {
1099         uint32_t num;
1100         struct ctdb_control_iface_info ifaces[1];
1101 };
1102
1103 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
1104                                         struct ctdb_req_control *c,
1105                                         TDB_DATA indata,
1106                                         TDB_DATA *outdata);
1107 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
1108                                 struct ctdb_req_control *c,
1109                                 TDB_DATA *outdata);
1110 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
1111                                     struct ctdb_req_control *c,
1112                                     TDB_DATA indata);
1113 int ctdb_ctrl_get_public_ip_info(struct ctdb_context *ctdb,
1114                                  struct timeval timeout, uint32_t destnode,
1115                                  TALLOC_CTX *mem_ctx,
1116                                  const ctdb_sock_addr *addr,
1117                                  struct ctdb_control_public_ip_info **info);
1118 int ctdb_ctrl_get_ifaces(struct ctdb_context *ctdb,
1119                          struct timeval timeout, uint32_t destnode,
1120                          TALLOC_CTX *mem_ctx,
1121                          struct ctdb_control_get_ifaces **ifaces);
1122 int ctdb_ctrl_set_iface_link(struct ctdb_context *ctdb,
1123                              struct timeval timeout, uint32_t destnode,
1124                              TALLOC_CTX *mem_ctx,
1125                              const struct ctdb_control_iface_info *info);
1126
1127 /* from takeover/system.c */
1128 uint32_t uint16_checksum(uint16_t *data, size_t n);
1129 int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface);
1130 bool ctdb_sys_have_ip(ctdb_sock_addr *addr);
1131 char *ctdb_sys_find_ifname(ctdb_sock_addr *addr);
1132 bool ctdb_sys_check_iface_exists(const char *iface);
1133 int ctdb_get_peer_pid(const int fd, pid_t *peer_pid);
1134 int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, 
1135                       const ctdb_sock_addr *src,
1136                       uint32_t seq, uint32_t ack, int rst);
1137
1138 /* Details of a byte range lock */
1139 struct ctdb_lock_info {
1140         ino_t inode;
1141         off_t start, end;
1142         bool waiting;
1143         bool read_only;
1144 };
1145
1146 int ctdb_set_process_name(const char *name);
1147
1148 typedef void (*client_async_callback)(struct ctdb_context *ctdb, uint32_t node_pnn, int32_t res, TDB_DATA outdata, void *callback_data);
1149
1150 int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses);
1151 int ctdb_set_single_public_ip(struct ctdb_context *ctdb,
1152                               const char *iface,
1153                               const char *ip);
1154 int ctdb_set_notification_script(struct ctdb_context *ctdb, const char *script);
1155 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap,
1156                       uint32_t *force_rebalance_nodes,
1157                       client_async_callback fail_callback, void *callback_data);
1158
1159 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id, 
1160                                 TDB_DATA indata);
1161 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata, bool tcp_update_needed);
1162 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
1163 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
1164 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata);
1165 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata);
1166 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1167 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata);
1168
1169 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
1170 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call);
1171 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
1172                            const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
1173 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
1174                                TALLOC_CTX *mem_ctx,
1175                                void (*callback)(struct ctdb_context *, int, void *),
1176                                void *private_data,
1177                                enum ctdb_eventscript_call call,
1178                                const char *fmt, ...) PRINTF_ATTRIBUTE(6,7);
1179 void ctdb_release_all_ips(struct ctdb_context *ctdb);
1180
1181 void set_nonblocking(int fd);
1182 void set_close_on_exec(int fd);
1183
1184 bool ctdb_recovery_have_lock(struct ctdb_context *ctdb);
1185 bool ctdb_recovery_lock(struct ctdb_context *ctdb);
1186 void ctdb_recovery_unlock(struct ctdb_context *ctdb);
1187
1188 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
1189
1190 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
1191                                  TDB_DATA *outdata);
1192 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata);
1193 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata);
1194 int32_t ctdb_control_try_delete_records(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1195 int32_t ctdb_control_receive_records(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1196 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1197 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb,
1198                                         struct ctdb_req_control *c,
1199                                         TDB_DATA recdata, bool *async_reply);
1200
1201 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb);
1202
1203 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata);
1204
1205 int ctdb_ctrl_get_all_tunables(struct ctdb_context *ctdb, 
1206                                struct timeval timeout, 
1207                                uint32_t destnode,
1208                                struct ctdb_tunable *tunables);
1209
1210 void ctdb_start_freeze(struct ctdb_context *ctdb, uint32_t priority);
1211
1212 bool parse_ip_mask(const char *s, const char *iface, ctdb_sock_addr *addr, unsigned *mask);
1213 bool parse_ip_port(const char *s, ctdb_sock_addr *addr);
1214 bool parse_ip(const char *s, const char *iface, unsigned port, ctdb_sock_addr *addr);
1215 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin);
1216  
1217
1218 int ctdb_sys_open_capture_socket(const char *iface, void **private_data);
1219 int ctdb_sys_close_capture_socket(void *private_data);
1220 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);
1221
1222 int ctdb_ctrl_killtcp(struct ctdb_context *ctdb, 
1223                       struct timeval timeout, 
1224                       uint32_t destnode,
1225                       struct ctdb_tcp_connection *killtcp);
1226
1227 int ctdb_ctrl_add_public_ip(struct ctdb_context *ctdb, 
1228                       struct timeval timeout, 
1229                       uint32_t destnode,
1230                       struct ctdb_control_ip_iface *pub);
1231
1232 int ctdb_ctrl_del_public_ip(struct ctdb_context *ctdb, 
1233                       struct timeval timeout, 
1234                       uint32_t destnode,
1235                       struct ctdb_control_ip_iface *pub);
1236
1237 int ctdb_ctrl_gratious_arp(struct ctdb_context *ctdb, 
1238                       struct timeval timeout, 
1239                       uint32_t destnode,
1240                       ctdb_sock_addr *addr,
1241                       const char *ifname);
1242
1243 int ctdb_ctrl_get_tcp_tickles(struct ctdb_context *ctdb, 
1244                       struct timeval timeout, 
1245                       uint32_t destnode,
1246                       TALLOC_CTX *mem_ctx,
1247                       ctdb_sock_addr *addr,
1248                       struct ctdb_control_tcp_tickle_list **list);
1249
1250
1251 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb, 
1252                       uint32_t client_id,
1253                       TDB_DATA indata);
1254 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb, 
1255                       TDB_DATA indata);
1256 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb, 
1257                       TDB_DATA indata);
1258 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb, 
1259                       TDB_DATA *outdata);
1260 int32_t ctdb_control_uptime(struct ctdb_context *ctdb, 
1261                       TDB_DATA *outdata);
1262
1263 int ctdb_attach_databases(struct ctdb_context *ctdb);
1264
1265 int32_t ctdb_control_persistent_store(struct ctdb_context *ctdb, 
1266                                       struct ctdb_req_control *c, 
1267                                       TDB_DATA recdata, bool *async_reply);
1268 int32_t ctdb_control_update_record(struct ctdb_context *ctdb, 
1269                                    struct ctdb_req_control *c, TDB_DATA recdata, 
1270                                    bool *async_reply);
1271
1272 int32_t ctdb_control_trans3_commit(struct ctdb_context *ctdb,
1273                                    struct ctdb_req_control *c,
1274                                    TDB_DATA recdata, bool *async_reply);
1275
1276 void ctdb_persistent_finish_trans3_commits(struct ctdb_context *ctdb);
1277
1278 int32_t ctdb_control_db_transaction_start(struct ctdb_context *ctdb,
1279                                           TDB_DATA indata);
1280 int32_t ctdb_control_db_transaction_commit(struct ctdb_context *ctdb,
1281                                            TDB_DATA indata);
1282 int32_t ctdb_control_db_transaction_cancel(struct ctdb_context *ctdb,
1283                                            TDB_DATA indata);
1284
1285 int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id);
1286 int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id);
1287 int32_t ctdb_control_transaction_cancel(struct ctdb_context *ctdb);
1288 int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata);
1289 int32_t ctdb_control_db_set_healthy(struct ctdb_context *ctdb, TDB_DATA indata);
1290 int32_t ctdb_control_db_get_health(struct ctdb_context *ctdb,
1291                                    TDB_DATA indata,
1292                                    TDB_DATA *outdata);
1293
1294
1295 int ctdb_repack(struct ctdb_context *ctdb, int argc, const char **argv);
1296
1297 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb);
1298 bool ctdb_stopped_monitoring(struct ctdb_context *ctdb);
1299 int ctdb_set_child_logging(struct ctdb_context *ctdb);
1300 void lockdown_memory(bool valgrinding);
1301
1302 struct client_async_data {
1303         enum ctdb_controls opcode;
1304         bool dont_log_errors;
1305         uint32_t count;
1306         uint32_t fail_count;
1307         client_async_callback callback;
1308         client_async_callback fail_callback;
1309         void *callback_data;
1310 };
1311 void ctdb_client_async_add(struct client_async_data *data, struct ctdb_client_control_state *state);
1312 int ctdb_client_async_wait(struct ctdb_context *ctdb, struct client_async_data *data);
1313 int ctdb_client_async_control(struct ctdb_context *ctdb,
1314                                 enum ctdb_controls opcode,
1315                                 uint32_t *nodes,
1316                                 uint64_t srvid,
1317                                 struct timeval timeout,
1318                                 bool dont_log_errors,
1319                                 TDB_DATA data,
1320                                 client_async_callback client_callback,
1321                                 client_async_callback fail_callback,
1322                                 void *callback_data);
1323
1324 struct ctdb_node_map *
1325 ctdb_node_list_to_map(struct ctdb_node **nodes, uint32_t num_nodes,
1326                       TALLOC_CTX *mem_ctx);
1327 struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
1328                                            const char *nlist);
1329 void ctdb_load_nodes_file(struct ctdb_context *ctdb);
1330
1331 int ctdb_control_reload_nodes_file(struct ctdb_context *ctdb, uint32_t opcode);
1332
1333 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata);
1334 int32_t ctdb_control_get_capabilities(struct ctdb_context *ctdb, TDB_DATA *outdata);
1335
1336 char *ctdb_addr_to_str(ctdb_sock_addr *addr);
1337 unsigned ctdb_addr_to_port(ctdb_sock_addr *addr);
1338 void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip);
1339
1340 int32_t ctdb_control_recd_ping(struct ctdb_context *ctdb);
1341 int32_t ctdb_control_set_recmaster(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata);
1342
1343 extern int script_log_level;
1344 extern bool fast_start;
1345 extern const char *ctdbd_pidfile;
1346
1347 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
1348                                              uint32_t call_type,
1349                                              TDB_DATA *outdata);
1350
1351 int ctdb_ctrl_report_recd_lock_latency(struct ctdb_context *ctdb, struct timeval timeout, double latency);
1352
1353 int32_t ctdb_control_stop_node(struct ctdb_context *ctdb);
1354 int32_t ctdb_control_continue_node(struct ctdb_context *ctdb);
1355
1356 void ctdb_stop_vacuuming(struct ctdb_context *ctdb);
1357 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db);
1358
1359 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1360 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1361
1362 void ctdb_local_node_got_banned(struct ctdb_context *ctdb);
1363 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata);
1364 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata);
1365 int32_t ctdb_control_set_db_priority(struct ctdb_context *ctdb, TDB_DATA indata,
1366                                      uint32_t client_id);
1367 void ctdb_ban_self(struct ctdb_context *ctdb);
1368
1369 int32_t ctdb_control_register_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata);
1370
1371 int32_t ctdb_control_deregister_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata);
1372
1373 struct ctdb_log_state *ctdb_vfork_with_logging(TALLOC_CTX *mem_ctx,
1374                                                struct ctdb_context *ctdb,
1375                                                const char *log_prefix,
1376                                                const char *helper,
1377                                                int helper_argc,
1378                                                const char **helper_argv,
1379                                                void (*logfn)(const char *, uint16_t, void *),
1380                                                void *logfn_private, pid_t *pid);
1381
1382
1383 int32_t ctdb_control_process_exists(struct ctdb_context *ctdb, pid_t pid);
1384 struct ctdb_client *ctdb_find_client_by_pid(struct ctdb_context *ctdb, pid_t pid);
1385
1386 int32_t ctdb_control_get_db_seqnum(struct ctdb_context *ctdb,
1387                                    TDB_DATA indata,
1388                                    TDB_DATA *outdata);
1389
1390 int ctdb_load_persistent_health(struct ctdb_context *ctdb,
1391                                 struct ctdb_db_context *ctdb_db);
1392 int ctdb_update_persistent_health(struct ctdb_context *ctdb,
1393                                   struct ctdb_db_context *ctdb_db,
1394                                   const char *reason,/* NULL means healthy */
1395                                   int num_healthy_nodes);
1396 int ctdb_recheck_persistent_health(struct ctdb_context *ctdb);
1397
1398 void ctdb_run_notification_script(struct ctdb_context *ctdb, const char *event);
1399
1400 int verify_remote_ip_allocation(struct ctdb_context *ctdb,
1401                                 struct ctdb_all_public_ips *ips,
1402                                 uint32_t pnn);
1403 int update_ip_assignment_tree(struct ctdb_context *ctdb,
1404                                 struct ctdb_public_ip *ip);
1405 void clear_ip_assignment_tree(struct ctdb_context *ctdb);
1406
1407 int ctdb_init_tevent_logging(struct ctdb_context *ctdb);
1408
1409 int ctdb_statistics_init(struct ctdb_context *ctdb);
1410
1411 int32_t ctdb_control_get_stat_history(struct ctdb_context *ctdb,
1412                                       struct ctdb_req_control *c,
1413                                       TDB_DATA *outdata);
1414
1415 int ctdb_deferred_drop_all_ips(struct ctdb_context *ctdb);
1416
1417 int ctdb_process_deferred_attach(struct ctdb_context *ctdb);
1418
1419 /**
1420  * structure to pass to a schedule_for_deletion_control
1421  */
1422 struct ctdb_control_schedule_for_deletion {
1423         uint32_t db_id;
1424         struct ctdb_ltdb_header hdr;
1425         uint32_t keylen;
1426         uint8_t key[1]; /* key[] */
1427 };
1428
1429 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1430                                            TDB_DATA indata);
1431
1432
1433 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1434                                          const struct ctdb_ltdb_header *hdr,
1435                                          TDB_DATA key);
1436
1437 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1438                                          const struct ctdb_ltdb_header *hdr,
1439                                          const TDB_DATA key);
1440
1441 struct ctdb_ltdb_header *ctdb_header_from_record_handle(struct ctdb_record_handle *h);
1442
1443 int ctdb_trackingdb_add_pnn(struct ctdb_context *ctdb, TDB_DATA *data, uint32_t pnn);
1444
1445 typedef void (*ctdb_trackingdb_cb)(struct ctdb_context *ctdb, uint32_t pnn, void *private_data);
1446
1447 void ctdb_trackingdb_traverse(struct ctdb_context *ctdb, TDB_DATA data, ctdb_trackingdb_cb cb, void *private_data);
1448
1449 int ctdb_start_revoke_ro_record(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA data);
1450
1451 typedef void (*deferred_requeue_fn)(void *call_context, struct ctdb_req_header *hdr);
1452
1453 int ctdb_add_revoke_deferred_call(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_req_header *hdr, deferred_requeue_fn fn, void *call_context);
1454
1455 int ctdb_set_db_readonly(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db);
1456
1457 int ctdb_null_func(struct ctdb_call_info *call);
1458
1459 int ctdb_fetch_func(struct ctdb_call_info *call);
1460
1461 int ctdb_fetch_with_header_func(struct ctdb_call_info *call);
1462
1463 void ctdb_db_statistics_reset(struct ctdb_db_context *ctdb_db);
1464
1465 int32_t ctdb_control_get_db_statistics(struct ctdb_context *ctdb,
1466                                 uint32_t db_id,
1467                                 TDB_DATA *outdata);
1468
1469 int ctdb_set_db_sticky(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db);
1470
1471 /*
1472   description for a message to reload all ips via recovery master/daemon
1473  */
1474 struct reloadips_all_reply {
1475         uint32_t pnn;
1476         uint64_t srvid;
1477 };
1478
1479 int32_t ctdb_control_reload_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1480
1481 /* from server/ctdb_lock.c */
1482 struct lock_request;
1483
1484 typedef int (*ctdb_db_handler_t)(struct ctdb_db_context *ctdb_db,
1485                                  void *private_data);
1486
1487 int ctdb_db_prio_iterator(struct ctdb_context *ctdb, uint32_t priority,
1488                           ctdb_db_handler_t handler, void *private_data);
1489 int ctdb_db_iterator(struct ctdb_context *ctdb, ctdb_db_handler_t handler,
1490                      void *private_data);
1491
1492 int ctdb_lockall_mark_prio(struct ctdb_context *ctdb, uint32_t priority);
1493 int ctdb_lockall_unmark_prio(struct ctdb_context *ctdb, uint32_t priority);
1494
1495 int ctdb_lockdb_mark(struct ctdb_db_context *ctdb_db);
1496 int ctdb_lockdb_unmark(struct ctdb_db_context *ctdb_db);
1497
1498 struct lock_request *ctdb_lock_record(TALLOC_CTX *mem_ctx,
1499                                       struct ctdb_db_context *ctdb_db,
1500                                       TDB_DATA key,
1501                                       bool auto_mark,
1502                                       void (*callback)(void *, bool),
1503                                       void *private_data);
1504
1505 struct lock_request *ctdb_lock_db(TALLOC_CTX *mem_ctx,
1506                                   struct ctdb_db_context *ctdb_db,
1507                                   bool auto_mark,
1508                                   void (*callback)(void *, bool),
1509                                   void *private_data);
1510
1511 struct lock_request *ctdb_lock_alldb_prio(TALLOC_CTX *mem_ctx,
1512                                           struct ctdb_context *ctdb,
1513                                           uint32_t priority,
1514                                           bool auto_mark,
1515                                           void (*callback)(void *, bool),
1516                                           void *private_data);
1517
1518 struct lock_request *ctdb_lock_alldb(TALLOC_CTX *mem_ctx,
1519                                      struct ctdb_context *ctdb,
1520                                      bool auto_mark,
1521                                      void (*callback)(void *, bool),
1522                                      void *private_data);
1523
1524 int mkdir_p(const char *dir, int mode);
1525 void mkdir_p_or_die(const char *dir, int mode);
1526
1527 ssize_t sys_read(int fd, void *buf, size_t count);
1528 ssize_t sys_write(int fd, const void *buf, size_t count);
1529
1530 #endif