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