TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / include / ctdb.h
1 /*
2    ctdb database library
3
4    Copyright (C) Ronnie sahlberg 2010
5    Copyright (C) Rusty Russell 2010
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef _CTDB_H
22 #define _CTDB_H
23 #include <sys/types.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <tdb.h>
29 #include <netinet/in.h>
30 #include <ctdb_protocol.h>
31
32 /**
33  * ctdb - a library for accessing tdbs controlled by ctdbd
34  *
35  * ctdbd (clustered tdb daemon) is a daemon designed to syncronize TDB
36  * databases across a cluster.  Using this library, you can communicate with
37  * the daemon to access the databases, pass messages across the cluster, and
38  * control the daemon itself.
39  *
40  * The general API is event-driven and asynchronous: you call the
41  * *_send functions, supplying callbacks, then when the ctdbd file
42  * descriptor is usable, call ctdb_service() to perform read from it
43  * and call your callbacks, which use the *_recv functions to unpack
44  * the replies from ctdbd.
45  *
46  * There is also a synchronous wrapper for each function for trivial
47  * programs; these can be found in the section marked "Synchronous API".
48  */
49
50 /**
51  * ctdb_log_fn_t - logging function for ctdbd
52  * @log_priv: private (typesafe) arg via ctdb_connect
53  * @severity: syslog-style severity
54  * @format: printf-style format string.
55  * @ap: arguments for formatting.
56  *
57  * The severity passed to log() are as per syslog(3).  In particular,
58  * LOG_DEBUG is used for tracing, LOG_WARNING is used for unusual
59  * conditions which don't necessarily return an error through the API,
60  * LOG_ERR is used for errors such as lost communication with ctdbd or
61  * out-of-memory, LOG_ALERT is used for library usage bugs, LOG_CRIT is
62  * used for libctdb internal consistency checks.
63  *
64  * The log() function can be typesafe: the @log_priv arg to
65  * ctdb_donnect and signature of log() should match.
66  */
67 typedef void (*ctdb_log_fn_t)(void *log_priv,
68                               int severity, const char *format, va_list ap);
69
70 /**
71  * ctdb_connect - connect to ctdb using the specified domain socket.
72  * @addr: the socket address, or NULL for default
73  * @log: the logging function
74  * @log_priv: the private argument to the logging function.
75  *
76  * Returns a ctdb context if successful or NULL.  Use ctdb_disconnect() to
77  * release the returned ctdb_connection when finished.
78  *
79  * See Also:
80  *      ctdb_log_fn_t, ctdb_log_file()
81  */
82 struct ctdb_connection *ctdb_connect(const char *addr,
83                                      ctdb_log_fn_t log_fn, void *log_priv);
84
85 /**
86  * ctdb_log_file - example logging function
87  *
88  * Logs everything at priority LOG_WARNING or above to the file given (via
89  * the log_priv argument, usually stderr).
90  */
91 void ctdb_log_file(FILE *, int, const char *, va_list);
92
93 /**
94  * ctdb_log_level - level at which to call logging function
95  *
96  * This variable globally controls filtering on the logging function.
97  * It is initialized to LOG_WARNING, meaning that strange but nonfatal
98  * events, as well as errors and API misuses are reported.
99  *
100  * Set it to LOG_DEBUG to receive all messages.
101  */
102 extern int ctdb_log_level;
103
104 /**
105  * ctdb_disconnect - close down a connection to ctdbd.
106  * @ctdb: the ctdb connectio returned from ctdb_connect.
107  *
108  * The @ctdb arg will be freed by this call, and must not be used again.
109  */
110 void ctdb_disconnect(struct ctdb_connection *ctdb);
111
112 /***
113  *
114  *  Asynchronous API
115  *
116  ***/
117
118 /**
119  * ctdb_num_active - get the number of active commands
120  * @ctdb: the ctdb_connection from ctdb_connect.
121  *
122  * This command can be used to find the number of active commands we have
123  * issued. An active command is a command we have queued, or sent
124  * to the ctdb daemon but which we have not yet received a reply to.
125  *
126  * See Also:
127  *      ctdb_num_in_flight(), ctdb_num_out_queue()
128  */
129 int ctdb_num_active(struct ctdb_connection *ctdb);
130
131 /**
132  * ctdb_num_in_flight - get the number of commands in flight.
133  * @ctdb: the ctdb_connection from ctdb_connect.
134  *
135  * This command can be used to find the number of commands we have
136  * sent to the ctdb daemon to which we have not yet received/processed
137  * the reply.
138  *
139  * See Also:
140  *      ctdb_num_out_queue(), ctdb_num_active()
141  */
142 int ctdb_num_in_flight(struct ctdb_connection *ctdb);
143
144 /**
145  * ctdb_num_out_queue - get the number of commands in the out queue
146  * @ctdb: the ctdb_connection from ctdb_connect.
147  *
148  * This command can be used to find the number of commands we have
149  * queued for delivery to the ctdb daemon but have not yet been
150  * written to the domain socket.
151  *
152  * See Also:
153  *      ctdb_num_in_flight(), ctdb_num_active()
154  */
155 int ctdb_num_out_queue(struct ctdb_connection *ctdb);
156
157 /**
158  * ctdb_get_fd - get the filedescriptor to select/poll on
159  * @ctdb: the ctdb_connection from ctdb_connect.
160  *
161  * By using poll or select on this file descriptor, you will know when to call
162  * ctdb_service().
163  *
164  * See Also:
165  *      ctdb_which_events(), ctdb_service()
166  */
167 int ctdb_get_fd(struct ctdb_connection *ctdb);
168
169 /**
170  * ctdb_which_events - determine which events ctdb_service wants to see
171  * @ctdb: the ctdb_connection from ctdb_connect.
172  *
173  * This returns POLLIN, possibly or'd with POLLOUT if there are writes
174  * pending.  You can set this straight into poll.events.
175  *
176  * See Also:
177  *      ctdb_service()
178  */
179 int ctdb_which_events(struct ctdb_connection *ctdb);
180
181 /**
182  * ctdb_service - service any I/O and callbacks from ctdbd communication
183  * @ctdb: the ctdb_connection from ctdb_connect.
184  * @revents: which events are available.
185  *
186  * This is the core of the library: it read and writes to the ctdbd
187  * socket.  It may call callbacks registered with the various _send
188  * functions.
189  *
190  * revents is a bitset: POLLIN and/or POLLOUT may be set to indicate
191  * it is worth attempting to read/write the (nonblocking)
192  * filedescriptor respectively.
193  *
194  * Note that the synchronous functions call this internally.
195  * Returns false on catastrophic failure.
196  */
197 bool ctdb_service(struct ctdb_connection *ctdb, int revents);
198
199 /**
200  * struct ctdb_request - handle for an outstanding request
201  *
202  * This opaque structure returned from various *_send functions gives
203  * you a handle by which you can cancel a request.  You can't do
204  * anything else with it until the request is completed and it is
205  * handed to your callback function.
206  */
207 struct ctdb_request;
208
209 /**
210  * ctdb_request_free - free a completed request
211  *
212  * This frees a request: you should only call it once it has been
213  * handed to your callback.  For incomplete requests, see ctdb_cancel().
214  */
215 void ctdb_request_free(struct ctdb_request *req);
216
217 /**
218  * ctdb_callback_t - callback for completed requests.
219  *
220  * This would normally unpack the request using ctdb_*_recv().  You
221  * must free the request using ctdb_request_free().
222  *
223  * Note that due to macro magic, actual your callback can be typesafe:
224  * instead of taking a void *, it can take a type which matches the
225  * actual private parameter.
226  */
227 typedef void (*ctdb_callback_t)(struct ctdb_connection *ctdb,
228                                 struct ctdb_request *req, void *private_data);
229
230 /**
231  * struct ctdb_db - connection to a particular open TDB
232  *
233  * This represents a particular open database: you receive it from
234  * ctdb_attachdb or ctdb_attachdb_recv to manipulate a database.
235  *
236  * You have to free the handle with ctdb_detachdb() when finished with it.
237  */
238 struct ctdb_db;
239
240 /**
241  * ctdb_attachdb_send - open a clustered TDB
242  * @ctdb: the ctdb_connection from ctdb_connect.
243  * @name: the filename of the database (no /).
244  * @persistent: whether the database is persistent across ctdbd's life
245  * @tdb_flags: the flags to pass to tdb_open.
246  * @callback: the callback when we're attached or failed (typesafe)
247  * @cbdata: the argument to callback()
248  *
249  * This function connects to a TDB controlled by ctdbd.  It can create
250  * a new TDB if it does not exist, depending on tdb_flags.  Returns
251  * the pending request, or NULL on error.
252  */
253 struct ctdb_request *
254 ctdb_attachdb_send(struct ctdb_connection *ctdb,
255                    const char *name, bool persistent, uint32_t tdb_flags,
256                    ctdb_callback_t callback, void *cbdata);
257
258 /**
259  * ctdb_attachdb_recv - read an ctdb_attach reply from ctdbd
260  * @ctdb: the ctdb_connection from ctdb_connect.
261  * @req: the completed request.
262  *
263  * This returns NULL if something went wrong, or otherwise the open database.
264  */
265 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
266                                    struct ctdb_request *req);
267
268
269 /**
270  * struct ctdb_lock - a record lock on a clustered TDB database
271  *
272  * This locks a subset of the database across the entire cluster; it
273  * is the fundamental sychronization element for ctdb.  You cannot have
274  * more than one lock at once.
275  *
276  * You MUST NOT block during holding this lock and MUST release it
277  * quickly by performing ctdb_release_lock(lock).
278  * Do NOT make any system calls that may block while holding the lock.
279  *
280  * Try to release the lock as quickly as possible.
281  */
282 struct ctdb_lock;
283
284 /**
285  * ctdb_rrl_callback_t - callback for ctdb_readrecordlock_async
286  *
287  * This is not the standard ctdb_callback_t, because there is often no
288  * request required to access a database record (ie. if it is local already).
289  * So the callback is handed the lock directly: it might be NULL if there
290  * was an error obtaining the lock.
291  *
292  * See Also:
293  *      ctdb_readrecordlock_async(), ctdb_readrecordlock()
294  */
295 typedef void (*ctdb_rrl_callback_t)(struct ctdb_db *ctdb_db,
296                                     struct ctdb_lock *lock,
297                                     TDB_DATA data,
298                                     void *private_data);
299
300 /**
301  * ctdb_readrecordlock_async - read and lock a record
302  * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
303  * @key: the key of the record to lock.
304  * @callback: the callback once the record is locked (typesafe).
305  * @cbdata: the argument to callback()
306  *
307  * This returns true on success.  Commonly, we can obtain the record
308  * immediately and so the callback will be invoked.  Otherwise a request
309  * will be queued to ctdbd for the record.
310  *
311  * If failure is immediate, false is returned.  Otherwise, the callback
312  * may receive a NULL lock arg to indicate asynchronous failure.
313  */
314 bool ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
315                                ctdb_rrl_callback_t callback, void *cbdata);
316
317 /**
318  * ctdb_writerecord - write a locked record in a TDB
319  * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
320  * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_recv
321  * @data: the new data to place in the record.
322  */
323 bool ctdb_writerecord(struct ctdb_db *ctdb_db,
324                       struct ctdb_lock *lock, TDB_DATA data);
325
326 /**
327  * ctdb_release_lock - release a record lock on a TDB
328  * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
329  * @lock: the lock from ctdb_readrecordlock/ctdb_readrecordlock_async
330  */
331 void ctdb_release_lock(struct ctdb_db *ctdb_db, struct ctdb_lock *lock);
332
333
334
335 /**
336  * ctdb_traverse_callback_t - callback for ctdb_traverse_async.
337  * return 0 - to continue traverse
338  * return 1 - to abort the traverse
339  *
340  * See Also:
341  *      ctdb_traverse_async()
342  */
343 #define TRAVERSE_STATUS_RECORD          0
344 #define TRAVERSE_STATUS_FINISHED        1
345 #define TRAVERSE_STATUS_ERROR           2
346 typedef int (*ctdb_traverse_callback_t)(struct ctdb_connection *ctdb,
347                                     struct ctdb_db *ctdb_db,
348                                     int status,
349                                     TDB_DATA key,
350                                     TDB_DATA data,
351                                     void *private_data);
352
353 /**
354  * ctdb_traverse_async - traverse a database.
355  * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
356  * @callback: the callback once the record is locked (typesafe).
357  * @cbdata: the argument to callback()
358  *
359  * This returns true on success.
360  * when successfull, the callback will be invoked for each record
361  * until the traversal is finished.
362  *
363  * status == 
364  * TRAVERSE_STATUS_RECORD         key/data contains a record.
365  * TRAVERSE_STATUS_FINISHED       traverse is finished. key/data is undefined.
366  * TRAVERSE_STATUS_ERROR          an error occured during traverse.
367  *                                key/data is undefined.
368  *
369  * If failure is immediate, false is returned.
370  */
371 bool ctdb_traverse_async(struct ctdb_db *ctdb_db,
372                          ctdb_traverse_callback_t callback, void *cbdata);
373
374 /**
375  * ctdb_message_fn_t - messaging callback for ctdb messages
376  *
377  * ctdbd provides a simple messaging API; you can register for a particular
378  * 64-bit id on which you want to send messages, and send to other ids.
379  *
380  * See Also:
381  *      ctdb_set_message_handler_send()
382  */
383 typedef void (*ctdb_message_fn_t)(struct ctdb_connection *,
384                                   uint64_t srvid, TDB_DATA data, void *);
385
386 /**
387  * ctdb_set_message_handler_send - register for messages to a srvid
388  * @ctdb: the ctdb_connection from ctdb_connect.
389  * @srvid: the 64 bit identifier for our messages.
390  * @handler: the callback when we receive such a message (typesafe)
391  * @handler_data: the argument to handler()
392  * @callback: the callback when ctdb replies to our message (typesafe)
393  * @cbdata: the argument to callback()
394  *
395  * Note: our callback will always be called before handler.
396  *
397  * See Also:
398  *      ctdb_set_message_handler_recv(), ctdb_remove_message_handler_send()
399  */
400 struct ctdb_request *
401 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
402                               ctdb_message_fn_t handler,
403                               void *handler_data,
404                               ctdb_callback_t callback,
405                               void *cbdata);
406
407 /**
408  * ctdb_set_message_handler_recv - read a set_message_handler result
409  * @ctdb: the ctdb_connection from ctdb_connect.
410  * @req: the completed request
411  *
412  * If this returns true, the registered handler may be called from the next
413  * ctdb_service().  If this returns false, the registration failed.
414  */
415 bool ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
416                                    struct ctdb_request *handle);
417
418 /**
419  * ctdb_remove_message_handler_send - unregister for messages to a srvid
420  * @ctdb: the ctdb_connection from ctdb_connect.
421  * @srvid: the 64 bit identifier for our messages.
422  * @handler: the callback when we receive such a message (typesafe)
423  * @handler_data: the argument to handler()
424  * @callback: the callback when ctdb replies to our message (typesafe)
425  * @cbdata: the argument to callback()
426  *
427  * This undoes a successful ctdb_set_message_handler or
428  * ctdb_set_message_handler_recv.
429  */
430 struct ctdb_request *
431 ctdb_remove_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
432                                  ctdb_message_fn_t handler, void *handler_data,
433                                  ctdb_callback_t callback, void *cbdata);
434
435 /**
436  * ctdb_remove_message_handler_recv - read a remove_message_handler result
437  * @ctdb: the ctdb_connection from ctdb_connect.
438  * @req: the completed request
439  *
440  * After this returns true, the registered handler will no longer be called.
441  * If this returns false, the de-registration failed.
442  */
443 bool ctdb_remove_message_handler_recv(struct ctdb_connection *ctdb,
444                                       struct ctdb_request *req);
445
446
447 /**
448  * ctdb_send_message - send a message via ctdbd
449  * @ctdb: the ctdb_connection from ctdb_connect.
450  * @pnn: the physical node number to send to
451  * @srvid: the 64 bit identifier for this message type.
452  * @data: the data to send
453  *
454  * This allows arbitrary messages to be sent across the cluster to those
455  * listening (via ctdb_set_message_handler et al).
456  *
457  * This queues a message to be sent: you will need to call
458  * ctdb_service() to actually send the message.  There is no callback
459  * because there is no acknowledgement.
460  *
461  * See Also:
462  *      ctdb_getpnn_send(), ctdb_getpnn()
463  */
464 bool ctdb_send_message(struct ctdb_connection *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
465
466 /**
467  * ctdb_getpnn_send - read the pnn number of a node.
468  * @ctdb: the ctdb_connection from ctdb_connect.
469  * @destnode: the destination node (see below)
470  * @callback: the callback when ctdb replies to our message (typesafe)
471  * @cbdata: the argument to callback()
472  *
473  * There are several special values for destnode, detailed in
474  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
475  * local ctdbd.
476  */
477 struct ctdb_request *
478 ctdb_getpnn_send(struct ctdb_connection *ctdb,
479                  uint32_t destnode,
480                  ctdb_callback_t callback,
481                  void *cbdata);
482 /**
483  * ctdb_getpnn_recv - read an ctdb_getpnn reply from ctdbd
484  * @ctdb: the ctdb_connection from ctdb_connect.
485  * @req: the completed request.
486  * @pnn: a pointer to the pnn to fill in
487  *
488  * This returns false if something went wrong, or otherwise fills in pnn.
489  */
490 bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
491                       struct ctdb_request *req, uint32_t *pnn);
492
493
494 /**
495  * ctdb_getnodemap_send - read the nodemap number from a node.
496  * @ctdb: the ctdb_connection from ctdb_connect.
497  * @destnode: the destination node (see below)
498  * @callback: the callback when ctdb replies to our message (typesafe)
499  * @cbdata: the argument to callback()
500  *
501  * There are several special values for destnode, detailed in
502  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
503  * local ctdbd.
504  */
505 struct ctdb_request *
506 ctdb_getnodemap_send(struct ctdb_connection *ctdb,
507                  uint32_t destnode,
508                  ctdb_callback_t callback,
509                  void *cbdata);
510 /**
511  * ctdb_getnodemap_recv - read an ctdb_getnodemap reply from ctdbd
512  * @ctdb: the ctdb_connection from ctdb_connect.
513  * @req: the completed request.
514  * @nodemap: a pointer to the returned nodemap structure
515  *
516  * This returns false if something went wrong.
517  * If the command failed, it guarantees to set nodemap to NULL.
518  * A non-NULL value for nodemap means the command was successful.
519  *
520  * A non-NULL value of the nodemap must be release released/freed
521  * by ctdb_free_nodemap().
522  */
523 bool ctdb_getnodemap_recv(struct ctdb_connection *ctdb,
524                       struct ctdb_request *req, struct ctdb_node_map **nodemap);
525
526 /**
527  * ctdb_getpublicips_send - read the public ip list from a node.
528  * @ctdb: the ctdb_connection from ctdb_connect.
529  * @destnode: the destination node (see below)
530  * @callback: the callback when ctdb replies to our message (typesafe)
531  * @cbdata: the argument to callback()
532  *
533  * This control returns the list of public ips known to the local node.
534  * Deamons only know about those ips that are listed in the local
535  * public addresses file, which means the returned list of ips may
536  * be only a subset of all ips across the entire cluster.
537  *
538  * There are several special values for destnode, detailed in
539  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
540  * local ctdbd.
541  */
542 struct ctdb_request *
543 ctdb_getpublicips_send(struct ctdb_connection *ctdb,
544                  uint32_t destnode,
545                  ctdb_callback_t callback,
546                  void *cbdata);
547 /**
548  * ctdb_getpublicips_recv - read the public ip list from a node
549  * @ctdb: the ctdb_connection from ctdb_connect.
550  * @req: the completed request.
551  * @ips: a pointer to the returned public ip list
552  *
553  * This returns false if something went wrong.
554  * If the command failed, it guarantees to set ips to NULL.
555  * A non-NULL value for nodemap means the command was successful.
556  *
557  * A non-NULL value of the nodemap must be release released/freed
558  * by ctdb_free_publicips().
559  */
560 bool ctdb_getpublicips_recv(struct ctdb_connection *ctdb,
561                       struct ctdb_request *req, struct ctdb_all_public_ips **ips);
562
563
564 /**
565  * ctdb_getrecmaster_send - read the recovery master of a node
566  * @ctdb: the ctdb_connection from ctdb_connect.
567  * @destnode: the destination node (see below)
568  * @callback: the callback when ctdb replies to our message (typesafe)
569  * @cbdata: the argument to callback()
570  *
571  * There are several special values for destnode, detailed in
572  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
573  * local ctdbd.
574  */
575 struct ctdb_request *
576 ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
577                         uint32_t destnode,
578                         ctdb_callback_t callback, void *cbdata);
579
580 /**
581  * ctdb_getrecmaster_recv - read an ctdb_getrecmaster reply from ctdbd
582  * @ctdb: the ctdb_connection from ctdb_connect.
583  * @req: the completed request.
584  * @recmaster: a pointer to the recmaster to fill in
585  *
586  * This returns false if something went wrong, or otherwise fills in
587  * recmaster.
588  */
589 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
590                             struct ctdb_request *handle,
591                             uint32_t *recmaster);
592
593 /**
594  * ctdb_getrecmode_send - read the recovery mode of a node
595  * @ctdb: the ctdb_connection from ctdb_connect.
596  * @destnode: the destination node (see below)
597  * @callback: the callback when ctdb replies to our message (typesafe)
598  * @cbdata: the argument to callback()
599  *
600  * There are several special values for destnode, detailed in
601  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
602  * local ctdbd.
603  */
604 struct ctdb_request *
605 ctdb_getrecmode_send(struct ctdb_connection *ctdb,
606                      uint32_t destnode,
607                      ctdb_callback_t callback, void *cbdata);
608
609 /**
610  * ctdb_getrecmode_recv - read an ctdb_getrecmode reply from ctdbd
611  * @ctdb: the ctdb_connection from ctdb_connect.
612  * @req: the completed request.
613  * @recmode: a pointer to the recmode to fill in
614  *
615  * This returns false if something went wrong, or otherwise fills in
616  * recmode.
617  */
618 bool ctdb_getrecmode_recv(struct ctdb_connection *ctdb,
619                           struct ctdb_request *handle,
620                           uint32_t *recmode);
621
622 /**
623  * ctdb_cancel - cancel an uncompleted request
624  * @ctdb: the ctdb_connection from ctdb_connect.
625  * @req: the uncompleted request.
626  *
627  * This cancels a request, returning true.  You may not cancel a
628  * request which has already been completed (ie. once its callback has
629  * been called); you should simply use ctdb_request_free() in that case.
630  */
631 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req);
632
633 /***
634  *
635  *  Synchronous API
636  *
637  ***/
638
639 /**
640  * ctdb_attachdb - open a clustered TDB (synchronous)
641  * @ctdb: the ctdb_connection from ctdb_connect.
642  * @name: the filename of the database (no /).
643  * @persistent: whether the database is persistent across ctdbd's life
644  * @tdb_flags: the flags to pass to tdb_open.
645  *
646  * Do a ctdb_attachdb_send and wait for it to complete.
647  * Returns NULL on failure.
648  */
649 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
650                               const char *name, bool persistent,
651                               uint32_t tdb_flags);
652
653 /**
654  * ctdb_detachdb - close a clustered TDB.
655  * @ctdb: the ctdb_connection from ctdb_connect.
656  * @db: the database from ctdb_attachdb/ctdb_attachdb_send
657  *
658  * Closes a clustered tdb.
659  */
660 void ctdb_detachdb(struct ctdb_connection *ctdb, struct ctdb_db *db);
661
662 /**
663  * ctdb_readrecordlock - read and lock a record (synchronous)
664  * @ctdb: the ctdb_connection from ctdb_connect.
665  * @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
666  * @key: the key of the record to lock.
667  * @req: a pointer to the request, if one is needed.
668  *
669  * Do a ctdb_readrecordlock_send and wait for it to complete.
670  * Returns NULL on failure.
671  */
672 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_connection *ctdb,
673                                       struct ctdb_db *ctdb_db, TDB_DATA key,
674                                       TDB_DATA *data);
675
676
677 /**
678  * ctdb_set_message_handler - register for messages to a srvid (synchronous)
679  * @ctdb: the ctdb_connection from ctdb_connect.
680  * @srvid: the 64 bit identifier for our messages.
681  * @handler: the callback when we receive such a message (typesafe)
682  * @cbdata: the argument to handler()
683  *
684  * If this returns true, the message handler can be called from any
685  * ctdb_service() (which is also called indirectly by other
686  * synchronous functions).  If this returns false, the registration
687  * failed.
688  */
689 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
690                               ctdb_message_fn_t handler, void *cbdata);
691
692
693 /**
694  * ctdb_remove_message_handler - deregister for messages (synchronous)
695  * @ctdb: the ctdb_connection from ctdb_connect.
696  * @srvid: the 64 bit identifier for our messages.
697  * @handler: the callback when we receive such a message (typesafe)
698  * @handler_data: the argument to handler()
699  *
700  * If this returns true, the message handler will no longer be called.
701  * If this returns false, the deregistration failed.
702  */
703 bool ctdb_remove_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
704                                  ctdb_message_fn_t handler, void *handler_data);
705
706 /**
707  * ctdb_getpnn - read the pnn number of a node (synchronous)
708  * @ctdb: the ctdb_connection from ctdb_connect.
709  * @destnode: the destination node (see below)
710  * @pnn: a pointer to the pnn to fill in
711  *
712  * There are several special values for destnode, detailed in
713  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
714  * local ctdbd.
715  *
716  * Returns true and fills in *pnn on success.
717  */
718 bool ctdb_getpnn(struct ctdb_connection *ctdb,
719                  uint32_t destnode,
720                  uint32_t *pnn);
721
722 /**
723  * ctdb_getrecmaster - read the recovery master of a node (synchronous)
724  * @ctdb: the ctdb_connection from ctdb_connect.
725  * @destnode: the destination node (see below)
726  * @recmaster: a pointer to the recmaster to fill in
727  *
728  * There are several special values for destnode, detailed in
729  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
730  * local ctdbd.
731  *
732  * Returns true and fills in *recmaster on success.
733  */
734 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
735                        uint32_t destnode,
736                        uint32_t *recmaster);
737
738
739 /**
740  * ctdb_getrecmode - read the recovery mode of a node (synchronous)
741  * @ctdb: the ctdb_connection from ctdb_connect.
742  * @destnode: the destination node (see below)
743  * @recmode: a pointer to the recmode to fill in
744  *
745  * There are several special values for destnode, detailed in
746  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
747  * local ctdbd.
748  *
749  * Returns true and fills in *recmode on success.
750  */
751 bool ctdb_getrecmode(struct ctdb_connection *ctdb,
752                      uint32_t destnode,
753                      uint32_t *recmode);
754
755
756 /**
757  * ctdb_getnodemap - read the nodemap from a node (synchronous)
758  * @ctdb: the ctdb_connection from ctdb_connect.
759  * @destnode: the destination node (see below)
760  * @nodemap: a pointer to the nodemap to fill in
761  *
762  * There are several special values for destnode, detailed in
763  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
764  * local ctdbd.
765  *
766  * Returns true and fills in *nodemap on success.
767  * A non-NULL nodemap must be freed by calling ctdb_free_nodemap.
768  */
769 bool ctdb_getnodemap(struct ctdb_connection *ctdb,
770                      uint32_t destnode, struct ctdb_node_map **nodemap);
771
772 /*
773  * This function is used to release/free the nodemap structure returned
774  * by ctdb_getnodemap() and ctdb_getnodemap_recv()
775  */
776 void ctdb_free_nodemap(struct ctdb_node_map *nodemap);
777
778
779 /**
780  * ctdb_getpublicips - read the public ip list from a node.
781  * @ctdb: the ctdb_connection from ctdb_connect.
782  * @destnode: the destination node (see below)
783  * @ips: a pointer to the returned public ip list
784  *
785  * This control returns the list of public ips known to the local node.
786  * Deamons only know about those ips that are listed in the local
787  * public addresses file, which means the returned list of ips may
788  * be only a subset of all ips across the entire cluster.
789  *
790  * There are several special values for destnode, detailed in
791  * ctdb_protocol.h, particularly CTDB_CURRENT_NODE which means the
792  * local ctdbd.
793  *
794  * This returns false if something went wrong.
795  * If the command failed, it guarantees to set ips to NULL.
796  * A non-NULL value for nodemap means the command was successful.
797  *
798  * A non-NULL value of the nodemap must be release released/freed
799  * by ctdb_free_publicips().
800  */
801 bool ctdb_getpublicips(struct ctdb_connection *ctdb,
802                      uint32_t destnode, struct ctdb_all_public_ips **ips);
803
804 /*
805  * This function is used to release/free the public ip structure returned
806  * by ctdb_getpublicips() and ctdb_getpublicips_recv()
807  */
808 void ctdb_free_publicips(struct ctdb_all_public_ips *ips);
809
810
811 /* These ugly macro wrappers make the callbacks typesafe. */
812 #include <ctdb_typesafe_cb.h>
813 #define ctdb_sendcb(cb, cbdata)                                         \
814          typesafe_cb_preargs(void, (cb), (cbdata),                      \
815                              struct ctdb_connection *, struct ctdb_request *)
816
817 #define ctdb_msgcb(cb, cbdata)                                          \
818         typesafe_cb_preargs(void, (cb), (cbdata),                       \
819                             struct ctdb_connection *, uint64_t, TDB_DATA)
820
821 #define ctdb_connect(addr, log, logpriv)                                \
822         ctdb_connect((addr),                                            \
823                      typesafe_cb_postargs(void, (log), (logpriv),       \
824                                           int, const char *, va_list),  \
825                      (logpriv))
826
827 #define ctdb_set_message_handler(ctdb, srvid, handler, hdata)           \
828         ctdb_set_message_handler((ctdb), (srvid),                       \
829                                  ctdb_msgcb((handler), (hdata)), (hdata))
830
831 #define ctdb_remove_message_handler(ctdb, srvid, handler, hdata)        \
832         ctdb_remove_message_handler((ctdb), (srvid),                    \
833                                     ctdb_msgcb((handler), (hdata)), (hdata))
834
835 #define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
836         ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags),   \
837                            ctdb_sendcb((cb), (cbdata)), (cbdata))
838
839 #define ctdb_readrecordlock_async(_ctdb_db, key, cb, cbdata)            \
840         ctdb_readrecordlock_async((_ctdb_db), (key),                    \
841                 typesafe_cb_preargs(void, (cb), (cbdata),               \
842                                     struct ctdb_db *, struct ctdb_lock *, \
843                                     TDB_DATA), (cbdata))
844
845 #define ctdb_set_message_handler_send(ctdb, srvid, handler, hdata, cb, cbdata) \
846         ctdb_set_message_handler_send((ctdb), (srvid),                  \
847                                       ctdb_msgcb((handler), (hdata)), (hdata), \
848                                       ctdb_sendcb((cb), (cbdata)), (cbdata))
849
850 #define ctdb_remove_message_handler_send(ctdb, srvid, handler, hdata, cb, cbdata) \
851         ctdb_remove_message_handler_send((ctdb), (srvid),               \
852               ctdb_msgcb((handler), (hdata)), (hdata),                  \
853               ctdb_sendcb((cb), (cbdata)), (cbdata))
854
855 #define ctdb_getpnn_send(ctdb, destnode, cb, cbdata)                    \
856         ctdb_getpnn_send((ctdb), (destnode),                            \
857                          ctdb_sendcb((cb), (cbdata)), (cbdata))
858
859 #define ctdb_getrecmaster_send(ctdb, destnode, cb, cbdata)              \
860         ctdb_getrecmaster_send((ctdb), (destnode),                      \
861                                ctdb_sendcb((cb), (cbdata)), (cbdata))
862
863 #define ctdb_getrecmode_send(ctdb, destnode, cb, cbdata)                \
864         ctdb_getrecmode_send((ctdb), (destnode),                        \
865                                ctdb_sendcb((cb), (cbdata)), (cbdata))
866
867 #define ctdb_getnodemap_send(ctdb, destnode, cb, cbdata)                \
868         ctdb_getnodemap_send((ctdb), (destnode),                        \
869                          ctdb_sendcb((cb), (cbdata)), (cbdata))
870
871 #define ctdb_getpublicips_send(ctdb, destnode, cb, cbdata)              \
872         ctdb_getpublicips_send((ctdb), (destnode),                      \
873                          ctdb_sendcb((cb), (cbdata)), (cbdata))
874
875 #endif