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