- start moving tunable variables into their own structure
[samba.git] / ctdb / include / ctdb_private.h
1 /* 
2    ctdb database library
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #ifndef _CTDB_PRIVATE_H
22 #define _CTDB_PRIVATE_H
23
24 #include "ctdb.h"
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27
28 /* location of daemon socket */
29 #define CTDB_PATH       "/tmp/ctdb.socket"
30
31 /* default ctdb port number */
32 #define CTDB_PORT 9001
33
34 /* we must align packets to ensure ctdb works on all architectures (eg. sparc) */
35 #define CTDB_DS_ALIGNMENT 8
36
37
38 #define CTDB_NULL_FUNC      0xFF000001
39 #define CTDB_FETCH_FUNC     0xFF000002
40
41 /* all tunable variables go in here */
42 struct ctdb_tunable {
43         uint32_t max_redirect_count;
44         uint32_t seqnum_frequency;
45         uint32_t control_timeout;
46         uint32_t traverse_timeout;
47         uint32_t monitoring_timeout;
48         uint32_t monitoring_limit;
49         uint32_t max_lacount;
50 };
51
52 /*
53   an installed ctdb remote call
54 */
55 struct ctdb_registered_call {
56         struct ctdb_registered_call *next, *prev;
57         uint32_t id;
58         ctdb_fn_t fn;
59 };
60
61 /*
62   this address structure might need to be generalised later for some
63   transports
64 */
65 struct ctdb_address {
66         const char *address;
67         int port;
68 };
69
70 /*
71   check a vnn is valid
72  */
73 #define ctdb_validate_vnn(ctdb, vnn) (((uint32_t)(vnn)) < (ctdb)->num_nodes)
74
75
76 /* called from the queue code when a packet comes in. Called with data==NULL
77    on error */
78 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
79                                    void *private_data);
80
81 /* used for callbacks in ctdb_control requests */
82 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
83                                            int32_t status, TDB_DATA data, 
84                                            const char *errormsg,
85                                            void *private_data);
86
87 /*
88   structure describing a connected client in the daemon
89  */
90 struct ctdb_client {
91         struct ctdb_context *ctdb;
92         int fd;
93         struct ctdb_queue *queue;
94         uint32_t client_id;
95         struct ctdb_tcp_list *tcp_list;
96 };
97
98
99 /*
100   state associated with one node
101 */
102 struct ctdb_node {
103         struct ctdb_context *ctdb;
104         struct ctdb_address address;
105         const char *name; /* for debug messages */
106         void *private_data; /* private to transport */
107         uint32_t vnn;
108 #define NODE_FLAGS_CONNECTED 0x00000001
109         uint32_t flags;
110
111         /* used by the dead node monitoring */
112         uint32_t dead_count;
113         uint32_t rx_cnt;
114         uint32_t tx_cnt;
115
116         /* a list of controls pending to this node, so we can time them out quickly
117            if the node becomes disconnected */
118         struct daemon_control_state *pending_controls;
119
120         /* the public address of this node, if known */
121         const char *public_address;
122         uint8_t public_netmask_bits;
123
124         /* the node number that has taken over this nodes public address, if any. 
125            If not taken over, then set to -1 */
126         int32_t takeover_vnn;
127 };
128
129 /*
130   transport specific methods
131 */
132 struct ctdb_methods {
133         int (*initialise)(struct ctdb_context *); /* initialise transport structures */ 
134         int (*start)(struct ctdb_context *); /* start protocol processing */    
135         int (*add_node)(struct ctdb_node *); /* setup a new node */     
136         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
137         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
138         void (*shutdown)(struct ctdb_context *); /* shutdown transport */
139 };
140
141 /*
142   transport calls up to the ctdb layer
143 */
144 struct ctdb_upcalls {
145         /* recv_pkt is called when a packet comes in */
146         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
147
148         /* node_dead is called when an attempt to send to a node fails */
149         void (*node_dead)(struct ctdb_node *);
150
151         /* node_connected is called when a connection to a node is established */
152         void (*node_connected)(struct ctdb_node *);
153 };
154
155 /* list of message handlers - needs to be changed to a more efficient data
156    structure so we can find a message handler given a srvid quickly */
157 struct ctdb_message_list {
158         struct ctdb_context *ctdb;
159         struct ctdb_message_list *next, *prev;
160         uint64_t srvid;
161         ctdb_message_fn_t message_handler;
162         void *message_private;
163 };
164
165 /* additional data required for the daemon mode */
166 struct ctdb_daemon_data {
167         int sd;
168         char *name;
169         struct ctdb_queue *queue;
170 };
171
172 /*
173   ctdb status information
174  */
175 struct ctdb_statistics {
176         uint32_t num_clients;
177         uint32_t frozen;
178         uint32_t recovering;
179         uint32_t client_packets_sent;
180         uint32_t client_packets_recv;
181         uint32_t node_packets_sent;
182         uint32_t node_packets_recv;
183         uint32_t keepalive_packets_sent;
184         uint32_t keepalive_packets_recv;
185         struct {
186                 uint32_t req_call;
187                 uint32_t reply_call;
188                 uint32_t req_dmaster;
189                 uint32_t reply_dmaster;
190                 uint32_t reply_error;
191                 uint32_t req_message;
192                 uint32_t req_control;
193                 uint32_t reply_control;
194         } node;
195         struct {
196                 uint32_t req_call;
197                 uint32_t req_message;
198                 uint32_t req_control;
199         } client;
200         struct {
201                 uint32_t statistics;
202                 uint32_t get_config;
203                 uint32_t ping;
204                 uint32_t attach;
205                 uint32_t set_call;
206                 uint32_t process_exists;
207                 uint32_t traverse_start;
208                 uint32_t traverse_all;
209                 uint32_t traverse_data;
210                 uint32_t update_seqnum;
211                 uint32_t enable_seqnum;
212                 uint32_t set_seqnum_frequency;
213                 uint32_t register_srvid;
214                 uint32_t deregister_srvid;
215         } controls;
216         struct {
217                 uint32_t call;
218                 uint32_t control;
219                 uint32_t traverse;
220         } timeouts;
221         uint32_t total_calls;
222         uint32_t pending_calls;
223         uint32_t lockwait_calls;
224         uint32_t pending_lockwait_calls;
225         uint32_t memory_used;
226         uint32_t __last_counter; /* hack for control_statistics_all */
227         uint32_t max_hop_count;
228         double max_call_latency;
229         double max_lockwait_latency;
230 };
231
232 /* table that contains the mapping between a hash value and lmaster
233  */
234 struct ctdb_vnn_map {
235         uint32_t generation;
236         uint32_t size;
237         uint32_t *map;
238 };
239
240 /* 
241    a wire representation of the vnn map
242  */
243 struct ctdb_vnn_map_wire {
244         uint32_t generation;
245         uint32_t size;
246         uint32_t map[1];
247 };
248
249 /* a structure that contains the elements required for the write record
250    control
251 */
252 struct ctdb_write_record {
253         uint32_t dbid;
254         uint32_t keylen;
255         uint32_t datalen;
256         unsigned char blob[1];
257 };
258
259 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
260
261 #define CTDB_MONITORING_ACTIVE          0
262 #define CTDB_MONITORING_DISABLED        1
263
264 /* information about IP takeover */
265 struct ctdb_takeover {
266         bool enabled;
267         const char *interface;
268         const char *event_script;
269         TALLOC_CTX *last_ctx;
270 };
271
272
273 /* main state of the ctdb daemon */
274 struct ctdb_context {
275         struct event_context *ev;
276         uint32_t recovery_mode;
277         uint32_t monitoring_mode;
278         struct ctdb_tunable tunable;
279         enum ctdb_freeze_mode freeze_mode;
280         struct ctdb_freeze_handle *freeze_handle;
281         struct ctdb_address address;
282         const char *name;
283         const char *db_directory;
284         const char *transport;
285         const char *logfile;
286         char *node_list_file;
287         char *recovery_lock_file;
288         int recovery_lock_fd;
289         uint32_t vnn; /* our own vnn */
290         uint32_t num_nodes;
291         uint32_t num_connected;
292         unsigned flags;
293         struct idr_context *idr;
294         uint16_t idr_cnt;
295         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
296         char *err_msg;
297         const struct ctdb_methods *methods; /* transport methods */
298         const struct ctdb_upcalls *upcalls; /* transport upcalls */
299         void *private_data; /* private to transport */
300         struct ctdb_db_context *db_list;
301         struct ctdb_message_list *message_list;
302         struct ctdb_daemon_data daemon;
303         struct ctdb_statistics statistics;
304         struct ctdb_vnn_map *vnn_map;
305         uint32_t num_clients;
306         uint32_t recovery_master;
307         struct ctdb_call_state *pending_calls;
308         struct ctdb_takeover takeover;
309         struct ctdb_tcp_list *tcp_list;
310 };
311
312 struct ctdb_db_context {
313         struct ctdb_db_context *next, *prev;
314         struct ctdb_context *ctdb;
315         uint32_t db_id;
316         const char *db_name;
317         const char *db_path;
318         struct tdb_wrap *ltdb;
319         struct ctdb_registered_call *calls; /* list of registered calls */
320         uint32_t seqnum;
321         struct timed_event *te;
322 };
323
324
325 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
326           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
327           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
328           return -1; }} while (0)
329
330 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
331           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
332           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
333           }} while (0)
334
335 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
336           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
337           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
338           return NULL; }} while (0)
339
340 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
341           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
342           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
343           }} while (0)
344
345 /*
346   the extended header for records in the ltdb
347 */
348 struct ctdb_ltdb_header {
349         uint64_t rsn;
350         uint32_t dmaster;
351         uint32_t laccessor;
352         uint32_t lacount;
353 };
354
355 enum ctdb_controls {CTDB_CONTROL_PROCESS_EXISTS          = 0, 
356                     CTDB_CONTROL_STATISTICS              = 1, 
357                     /* #2 removed */
358                     CTDB_CONTROL_PING                    = 3,
359                     CTDB_CONTROL_GETDBPATH               = 4,
360                     CTDB_CONTROL_GETVNNMAP               = 5,
361                     CTDB_CONTROL_SETVNNMAP               = 6,
362                     CTDB_CONTROL_GET_DEBUG               = 7,
363                     CTDB_CONTROL_SET_DEBUG               = 8,
364                     CTDB_CONTROL_GET_DBMAP               = 9,
365                     CTDB_CONTROL_GET_NODEMAP             = 10,
366                     CTDB_CONTROL_SET_DMASTER             = 11,
367                     CTDB_CONTROL_CLEAR_DB                = 12,
368                     CTDB_CONTROL_PULL_DB                 = 13,
369                     CTDB_CONTROL_PUSH_DB                 = 14,
370                     CTDB_CONTROL_GET_RECMODE             = 15,
371                     CTDB_CONTROL_SET_RECMODE             = 16,
372                     CTDB_CONTROL_STATISTICS_RESET        = 17,
373                     CTDB_CONTROL_DB_ATTACH               = 18,
374                     CTDB_CONTROL_SET_CALL                = 19,
375                     CTDB_CONTROL_TRAVERSE_START          = 20,
376                     CTDB_CONTROL_TRAVERSE_ALL            = 21,
377                     CTDB_CONTROL_TRAVERSE_DATA           = 22,
378                     CTDB_CONTROL_REGISTER_SRVID          = 23,
379                     CTDB_CONTROL_DEREGISTER_SRVID        = 24,
380                     CTDB_CONTROL_GET_DBNAME              = 25,
381                     CTDB_CONTROL_ENABLE_SEQNUM           = 26,
382                     CTDB_CONTROL_UPDATE_SEQNUM           = 27,
383                     /* #28 removed */
384                     CTDB_CONTROL_DUMP_MEMORY             = 29,
385                     CTDB_CONTROL_GET_PID                 = 30,
386                     CTDB_CONTROL_GET_RECMASTER           = 31,
387                     CTDB_CONTROL_SET_RECMASTER           = 32,
388                     CTDB_CONTROL_FREEZE                  = 33,
389                     CTDB_CONTROL_THAW                    = 34,
390                     CTDB_CONTROL_GET_VNN                 = 35,
391                     CTDB_CONTROL_SHUTDOWN                = 36,
392                     CTDB_CONTROL_GET_MONMODE             = 37,
393                     CTDB_CONTROL_SET_MONMODE             = 38,
394                     CTDB_CONTROL_MAX_RSN                 = 39,
395                     CTDB_CONTROL_SET_RSN_NONEMPTY        = 40,
396                     CTDB_CONTROL_DELETE_LOW_RSN          = 41,
397                     CTDB_CONTROL_TAKEOVER_IP             = 42,
398                     CTDB_CONTROL_RELEASE_IP              = 43,
399                     CTDB_CONTROL_TCP_CLIENT              = 44,
400                     CTDB_CONTROL_TCP_ADD                 = 45,
401                     CTDB_CONTROL_TCP_REMOVE              = 46,
402                     CTDB_CONTROL_STARTUP                 = 47,
403 };
404
405 /*
406   structure passed in ctdb_control_set_rsn_nonempty
407  */
408 struct ctdb_control_set_rsn_nonempty {
409         uint32_t db_id;
410         uint64_t rsn;
411 };
412
413 /*
414   structure passed in ctdb_control_delete_low_rsn
415  */
416 struct ctdb_control_delete_low_rsn {
417         uint32_t db_id;
418         uint64_t rsn;
419 };
420
421 /*
422   structure passed in set_call control
423  */
424 struct ctdb_control_set_call {
425         uint32_t db_id;
426         ctdb_fn_t fn;
427         uint32_t id;
428 };
429
430 /*
431   struct for tcp_client control
432  */
433 struct ctdb_control_tcp {
434         struct sockaddr_in src;
435         struct sockaddr_in dest;
436 };
437
438 /*
439   struct for tcp_add and tcp_remove controls
440  */
441 struct ctdb_control_tcp_vnn {
442         uint32_t vnn;
443         struct sockaddr_in src;
444         struct sockaddr_in dest;
445 };
446
447 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
448
449 #define CTDB_LMASTER_ANY        0xffffffff
450
451 /*
452   state of a in-progress ctdb call
453 */
454 struct ctdb_call_state {
455         struct ctdb_call_state *next, *prev;
456         enum call_state state;
457         uint32_t reqid;
458         struct ctdb_req_call *c;
459         struct ctdb_db_context *ctdb_db;
460         const char *errmsg;
461         struct ctdb_call call;
462         uint32_t generation;
463         struct {
464                 void (*fn)(struct ctdb_call_state *);
465                 void *private_data;
466         } async;
467 };
468
469
470 /* used for fetch_lock */
471 struct ctdb_fetch_handle {
472         struct ctdb_db_context *ctdb_db;
473         TDB_DATA key;
474         TDB_DATA *data;
475         struct ctdb_ltdb_header header;
476 };
477
478 /*
479   operation IDs
480 */
481 enum ctdb_operation {
482         CTDB_REQ_CALL           = 0,
483         CTDB_REPLY_CALL         = 1,
484         CTDB_REQ_DMASTER        = 2,
485         CTDB_REPLY_DMASTER      = 3,
486         CTDB_REPLY_ERROR        = 4,
487         CTDB_REQ_MESSAGE        = 5,
488         /* #6 removed */
489         CTDB_REQ_CONTROL        = 7,
490         CTDB_REPLY_CONTROL      = 8,
491         CTDB_REQ_KEEPALIVE      = 9,
492 };
493
494 #define CTDB_MAGIC 0x43544442 /* CTDB */
495 #define CTDB_VERSION 1
496
497 /*
498   packet structures
499 */
500 struct ctdb_req_header {
501         uint32_t length;
502         uint32_t ctdb_magic;
503         uint32_t ctdb_version;
504         uint32_t generation;
505         uint32_t operation;
506         uint32_t destnode;
507         uint32_t srcnode;
508         uint32_t reqid;
509 };
510
511 struct ctdb_req_call {
512         struct ctdb_req_header hdr;
513         uint32_t flags;
514         uint32_t db_id;
515         uint32_t callid;
516         uint32_t hopcount;
517         uint32_t keylen;
518         uint32_t calldatalen;
519         uint8_t data[1]; /* key[] followed by calldata[] */
520 };
521
522 struct ctdb_reply_call {
523         struct ctdb_req_header hdr;
524         uint32_t status;
525         uint32_t datalen;
526         uint8_t  data[1];
527 };
528
529 struct ctdb_reply_error {
530         struct ctdb_req_header hdr;
531         uint32_t status;
532         uint32_t msglen;
533         uint8_t  msg[1];
534 };
535
536 struct ctdb_req_dmaster {
537         struct ctdb_req_header hdr;
538         uint32_t db_id;
539         uint64_t rsn;
540         uint32_t dmaster;
541         uint32_t keylen;
542         uint32_t datalen;
543         uint8_t  data[1];
544 };
545
546 struct ctdb_reply_dmaster {
547         struct ctdb_req_header hdr;
548         uint32_t db_id;
549         uint64_t rsn;
550         uint32_t keylen;
551         uint32_t datalen;
552         uint8_t  data[1];
553 };
554
555 struct ctdb_req_message {
556         struct ctdb_req_header hdr;
557         uint64_t srvid;
558         uint32_t datalen;
559         uint8_t data[1];
560 };
561
562 struct ctdb_req_getdbpath {
563         struct ctdb_req_header hdr;
564         uint32_t db_id;
565 };
566
567 struct ctdb_reply_getdbpath {
568         struct ctdb_req_header hdr;
569         uint32_t datalen;
570         uint8_t data[1];
571 };
572
573 struct ctdb_req_control {
574         struct ctdb_req_header hdr;
575         uint32_t opcode;
576         uint64_t srvid;
577         uint32_t client_id;
578 #define CTDB_CTRL_FLAG_NOREPLY   1
579         uint32_t flags;
580         uint32_t datalen;
581         uint8_t data[1];
582 };
583
584 struct ctdb_reply_control {
585         struct ctdb_req_header hdr;
586         int32_t  status;
587         uint32_t datalen;
588         uint32_t errorlen;
589         uint8_t data[1];
590 };
591
592 struct ctdb_req_keepalive {
593         struct ctdb_req_header hdr;
594 };
595
596 /* internal prototypes */
597 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
598 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
599 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
600 int ctdb_parse_address(struct ctdb_context *ctdb,
601                        TALLOC_CTX *mem_ctx, const char *str,
602                        struct ctdb_address *address);
603 uint32_t ctdb_hash(const TDB_DATA *key);
604 uint32_t ctdb_hash_string(const char *str);
605 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
606 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
607 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
608 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
609 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
610 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
611
612 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
613 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
614                     TDB_DATA key, struct ctdb_ltdb_header *header, 
615                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
616 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
617                     struct ctdb_ltdb_header *header, TDB_DATA data);
618 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
619 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
620                            TDB_DATA key, struct ctdb_req_header *hdr,
621                            void (*recv_pkt)(void *, struct ctdb_req_header *),
622                            void *recv_context, bool ignore_generation);
623 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
624                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
625                                  struct ctdb_req_header *hdr, TDB_DATA *data,
626                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
627                                  void *recv_context, bool ignore_generation);
628 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
629
630 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
631                                              struct ctdb_call *call,
632                                              struct ctdb_ltdb_header *header,
633                                              TDB_DATA *data);
634
635
636 int ctdbd_start(struct ctdb_context *ctdb);
637 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
638 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
639
640 /*
641   queue a packet for sending
642 */
643 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
644
645 /*
646   setup the fd used by the queue
647  */
648 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
649
650 /*
651   setup a packet queue on a socket
652  */
653 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
654                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
655                                     
656                                     ctdb_queue_cb_fn_t callback,
657                                     void *private_data);
658
659 /*
660   allocate a packet for use in client<->daemon communication
661  */
662 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
663                                             TALLOC_CTX *mem_ctx, 
664                                             enum ctdb_operation operation, 
665                                             size_t length, size_t slength,
666                                             const char *type);
667 #define ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, type) \
668         (type *)_ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, sizeof(type), #type)
669
670 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
671                                                  TALLOC_CTX *mem_ctx, 
672                                                  enum ctdb_operation operation, 
673                                                  size_t length, size_t slength,
674                                                  const char *type);
675 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
676         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, sizeof(type), #type)
677
678
679 /*
680   lock a record in the ltdb, given a key
681  */
682 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
683
684 /*
685   unlock a record in the ltdb, given a key
686  */
687 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
688
689
690 /*
691   make a ctdb call to the local daemon - async send. Called from client context.
692
693   This constructs a ctdb_call request and queues it for processing. 
694   This call never blocks.
695 */
696 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db, 
697                                               struct ctdb_call *call);
698
699 /*
700   make a recv call to the local ctdb daemon - called from client context
701
702   This is called when the program wants to wait for a ctdb_call to complete and get the 
703   results. This call will block unless the call has already completed.
704 */
705 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
706
707 int ctdb_daemon_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
708                              ctdb_message_fn_t handler,
709                              void *private_data);
710
711 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
712                              uint64_t srvid, TDB_DATA data);
713
714 /*
715   send a ctdb message
716 */
717 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
718                              uint64_t srvid, TDB_DATA data);
719
720
721 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
722                                       TDB_DATA key,
723                                       void (*callback)(void *), void *private_data);
724
725 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db, 
726                                               struct ctdb_call *call);
727
728 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
729
730 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
731                                                      struct ctdb_call *call, 
732                                                      struct ctdb_ltdb_header *header);
733
734 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
735                     struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx, TDB_DATA *data,
736                     uint32_t caller);
737
738 #define ctdb_reqid_find(ctdb, reqid, type)      (type *)_ctdb_reqid_find(ctdb, reqid, #type, __location__)
739
740 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
741
742 int ctdb_socket_connect(struct ctdb_context *ctdb);
743
744 void ctdb_latency(double *latency, struct timeval t);
745
746 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state);
747 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location);
748 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid);
749
750 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
751 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
752
753 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
754                              uint64_t srvid, uint32_t opcode, uint32_t client_id, uint32_t flags,
755                              TDB_DATA data,
756                              ctdb_control_callback_fn_t callback,
757                              void *private_data);
758
759 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata, 
760                                TDB_DATA *outdata);
761
762 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
763                          ctdb_fn_t fn, int id);
764
765 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid, 
766                  uint32_t opcode, uint32_t flags, TDB_DATA data, 
767                  TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
768                  struct timeval *timeout, char **errormsg);
769
770
771
772
773 #define CHECK_CONTROL_DATA_SIZE(size) do { \
774  if (indata.dsize != size) { \
775          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
776                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
777          return -1; \
778  } \
779  } while (0)
780
781 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
782 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
783 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
784 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
785 int ctdb_control_writerecord(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
786
787
788 struct ctdb_traverse_start {
789         uint32_t db_id;
790         uint32_t reqid;
791         uint64_t srvid;
792 };
793
794 /*
795   structure used to pass record data between the child and parent
796  */
797 struct ctdb_rec_data {
798         uint32_t length;
799         uint32_t reqid;
800         uint32_t keylen;
801         uint32_t datalen;
802         uint8_t  data[1];
803 };
804                                    
805
806 /* structure used for pulldb control */
807 struct ctdb_control_pulldb {
808         uint32_t db_id;
809         uint32_t lmaster;
810 };
811
812 /* structure used for pulldb control */
813 struct ctdb_control_pulldb_reply {
814         uint32_t db_id;
815         uint32_t count;
816         uint8_t data[1];
817 };
818
819 /* set dmaster control structure */
820 struct ctdb_control_set_dmaster {
821         uint32_t db_id;
822         uint32_t dmaster;
823 };
824
825 /* table that contains a list of all nodes a ctdb knows about and their 
826    status
827  */
828 struct ctdb_node_and_flags {
829         uint32_t vnn;
830         uint32_t flags;
831         struct sockaddr_in sin;
832
833 };
834
835 struct ctdb_node_map {
836         uint32_t num;
837         struct ctdb_node_and_flags nodes[1];
838 };
839
840 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA indata, 
841                                     TDB_DATA *outdata, uint32_t srcnode);
842 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
843 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
844
845 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data);
846
847 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
848 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
849 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
850
851 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
852 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint32_t srcnode);
853 int32_t ctdb_ltdb_set_seqnum_frequency(struct ctdb_context *ctdb, uint32_t frequency);
854
855 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, TDB_DATA key, TDB_DATA data);
856
857 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
858 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
859 int32_t ctdb_control_set_dmaster(struct ctdb_context *ctdb, TDB_DATA indata);
860 int32_t ctdb_control_clear_db(struct ctdb_context *ctdb, TDB_DATA indata);
861
862 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
863                                  struct ctdb_req_control *c,
864                                  TDB_DATA indata, bool *async_reply,
865                                  const char **errormsg);
866 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
867                                 TDB_DATA *outdata, int32_t status, const char *errormsg);
868
869 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
870 int32_t ctdb_control_thaw(struct ctdb_context *ctdb);
871
872 int ctdb_start_recoverd(struct ctdb_context *ctdb);
873
874 uint32_t ctdb_get_num_connected_nodes(struct ctdb_context *ctdb);
875
876 int ctdb_start_monitoring(struct ctdb_context *ctdb);
877 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
878
879 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node);
880 void ctdb_call_resend_all(struct ctdb_context *ctdb);
881 void ctdb_node_dead(struct ctdb_node *node);
882 void ctdb_node_connected(struct ctdb_node *node);
883 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
884 int32_t ctdb_control_max_rsn(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
885 int32_t ctdb_control_set_rsn_nonempty(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
886 int32_t ctdb_control_delete_low_rsn(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
887 int ctdb_ctrl_get_max_rsn(struct ctdb_context *ctdb, struct timeval timeout, 
888                           uint32_t destnode, uint32_t db_id, uint64_t *max_rsn);
889 int ctdb_ctrl_set_rsn_nonempty(struct ctdb_context *ctdb, struct timeval timeout, 
890                                uint32_t destnode, uint32_t db_id, uint64_t rsn);
891 int ctdb_ctrl_delete_low_rsn(struct ctdb_context *ctdb, struct timeval timeout, 
892                              uint32_t destnode, uint32_t db_id, uint64_t rsn);
893 void ctdb_set_realtime(bool enable);
894 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, 
895                                  struct ctdb_req_control *c,
896                                  TDB_DATA indata, 
897                                  bool *async_reply);
898 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout, 
899                           uint32_t destnode, const char *ip);
900 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, 
901                                  struct ctdb_req_control *c,
902                                  TDB_DATA indata, 
903                                  bool *async_reply);
904 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout, 
905                          uint32_t destnode, const char *ip);
906
907 /* from takeover/system.c */
908 int ctdb_sys_send_arp(const struct sockaddr_in *saddr, const char *iface);
909 bool ctdb_sys_have_ip(const char *ip);
910 int ctdb_sys_send_ack(const struct sockaddr_in *dest, 
911                       const struct sockaddr_in *src);
912
913 int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist);
914 int ctdb_set_event_script(struct ctdb_context *ctdb, const char *script);
915 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap);
916
917 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id, 
918                                 uint32_t srcnode, TDB_DATA indata);
919 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata);
920 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
921 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
922
923 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
924 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
925 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
926                                TALLOC_CTX *mem_ctx,
927                                void (*callback)(struct ctdb_context *, int, void *),
928                                void *private_data,
929                                const char *fmt, ...) PRINTF_ATTRIBUTE(5,6);
930 void ctdb_release_all_ips(struct ctdb_context *ctdb);
931
932 void set_nonblocking(int fd);
933 void set_close_on_exec(int fd);
934
935 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep);
936
937 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
938
939 #endif