merged patch from tridge
[tridge/ctdb.git] / include / ctdb.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_H
21 #define _CTDB_H
22
23 #define CTDB_IMMEDIATE_MIGRATION        0x00000001
24 struct ctdb_call {
25         int call_id;
26         TDB_DATA key;
27         TDB_DATA call_data;
28         TDB_DATA reply_data;
29         uint32_t status;
30         uint32_t flags;
31 };
32
33 /*
34   structure passed to a ctdb call backend function
35 */
36 struct ctdb_call_info {
37         TDB_DATA key;          /* record key */
38         TDB_DATA record_data;  /* current data in the record */
39         TDB_DATA *new_data;    /* optionally updated record data */
40         TDB_DATA *call_data;   /* optionally passed from caller */
41         TDB_DATA *reply_data;  /* optionally returned by function */
42         uint32_t status;       /* optional reply status - defaults to zero */
43 };
44
45 #define CTDB_ERR_INVALID 1
46 #define CTDB_ERR_NOMEM 2
47
48 /*
49   ctdb flags
50 */
51 #define CTDB_FLAG_TORTURE      (1<<1)
52
53 /* 
54    a message handler ID meaning "give me all messages"
55  */
56 #define CTDB_SRVID_ALL (~(uint64_t)0)
57
58 /*
59   srvid type : RECOVERY
60 */
61 #define CTDB_SRVID_RECOVERY     0xF100000000000000LL
62
63 /* 
64    a message handler ID meaning that the cluster has been reconfigured
65  */
66 #define CTDB_SRVID_RECONFIGURE 0xF200000000000000LL
67
68 /* 
69    a message handler ID meaning that an IP address has been released
70  */
71 #define CTDB_SRVID_RELEASE_IP 0xF300000000000000LL
72
73 /* 
74    a message ID meaning that a nodes flags have changed
75  */
76 #define CTDB_SRVID_NODE_FLAGS_CHANGED 0xF400000000000000LL
77
78 /* 
79    a message ID meaning that a node should be banned
80  */
81 #define CTDB_SRVID_BAN_NODE 0xF500000000000000LL
82
83 /* 
84    a message ID meaning that a node should be unbanned
85  */
86 #define CTDB_SRVID_UNBAN_NODE 0xF600000000000000LL
87
88
89 /* used on the domain socket, send a pdu to the local daemon */
90 #define CTDB_CURRENT_NODE     0xF0000001
91 /* send a broadcast to all nodes in the cluster, active or not */
92 #define CTDB_BROADCAST_ALL    0xF0000002
93 /* send a broadcast to all nodes in the current vnn map */
94 #define CTDB_BROADCAST_VNNMAP 0xF0000003
95 /* send a broadcast to all connected nodes */
96 #define CTDB_BROADCAST_CONNECTED 0xF0000004
97
98
99 enum control_state {CTDB_CONTROL_WAIT, CTDB_CONTROL_DONE, CTDB_CONTROL_ERROR, CTDB_CONTROL_TIMEOUT};
100
101 struct ctdb_client_control_state {
102         struct ctdb_context *ctdb;
103         uint32_t reqid;
104         int32_t status;
105         TDB_DATA outdata;
106         enum control_state state;
107         char *errormsg;
108         struct ctdb_req_control *c;
109
110         /* if we have a callback registered for the completion (or failure) of
111            this control
112            if a callback is used, it MUST talloc_free the cb_data passed to it
113         */
114         struct {
115                 void (*fn)(struct ctdb_client_control_state *);
116                 void *private;
117         } async;        
118 };
119
120
121 struct event_context;
122
123 /*
124   initialise ctdb subsystem
125 */
126 struct ctdb_context *ctdb_init(struct event_context *ev);
127
128 /*
129   choose the transport
130 */
131 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport);
132
133 /*
134   set the directory for the local databases
135 */
136 int ctdb_set_tdb_dir(struct ctdb_context *ctdb, const char *dir);
137
138 /*
139   set some flags
140 */
141 void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags);
142
143 /*
144   set max acess count before a dmaster migration
145 */
146 void ctdb_set_max_lacount(struct ctdb_context *ctdb, unsigned count);
147
148 /*
149   tell ctdb what address to listen on, in transport specific format
150 */
151 int ctdb_set_address(struct ctdb_context *ctdb, const char *address);
152
153 int ctdb_set_socketname(struct ctdb_context *ctdb, const char *socketname);
154
155 /*
156   tell ctdb what nodes are available. This takes a filename, which will contain
157   1 node address per line, in a transport specific format
158 */
159 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist);
160
161 /*
162   start the ctdb protocol
163 */
164 int ctdb_start(struct ctdb_context *ctdb);
165 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork);
166
167 /*
168   attach to a ctdb database
169 */
170 struct ctdb_db_context *ctdb_attach(struct ctdb_context *ctdb, const char *name);
171
172 /*
173   find an attached ctdb_db handle given a name
174  */
175 struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, const char *name);
176
177 /*
178   error string for last ctdb error
179 */
180 const char *ctdb_errstr(struct ctdb_context *);
181
182 /* a ctdb call function */
183 typedef int (*ctdb_fn_t)(struct ctdb_call_info *);
184
185 /*
186   setup a ctdb call function
187 */
188 int ctdb_set_call(struct ctdb_db_context *ctdb_db, ctdb_fn_t fn, uint32_t id);
189
190
191
192 /*
193   make a ctdb call. The associated ctdb call function will be called on the DMASTER
194   for the given record
195 */
196 int ctdb_call(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
197
198 /*
199   initiate an ordered ctdb cluster shutdown
200   this function will never return
201 */
202 void ctdb_shutdown(struct ctdb_context *ctdb);
203
204 /* return pnn of this node */
205 uint32_t ctdb_get_pnn(struct ctdb_context *ctdb);
206
207 /*
208   return the number of nodes
209 */
210 uint32_t ctdb_get_num_nodes(struct ctdb_context *ctdb);
211
212 /* setup a handler for ctdb messages */
213 typedef void (*ctdb_message_fn_t)(struct ctdb_context *, uint64_t srvid, 
214                                   TDB_DATA data, void *);
215 int ctdb_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
216                              ctdb_message_fn_t handler,
217                              void *private_data);
218
219
220 int ctdb_call(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
221 struct ctdb_client_call_state *ctdb_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
222 int ctdb_call_recv(struct ctdb_client_call_state *state, struct ctdb_call *call);
223
224 /* send a ctdb message */
225 int ctdb_send_message(struct ctdb_context *ctdb, uint32_t pnn,
226                       uint64_t srvid, TDB_DATA data);
227
228
229 /* 
230    Fetch a ctdb record from a remote node
231  . Underneath this will force the
232    dmaster for the record to be moved to the local node. 
233 */
234 struct ctdb_record_handle *ctdb_fetch_lock(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx, 
235                                            TDB_DATA key, TDB_DATA *data);
236
237 int ctdb_record_store(struct ctdb_record_handle *h, TDB_DATA data);
238
239 int ctdb_fetch(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx, 
240                TDB_DATA key, TDB_DATA *data);
241
242 int ctdb_register_message_handler(struct ctdb_context *ctdb, 
243                                   TALLOC_CTX *mem_ctx,
244                                   uint64_t srvid,
245                                   ctdb_message_fn_t handler,
246                                   void *private_data);
247
248 struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id);
249
250
251 struct ctdb_context *ctdb_cmdline_client(struct event_context *ev);
252
253 struct ctdb_statistics;
254 int ctdb_ctrl_statistics(struct ctdb_context *ctdb, uint32_t destnode, struct ctdb_statistics *status);
255
256 int ctdb_ctrl_shutdown(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
257
258 struct ctdb_vnn_map;
259 int ctdb_ctrl_getvnnmap(struct ctdb_context *ctdb, 
260                 struct timeval timeout, uint32_t destnode, 
261                 TALLOC_CTX *mem_ctx, struct ctdb_vnn_map **vnnmap);
262 int ctdb_ctrl_setvnnmap(struct ctdb_context *ctdb,
263                 struct timeval timeout, uint32_t destnode, 
264                 TALLOC_CTX *mem_ctx, struct ctdb_vnn_map *vnnmap);
265
266 /* table that contains a list of all dbids on a node
267  */
268 struct ctdb_dbid_map {
269         uint32_t num;
270         uint32_t dbids[1];
271 };
272 int ctdb_ctrl_getdbmap(struct ctdb_context *ctdb, 
273         struct timeval timeout, uint32_t destnode, 
274         TALLOC_CTX *mem_ctx, struct ctdb_dbid_map **dbmap);
275
276
277 struct ctdb_node_map;
278
279 int ctdb_ctrl_getnodemap(struct ctdb_context *ctdb, 
280                     struct timeval timeout, uint32_t destnode, 
281                     TALLOC_CTX *mem_ctx, struct ctdb_node_map **nodemap);
282
283 struct ctdb_key_list {
284         uint32_t dbid;
285         uint32_t num;
286         TDB_DATA *keys;
287         struct ctdb_ltdb_header *headers;
288         TDB_DATA *data;
289 };
290 int ctdb_ctrl_pulldb(struct ctdb_context *ctdb, uint32_t destnode, uint32_t dbid, uint32_t lmaster, TALLOC_CTX *mem_ctx, struct ctdb_key_list *keys);
291 int ctdb_ctrl_copydb(struct ctdb_context *ctdb, 
292         struct timeval timeout, uint32_t sourcenode, 
293         uint32_t destnode, uint32_t dbid, uint32_t lmaster, 
294         TALLOC_CTX *mem_ctx);
295
296 int ctdb_ctrl_getdbpath(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx, const char **path);
297 int ctdb_ctrl_getdbname(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t dbid, TALLOC_CTX *mem_ctx, const char **name);
298 int ctdb_ctrl_createdb(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, TALLOC_CTX *mem_ctx, const char *name);
299
300 int ctdb_ctrl_process_exists(struct ctdb_context *ctdb, uint32_t destnode, pid_t pid);
301
302 int ctdb_ctrl_ping(struct ctdb_context *ctdb, uint32_t destnode);
303
304 int ctdb_ctrl_get_config(struct ctdb_context *ctdb);
305
306 int ctdb_ctrl_get_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *level);
307 int ctdb_ctrl_set_debuglevel(struct ctdb_context *ctdb, uint32_t destnode, uint32_t level);
308
309 /*
310   change dmaster for all keys in the database to the new value
311  */
312 int ctdb_ctrl_setdmaster(struct ctdb_context *ctdb, 
313         struct timeval timeout, uint32_t destnode, 
314         TALLOC_CTX *mem_ctx, uint32_t dbid, uint32_t dmaster);
315
316 /*
317   write a record on a specific db (this implicitely updates dmaster of the record to locally be the vnn of the node where the control is executed on)
318  */
319 int ctdb_ctrl_write_record(struct ctdb_context *ctdb, uint32_t destnode, TALLOC_CTX *mem_ctx, uint32_t dbid, TDB_DATA key, TDB_DATA data);
320
321 #define CTDB_RECOVERY_NORMAL            0
322 #define CTDB_RECOVERY_ACTIVE            1
323
324 /*
325   get the recovery mode of a remote node
326  */
327 int ctdb_ctrl_getrecmode(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmode);
328
329 struct ctdb_client_control_state *ctdb_ctrl_getrecmode_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
330
331 int ctdb_ctrl_getrecmode_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmode);
332
333
334 /*
335   set the recovery mode of a remote node
336  */
337 int ctdb_ctrl_setrecmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmode);
338 /*
339   get the monitoring mode of a remote node
340  */
341 int ctdb_ctrl_getmonmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *monmode);
342 /*
343   set the monitoringmode of a remote node
344  */
345 int ctdb_ctrl_setmonmode(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t monmode);
346
347 /*
348   get the recovery master of a remote node
349  */
350 int ctdb_ctrl_getrecmaster(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode, uint32_t *recmaster);
351
352 struct ctdb_client_control_state *ctdb_ctrl_getrecmaster_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct timeval timeout, uint32_t destnode);
353
354 int ctdb_ctrl_getrecmaster_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, struct ctdb_client_control_state *state, uint32_t *recmaster);
355
356
357
358 /*
359   set the recovery master of a remote node
360  */
361 int ctdb_ctrl_setrecmaster(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t recmaster);
362
363 uint32_t *ctdb_get_connected_nodes(struct ctdb_context *ctdb, 
364                                    struct timeval timeout, 
365                                    TALLOC_CTX *mem_ctx,
366                                    uint32_t *num_nodes);
367
368 int ctdb_statistics_reset(struct ctdb_context *ctdb, uint32_t destnode);
369
370 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile);
371
372 typedef int (*ctdb_traverse_func)(struct ctdb_context *, TDB_DATA, TDB_DATA, void *);
373 int ctdb_traverse(struct ctdb_db_context *ctdb_db, ctdb_traverse_func fn, void *private_data);
374
375 int ctdb_dump_db(struct ctdb_db_context *ctdb_db, FILE *f);
376
377 /*
378   get the pid of a ctdb daemon
379  */
380 int ctdb_ctrl_getpid(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode, uint32_t *pid);
381
382 int ctdb_ctrl_freeze(struct ctdb_context *ctdb, struct timeval timeout, 
383                         uint32_t destnode);
384
385 struct ctdb_client_control_state *
386 ctdb_ctrl_freeze_send(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, 
387                         struct timeval timeout, uint32_t destnode);
388
389 int ctdb_ctrl_freeze_recv(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx, 
390                         struct ctdb_client_control_state *state);
391
392 int ctdb_ctrl_thaw(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
393
394 int ctdb_ctrl_getpnn(struct ctdb_context *ctdb, struct timeval timeout, uint32_t destnode);
395
396 int ctdb_ctrl_get_tunable(struct ctdb_context *ctdb, 
397                           struct timeval timeout, 
398                           uint32_t destnode,
399                           const char *name, uint32_t *value);
400
401 int ctdb_ctrl_set_tunable(struct ctdb_context *ctdb, 
402                           struct timeval timeout, 
403                           uint32_t destnode,
404                           const char *name, uint32_t value);
405
406 int ctdb_ctrl_list_tunables(struct ctdb_context *ctdb, 
407                             struct timeval timeout, 
408                             uint32_t destnode,
409                             TALLOC_CTX *mem_ctx,
410                             const char ***list, uint32_t *count);
411
412 int ctdb_ctrl_modflags(struct ctdb_context *ctdb, 
413                        struct timeval timeout, 
414                        uint32_t destnode, 
415                        uint32_t set, uint32_t clear);
416
417 enum ctdb_server_id_type { SERVER_TYPE_SAMBA=1 };
418
419 struct ctdb_server_id {
420         enum ctdb_server_id_type type;
421         uint32_t pnn;
422         uint32_t server_id;
423 };
424
425 struct ctdb_server_id_list {
426         uint32_t num;
427         struct ctdb_server_id server_ids[1];
428 };
429
430
431 int ctdb_ctrl_register_server_id(struct ctdb_context *ctdb,
432                 struct timeval timeout,
433                 struct ctdb_server_id *id);
434 int ctdb_ctrl_unregister_server_id(struct ctdb_context *ctdb, 
435                 struct timeval timeout, 
436                 struct ctdb_server_id *id);
437 int ctdb_ctrl_check_server_id(struct ctdb_context *ctdb,
438                 struct timeval timeout, uint32_t destnode, 
439                 struct ctdb_server_id *id, uint32_t *status);
440 int ctdb_ctrl_get_server_id_list(struct ctdb_context *ctdb,
441                 TALLOC_CTX *mem_ctx,
442                 struct timeval timeout, uint32_t destnode, 
443                 struct ctdb_server_id_list **svid_list);
444
445 int ctdb_socket_connect(struct ctdb_context *ctdb);
446
447 #endif