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