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