ctdb-daemon: Move switch_from_server_to_client() to ctdb_daemon.c
[metze/samba/wip.git] / ctdb / include / ctdb_private.h
1 /* 
2    ctdb database library
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef _CTDB_PRIVATE_H
21 #define _CTDB_PRIVATE_H
22
23 #include "ctdb_client.h"
24 #include <sys/socket.h>
25
26 /*
27   array of tcp connections
28  */
29 struct ctdb_tcp_array {
30         uint32_t num;
31         struct ctdb_connection *connections;
32 };
33
34 /*
35   an installed ctdb remote call
36 */
37 struct ctdb_registered_call {
38         struct ctdb_registered_call *next, *prev;
39         uint32_t id;
40         ctdb_fn_t fn;
41 };
42
43 /*
44   check that a pnn is valid
45  */
46 #define ctdb_validate_pnn(ctdb, pnn) (((uint32_t)(pnn)) < (ctdb)->num_nodes)
47
48
49 /* called from the queue code when a packet comes in. Called with data==NULL
50    on error */
51 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
52                                    void *private_data);
53
54 /* used for callbacks in ctdb_control requests */
55 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
56                                            int32_t status, TDB_DATA data, 
57                                            const char *errormsg,
58                                            void *private_data);
59 /*
60   structure describing a connected client in the daemon
61  */
62 struct ctdb_client {
63         struct ctdb_context *ctdb;
64         int fd;
65         struct ctdb_queue *queue;
66         uint32_t client_id;
67         pid_t pid;
68         struct ctdb_tcp_list *tcp_list;
69         uint32_t db_id;
70         uint32_t num_persistent_updates;
71         struct ctdb_client_notify_list *notify;
72 };
73
74 /* state associated with a public ip address */
75 struct ctdb_vnn {
76         struct ctdb_vnn *prev, *next;
77
78         struct ctdb_interface *iface;
79         const char **ifaces;
80         ctdb_sock_addr public_address;
81         uint8_t public_netmask_bits;
82
83         /* the node number that is serving this public address, if any. 
84            If no node serves this ip it is set to -1 */
85         int32_t pnn;
86
87         /* List of clients to tickle for this public address */
88         struct ctdb_tcp_array *tcp_array;
89
90         /* whether we need to update the other nodes with changes to our list
91            of connected clients */
92         bool tcp_update_needed;
93
94         /* a context to hang sending gratious arp events off */
95         TALLOC_CTX *takeover_ctx;
96
97         struct ctdb_kill_tcp *killtcp;
98
99         /* Set to true any time an update to this VNN is in flight.
100            This helps to avoid races. */
101         bool update_in_flight;
102
103         /* If CTDB_CONTROL_DEL_PUBLIC_IP is received for this IP
104          * address then this flag is set.  It will be deleted in the
105          * release IP callback. */
106         bool delete_pending;
107 };
108
109 /*
110   state associated with one node
111 */
112 struct ctdb_node {
113         struct ctdb_context *ctdb;
114         ctdb_sock_addr address;
115         const char *name; /* for debug messages */
116         void *private_data; /* private to transport */
117         uint32_t pnn;
118         uint32_t flags;
119
120         /* used by the dead node monitoring */
121         uint32_t dead_count;
122         uint32_t rx_cnt;
123         uint32_t tx_cnt;
124
125         /* a list of controls pending to this node, so we can time them out quickly
126            if the node becomes disconnected */
127         struct daemon_control_state *pending_controls;
128
129         /* used by the recovery daemon when distributing ip addresses 
130            across the nodes.  it needs to know which public ip's can be handled
131            by each node.
132         */
133         struct ctdb_public_ip_list_old *known_public_ips;
134         struct ctdb_public_ip_list_old *available_public_ips;
135         /* used by the recovery dameon to track when a node should be banned */
136         struct ctdb_banning_state *ban_state; 
137 };
138
139 /*
140   transport specific methods
141 */
142 struct ctdb_methods {
143         int (*initialise)(struct ctdb_context *); /* initialise transport structures */ 
144         int (*start)(struct ctdb_context *); /* start the transport */
145         int (*add_node)(struct ctdb_node *); /* setup a new node */     
146         int (*connect_node)(struct ctdb_node *); /* connect to node */
147         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
148         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
149         void (*shutdown)(struct ctdb_context *); /* shutdown transport */
150         void (*restart)(struct ctdb_node *); /* stop and restart the connection */
151 };
152
153 /*
154   transport calls up to the ctdb layer
155 */
156 struct ctdb_upcalls {
157         /* recv_pkt is called when a packet comes in */
158         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
159
160         /* node_dead is called when an attempt to send to a node fails */
161         void (*node_dead)(struct ctdb_node *);
162
163         /* node_connected is called when a connection to a node is established */
164         void (*node_connected)(struct ctdb_node *);
165 };
166
167 /* additional data required for the daemon mode */
168 struct ctdb_daemon_data {
169         int sd;
170         char *name;
171         struct ctdb_queue *queue;
172 };
173
174
175 #define CTDB_UPDATE_STAT(ctdb, counter, value) \
176         {                                                                               \
177                 if (value > ctdb->statistics.counter) {                                 \
178                         ctdb->statistics.counter = c->hopcount;                         \
179                 }                                                                       \
180                 if (value > ctdb->statistics_current.counter) {                         \
181                         ctdb->statistics_current.counter = c->hopcount;                 \
182                 }                                                                       \
183         }
184
185 #define CTDB_INCREMENT_STAT(ctdb, counter) \
186         {                                                                               \
187                 ctdb->statistics.counter++;                                             \
188                 ctdb->statistics_current.counter++;                                     \
189         }
190
191 #define CTDB_DECREMENT_STAT(ctdb, counter) \
192         {                                                                               \
193                 if (ctdb->statistics.counter > 0)                                       \
194                         ctdb->statistics.counter--;                                     \
195                 if (ctdb->statistics_current.counter > 0)                               \
196                         ctdb->statistics_current.counter--;                             \
197         }
198
199 #define CTDB_INCREMENT_DB_STAT(ctdb_db, counter) \
200         {                                                                               \
201                 ctdb_db->statistics.counter++;                                          \
202         }
203
204 #define CTDB_DECREMENT_DB_STAT(ctdb_db, counter) \
205         {                                                                               \
206                 if (ctdb_db->statistics.counter > 0)                                    \
207                         ctdb_db->statistics.counter--;                                  \
208         }
209
210 #define CTDB_UPDATE_RECLOCK_LATENCY(ctdb, name, counter, value) \
211         {                                                                               \
212                 if (value > ctdb->statistics.counter.max)                               \
213                         ctdb->statistics.counter.max = value;                           \
214                 if (value > ctdb->statistics_current.counter.max)                       \
215                         ctdb->statistics_current.counter.max = value;                   \
216                                                                                         \
217                 if (ctdb->statistics.counter.num == 0 ||                                \
218                     value < ctdb->statistics.counter.min)                               \
219                         ctdb->statistics.counter.min = value;                           \
220                 if (ctdb->statistics_current.counter.num == 0 ||                        \
221                     value < ctdb->statistics_current.counter.min)                       \
222                         ctdb->statistics_current.counter.min = value;                   \
223                                                                                         \
224                 ctdb->statistics.counter.total += value;                                \
225                 ctdb->statistics_current.counter.total += value;                        \
226                                                                                         \
227                 ctdb->statistics.counter.num++;                                         \
228                 ctdb->statistics_current.counter.num++;                                 \
229                                                                                         \
230                 if (ctdb->tunable.reclock_latency_ms != 0) {                            \
231                         if (value*1000 > ctdb->tunable.reclock_latency_ms) {            \
232                                 DEBUG(DEBUG_ERR,                                        \
233                                       ("High RECLOCK latency %fs for operation %s\n",   \
234                                        value, name));                                   \
235                         }                                                               \
236                 }                                                                       \
237         }
238
239 #define CTDB_UPDATE_DB_LATENCY(ctdb_db, operation, counter, value)                      \
240         {                                                                               \
241                 if (value > ctdb_db->statistics.counter.max)                            \
242                         ctdb_db->statistics.counter.max = value;                        \
243                 if (ctdb_db->statistics.counter.num == 0 ||                             \
244                     value < ctdb_db->statistics.counter.min)                            \
245                         ctdb_db->statistics.counter.min = value;                        \
246                                                                                         \
247                 ctdb_db->statistics.counter.total += value;                             \
248                 ctdb_db->statistics.counter.num++;                                      \
249                                                                                         \
250                 if (ctdb_db->ctdb->tunable.log_latency_ms != 0) {                       \
251                         if (value*1000 > ctdb_db->ctdb->tunable.log_latency_ms) {       \
252                                 DEBUG(DEBUG_ERR,                                        \
253                                       ("High latency %.6fs for operation %s on database %s\n",\
254                                        value, operation, ctdb_db->db_name));            \
255                         }                                                               \
256                 }                                                                       \
257         }
258
259 #define CTDB_UPDATE_LATENCY(ctdb, db, operation, counter, t) \
260         {                                                                               \
261                 double l = timeval_elapsed(&t);                                         \
262                                                                                         \
263                 if (l > ctdb->statistics.counter.max)                                   \
264                         ctdb->statistics.counter.max = l;                               \
265                 if (l > ctdb->statistics_current.counter.max)                           \
266                         ctdb->statistics_current.counter.max = l;                       \
267                                                                                         \
268                 if (ctdb->statistics.counter.num == 0 ||                                \
269                     l < ctdb->statistics.counter.min)                                   \
270                         ctdb->statistics.counter.min = l;                               \
271                 if (ctdb->statistics_current.counter.num == 0 ||                        \
272                     l < ctdb->statistics_current.counter.min)                           \
273                         ctdb->statistics_current.counter.min = l;                       \
274                                                                                         \
275                 ctdb->statistics.counter.total += l;                                    \
276                 ctdb->statistics_current.counter.total += l;                            \
277                                                                                         \
278                 ctdb->statistics.counter.num++;                                         \
279                 ctdb->statistics_current.counter.num++;                                 \
280                                                                                         \
281                 if (ctdb->tunable.log_latency_ms != 0) {                                \
282                         if (l*1000 > ctdb->tunable.log_latency_ms) {                    \
283                                 DEBUG(DEBUG_WARNING,                                    \
284                                       ("High latency %.6fs for operation %s on database %s\n",\
285                                        l, operation, db->db_name));                     \
286                         }                                                               \
287                 }                                                                       \
288         }
289
290
291 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
292
293 #define NUM_DB_PRIORITIES 3
294 /* main state of the ctdb daemon */
295 struct ctdb_context {
296         struct tevent_context *ev;
297         struct timeval ctdbd_start_time;
298         struct timeval last_recovery_started;
299         struct timeval last_recovery_finished;
300         uint32_t recovery_mode;
301         TALLOC_CTX *tickle_update_context;
302         TALLOC_CTX *keepalive_ctx;
303         TALLOC_CTX *check_public_ifaces_ctx;
304         struct ctdb_tunable_list tunable;
305         enum ctdb_freeze_mode freeze_mode[NUM_DB_PRIORITIES+1];
306         struct ctdb_freeze_handle *freeze_handles[NUM_DB_PRIORITIES+1];
307         bool freeze_transaction_started;
308         uint32_t freeze_transaction_id;
309         ctdb_sock_addr *address;
310         const char *name;
311         const char *db_directory;
312         const char *db_directory_persistent;
313         const char *db_directory_state;
314         struct tdb_wrap *db_persistent_health;
315         uint32_t db_persistent_startup_generation;
316         uint64_t db_persistent_check_errors;
317         uint64_t max_persistent_check_errors;
318         const char *transport;
319         char *recovery_lock_file;
320         int recovery_lock_fd;
321         uint32_t pnn; /* our own pnn */
322         uint32_t num_nodes;
323         uint32_t num_connected;
324         unsigned flags;
325         uint32_t capabilities;
326         struct reqid_context *idr;
327         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
328         struct ctdb_vnn *vnn; /* list of public ip addresses and interfaces */
329         struct ctdb_vnn *single_ip_vnn; /* a structure for the single ip */
330         struct ctdb_interface *ifaces; /* list of local interfaces */
331         char *err_msg;
332         const struct ctdb_methods *methods; /* transport methods */
333         const struct ctdb_upcalls *upcalls; /* transport upcalls */
334         void *private_data; /* private to transport */
335         struct ctdb_db_context *db_list;
336         struct srvid_context *srv;
337         struct ctdb_daemon_data daemon;
338         struct ctdb_statistics statistics;
339         struct ctdb_statistics statistics_current;
340 #define MAX_STAT_HISTORY 100
341         struct ctdb_statistics statistics_history[MAX_STAT_HISTORY];
342         struct ctdb_vnn_map *vnn_map;
343         uint32_t num_clients;
344         uint32_t recovery_master;
345         struct ctdb_client_ip *client_ip_list;
346         bool do_checkpublicip;
347         struct trbt_tree *server_ids; 
348         bool do_setsched;
349         const char *event_script_dir;
350         const char *notification_script;
351         const char *default_public_interface;
352         pid_t ctdbd_pid;
353         pid_t recoverd_pid;
354         enum ctdb_runstate runstate;
355         struct ctdb_monitor_state *monitor;
356         int start_as_disabled;
357         int start_as_stopped;
358         bool valgrinding;
359         uint32_t event_script_timeouts; /* counting how many consecutive times an eventscript has timedout */
360         uint32_t *recd_ping_count;
361         TALLOC_CTX *recd_ctx; /* a context used to track recoverd monitoring events */
362         TALLOC_CTX *release_ips_ctx; /* a context used to automatically drop all IPs if we fail to recover the node */
363
364         TALLOC_CTX *event_script_ctx;
365         int active_events;
366
367         struct ctdb_event_script_state *current_monitor;
368         struct ctdb_script_list_old *last_status[CTDB_EVENT_MAX];
369
370         TALLOC_CTX *banning_ctx;
371
372         struct ctdb_vacuum_child_context *vacuumers;
373
374         /* mapping from pid to ctdb_client * */
375         struct ctdb_client_pid_list *client_pids;
376
377         /* used in the recovery daemon to remember the ip allocation */
378         struct trbt_tree *ip_tree;
379
380         /* Used to defer db attach requests while in recovery mode */
381         struct ctdb_deferred_attach_context *deferred_attach;
382
383         /* if we are a child process, do we have a domain socket to send controls on */
384         bool can_send_controls;
385
386         /* list of event script callback functions that are active */
387         struct event_script_callback *script_callbacks;
388
389         struct ctdb_reloadips_handle *reload_ips;
390
391         const char *nodes_file;
392         const char *public_addresses_file;
393         struct trbt_tree *child_processes; 
394
395         /* Used for locking record/db/alldb */
396         struct lock_context *lock_current;
397         struct lock_context *lock_pending;
398 };
399
400 struct ctdb_db_context {
401         struct ctdb_db_context *next, *prev;
402         struct ctdb_context *ctdb;
403         uint32_t db_id;
404         uint32_t priority;
405         bool persistent;
406         bool readonly; /* Do we support read-only delegations ? */
407         bool sticky; /* Do we support sticky records ? */
408         const char *db_name;
409         const char *db_path;
410         struct tdb_wrap *ltdb;
411         struct tdb_context *rottdb; /* ReadOnly tracking TDB */
412         struct ctdb_registered_call *calls; /* list of registered calls */
413         uint32_t seqnum;
414         struct tevent_timer *seqnum_update;
415         struct ctdb_traverse_local_handle *traverse;
416         struct ctdb_vacuum_handle *vacuum_handle;
417         char *unhealthy_reason;
418         int pending_requests;
419         struct revokechild_handle *revokechild_active;
420         struct ctdb_persistent_state *persistent_state;
421         struct trbt_tree *delete_queue;
422         struct trbt_tree *sticky_records; 
423         int (*ctdb_ltdb_store_fn)(struct ctdb_db_context *ctdb_db,
424                                   TDB_DATA key,
425                                   struct ctdb_ltdb_header *header,
426                                   TDB_DATA data);
427
428         /* used to track which records we are currently fetching
429            so we can avoid sending duplicate fetch requests
430         */
431         struct trbt_tree *deferred_fetch;
432         struct trbt_tree *defer_dmaster;
433
434         struct ctdb_db_statistics_old statistics;
435
436         struct lock_context *lock_current;
437         struct lock_context *lock_pending;
438         int lock_num_current;
439
440         struct ctdb_call_state *pending_calls;
441
442         enum ctdb_freeze_mode freeze_mode;
443         struct ctdb_db_freeze_handle *freeze_handle;
444         bool freeze_transaction_started;
445         uint32_t freeze_transaction_id;
446         uint32_t generation;
447 };
448
449
450 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
451           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
452           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
453           return -1; }} while (0)
454
455 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
456           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
457           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
458           return; }} while (0)
459
460 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
461           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
462           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
463           return NULL; }} while (0)
464
465 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
466           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
467           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
468           }} while (0)
469
470
471 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
472
473 /*
474   state of a in-progress ctdb call
475 */
476 struct ctdb_call_state {
477         struct ctdb_call_state *next, *prev;
478         enum call_state state;
479         uint32_t reqid;
480         struct ctdb_req_call_old *c;
481         struct ctdb_db_context *ctdb_db;
482         const char *errmsg;
483         struct ctdb_call *call;
484         uint32_t generation;
485         struct {
486                 void (*fn)(struct ctdb_call_state *);
487                 void *private_data;
488         } async;
489 };
490
491 /* internal prototypes */
492
493 #define CHECK_CONTROL_DATA_SIZE(size) do { \
494  if (indata.dsize != size) { \
495          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
496                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
497          return -1; \
498  } \
499  } while (0)
500
501 #define CHECK_CONTROL_MIN_DATA_SIZE(size) do { \
502  if (indata.dsize < size) { \
503          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected >= %u\n", \
504                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
505          return -1; \
506  } \
507  } while (0)
508
509 /*
510   state of a in-progress ctdb call in client
511 */
512 struct ctdb_client_call_state {
513         enum call_state state;
514         uint32_t reqid;
515         struct ctdb_db_context *ctdb_db;
516         struct ctdb_call *call;
517         struct {
518                 void (*fn)(struct ctdb_client_call_state *);
519                 void *private_data;
520         } async;
521 };
522
523 extern int script_log_level;
524 extern bool fast_start;
525 extern const char *ctdbd_pidfile;
526
527 typedef void (*deferred_requeue_fn)(void *call_context, struct ctdb_req_header *hdr);
528
529
530 /* from tcp/ and ib/ */
531
532 int ctdb_tcp_init(struct ctdb_context *ctdb);
533 int ctdb_ibw_init(struct ctdb_context *ctdb);
534
535 /* from ctdb_banning.c */
536
537 void ctdb_local_node_got_banned(struct ctdb_context *ctdb);
538 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata);
539 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata);
540 void ctdb_ban_self(struct ctdb_context *ctdb);
541
542 /* from ctdb_call.c */
543
544 struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id);
545
546 void ctdb_request_dmaster(struct ctdb_context *ctdb,
547                           struct ctdb_req_header *hdr);
548 void ctdb_reply_dmaster(struct ctdb_context *ctdb,
549                         struct ctdb_req_header *hdr);
550 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
551 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
552 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
553
554 void ctdb_call_resend_db(struct ctdb_db_context *ctdb);
555 void ctdb_call_resend_all(struct ctdb_context *ctdb);
556
557 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db,
558                                              struct ctdb_call *call,
559                                              struct ctdb_ltdb_header *header,
560                                              TDB_DATA *data);
561
562 struct ctdb_call_state *ctdb_daemon_call_send_remote(
563                                         struct ctdb_db_context *ctdb_db,
564                                         struct ctdb_call *call,
565                                         struct ctdb_ltdb_header *header);
566 int ctdb_daemon_call_recv(struct ctdb_call_state *state,
567                           struct ctdb_call *call);
568
569 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
570
571 int ctdb_start_revoke_ro_record(struct ctdb_context *ctdb,
572                                 struct ctdb_db_context *ctdb_db,
573                                 TDB_DATA key, struct ctdb_ltdb_header *header,
574                                 TDB_DATA data);
575
576 int ctdb_add_revoke_deferred_call(struct ctdb_context *ctdb,
577                                   struct ctdb_db_context *ctdb_db,
578                                   TDB_DATA key, struct ctdb_req_header *hdr,
579                                   deferred_requeue_fn fn, void *call_context);
580
581 /* from server/ctdb_control.c */
582
583 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata);
584
585 void ctdb_request_control_reply(struct ctdb_context *ctdb,
586                                 struct ctdb_req_control_old *c,
587                                 TDB_DATA *outdata, int32_t status,
588                                 const char *errormsg);
589
590 void ctdb_request_control(struct ctdb_context *ctdb,
591                           struct ctdb_req_header *hdr);
592 void ctdb_reply_control(struct ctdb_context *ctdb,
593                         struct ctdb_req_header *hdr);
594
595 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
596                              uint64_t srvid, uint32_t opcode,
597                              uint32_t client_id, uint32_t flags,
598                              TDB_DATA data,
599                              ctdb_control_callback_fn_t callback,
600                              void *private_data);
601
602 /* from server/ctdb_daemon.c */
603
604 int daemon_register_message_handler(struct ctdb_context *ctdb,
605                                     uint32_t client_id, uint64_t srvid);
606 int daemon_deregister_message_handler(struct ctdb_context *ctdb,
607                                       uint32_t client_id, uint64_t srvid);
608 int daemon_check_srvids(struct ctdb_context *ctdb, TDB_DATA indata,
609                         TDB_DATA *outdata);
610
611 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork);
612
613 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
614                                                  TALLOC_CTX *mem_ctx,
615                                                  enum ctdb_operation operation,
616                                                  size_t length, size_t slength,
617                                                  const char *type);
618
619 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
620         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, \
621                                          sizeof(type), #type)
622
623 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb,
624                                  struct ctdb_node *node);
625
626 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
627                          ctdb_fn_t fn, int id);
628
629 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
630                              uint64_t srvid, TDB_DATA data);
631
632 int32_t ctdb_control_register_notify(struct ctdb_context *ctdb,
633                                      uint32_t client_id, TDB_DATA indata);
634 int32_t ctdb_control_deregister_notify(struct ctdb_context *ctdb,
635                                        uint32_t client_id, TDB_DATA indata);
636
637 struct ctdb_client *ctdb_find_client_by_pid(struct ctdb_context *ctdb,
638                                             pid_t pid);
639
640 int32_t ctdb_control_process_exists(struct ctdb_context *ctdb, pid_t pid);
641
642 int ctdb_control_getnodesfile(struct ctdb_context *ctdb, uint32_t opcode,
643                               TDB_DATA indata, TDB_DATA *outdata);
644
645 void ctdb_shutdown_sequence(struct ctdb_context *ctdb, int exit_code);
646
647 int switch_from_server_to_client(struct ctdb_context *ctdb,
648                                  const char *fmt, ...);
649
650 /* from server/ctdb_freeze.c */
651
652 int32_t ctdb_control_db_freeze(struct ctdb_context *ctdb,
653                                struct ctdb_req_control_old *c,
654                                uint32_t db_id, bool *async_reply);
655 int32_t ctdb_control_db_thaw(struct ctdb_context *ctdb, uint32_t db_id);
656
657 int32_t ctdb_control_freeze(struct ctdb_context *ctdb,
658                             struct ctdb_req_control_old *c, bool *async_reply);
659 int32_t ctdb_control_thaw(struct ctdb_context *ctdb, uint32_t priority,
660                           bool check_recmode);
661
662 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
663
664 int32_t ctdb_control_db_transaction_start(struct ctdb_context *ctdb,
665                                           TDB_DATA indata);
666 int32_t ctdb_control_db_transaction_cancel(struct ctdb_context *ctdb,
667                                            TDB_DATA indata);
668 int32_t ctdb_control_db_transaction_commit(struct ctdb_context *ctdb,
669                                            TDB_DATA indata);
670
671 int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id);
672 int32_t ctdb_control_transaction_cancel(struct ctdb_context *ctdb);
673 int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id);
674
675 int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata);
676
677 bool ctdb_db_frozen(struct ctdb_db_context *ctdb_db);
678 bool ctdb_db_prio_frozen(struct ctdb_context *ctdb, uint32_t priority);
679 bool ctdb_db_all_frozen(struct ctdb_context *ctdb);
680
681 /* from server/ctdb_keepalive.c */
682
683 void ctdb_start_keepalive(struct ctdb_context *ctdb);
684 void ctdb_stop_keepalive(struct ctdb_context *ctdb);
685
686 /* from server/ctdb_lock.c */
687
688 struct lock_request;
689
690 typedef int (*ctdb_db_handler_t)(struct ctdb_db_context *ctdb_db,
691                                  void *private_data);
692
693 int ctdb_db_prio_iterator(struct ctdb_context *ctdb, uint32_t priority,
694                           ctdb_db_handler_t handler, void *private_data);
695 int ctdb_db_iterator(struct ctdb_context *ctdb, ctdb_db_handler_t handler,
696                      void *private_data);
697
698 int ctdb_lockdb_mark(struct ctdb_db_context *ctdb_db);
699 int ctdb_lockall_mark_prio(struct ctdb_context *ctdb, uint32_t priority);
700
701 int ctdb_lockdb_unmark(struct ctdb_db_context *ctdb_db);
702 int ctdb_lockall_unmark_prio(struct ctdb_context *ctdb, uint32_t priority);
703
704 struct lock_request *ctdb_lock_record(TALLOC_CTX *mem_ctx,
705                                       struct ctdb_db_context *ctdb_db,
706                                       TDB_DATA key,
707                                       bool auto_mark,
708                                       void (*callback)(void *, bool),
709                                       void *private_data);
710
711 struct lock_request *ctdb_lock_db(TALLOC_CTX *mem_ctx,
712                                   struct ctdb_db_context *ctdb_db,
713                                   bool auto_mark,
714                                   void (*callback)(void *, bool),
715                                   void *private_data);
716
717 struct lock_request *ctdb_lock_alldb_prio(TALLOC_CTX *mem_ctx,
718                                           struct ctdb_context *ctdb,
719                                           uint32_t priority,
720                                           bool auto_mark,
721                                           void (*callback)(void *, bool),
722                                           void *private_data);
723
724 struct lock_request *ctdb_lock_alldb(TALLOC_CTX *mem_ctx,
725                                      struct ctdb_context *ctdb,
726                                      bool auto_mark,
727                                      void (*callback)(void *, bool),
728                                      void *private_data);
729
730 /* from ctdb_logging.c */
731
732 typedef int (*ctdb_log_setup_fn_t)(TALLOC_CTX *mem_ctx,
733                                    const char *logging,
734                                    const char *app_name);
735
736 void ctdb_log_register_backend(const char *prefix, ctdb_log_setup_fn_t init);
737
738 bool ctdb_logging_init(TALLOC_CTX *mem_ctx, const char *logging);
739
740 struct ctdb_log_state *ctdb_vfork_with_logging(TALLOC_CTX *mem_ctx,
741                                                struct ctdb_context *ctdb,
742                                                const char *log_prefix,
743                                                const char *helper,
744                                                int helper_argc,
745                                                const char **helper_argv,
746                                                void (*logfn)(const char *,
747                                                              uint16_t, void *),
748                                                void *logfn_private, pid_t *pid);
749
750 int ctdb_set_child_logging(struct ctdb_context *ctdb);
751 int ctdb_init_tevent_logging(struct ctdb_context *ctdb);
752
753 /* from ctdb_logging_file.c */
754
755 void ctdb_log_init_file(void);
756
757 /* from ctdb_logging_syslog.c */
758
759 void ctdb_log_init_syslog(void);
760
761 /* from ctdb_ltdb_server.c */
762
763 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db,
764                            TDB_DATA key, struct ctdb_req_header *hdr,
765                            void (*recv_pkt)(void *, struct ctdb_req_header *),
766                            void *recv_context, bool ignore_generation);
767
768 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db,
769                                  TDB_DATA key, struct ctdb_ltdb_header *header,
770                                  struct ctdb_req_header *hdr, TDB_DATA *data,
771                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
772                                  void *recv_context, bool ignore_generation);
773
774 int ctdb_load_persistent_health(struct ctdb_context *ctdb,
775                                 struct ctdb_db_context *ctdb_db);
776 int ctdb_update_persistent_health(struct ctdb_context *ctdb,
777                                   struct ctdb_db_context *ctdb_db,
778                                   const char *reason,/* NULL means healthy */
779                                   int num_healthy_nodes);
780 int ctdb_recheck_persistent_health(struct ctdb_context *ctdb);
781
782 int32_t ctdb_control_db_set_healthy(struct ctdb_context *ctdb,
783                                     TDB_DATA indata);
784 int32_t ctdb_control_db_get_health(struct ctdb_context *ctdb,
785                                    TDB_DATA indata, TDB_DATA *outdata);
786
787 int ctdb_set_db_readonly(struct ctdb_context *ctdb,
788                          struct ctdb_db_context *ctdb_db);
789
790 int ctdb_process_deferred_attach(struct ctdb_context *ctdb);
791
792 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata,
793                                TDB_DATA *outdata, uint64_t tdb_flags,
794                                bool persistent, uint32_t client_id,
795                                struct ctdb_req_control_old *c,
796                                bool *async_reply);
797 int32_t ctdb_control_db_detach(struct ctdb_context *ctdb, TDB_DATA indata,
798                                uint32_t client_id);
799
800 int ctdb_attach_databases(struct ctdb_context *ctdb);
801
802 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id,
803                                 uint32_t srcnode);
804 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
805
806 int32_t ctdb_control_set_db_priority(struct ctdb_context *ctdb, TDB_DATA indata,
807                                      uint32_t client_id);
808
809 int ctdb_set_db_sticky(struct ctdb_context *ctdb,
810                        struct ctdb_db_context *ctdb_db);
811
812 void ctdb_db_statistics_reset(struct ctdb_db_context *ctdb_db);
813
814 int32_t ctdb_control_get_db_statistics(struct ctdb_context *ctdb,
815                                        uint32_t db_id, TDB_DATA *outdata);
816
817 /* from ctdb_monitor.c */
818
819 int ctdb_set_notification_script(struct ctdb_context *ctdb, const char *script);
820 void ctdb_run_notification_script(struct ctdb_context *ctdb, const char *event);
821
822 void ctdb_disable_monitoring(struct ctdb_context *ctdb);
823 void ctdb_enable_monitoring(struct ctdb_context *ctdb);
824 void ctdb_stop_monitoring(struct ctdb_context *ctdb);
825
826 void ctdb_wait_for_first_recovery(struct ctdb_context *ctdb);
827
828 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata);
829
830 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb);
831 bool ctdb_stopped_monitoring(struct ctdb_context *ctdb);
832
833 /* from ctdb_persistent.c */
834
835 void ctdb_persistent_finish_trans3_commits(struct ctdb_context *ctdb);
836
837 int32_t ctdb_control_trans3_commit(struct ctdb_context *ctdb,
838                                    struct ctdb_req_control_old *c,
839                                    TDB_DATA recdata, bool *async_reply);
840
841 int32_t ctdb_control_start_persistent_update(struct ctdb_context *ctdb,
842                                              struct ctdb_req_control_old *c,
843                                              TDB_DATA recdata);
844 int32_t ctdb_control_cancel_persistent_update(struct ctdb_context *ctdb,
845                                               struct ctdb_req_control_old *c,
846                                               TDB_DATA recdata);
847
848 int32_t ctdb_control_get_db_seqnum(struct ctdb_context *ctdb,
849                                    TDB_DATA indata, TDB_DATA *outdata);
850
851 /* from ctdb_recover.c */
852
853 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode,
854                            TDB_DATA indata, TDB_DATA *outdata);
855 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode,
856                            TDB_DATA indata, TDB_DATA *outdata);
857
858 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode,
859                           TDB_DATA indata, TDB_DATA *outdata);
860 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode,
861                             TDB_DATA indata, TDB_DATA *outdata);
862
863 int ctdb_control_reload_nodes_file(struct ctdb_context *ctdb, uint32_t opcode);
864
865 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata,
866                              TDB_DATA *outdata);
867 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
868
869 int ctdb_deferred_drop_all_ips(struct ctdb_context *ctdb);
870
871 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb,
872                                  struct ctdb_req_control_old *c,
873                                  TDB_DATA indata, bool *async_reply,
874                                  const char **errormsg);
875
876 bool ctdb_recovery_have_lock(struct ctdb_context *ctdb);
877 bool ctdb_recovery_lock(struct ctdb_context *ctdb);
878 void ctdb_recovery_unlock(struct ctdb_context *ctdb);
879
880 int32_t ctdb_control_end_recovery(struct ctdb_context *ctdb,
881                                  struct ctdb_req_control_old *c,
882                                  bool *async_reply);
883 int32_t ctdb_control_start_recovery(struct ctdb_context *ctdb,
884                                  struct ctdb_req_control_old *c,
885                                  bool *async_reply);
886
887 int32_t ctdb_control_try_delete_records(struct ctdb_context *ctdb,
888                                         TDB_DATA indata, TDB_DATA *outdata);
889 int32_t ctdb_control_receive_records(struct ctdb_context *ctdb,
890                                      TDB_DATA indata, TDB_DATA *outdata);
891
892 int32_t ctdb_control_get_capabilities(struct ctdb_context *ctdb,
893                                       TDB_DATA *outdata);
894
895 int32_t ctdb_control_recd_ping(struct ctdb_context *ctdb);
896 int32_t ctdb_control_set_recmaster(struct ctdb_context *ctdb,
897                                    uint32_t opcode, TDB_DATA indata);
898
899 int32_t ctdb_control_stop_node(struct ctdb_context *ctdb);
900 int32_t ctdb_control_continue_node(struct ctdb_context *ctdb);
901
902 /* from ctdb_recoverd.c */
903
904 int ctdb_start_recoverd(struct ctdb_context *ctdb);
905 void ctdb_stop_recoverd(struct ctdb_context *ctdb);
906
907 /* from ctdb_server.c */
908
909 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport);
910
911 int ctdb_ip_to_nodeid(struct ctdb_context *ctdb, const ctdb_sock_addr *nodeip);
912
913 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
914
915 void ctdb_load_nodes_file(struct ctdb_context *ctdb);
916
917 int ctdb_set_address(struct ctdb_context *ctdb, const char *address);
918
919 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb);
920
921 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
922
923 void ctdb_node_dead(struct ctdb_node *node);
924 void ctdb_node_connected(struct ctdb_node *node);
925
926 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
927 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb,
928                               struct ctdb_req_header *hdr, unsigned opcode);
929
930 /* from ctdb_serverids.c */
931
932 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb,
933                                         uint32_t client_id, TDB_DATA indata);
934 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb,
935                                      TDB_DATA indata);
936 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb,
937                                           TDB_DATA indata);
938 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb,
939                                         TDB_DATA *outdata);
940
941 /* from ctdb_statistics.c */
942
943 int ctdb_statistics_init(struct ctdb_context *ctdb);
944
945 int32_t ctdb_control_get_stat_history(struct ctdb_context *ctdb,
946                                       struct ctdb_req_control_old *c,
947                                       TDB_DATA *outdata);
948
949 /* from ctdb_takeover.c */
950
951 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb,
952                                  struct ctdb_req_control_old *c,
953                                  TDB_DATA indata,
954                                  bool *async_reply);
955 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb,
956                                  struct ctdb_req_control_old *c,
957                                  TDB_DATA indata,
958                                  bool *async_reply);
959 int32_t ctdb_control_ipreallocated(struct ctdb_context *ctdb,
960                                  struct ctdb_req_control_old *c,
961                                  bool *async_reply);
962
963 int ctdb_set_public_addresses(struct ctdb_context *ctdb, bool check_addresses);
964 int ctdb_set_single_public_ip(struct ctdb_context *ctdb, const char *iface,
965                               const char *ip);
966
967 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map_old *nodemap,
968                       uint32_t *force_rebalance_nodes,
969                       client_async_callback fail_callback, void *callback_data);
970
971 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id,
972                                 TDB_DATA indata);
973 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata,
974                              bool tcp_update_needed);
975 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
976 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
977
978 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
979
980 void ctdb_release_all_ips(struct ctdb_context *ctdb);
981
982 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb,
983                                     struct ctdb_req_control_old *c,
984                                     TDB_DATA *outdata);
985 int32_t ctdb_control_get_public_ip_info(struct ctdb_context *ctdb,
986                                         struct ctdb_req_control_old *c,
987                                         TDB_DATA indata, TDB_DATA *outdata);
988
989 int32_t ctdb_control_get_ifaces(struct ctdb_context *ctdb,
990                                 struct ctdb_req_control_old *c,
991                                 TDB_DATA *outdata);
992 int32_t ctdb_control_set_iface_link(struct ctdb_context *ctdb,
993                                     struct ctdb_req_control_old *c,
994                                     TDB_DATA indata);
995
996 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata);
997 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb,
998                                          TDB_DATA indata);
999 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb,
1000                                          TDB_DATA indata, TDB_DATA *outdata);
1001
1002 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb);
1003
1004 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb,
1005                                        TDB_DATA indata);
1006
1007 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb,
1008                                         TDB_DATA indata);
1009 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb,
1010                                         struct ctdb_req_control_old *c,
1011                                         TDB_DATA recdata, bool *async_reply);
1012
1013 int update_ip_assignment_tree(struct ctdb_context *ctdb,
1014                                 struct ctdb_public_ip *ip);
1015 void clear_ip_assignment_tree(struct ctdb_context *ctdb);
1016
1017 int32_t ctdb_control_reload_public_ips(struct ctdb_context *ctdb,
1018                                        struct ctdb_req_control_old *c,
1019                                        bool *async_reply);
1020
1021 /* from ctdb_traverse.c */
1022
1023 int32_t ctdb_control_traverse_all_ext(struct ctdb_context *ctdb,
1024                                       TDB_DATA data, TDB_DATA *outdata);
1025 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb,
1026                                   TDB_DATA data, TDB_DATA *outdata);
1027 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb,
1028                                    TDB_DATA data, TDB_DATA *outdata);
1029 int32_t ctdb_control_traverse_kill(struct ctdb_context *ctdb, TDB_DATA indata,
1030                                     TDB_DATA *outdata, uint32_t srcnode);
1031
1032 int32_t ctdb_control_traverse_start_ext(struct ctdb_context *ctdb,
1033                                         TDB_DATA indata, TDB_DATA *outdata,
1034                                         uint32_t srcnode, uint32_t client_id);
1035 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb,
1036                                     TDB_DATA indata, TDB_DATA *outdata,
1037                                     uint32_t srcnode, uint32_t client_id);
1038
1039 /* from ctdb_tunables.c */
1040
1041 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb);
1042
1043 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata,
1044                                  TDB_DATA *outdata);
1045 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata);
1046 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb,
1047                                    TDB_DATA *outdata);
1048
1049 /* from ctdb_update_record.c */
1050
1051 int32_t ctdb_control_update_record(struct ctdb_context *ctdb,
1052                                    struct ctdb_req_control_old *c,
1053                                    TDB_DATA recdata, bool *async_reply);
1054
1055 /* from ctdb_uptime.c */
1056
1057 int32_t ctdb_control_uptime(struct ctdb_context *ctdb, TDB_DATA *outdata);
1058
1059 /* from ctdb_vacuum.c */
1060
1061 void ctdb_stop_vacuuming(struct ctdb_context *ctdb);
1062 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db);
1063
1064 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1065                                            TDB_DATA indata);
1066 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1067                                          const struct ctdb_ltdb_header *hdr,
1068                                          TDB_DATA key);
1069
1070 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1071                                          const struct ctdb_ltdb_header *hdr,
1072                                          const TDB_DATA key);
1073
1074 /* from eventscript.c */
1075
1076 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb,
1077                                              uint32_t call_type,
1078                                              TDB_DATA *outdata);
1079
1080 int ctdb_event_script_callback(struct ctdb_context *ctdb,
1081                                TALLOC_CTX *mem_ctx,
1082                                void (*callback)(struct ctdb_context *,
1083                                                 int, void *),
1084                                void *private_data,
1085                                enum ctdb_event call,
1086                                const char *fmt, ...) PRINTF_ATTRIBUTE(6,7);
1087
1088 int ctdb_event_script_args(struct ctdb_context *ctdb,
1089                            enum ctdb_event call,
1090                            const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
1091
1092 int ctdb_event_script(struct ctdb_context *ctdb,
1093                       enum ctdb_event call);
1094
1095 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
1096                               struct ctdb_req_control_old *c,
1097                               TDB_DATA data, bool *async_reply);
1098
1099 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1100 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1101
1102 #endif