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