2018-04-30 12:36:43 +03:00
/*
2007-04-11 05:02:26 +04:00
ctdb daemon code
Copyright ( C ) Andrew Tridgell 2007
Copyright ( C ) Ronnie Sahlberg 2007
2007-05-31 07:50:53 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 09:29:31 +04:00
the Free Software Foundation ; either version 3 of the License , or
2007-05-31 07:50:53 +04:00
( at your option ) any later version .
2018-04-30 12:36:43 +03:00
2007-05-31 07:50:53 +04:00
This program is distributed in the hope that it will be useful ,
2007-04-11 05:02:26 +04:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2007-05-31 07:50:53 +04:00
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2018-04-30 12:36:43 +03:00
2007-05-31 07:50:53 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 09:29:31 +04:00
along with this program ; if not , see < http : //www.gnu.org/licenses/>.
2007-04-11 05:02:26 +04:00
*/
2015-10-26 08:50:46 +03:00
# include "replace.h"
2007-04-11 05:02:26 +04:00
# include "system/network.h"
# include "system/filesys.h"
2008-07-04 11:04:26 +04:00
# include "system/locale.h"
2015-10-26 08:50:46 +03:00
# include <talloc.h>
# include <tevent.h>
# include <tdb.h>
# include "lib/tdb_wrap/tdb_wrap.h"
2007-06-07 16:26:27 +04:00
# include "lib/util/dlinklist.h"
2015-10-26 08:50:46 +03:00
# include "lib/util/time.h"
# include "lib/util/debug.h"
# include "lib/util/samba_util.h"
# include "ctdb_private.h"
# include "ctdb_client.h"
2015-03-17 06:30:18 +03:00
# include "common/reqid.h"
2015-10-23 06:11:53 +03:00
# include "common/system.h"
2015-10-23 06:17:34 +03:00
# include "common/common.h"
2015-11-11 07:17:56 +03:00
# include "common/logging.h"
2007-04-11 05:02:26 +04:00
2007-06-07 16:06:19 +04:00
/*
allocate a packet for use in client < - > daemon communication
*/
struct ctdb_req_header * _ctdbd_allocate_pkt ( struct ctdb_context * ctdb ,
2018-04-30 12:36:43 +03:00
TALLOC_CTX * mem_ctx ,
enum ctdb_operation operation ,
2007-06-07 16:06:19 +04:00
size_t length , size_t slength ,
const char * type )
{
int size ;
struct ctdb_req_header * hdr ;
length = MAX ( length , slength ) ;
size = ( length + ( CTDB_DS_ALIGNMENT - 1 ) ) & ~ ( CTDB_DS_ALIGNMENT - 1 ) ;
2011-12-22 20:18:38 +04:00
hdr = ( struct ctdb_req_header * ) talloc_zero_size ( mem_ctx , size ) ;
2007-06-07 16:06:19 +04:00
if ( hdr = = NULL ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( " Unable to allocate packet for operation %u of length %u \n " ,
2007-06-07 16:06:19 +04:00
operation , ( unsigned ) length ) ) ;
return NULL ;
}
talloc_set_name_const ( hdr , type ) ;
hdr - > length = length ;
hdr - > operation = operation ;
hdr - > ctdb_magic = CTDB_MAGIC ;
2014-10-21 04:53:29 +04:00
hdr - > ctdb_version = CTDB_PROTOCOL ;
2007-09-04 04:06:36 +04:00
hdr - > srcnode = ctdb - > pnn ;
2007-06-07 16:06:19 +04:00
if ( ctdb - > vnn_map ) {
hdr - > generation = ctdb - > vnn_map - > generation ;
}
return hdr ;
}
/*
local version of ctdb_call
*/
int ctdb_call_local ( struct ctdb_db_context * ctdb_db , struct ctdb_call * call ,
struct ctdb_ltdb_header * header , TALLOC_CTX * mem_ctx ,
2013-08-19 09:04:46 +04:00
TDB_DATA * data , bool updatetdb )
2007-06-07 16:06:19 +04:00
{
struct ctdb_call_info * c ;
struct ctdb_registered_call * fn ;
struct ctdb_context * ctdb = ctdb_db - > ctdb ;
2018-04-30 12:36:43 +03:00
2018-02-21 15:52:11 +03:00
c = talloc_zero ( mem_ctx , struct ctdb_call_info ) ;
2007-06-07 16:06:19 +04:00
CTDB_NO_MEMORY ( ctdb , c ) ;
c - > key = call - > key ;
c - > call_data = & call - > call_data ;
c - > record_data . dptr = talloc_memdup ( c , data - > dptr , data - > dsize ) ;
c - > record_data . dsize = data - > dsize ;
CTDB_NO_MEMORY ( ctdb , c - > record_data . dptr ) ;
2011-07-20 05:27:05 +04:00
c - > header = header ;
2007-06-07 16:06:19 +04:00
for ( fn = ctdb_db - > calls ; fn ; fn = fn - > next ) {
2019-06-07 23:40:12 +03:00
if ( fn - > id = = ( uint32_t ) call - > call_id ) {
break ;
}
2007-06-07 16:06:19 +04:00
}
if ( fn = = NULL ) {
ctdb_set_error ( ctdb , " Unknown call id %u \n " , call - > call_id ) ;
talloc_free ( c ) ;
return - 1 ;
}
if ( fn - > fn ( c ) ! = 0 ) {
ctdb_set_error ( ctdb , " ctdb_call %u failed \n " , call - > call_id ) ;
talloc_free ( c ) ;
return - 1 ;
}
2010-11-29 05:07:59 +03:00
/* we need to force the record to be written out if this was a remote access */
2013-08-19 09:04:46 +04:00
if ( c - > new_data = = NULL ) {
2007-06-07 16:06:19 +04:00
c - > new_data = & c - > record_data ;
}
2011-07-20 07:30:12 +04:00
if ( c - > new_data & & updatetdb ) {
2007-06-07 16:06:19 +04:00
/* XXX check that we always have the lock here? */
if ( ctdb_ltdb_store ( ctdb_db , call - > key , header , * c - > new_data ) ! = 0 ) {
ctdb_set_error ( ctdb , " ctdb_call tdb_store failed \n " ) ;
talloc_free ( c ) ;
return - 1 ;
}
}
2008-03-19 05:54:17 +03:00
if ( c - > reply_data ) {
2007-06-07 16:06:19 +04:00
call - > reply_data = * c - > reply_data ;
2008-03-19 04:08:29 +03:00
2008-03-19 05:54:17 +03:00
talloc_steal ( call , call - > reply_data . dptr ) ;
2007-06-07 16:06:19 +04:00
talloc_set_name_const ( call - > reply_data . dptr , __location__ ) ;
} else {
call - > reply_data . dptr = NULL ;
call - > reply_data . dsize = 0 ;
}
call - > status = c - > status ;
talloc_free ( c ) ;
return 0 ;
}
2007-04-11 05:02:26 +04:00
/*
queue a packet for sending from client to daemon
*/
static int ctdb_client_queue_pkt ( struct ctdb_context * ctdb , struct ctdb_req_header * hdr )
{
return ctdb_queue_send ( ctdb - > daemon . queue , ( uint8_t * ) hdr , hdr - > length ) ;
}
2007-04-19 05:28:01 +04:00
/*
called when a CTDB_REPLY_CALL packet comes in in the client
This packet comes in response to a CTDB_REQ_CALL request packet . It
contains any reply data from the call
*/
static void ctdb_client_reply_call ( struct ctdb_context * ctdb , struct ctdb_req_header * hdr )
{
2015-10-29 08:29:01 +03:00
struct ctdb_reply_call_old * c = ( struct ctdb_reply_call_old * ) hdr ;
2007-04-19 05:28:01 +04:00
struct ctdb_client_call_state * state ;
2015-03-17 06:30:18 +03:00
state = reqid_find ( ctdb - > idr , hdr - > reqid , struct ctdb_client_call_state ) ;
2007-04-19 05:28:01 +04:00
if ( state = = NULL ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " reqid %u not found \n " , hdr - > reqid ) ) ;
2007-04-17 14:03:01 +04:00
return ;
}
2007-04-23 12:19:50 +04:00
if ( hdr - > reqid ! = state - > reqid ) {
/* we found a record but it was the wrong one */
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( " Dropped client call reply with reqid:%u \n " , hdr - > reqid ) ) ;
2007-04-23 12:19:50 +04:00
return ;
}
2008-03-19 05:54:17 +03:00
state - > call - > reply_data . dptr = c - > data ;
state - > call - > reply_data . dsize = c - > datalen ;
state - > call - > status = c - > status ;
2007-04-12 09:46:50 +04:00
2007-04-19 05:28:01 +04:00
talloc_steal ( state , c ) ;
state - > state = CTDB_CALL_DONE ;
2008-01-08 09:23:27 +03:00
if ( state - > async . fn ) {
state - > async . fn ( state ) ;
}
2007-04-12 09:46:50 +04:00
}
2015-04-08 07:38:26 +03:00
void ctdb_request_message ( struct ctdb_context * ctdb ,
struct ctdb_req_header * hdr )
{
2015-10-29 08:36:30 +03:00
struct ctdb_req_message_old * c = ( struct ctdb_req_message_old * ) hdr ;
2015-04-08 07:38:26 +03:00
TDB_DATA data ;
data . dsize = c - > datalen ;
data . dptr = talloc_memdup ( c , & c - > data [ 0 ] , c - > datalen ) ;
if ( data . dptr = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " Memory allocation failure \n " ) ) ;
return ;
}
srvid_dispatch ( ctdb - > srv , c - > srvid , CTDB_SRVID_ALL , data ) ;
}
2007-04-26 16:27:49 +04:00
static void ctdb_client_reply_control ( struct ctdb_context * ctdb , struct ctdb_req_header * hdr ) ;
2007-04-20 14:07:47 +04:00
2007-04-11 05:02:26 +04:00
/*
this is called in the client , when data comes in from the daemon
*/
2011-08-03 12:38:27 +04:00
void ctdb_client_read_cb ( uint8_t * data , size_t cnt , void * args )
2007-04-11 05:02:26 +04:00
{
struct ctdb_context * ctdb = talloc_get_type ( args , struct ctdb_context ) ;
2007-04-18 05:20:24 +04:00
struct ctdb_req_header * hdr = ( struct ctdb_req_header * ) data ;
TALLOC_CTX * tmp_ctx ;
/* place the packet as a child of a tmp_ctx. We then use
talloc_free ( ) below to free it . If any of the calls want
to keep it , then they will steal it somewhere else , and the
talloc_free ( ) will be a no - op */
tmp_ctx = talloc_new ( ctdb ) ;
talloc_steal ( tmp_ctx , hdr ) ;
2007-04-11 05:02:26 +04:00
2007-04-18 09:35:41 +04:00
if ( cnt = = 0 ) {
2013-06-24 11:37:15 +04:00
DEBUG ( DEBUG_CRIT , ( " Daemon has exited - shutting down client \n " ) ) ;
exit ( 1 ) ;
2007-04-18 09:35:41 +04:00
}
2007-04-11 05:02:26 +04:00
if ( cnt < sizeof ( * hdr ) ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_CRIT , ( " Bad packet length %u in client \n " , ( unsigned ) cnt ) ) ;
2007-04-18 05:20:24 +04:00
goto done ;
2007-04-11 05:02:26 +04:00
}
if ( cnt ! = hdr - > length ) {
2018-04-30 12:36:43 +03:00
ctdb_set_error ( ctdb , " Bad header length %u expected %u in client \n " ,
2007-04-28 13:35:49 +04:00
( unsigned ) hdr - > length , ( unsigned ) cnt ) ;
2007-04-18 05:20:24 +04:00
goto done ;
2007-04-11 05:02:26 +04:00
}
if ( hdr - > ctdb_magic ! = CTDB_MAGIC ) {
2007-04-17 04:15:44 +04:00
ctdb_set_error ( ctdb , " Non CTDB packet rejected in client \n " ) ;
2007-04-18 05:20:24 +04:00
goto done ;
2007-04-11 05:02:26 +04:00
}
2014-10-21 04:53:29 +04:00
if ( hdr - > ctdb_version ! = CTDB_PROTOCOL ) {
2007-04-17 04:15:44 +04:00
ctdb_set_error ( ctdb , " Bad CTDB version 0x%x rejected in client \n " , hdr - > ctdb_version ) ;
2007-04-18 05:20:24 +04:00
goto done ;
2007-04-11 05:02:26 +04:00
}
2007-04-11 05:58:28 +04:00
switch ( hdr - > operation ) {
case CTDB_REPLY_CALL :
2007-04-19 05:28:01 +04:00
ctdb_client_reply_call ( ctdb , hdr ) ;
2007-04-11 05:58:28 +04:00
break ;
case CTDB_REQ_MESSAGE :
ctdb_request_message ( ctdb , hdr ) ;
break ;
2007-04-11 08:54:47 +04:00
2007-04-26 16:27:49 +04:00
case CTDB_REPLY_CONTROL :
ctdb_client_reply_control ( ctdb , hdr ) ;
break ;
2007-04-12 09:46:50 +04:00
default :
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_CRIT , ( " bogus operation code:%u \n " , hdr - > operation ) ) ;
2007-04-11 05:58:28 +04:00
}
2007-04-18 05:20:24 +04:00
done :
talloc_free ( tmp_ctx ) ;
2007-04-11 05:02:26 +04:00
}
/*
2013-04-05 06:19:34 +04:00
connect to a unix domain socket
2007-04-11 05:02:26 +04:00
*/
2013-04-05 06:19:34 +04:00
int ctdb_socket_connect ( struct ctdb_context * ctdb )
2007-04-11 05:02:26 +04:00
{
struct sockaddr_un addr ;
2016-08-10 10:18:55 +03:00
int ret ;
2007-04-11 05:02:26 +04:00
memset ( & addr , 0 , sizeof ( addr ) ) ;
addr . sun_family = AF_UNIX ;
2013-11-11 05:39:48 +04:00
strncpy ( addr . sun_path , ctdb - > daemon . name , sizeof ( addr . sun_path ) - 1 ) ;
2007-04-11 05:02:26 +04:00
ctdb - > daemon . sd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ;
if ( ctdb - > daemon . sd = = - 1 ) {
2009-05-14 12:25:00 +04:00
DEBUG ( DEBUG_ERR , ( __location__ " Failed to open client socket. Errno:%s(%d) \n " , strerror ( errno ) , errno ) ) ;
2007-04-11 05:02:26 +04:00
return - 1 ;
}
2007-05-30 09:43:25 +04:00
2013-04-05 06:19:34 +04:00
if ( connect ( ctdb - > daemon . sd , ( struct sockaddr * ) & addr , sizeof ( addr ) ) = = - 1 ) {
2016-08-15 07:50:09 +03:00
DEBUG ( DEBUG_ERR ,
( __location__
" Failed to connect client socket to daemon (%s) \n " ,
strerror ( errno ) ) ) ;
2007-04-11 05:02:26 +04:00
close ( ctdb - > daemon . sd ) ;
ctdb - > daemon . sd = - 1 ;
return - 1 ;
}
2016-08-10 10:18:55 +03:00
ret = set_blocking ( ctdb - > daemon . sd , false ) ;
if ( ret ! = 0 ) {
DEBUG ( DEBUG_ERR ,
( __location__
" failed to set socket non-blocking (%s) \n " ,
strerror ( errno ) ) ) ;
close ( ctdb - > daemon . sd ) ;
ctdb - > daemon . sd = - 1 ;
return - 1 ;
}
2013-03-18 06:45:08 +04:00
set_close_on_exec ( ctdb - > daemon . sd ) ;
2016-05-27 06:50:06 +03:00
2018-04-30 12:36:43 +03:00
ctdb - > daemon . queue = ctdb_queue_setup ( ctdb , ctdb , ctdb - > daemon . sd ,
CTDB_DS_ALIGNMENT ,
2010-07-01 17:08:49 +04:00
ctdb_client_read_cb , ctdb , " to-ctdbd " ) ;
2007-04-11 05:02:26 +04:00
return 0 ;
}
2007-04-18 05:20:24 +04:00
struct ctdb_record_handle {
struct ctdb_db_context * ctdb_db ;
TDB_DATA key ;
TDB_DATA * data ;
struct ctdb_ltdb_header header ;
} ;
2007-04-11 05:02:26 +04:00
2007-04-19 05:28:01 +04:00
2007-04-11 05:02:26 +04:00
/*
make a recv call to the local ctdb daemon - called from client context
2018-04-30 12:36:43 +03:00
This is called when the program wants to wait for a ctdb_call to complete and get the
2007-04-11 05:02:26 +04:00
results . This call will block unless the call has already completed .
*/
2007-04-19 05:28:01 +04:00
int ctdb_call_recv ( struct ctdb_client_call_state * state , struct ctdb_call * call )
2007-04-11 05:02:26 +04:00
{
2007-08-23 05:58:09 +04:00
if ( state = = NULL ) {
return - 1 ;
}
2007-04-11 05:02:26 +04:00
while ( state - > state < CTDB_CALL_DONE ) {
2015-10-26 08:50:09 +03:00
tevent_loop_once ( state - > ctdb_db - > ctdb - > ev ) ;
2007-04-11 05:02:26 +04:00
}
if ( state - > state ! = CTDB_CALL_DONE ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_call_recv failed \n " ) ) ;
2007-04-11 05:02:26 +04:00
talloc_free ( state ) ;
return - 1 ;
}
2008-03-19 05:54:17 +03:00
if ( state - > call - > reply_data . dsize ) {
2007-04-19 05:28:01 +04:00
call - > reply_data . dptr = talloc_memdup ( state - > ctdb_db ,
2008-03-19 05:54:17 +03:00
state - > call - > reply_data . dptr ,
state - > call - > reply_data . dsize ) ;
call - > reply_data . dsize = state - > call - > reply_data . dsize ;
2007-04-11 05:02:26 +04:00
} else {
call - > reply_data . dptr = NULL ;
call - > reply_data . dsize = 0 ;
}
2008-03-19 05:54:17 +03:00
call - > status = state - > call - > status ;
2007-04-11 05:02:26 +04:00
talloc_free ( state ) ;
2011-07-20 07:30:12 +04:00
return call - > status ;
2007-04-11 05:02:26 +04:00
}
/*
destroy a ctdb_call in client
*/
2018-04-30 12:36:43 +03:00
static int ctdb_client_call_destructor ( struct ctdb_client_call_state * state )
2007-04-11 05:02:26 +04:00
{
2015-03-17 06:30:18 +03:00
reqid_remove ( state - > ctdb_db - > ctdb - > idr , state - > reqid ) ;
2007-04-11 05:02:26 +04:00
return 0 ;
}
2007-04-19 05:28:01 +04:00
/*
construct an event driven local ctdb_call
this is used so that locally processed ctdb_call requests are processed
in an event driven manner
*/
2018-04-30 12:36:43 +03:00
static struct ctdb_client_call_state * ctdb_client_call_local_send ( struct ctdb_db_context * ctdb_db ,
2007-04-19 05:28:01 +04:00
struct ctdb_call * call ,
struct ctdb_ltdb_header * header ,
TDB_DATA * data )
{
struct ctdb_client_call_state * state ;
struct ctdb_context * ctdb = ctdb_db - > ctdb ;
int ret ;
state = talloc_zero ( ctdb_db , struct ctdb_client_call_state ) ;
CTDB_NO_MEMORY_NULL ( ctdb , state ) ;
2008-03-19 05:54:17 +03:00
state - > call = talloc_zero ( state , struct ctdb_call ) ;
CTDB_NO_MEMORY_NULL ( ctdb , state - > call ) ;
2007-04-19 05:28:01 +04:00
talloc_steal ( state , data - > dptr ) ;
2008-03-19 05:54:17 +03:00
state - > state = CTDB_CALL_DONE ;
* ( state - > call ) = * call ;
2007-04-19 05:28:01 +04:00
state - > ctdb_db = ctdb_db ;
2013-08-19 09:04:46 +04:00
ret = ctdb_call_local ( ctdb_db , state - > call , header , state , data , true ) ;
2011-11-09 08:20:07 +04:00
if ( ret ! = 0 ) {
DEBUG ( DEBUG_DEBUG , ( " ctdb_call_local() failed, ignoring return code %d \n " , ret ) ) ;
}
2007-04-11 05:02:26 +04:00
2007-04-19 05:28:01 +04:00
return state ;
}
2007-04-11 05:02:26 +04:00
/*
make a ctdb call to the local daemon - async send . Called from client context .
2018-04-30 12:36:43 +03:00
This constructs a ctdb_call request and queues it for processing .
2007-04-11 05:02:26 +04:00
This call never blocks .
*/
2018-04-30 12:36:43 +03:00
struct ctdb_client_call_state * ctdb_call_send ( struct ctdb_db_context * ctdb_db ,
2008-01-08 13:28:42 +03:00
struct ctdb_call * call )
2007-04-11 05:02:26 +04:00
{
2007-04-19 05:28:01 +04:00
struct ctdb_client_call_state * state ;
2007-04-11 05:02:26 +04:00
struct ctdb_context * ctdb = ctdb_db - > ctdb ;
struct ctdb_ltdb_header header ;
TDB_DATA data ;
int ret ;
size_t len ;
2015-10-29 08:26:29 +03:00
struct ctdb_req_call_old * c ;
2007-04-11 05:02:26 +04:00
/* if the domain socket is not yet open, open it */
if ( ctdb - > daemon . sd = = - 1 ) {
2007-04-20 14:07:47 +04:00
ctdb_socket_connect ( ctdb ) ;
2007-04-11 05:02:26 +04:00
}
2007-04-19 05:28:01 +04:00
ret = ctdb_ltdb_lock ( ctdb_db , call - > key ) ;
if ( ret ! = 0 ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " Failed to get chainlock \n " ) ) ;
2007-04-19 05:28:01 +04:00
return NULL ;
}
2007-04-11 05:02:26 +04:00
ret = ctdb_ltdb_fetch ( ctdb_db , call - > key , & header , ctdb_db , & data ) ;
2011-07-21 09:59:37 +04:00
if ( ( call - > flags & CTDB_IMMEDIATE_MIGRATION ) & & ( header . flags & CTDB_REC_RO_HAVE_DELEGATIONS ) ) {
ret = - 1 ;
}
2007-09-04 04:06:36 +04:00
if ( ret = = 0 & & header . dmaster = = ctdb - > pnn ) {
2007-04-19 05:28:01 +04:00
state = ctdb_client_call_local_send ( ctdb_db , call , & header , & data ) ;
2007-04-18 05:20:24 +04:00
talloc_free ( data . dptr ) ;
2007-04-19 05:28:01 +04:00
ctdb_ltdb_unlock ( ctdb_db , call - > key ) ;
2007-04-11 05:02:26 +04:00
return state ;
}
2007-04-19 05:28:01 +04:00
ctdb_ltdb_unlock ( ctdb_db , call - > key ) ;
talloc_free ( data . dptr ) ;
state = talloc_zero ( ctdb_db , struct ctdb_client_call_state ) ;
2007-04-11 05:02:26 +04:00
if ( state = = NULL ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " failed to allocate state \n " ) ) ;
2007-04-11 05:02:26 +04:00
return NULL ;
}
2008-03-19 05:54:17 +03:00
state - > call = talloc_zero ( state , struct ctdb_call ) ;
if ( state - > call = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " failed to allocate state->call \n " ) ) ;
return NULL ;
}
2007-04-11 05:02:26 +04:00
2015-10-29 08:26:29 +03:00
len = offsetof ( struct ctdb_req_call_old , data ) + call - > key . dsize + call - > call_data . dsize ;
c = ctdbd_allocate_pkt ( ctdb , state , CTDB_REQ_CALL , len , struct ctdb_req_call_old ) ;
2007-04-19 05:28:01 +04:00
if ( c = = NULL ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " failed to allocate packet \n " ) ) ;
2007-04-11 05:02:26 +04:00
return NULL ;
}
2007-04-19 05:28:01 +04:00
2015-03-17 06:30:18 +03:00
state - > reqid = reqid_new ( ctdb - > idr , state ) ;
2007-05-10 11:43:45 +04:00
state - > ctdb_db = ctdb_db ;
talloc_set_destructor ( state , ctdb_client_call_destructor ) ;
c - > hdr . reqid = state - > reqid ;
2007-04-19 05:28:01 +04:00
c - > flags = call - > flags ;
c - > db_id = ctdb_db - > db_id ;
c - > callid = call - > call_id ;
2007-05-01 07:25:02 +04:00
c - > hopcount = 0 ;
2007-04-19 05:28:01 +04:00
c - > keylen = call - > key . dsize ;
c - > calldatalen = call - > call_data . dsize ;
memcpy ( & c - > data [ 0 ] , call - > key . dptr , call - > key . dsize ) ;
2018-04-30 12:36:43 +03:00
memcpy ( & c - > data [ call - > key . dsize ] ,
2007-04-11 05:02:26 +04:00
call - > call_data . dptr , call - > call_data . dsize ) ;
2008-03-19 05:54:17 +03:00
* ( state - > call ) = * call ;
state - > call - > call_data . dptr = & c - > data [ call - > key . dsize ] ;
state - > call - > key . dptr = & c - > data [ 0 ] ;
2007-04-11 05:02:26 +04:00
state - > state = CTDB_CALL_WAIT ;
2007-04-19 05:28:01 +04:00
ctdb_client_queue_pkt ( ctdb , & c - > hdr ) ;
2007-04-11 05:02:26 +04:00
return state ;
}
2007-04-11 05:58:28 +04:00
2007-04-17 08:52:51 +04:00
/*
full ctdb_call . Equivalent to a ctdb_call_send ( ) followed by a ctdb_call_recv ( )
*/
int ctdb_call ( struct ctdb_db_context * ctdb_db , struct ctdb_call * call )
{
2007-04-19 05:28:01 +04:00
struct ctdb_client_call_state * state ;
2007-04-17 08:52:51 +04:00
2008-01-08 13:28:42 +03:00
state = ctdb_call_send ( ctdb_db , call ) ;
2007-04-17 08:52:51 +04:00
return ctdb_call_recv ( state , call ) ;
}
2007-04-11 05:58:28 +04:00
/*
tell the daemon what messaging srvid we will use , and register the message
handler function in the client
*/
2015-04-08 07:41:12 +03:00
int ctdb_client_set_message_handler ( struct ctdb_context * ctdb , uint64_t srvid ,
srvid_handler_fn handler ,
void * private_data )
2007-04-11 05:58:28 +04:00
{
int res ;
2007-05-04 05:41:29 +04:00
int32_t status ;
2013-08-23 10:49:46 +04:00
2015-04-08 07:41:12 +03:00
res = ctdb_control ( ctdb , CTDB_CURRENT_NODE , srvid ,
CTDB_CONTROL_REGISTER_SRVID , 0 ,
2007-05-12 15:25:26 +04:00
tdb_null , NULL , NULL , & status , NULL , NULL ) ;
2007-05-04 05:41:29 +04:00
if ( res ! = 0 | | status ! = 0 ) {
2015-04-08 07:41:12 +03:00
DEBUG ( DEBUG_ERR ,
( " Failed to register srvid %llu \n " ,
( unsigned long long ) srvid ) ) ;
2007-05-04 05:41:29 +04:00
return - 1 ;
2007-04-11 07:43:15 +04:00
}
2007-05-04 05:41:29 +04:00
/* also need to register the handler with our own ctdb structure */
2015-04-08 07:38:26 +03:00
return srvid_register ( ctdb - > srv , ctdb , srvid , handler , private_data ) ;
2007-05-04 05:41:29 +04:00
}
2007-04-11 05:58:28 +04:00
2007-05-04 05:41:29 +04:00
/*
tell the daemon we no longer want a srvid
*/
2015-04-08 07:41:12 +03:00
int ctdb_client_remove_message_handler ( struct ctdb_context * ctdb ,
uint64_t srvid , void * private_data )
2007-05-04 05:41:29 +04:00
{
int res ;
int32_t status ;
2013-08-23 10:49:46 +04:00
2015-04-08 07:41:12 +03:00
res = ctdb_control ( ctdb , CTDB_CURRENT_NODE , srvid ,
CTDB_CONTROL_DEREGISTER_SRVID , 0 ,
2007-05-12 15:25:26 +04:00
tdb_null , NULL , NULL , & status , NULL , NULL ) ;
2007-05-04 05:41:29 +04:00
if ( res ! = 0 | | status ! = 0 ) {
2015-04-08 07:41:12 +03:00
DEBUG ( DEBUG_ERR ,
( " Failed to deregister srvid %llu \n " ,
( unsigned long long ) srvid ) ) ;
2007-05-04 05:41:29 +04:00
return - 1 ;
2007-04-11 05:58:28 +04:00
}
2007-05-04 05:41:29 +04:00
/* also need to register the handler with our own ctdb structure */
2015-04-08 07:38:26 +03:00
srvid_deregister ( ctdb - > srv , srvid , private_data ) ;
2007-05-04 05:41:29 +04:00
return 0 ;
2007-04-11 05:58:28 +04:00
}
2007-04-11 08:54:47 +04:00
/*
send a message - from client context
*/
2010-06-02 03:45:21 +04:00
int ctdb_client_send_message ( struct ctdb_context * ctdb , uint32_t pnn ,
2007-04-27 18:31:45 +04:00
uint64_t srvid , TDB_DATA data )
2007-04-11 07:43:15 +04:00
{
2015-10-29 08:36:30 +03:00
struct ctdb_req_message_old * r ;
2007-04-11 07:43:15 +04:00
int len , res ;
2015-10-29 08:36:30 +03:00
len = offsetof ( struct ctdb_req_message_old , data ) + data . dsize ;
2018-04-30 12:36:43 +03:00
r = ctdbd_allocate_pkt ( ctdb , ctdb , CTDB_REQ_MESSAGE ,
2015-10-29 08:36:30 +03:00
len , struct ctdb_req_message_old ) ;
2007-04-11 07:43:15 +04:00
CTDB_NO_MEMORY ( ctdb , r ) ;
2007-09-04 04:42:20 +04:00
r - > hdr . destnode = pnn ;
2007-04-11 07:43:15 +04:00
r - > srvid = srvid ;
r - > datalen = data . dsize ;
memcpy ( & r - > data [ 0 ] , data . dptr , data . dsize ) ;
2018-04-30 12:36:43 +03:00
2007-04-11 07:43:15 +04:00
res = ctdb_client_queue_pkt ( ctdb , & r - > hdr ) ;
talloc_free ( r ) ;
2012-11-19 14:13:03 +04:00
return res ;
2007-04-11 07:43:15 +04:00
}
2007-04-11 08:54:47 +04:00
2007-08-23 13:27:09 +04:00
/*
called when a control completes or timesout to invoke the callback
function the user provided
*/
2015-10-26 08:50:09 +03:00
static void invoke_control_callback ( struct tevent_context * ev ,
struct tevent_timer * te ,
struct timeval t , void * private_data )
2007-08-23 13:27:09 +04:00
{
struct ctdb_client_control_state * state ;
TALLOC_CTX * tmp_ctx = talloc_new ( NULL ) ;
int ret ;
state = talloc_get_type ( private_data , struct ctdb_client_control_state ) ;
talloc_steal ( tmp_ctx , state ) ;
2007-08-24 04:54:34 +04:00
ret = ctdb_control_recv ( state - > ctdb , state , state ,
2018-04-30 12:36:43 +03:00
NULL ,
NULL ,
2007-08-24 04:54:34 +04:00
NULL ) ;
2011-11-09 08:20:07 +04:00
if ( ret ! = 0 ) {
DEBUG ( DEBUG_DEBUG , ( " ctdb_control_recv() failed, ignoring return code %d \n " , ret ) ) ;
}
2007-08-23 13:27:09 +04:00
talloc_free ( tmp_ctx ) ;
}
2007-04-26 16:27:49 +04:00
/*
called when a CTDB_REPLY_CONTROL packet comes in in the client
This packet comes in response to a CTDB_REQ_CONTROL request packet . It
contains any reply data from the control
*/
2018-04-30 12:36:43 +03:00
static void ctdb_client_reply_control ( struct ctdb_context * ctdb ,
2007-04-26 16:27:49 +04:00
struct ctdb_req_header * hdr )
{
2015-10-29 08:44:08 +03:00
struct ctdb_reply_control_old * c = ( struct ctdb_reply_control_old * ) hdr ;
2007-04-26 16:27:49 +04:00
struct ctdb_client_control_state * state ;
2015-03-17 06:30:18 +03:00
state = reqid_find ( ctdb - > idr , hdr - > reqid , struct ctdb_client_control_state ) ;
2007-04-26 16:27:49 +04:00
if ( state = = NULL ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " reqid %u not found \n " , hdr - > reqid ) ) ;
2007-04-26 16:27:49 +04:00
return ;
}
if ( hdr - > reqid ! = state - > reqid ) {
/* we found a record but it was the wrong one */
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( " Dropped orphaned reply control with reqid:%u \n " , hdr - > reqid ) ) ;
2007-04-26 16:27:49 +04:00
return ;
}
state - > outdata . dptr = c - > data ;
state - > outdata . dsize = c - > datalen ;
state - > status = c - > status ;
2007-05-12 15:25:26 +04:00
if ( c - > errorlen ) {
2018-04-30 12:36:43 +03:00
state - > errormsg = talloc_strndup ( state ,
( char * ) & c - > data [ c - > datalen ] ,
2007-05-12 15:25:26 +04:00
c - > errorlen ) ;
}
2007-04-26 16:27:49 +04:00
2015-07-27 00:02:57 +03:00
/* state->outdata now uses resources from c so we don't want c
2019-10-26 03:41:08 +03:00
to just disappear from under us while state is still alive
2007-08-24 03:34:04 +04:00
*/
2007-04-26 16:27:49 +04:00
talloc_steal ( state , c ) ;
2007-08-23 03:53:10 +04:00
state - > state = CTDB_CONTROL_DONE ;
2007-08-23 13:27:09 +04:00
/* if we had a callback registered for this control, pull the response
and call the callback .
*/
2007-08-24 04:42:06 +04:00
if ( state - > async . fn ) {
2015-10-26 08:50:09 +03:00
tevent_add_timer ( ctdb - > ev , state , timeval_zero ( ) ,
invoke_control_callback , state ) ;
2007-08-23 13:27:09 +04:00
}
2007-04-26 16:27:49 +04:00
}
2007-05-10 11:43:45 +04:00
/*
destroy a ctdb_control in client
*/
2011-11-11 07:02:09 +04:00
static int ctdb_client_control_destructor ( struct ctdb_client_control_state * state )
2007-05-10 11:43:45 +04:00
{
2015-03-17 06:30:18 +03:00
reqid_remove ( state - > ctdb - > idr , state - > reqid ) ;
2007-05-10 11:43:45 +04:00
return 0 ;
}
2007-08-23 05:58:09 +04:00
/* time out handler for ctdb_control */
2015-10-26 08:50:09 +03:00
static void control_timeout_func ( struct tevent_context * ev ,
struct tevent_timer * te ,
struct timeval t , void * private_data )
2007-08-23 05:58:09 +04:00
{
struct ctdb_client_control_state * state = talloc_get_type ( private_data , struct ctdb_client_control_state ) ;
2009-10-29 21:49:10 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " control timed out. reqid:%u opcode:%u "
" dstnode:%u \n " , state - > reqid , state - > c - > opcode ,
state - > c - > hdr . destnode ) ) ;
2007-08-23 07:00:10 +04:00
2007-08-23 05:58:09 +04:00
state - > state = CTDB_CONTROL_TIMEOUT ;
2007-08-23 13:27:09 +04:00
/* if we had a callback registered for this control, pull the response
and call the callback .
*/
2007-08-24 04:42:06 +04:00
if ( state - > async . fn ) {
2015-10-26 08:50:09 +03:00
tevent_add_timer ( state - > ctdb - > ev , state , timeval_zero ( ) ,
invoke_control_callback , state ) ;
2007-08-23 13:27:09 +04:00
}
2007-08-23 05:58:09 +04:00
}
/* async version of send control request */
2018-04-30 12:36:43 +03:00
struct ctdb_client_control_state * ctdb_control_send ( struct ctdb_context * ctdb ,
uint32_t destnode , uint64_t srvid ,
uint32_t opcode , uint32_t flags , TDB_DATA data ,
2008-01-16 02:23:26 +03:00
TALLOC_CTX * mem_ctx ,
2007-08-23 05:58:09 +04:00
struct timeval * timeout ,
2007-08-24 04:42:06 +04:00
char * * errormsg )
2007-04-26 16:27:49 +04:00
{
struct ctdb_client_control_state * state ;
size_t len ;
2015-10-29 08:42:05 +03:00
struct ctdb_req_control_old * c ;
2007-04-26 16:27:49 +04:00
int ret ;
2007-05-12 15:25:26 +04:00
if ( errormsg ) {
* errormsg = NULL ;
}
2007-04-26 16:27:49 +04:00
/* if the domain socket is not yet open, open it */
if ( ctdb - > daemon . sd = = - 1 ) {
ctdb_socket_connect ( ctdb ) ;
}
2007-08-23 13:27:09 +04:00
state = talloc_zero ( mem_ctx , struct ctdb_client_control_state ) ;
2007-08-23 05:58:09 +04:00
CTDB_NO_MEMORY_NULL ( ctdb , state ) ;
2007-04-26 16:27:49 +04:00
2007-08-23 13:27:09 +04:00
state - > ctdb = ctdb ;
2015-03-17 06:30:18 +03:00
state - > reqid = reqid_new ( ctdb - > idr , state ) ;
2007-08-23 13:27:09 +04:00
state - > state = CTDB_CONTROL_WAIT ;
state - > errormsg = NULL ;
2007-04-26 16:27:49 +04:00
2011-11-11 07:02:09 +04:00
talloc_set_destructor ( state , ctdb_client_control_destructor ) ;
2007-05-10 11:43:45 +04:00
2015-10-29 08:42:05 +03:00
len = offsetof ( struct ctdb_req_control_old , data ) + data . dsize ;
2018-04-30 12:36:43 +03:00
c = ctdbd_allocate_pkt ( ctdb , state , CTDB_REQ_CONTROL ,
2015-10-29 08:42:05 +03:00
len , struct ctdb_req_control_old ) ;
2018-04-30 12:36:43 +03:00
state - > c = c ;
2007-08-23 05:58:09 +04:00
CTDB_NO_MEMORY_NULL ( ctdb , c ) ;
2007-04-26 16:27:49 +04:00
c - > hdr . reqid = state - > reqid ;
c - > hdr . destnode = destnode ;
c - > opcode = opcode ;
2007-05-04 05:41:29 +04:00
c - > client_id = 0 ;
2007-04-30 17:31:40 +04:00
c - > flags = flags ;
2007-04-26 16:27:49 +04:00
c - > srvid = srvid ;
c - > datalen = data . dsize ;
if ( data . dsize ) {
memcpy ( & c - > data [ 0 ] , data . dptr , data . dsize ) ;
}
2007-08-23 13:27:09 +04:00
/* timeout */
if ( timeout & & ! timeval_is_zero ( timeout ) ) {
2015-10-26 08:50:09 +03:00
tevent_add_timer ( ctdb - > ev , state , * timeout ,
control_timeout_func , state ) ;
2007-08-23 13:27:09 +04:00
}
2007-04-26 16:27:49 +04:00
ret = ctdb_client_queue_pkt ( ctdb , & ( c - > hdr ) ) ;
if ( ret ! = 0 ) {
talloc_free ( state ) ;
2007-08-23 05:58:09 +04:00
return NULL ;
2007-04-26 16:27:49 +04:00
}
2007-04-30 17:31:40 +04:00
if ( flags & CTDB_CTRL_FLAG_NOREPLY ) {
talloc_free ( state ) ;
2007-08-23 05:58:09 +04:00
return NULL ;
2007-04-30 17:31:40 +04:00
}
2007-08-23 05:58:09 +04:00
return state ;
}
/* async version of receive control reply */
2018-04-30 12:36:43 +03:00
int ctdb_control_recv ( struct ctdb_context * ctdb ,
struct ctdb_client_control_state * state ,
2007-08-23 05:58:09 +04:00
TALLOC_CTX * mem_ctx ,
TDB_DATA * outdata , int32_t * status , char * * errormsg )
{
2008-01-06 04:38:01 +03:00
TALLOC_CTX * tmp_ctx ;
2008-07-04 11:15:06 +04:00
if ( status ! = NULL ) {
* status = - 1 ;
}
if ( errormsg ! = NULL ) {
* errormsg = NULL ;
}
2007-08-23 05:58:09 +04:00
if ( state = = NULL ) {
return - 1 ;
2007-05-04 02:30:18 +04:00
}
2007-08-23 05:58:09 +04:00
2008-01-06 04:38:01 +03:00
/* prevent double free of state */
tmp_ctx = talloc_new ( ctdb ) ;
talloc_steal ( tmp_ctx , state ) ;
2007-08-23 05:58:09 +04:00
/* loop one event at a time until we either timeout or the control
completes .
*/
while ( state - > state = = CTDB_CONTROL_WAIT ) {
2015-10-26 08:50:09 +03:00
tevent_loop_once ( ctdb - > ev ) ;
2007-04-26 16:27:49 +04:00
}
2007-08-24 04:42:06 +04:00
2007-08-23 05:58:09 +04:00
if ( state - > state ! = CTDB_CONTROL_DONE ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control_recv failed \n " ) ) ;
2007-08-24 04:42:06 +04:00
if ( state - > async . fn ) {
state - > async . fn ( state ) ;
}
2008-01-06 04:38:01 +03:00
talloc_free ( tmp_ctx ) ;
2007-08-23 05:58:09 +04:00
return - 1 ;
}
if ( state - > errormsg ) {
2016-07-20 07:41:13 +03:00
int s = ( state - > status = = 0 ? - 1 : state - > status ) ;
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( " ctdb_control error: '%s' \n " , state - > errormsg ) ) ;
2007-05-19 15:11:06 +04:00
if ( errormsg ) {
2007-08-23 05:58:09 +04:00
( * errormsg ) = talloc_move ( mem_ctx , & state - > errormsg ) ;
2007-05-19 15:11:06 +04:00
}
2007-08-24 04:42:06 +04:00
if ( state - > async . fn ) {
state - > async . fn ( state ) ;
}
2008-01-06 04:38:01 +03:00
talloc_free ( tmp_ctx ) ;
2016-07-20 07:41:13 +03:00
return s ;
2007-05-04 02:30:18 +04:00
}
2007-05-07 01:47:16 +04:00
2007-04-26 16:27:49 +04:00
if ( outdata ) {
* outdata = state - > outdata ;
2007-04-26 16:51:41 +04:00
outdata - > dptr = talloc_memdup ( mem_ctx , outdata - > dptr , outdata - > dsize ) ;
2007-04-26 16:27:49 +04:00
}
2007-08-23 05:58:09 +04:00
if ( status ) {
* status = state - > status ;
2007-05-12 15:25:26 +04:00
}
2007-08-24 04:42:06 +04:00
if ( state - > async . fn ) {
state - > async . fn ( state ) ;
}
2008-01-06 04:38:01 +03:00
talloc_free ( tmp_ctx ) ;
2007-08-23 05:58:09 +04:00
return 0 ;
}
2007-04-26 16:27:49 +04:00
2007-08-23 05:58:09 +04:00
/*
send a ctdb control message
timeout specifies how long we should wait for a reply .
if timeout is NULL we wait indefinitely
*/
2018-04-30 12:36:43 +03:00
int ctdb_control ( struct ctdb_context * ctdb , uint32_t destnode , uint64_t srvid ,
uint32_t opcode , uint32_t flags , TDB_DATA data ,
2007-08-23 05:58:09 +04:00
TALLOC_CTX * mem_ctx , TDB_DATA * outdata , int32_t * status ,
struct timeval * timeout ,
char * * errormsg )
{
struct ctdb_client_control_state * state ;
2018-04-30 12:36:43 +03:00
state = ctdb_control_send ( ctdb , destnode , srvid , opcode ,
2008-01-16 02:23:26 +03:00
flags , data , mem_ctx ,
2007-08-24 04:42:06 +04:00
timeout , errormsg ) ;
2013-04-22 18:21:02 +04:00
/* FIXME: Error conditions in ctdb_control_send return NULL without
2019-10-26 03:41:08 +03:00
* setting errormsg . So , there is no way to distinguish between success
2013-04-22 18:21:02 +04:00
* and failure when CTDB_CTRL_FLAG_NOREPLY is set */
if ( flags & CTDB_CTRL_FLAG_NOREPLY ) {
if ( status ! = NULL ) {
* status = 0 ;
}
return 0 ;
}
2018-04-30 12:36:43 +03:00
return ctdb_control_recv ( ctdb , state , mem_ctx , outdata , status ,
2007-08-23 05:58:09 +04:00
errormsg ) ;
2007-04-26 16:27:49 +04:00
}
2007-04-27 14:56:10 +04:00
/*
get vnn map from a remote node
*/
2007-05-04 03:45:53 +04:00
int ctdb_ctrl_getvnnmap ( struct ctdb_context * ctdb , struct timeval timeout , uint32_t destnode , TALLOC_CTX * mem_ctx , struct ctdb_vnn_map * * vnnmap )
2007-04-27 14:56:10 +04:00
{
int ret ;
2007-05-23 11:21:14 +04:00
TDB_DATA outdata ;
2007-05-03 05:06:24 +04:00
int32_t res ;
2007-05-10 02:13:19 +04:00
struct ctdb_vnn_map_wire * map ;
2007-04-27 14:56:10 +04:00
2018-04-30 12:36:43 +03:00
ret = ctdb_control ( ctdb , destnode , 0 ,
CTDB_CONTROL_GETVNNMAP , 0 , tdb_null ,
2007-05-12 15:25:26 +04:00
mem_ctx , & outdata , & res , & timeout , NULL ) ;
2007-04-27 14:56:10 +04:00
if ( ret ! = 0 | | res ! = 0 ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for getvnnmap failed \n " ) ) ;
2007-04-27 14:56:10 +04:00
return - 1 ;
}
2018-04-30 12:36:43 +03:00
2007-05-10 02:13:19 +04:00
map = ( struct ctdb_vnn_map_wire * ) outdata . dptr ;
if ( outdata . dsize < offsetof ( struct ctdb_vnn_map_wire , map ) | |
outdata . dsize ! = map - > size * sizeof ( uint32_t ) + offsetof ( struct ctdb_vnn_map_wire , map ) ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( " Bad vnn map size received in ctdb_ctrl_getvnnmap \n " ) ) ;
2007-05-10 02:13:19 +04:00
return - 1 ;
}
( * vnnmap ) = talloc ( mem_ctx , struct ctdb_vnn_map ) ;
CTDB_NO_MEMORY ( ctdb , * vnnmap ) ;
( * vnnmap ) - > generation = map - > generation ;
( * vnnmap ) - > size = map - > size ;
( * vnnmap ) - > map = talloc_array ( * vnnmap , uint32_t , map - > size ) ;
2007-04-27 14:56:10 +04:00
2007-05-10 02:13:19 +04:00
CTDB_NO_MEMORY ( ctdb , ( * vnnmap ) - > map ) ;
memcpy ( ( * vnnmap ) - > map , map - > map , sizeof ( uint32_t ) * map - > size ) ;
2007-05-23 11:21:14 +04:00
talloc_free ( outdata . dptr ) ;
2018-04-30 12:36:43 +03:00
2007-04-27 14:56:10 +04:00
return 0 ;
}
2007-08-23 07:00:10 +04:00
2007-08-23 13:27:09 +04:00
/*
get the recovery mode of a remote node
*/
2007-08-23 07:00:10 +04:00
struct ctdb_client_control_state *
ctdb_ctrl_getrecmode_send ( struct ctdb_context * ctdb , TALLOC_CTX * mem_ctx , struct timeval timeout , uint32_t destnode )
{
2018-04-30 12:36:43 +03:00
return ctdb_control_send ( ctdb , destnode , 0 ,
CTDB_CONTROL_GET_RECMODE , 0 , tdb_null ,
2008-01-16 02:23:26 +03:00
mem_ctx , & timeout , NULL ) ;
2007-08-23 07:00:10 +04:00
}
int ctdb_ctrl_getrecmode_recv ( struct ctdb_context * ctdb , TALLOC_CTX * mem_ctx , struct ctdb_client_control_state * state , uint32_t * recmode )
2007-04-29 16:51:56 +04:00
{
int ret ;
int32_t res ;
2007-08-23 07:00:10 +04:00
ret = ctdb_control_recv ( ctdb , state , mem_ctx , NULL , & res , NULL ) ;
2007-05-06 02:05:22 +04:00
if ( ret ! = 0 ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_ctrl_getrecmode_recv failed \n " ) ) ;
2007-04-29 16:51:56 +04:00
return - 1 ;
}
2007-08-23 07:00:10 +04:00
if ( recmode ) {
* recmode = ( uint32_t ) res ;
}
2007-04-29 16:51:56 +04:00
return 0 ;
}
2007-08-23 07:00:10 +04:00
int ctdb_ctrl_getrecmode ( struct ctdb_context * ctdb , TALLOC_CTX * mem_ctx , struct timeval timeout , uint32_t destnode , uint32_t * recmode )
{
struct ctdb_client_control_state * state ;
state = ctdb_ctrl_getrecmode_send ( ctdb , mem_ctx , timeout , destnode ) ;
return ctdb_ctrl_getrecmode_recv ( ctdb , mem_ctx , state , recmode ) ;
}
2007-04-29 16:51:56 +04:00
/*
set the recovery mode of a remote node
*/
2007-05-04 09:21:40 +04:00
int ctdb_ctrl_setrecmode ( struct ctdb_context * ctdb , struct timeval timeout , uint32_t destnode , uint32_t recmode )
2007-04-29 16:51:56 +04:00
{
int ret ;
2007-05-23 11:21:14 +04:00
TDB_DATA data ;
2007-04-29 16:51:56 +04:00
int32_t res ;
data . dsize = sizeof ( uint32_t ) ;
data . dptr = ( unsigned char * ) & recmode ;
2018-04-30 12:36:43 +03:00
ret = ctdb_control ( ctdb , destnode , 0 ,
CTDB_CONTROL_SET_RECMODE , 0 , data ,
2007-05-23 11:21:14 +04:00
NULL , NULL , & res , & timeout , NULL ) ;
2007-04-29 16:51:56 +04:00
if ( ret ! = 0 | | res ! = 0 ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for setrecmode failed \n " ) ) ;
2007-04-29 16:51:56 +04:00
return - 1 ;
}
return 0 ;
}
2007-08-23 13:27:09 +04:00
2007-04-28 14:40:26 +04:00
/*
get a list of nodes ( vnn and flags ) from a remote node
*/
2018-04-30 12:36:43 +03:00
int ctdb_ctrl_getnodemap ( struct ctdb_context * ctdb ,
struct timeval timeout , uint32_t destnode ,
2015-10-29 09:22:48 +03:00
TALLOC_CTX * mem_ctx , struct ctdb_node_map_old * * nodemap )
2007-04-28 14:40:26 +04:00
{
int ret ;
2007-05-23 11:21:14 +04:00
TDB_DATA outdata ;
2007-05-03 07:30:38 +04:00
int32_t res ;
2007-04-28 14:40:26 +04:00
2015-03-23 09:06:31 +03:00
ret = ctdb_control ( ctdb , destnode , 0 ,
CTDB_CONTROL_GET_NODEMAP , 0 , tdb_null ,
2007-05-12 15:25:26 +04:00
mem_ctx , & outdata , & res , & timeout , NULL ) ;
2007-09-05 08:59:29 +04:00
if ( ret ! = 0 | | res ! = 0 | | outdata . dsize = = 0 ) {
2008-10-14 17:24:44 +04:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for getnodes failed ret:%d res:%d \n " , ret , res ) ) ;
2007-04-28 14:40:26 +04:00
return - 1 ;
}
2015-10-29 09:22:48 +03:00
* nodemap = ( struct ctdb_node_map_old * ) talloc_memdup ( mem_ctx , outdata . dptr , outdata . dsize ) ;
2007-05-23 11:21:14 +04:00
talloc_free ( outdata . dptr ) ;
2007-04-28 14:40:26 +04:00
return 0 ;
}
2018-04-30 12:32:13 +03:00
int ctdb_ctrl_get_runstate ( struct ctdb_context * ctdb ,
struct timeval timeout ,
uint32_t destnode ,
uint32_t * runstate )
2015-02-20 13:19:01 +03:00
{
TDB_DATA outdata ;
int32_t res ;
2018-04-30 12:32:13 +03:00
int ret ;
2015-02-20 13:19:01 +03:00
2018-04-30 12:32:13 +03:00
ret = ctdb_control ( ctdb , destnode , 0 , CTDB_CONTROL_GET_RUNSTATE , 0 ,
tdb_null , ctdb , & outdata , & res , & timeout , NULL ) ;
if ( ret ! = 0 | | res ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " ctdb_control for get_runstate failed \n " ) ) ;
return ret ! = 0 ? ret : res ;
}
if ( outdata . dsize ! = sizeof ( uint32_t ) ) {
DEBUG ( DEBUG_ERR , ( " Invalid return data in get_runstate \n " ) ) ;
talloc_free ( outdata . dptr ) ;
2015-02-20 13:19:01 +03:00
return - 1 ;
}
2018-04-30 12:32:13 +03:00
if ( runstate ! = NULL ) {
* runstate = * ( uint32_t * ) outdata . dptr ;
}
2015-02-20 13:19:01 +03:00
talloc_free ( outdata . dptr ) ;
2007-05-04 09:21:40 +04:00
return 0 ;
}
2007-04-27 17:14:36 +04:00
/*
get debug level on a node
*/
2008-02-05 02:26:23 +03:00
int ctdb_ctrl_get_debuglevel ( struct ctdb_context * ctdb , uint32_t destnode , int32_t * level )
2007-04-27 17:14:36 +04:00
{
int ret ;
int32_t res ;
TDB_DATA data ;
2018-04-30 12:36:43 +03:00
ret = ctdb_control ( ctdb , destnode , 0 , CTDB_CONTROL_GET_DEBUG , 0 , tdb_null ,
2007-05-12 15:25:26 +04:00
ctdb , & data , & res , NULL , NULL ) ;
2007-04-27 17:14:36 +04:00
if ( ret ! = 0 | | res ! = 0 ) {
return - 1 ;
}
2008-02-05 02:26:23 +03:00
if ( data . dsize ! = sizeof ( int32_t ) ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( " Bad control reply size in ctdb_get_debuglevel (got %u) \n " ,
2007-05-29 07:58:41 +04:00
( unsigned ) data . dsize ) ) ;
2007-04-27 17:14:36 +04:00
return - 1 ;
}
2008-02-05 02:26:23 +03:00
* level = * ( int32_t * ) data . dptr ;
2007-04-27 17:14:36 +04:00
talloc_free ( data . dptr ) ;
return 0 ;
}
2016-07-22 08:00:14 +03:00
/* Freeze all databases */
int ctdb_ctrl_freeze ( struct ctdb_context * ctdb , struct timeval timeout ,
uint32_t destnode )
2007-08-27 04:31:22 +04:00
{
int ret ;
2016-07-21 05:47:16 +03:00
int32_t res ;
2007-08-27 04:31:22 +04:00
2016-07-22 08:00:14 +03:00
ret = ctdb_control ( ctdb , destnode , 0 ,
2016-07-21 05:47:16 +03:00
CTDB_CONTROL_FREEZE , 0 , tdb_null ,
NULL , NULL , & res , & timeout , NULL ) ;
if ( ret ! = 0 | | res ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " ctdb_ctrl_freeze_priority failed \n " ) ) ;
return - 1 ;
}
2007-08-27 04:31:22 +04:00
2016-07-21 05:47:16 +03:00
return 0 ;
2007-08-27 04:31:22 +04:00
}
2007-05-23 08:35:19 +04:00
/*
2007-09-04 04:38:48 +04:00
get pnn of a node , or - 1
2007-05-23 08:35:19 +04:00
*/
2007-09-04 04:38:48 +04:00
int ctdb_ctrl_getpnn ( struct ctdb_context * ctdb , struct timeval timeout , uint32_t destnode )
2007-05-23 08:35:19 +04:00
{
int ret ;
int32_t res ;
2018-04-30 12:36:43 +03:00
ret = ctdb_control ( ctdb , destnode , 0 ,
CTDB_CONTROL_GET_PNN , 0 , tdb_null ,
2007-05-23 08:35:19 +04:00
NULL , NULL , & res , & timeout , NULL ) ;
if ( ret ! = 0 ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for getpnn failed \n " ) ) ;
2007-05-23 08:35:19 +04:00
return - 1 ;
}
return res ;
}
2007-05-23 08:50:41 +04:00
2009-12-16 17:50:06 +03:00
int ctdb_ctrl_get_public_ips_flags ( struct ctdb_context * ctdb ,
struct timeval timeout , uint32_t destnode ,
TALLOC_CTX * mem_ctx ,
uint32_t flags ,
2015-10-28 09:16:24 +03:00
struct ctdb_public_ip_list_old * * ips )
2007-06-04 15:11:51 +04:00
{
int ret ;
TDB_DATA outdata ;
int32_t res ;
2015-03-23 09:06:31 +03:00
ret = ctdb_control ( ctdb , destnode , 0 ,
2009-12-16 17:50:06 +03:00
CTDB_CONTROL_GET_PUBLIC_IPS , flags , tdb_null ,
2007-06-04 15:11:51 +04:00
mem_ctx , & outdata , & res , & timeout , NULL ) ;
if ( ret ! = 0 | | res ! = 0 ) {
2015-03-23 09:06:31 +03:00
DEBUG ( DEBUG_ERR , ( __location__
" ctdb_control for getpublicips failed ret:%d res:%d \n " ,
ret , res ) ) ;
2007-06-04 15:11:51 +04:00
return - 1 ;
}
2015-10-28 09:16:24 +03:00
* ips = ( struct ctdb_public_ip_list_old * ) talloc_memdup ( mem_ctx , outdata . dptr , outdata . dsize ) ;
2007-06-04 15:11:51 +04:00
talloc_free ( outdata . dptr ) ;
2015-03-23 09:06:31 +03:00
2007-06-04 15:11:51 +04:00
return 0 ;
}
2009-12-16 17:50:06 +03:00
int ctdb_ctrl_get_public_ips ( struct ctdb_context * ctdb ,
struct timeval timeout , uint32_t destnode ,
TALLOC_CTX * mem_ctx ,
2015-10-28 09:16:24 +03:00
struct ctdb_public_ip_list_old * * ips )
2009-12-16 17:50:06 +03:00
{
return ctdb_ctrl_get_public_ips_flags ( ctdb , timeout ,
destnode , mem_ctx ,
0 , ips ) ;
}
2009-12-16 16:40:21 +03:00
int ctdb_ctrl_get_ifaces ( struct ctdb_context * ctdb ,
struct timeval timeout , uint32_t destnode ,
TALLOC_CTX * mem_ctx ,
2015-10-28 11:43:48 +03:00
struct ctdb_iface_list_old * * _ifaces )
2009-12-16 16:40:21 +03:00
{
2009-12-16 17:30:07 +03:00
int ret ;
TDB_DATA outdata ;
int32_t res ;
2015-10-28 11:43:48 +03:00
struct ctdb_iface_list_old * ifaces ;
2009-12-16 17:30:07 +03:00
uint32_t len ;
uint32_t i ;
ret = ctdb_control ( ctdb , destnode , 0 ,
CTDB_CONTROL_GET_IFACES , 0 , tdb_null ,
mem_ctx , & outdata , & res , & timeout , NULL ) ;
if ( ret ! = 0 | | res ! = 0 ) {
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for get ifaces "
" failed ret:%d res:%d \n " ,
ret , res ) ) ;
return - 1 ;
}
2015-10-28 11:43:48 +03:00
len = offsetof ( struct ctdb_iface_list_old , ifaces ) ;
2009-12-16 17:30:07 +03:00
if ( len > outdata . dsize ) {
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for get ifaces "
" returned invalid data with size %u > %u \n " ,
( unsigned int ) outdata . dsize ,
( unsigned int ) len ) ) ;
dump_data ( DEBUG_DEBUG , outdata . dptr , outdata . dsize ) ;
return - 1 ;
}
2015-10-28 11:43:48 +03:00
ifaces = ( struct ctdb_iface_list_old * ) outdata . dptr ;
2015-10-28 11:37:17 +03:00
len + = ifaces - > num * sizeof ( struct ctdb_iface ) ;
2009-12-16 17:30:07 +03:00
if ( len > outdata . dsize ) {
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for get ifaces "
" returned invalid data with size %u > %u \n " ,
( unsigned int ) outdata . dsize ,
( unsigned int ) len ) ) ;
dump_data ( DEBUG_DEBUG , outdata . dptr , outdata . dsize ) ;
return - 1 ;
}
/* make sure we null terminate the returned strings */
for ( i = 0 ; i < ifaces - > num ; i + + ) {
ifaces - > ifaces [ i ] . name [ CTDB_IFACE_SIZE ] = ' \0 ' ;
}
2015-10-28 11:43:48 +03:00
* _ifaces = ( struct ctdb_iface_list_old * ) talloc_memdup ( mem_ctx ,
2009-12-16 17:30:07 +03:00
outdata . dptr ,
outdata . dsize ) ;
talloc_free ( outdata . dptr ) ;
if ( * _ifaces = = NULL ) {
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for get ifaces "
" talloc_memdup size %u failed \n " ,
( unsigned int ) outdata . dsize ) ) ;
return - 1 ;
}
return 0 ;
2009-12-16 16:40:21 +03:00
}
2007-06-07 12:05:25 +04:00
/*
get all tunables
*/
2015-10-28 10:51:22 +03:00
int ctdb_ctrl_get_all_tunables ( struct ctdb_context * ctdb ,
struct timeval timeout ,
2007-06-07 12:05:25 +04:00
uint32_t destnode ,
2015-10-28 10:51:22 +03:00
struct ctdb_tunable_list * tunables )
2007-06-07 12:05:25 +04:00
{
TDB_DATA outdata ;
int ret ;
int32_t res ;
ret = ctdb_control ( ctdb , destnode , 0 , CTDB_CONTROL_GET_ALL_TUNABLES , 0 , tdb_null , ctdb ,
& outdata , & res , & timeout , NULL ) ;
if ( ret ! = 0 | | res ! = 0 ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for get all tunables failed \n " ) ) ;
2007-06-07 12:05:25 +04:00
return - 1 ;
}
if ( outdata . dsize ! = sizeof ( * tunables ) ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " bad data size %u in ctdb_ctrl_get_all_tunables should be %u \n " ,
2007-06-07 12:37:27 +04:00
( unsigned ) outdata . dsize , ( unsigned ) sizeof ( * tunables ) ) ) ;
2015-10-28 10:51:22 +03:00
return - 1 ;
2007-06-07 12:05:25 +04:00
}
2015-10-28 10:51:22 +03:00
* tunables = * ( struct ctdb_tunable_list * ) outdata . dptr ;
2007-06-07 12:05:25 +04:00
talloc_free ( outdata . dptr ) ;
return 0 ;
}
2007-06-07 16:06:19 +04:00
/*
set some ctdb flags
*/
void ctdb_set_flags ( struct ctdb_context * ctdb , unsigned flags )
{
ctdb - > flags | = flags ;
}
2010-06-02 04:25:31 +04:00
const char * ctdb_get_socketname ( struct ctdb_context * ctdb )
{
return ctdb - > daemon . name ;
}
2007-06-07 16:06:19 +04:00
/*
2007-09-04 04:18:44 +04:00
return the pnn of this node
2007-06-07 16:06:19 +04:00
*/
2007-09-04 04:18:44 +04:00
uint32_t ctdb_get_pnn ( struct ctdb_context * ctdb )
2007-06-07 16:06:19 +04:00
{
2007-09-04 04:06:36 +04:00
return ctdb - > pnn ;
2007-06-07 16:06:19 +04:00
}
2018-04-30 12:36:43 +03:00
/*
2008-01-29 05:59:28 +03:00
callback for the async helpers used when sending the same control
2019-10-26 03:41:08 +03:00
to multiple nodes in parallel .
2008-01-29 05:59:28 +03:00
*/
static void async_callback ( struct ctdb_client_control_state * state )
{
struct client_async_data * data = talloc_get_type ( state - > async . private_data , struct client_async_data ) ;
2008-05-06 09:42:59 +04:00
struct ctdb_context * ctdb = talloc_get_type ( state - > ctdb , struct ctdb_context ) ;
2008-01-29 05:59:28 +03:00
int ret ;
2008-05-06 09:42:59 +04:00
TDB_DATA outdata ;
2011-08-10 19:53:56 +04:00
int32_t res = - 1 ;
2008-05-06 09:42:59 +04:00
uint32_t destnode = state - > c - > hdr . destnode ;
2008-01-29 05:59:28 +03:00
2013-11-11 05:39:48 +04:00
outdata . dsize = 0 ;
outdata . dptr = NULL ;
2008-01-29 05:59:28 +03:00
/* one more node has responded with recmode data */
data - > count - - ;
/* if we failed to push the db, then return an error and let
the main loop try again .
*/
if ( state - > state ! = CTDB_CONTROL_DONE ) {
if ( ! data - > dont_log_errors ) {
2009-07-12 02:39:29 +04:00
DEBUG ( DEBUG_ERR , ( " Async operation failed with state %d, opcode:%u \n " , state - > state , data - > opcode ) ) ;
2008-01-29 05:59:28 +03:00
}
data - > fail_count + + ;
2013-05-23 10:09:38 +04:00
if ( state - > state = = CTDB_CONTROL_TIMEOUT ) {
2018-07-10 11:18:33 +03:00
res = - ETIMEDOUT ;
2013-05-23 10:09:38 +04:00
} else {
res = - 1 ;
}
2008-06-12 10:53:36 +04:00
if ( data - > fail_callback ) {
data - > fail_callback ( ctdb , destnode , res , outdata ,
data - > callback_data ) ;
}
2008-01-29 05:59:28 +03:00
return ;
}
2018-04-30 12:36:43 +03:00
2008-01-29 05:59:28 +03:00
state - > async . fn = NULL ;
2008-05-06 09:42:59 +04:00
ret = ctdb_control_recv ( ctdb , state , data , & outdata , & res , NULL ) ;
2008-01-29 05:59:28 +03:00
if ( ( ret ! = 0 ) | | ( res ! = 0 ) ) {
if ( ! data - > dont_log_errors ) {
2008-07-02 06:21:53 +04:00
DEBUG ( DEBUG_ERR , ( " Async operation failed with ret=%d res=%d opcode=%u \n " , ret , ( int ) res , data - > opcode ) ) ;
2008-01-29 05:59:28 +03:00
}
data - > fail_count + + ;
2008-06-12 10:53:36 +04:00
if ( data - > fail_callback ) {
data - > fail_callback ( ctdb , destnode , res , outdata ,
data - > callback_data ) ;
}
2008-01-29 05:59:28 +03:00
}
2008-05-06 09:42:59 +04:00
if ( ( ret = = 0 ) & & ( data - > callback ! = NULL ) ) {
2008-06-12 10:53:36 +04:00
data - > callback ( ctdb , destnode , res , outdata ,
data - > callback_data ) ;
2008-05-06 09:42:59 +04:00
}
2008-01-29 05:59:28 +03:00
}
void ctdb_client_async_add ( struct client_async_data * data , struct ctdb_client_control_state * state )
{
/* set up the callback functions */
state - > async . fn = async_callback ;
state - > async . private_data = data ;
2018-04-30 12:36:43 +03:00
2008-01-29 05:59:28 +03:00
/* one more control to wait for to complete */
data - > count + + ;
}
/* wait for up to the maximum number of seconds allowed
or until all nodes we expect a response from has replied
*/
int ctdb_client_async_wait ( struct ctdb_context * ctdb , struct client_async_data * data )
{
while ( data - > count > 0 ) {
2015-10-26 08:50:09 +03:00
tevent_loop_once ( ctdb - > ev ) ;
2008-01-29 05:59:28 +03:00
}
if ( data - > fail_count ! = 0 ) {
if ( ! data - > dont_log_errors ) {
2018-04-30 12:36:43 +03:00
DEBUG ( DEBUG_ERR , ( " Async wait failed - fail_count=%u \n " ,
2008-01-29 05:59:28 +03:00
data - > fail_count ) ) ;
}
return - 1 ;
}
return 0 ;
}
2018-04-30 12:36:43 +03:00
/*
2008-01-29 05:59:28 +03:00
perform a simple control on the listed nodes
The control cannot return data
*/
int ctdb_client_async_control ( struct ctdb_context * ctdb ,
enum ctdb_controls opcode ,
uint32_t * nodes ,
2009-10-12 05:08:39 +04:00
uint64_t srvid ,
2008-01-29 05:59:28 +03:00
struct timeval timeout ,
bool dont_log_errors ,
2008-05-06 09:42:59 +04:00
TDB_DATA data ,
2008-06-12 10:53:36 +04:00
client_async_callback client_callback ,
client_async_callback fail_callback ,
void * callback_data )
2008-01-29 05:59:28 +03:00
{
struct client_async_data * async_data ;
struct ctdb_client_control_state * state ;
int j , num_nodes ;
2008-05-06 09:42:59 +04:00
2008-01-29 05:59:28 +03:00
async_data = talloc_zero ( ctdb , struct client_async_data ) ;
CTDB_NO_MEMORY_FATAL ( ctdb , async_data ) ;
async_data - > dont_log_errors = dont_log_errors ;
2008-05-06 09:42:59 +04:00
async_data - > callback = client_callback ;
2008-06-12 10:53:36 +04:00
async_data - > fail_callback = fail_callback ;
async_data - > callback_data = callback_data ;
2008-07-02 06:21:53 +04:00
async_data - > opcode = opcode ;
2008-01-29 05:59:28 +03:00
num_nodes = talloc_get_size ( nodes ) / sizeof ( uint32_t ) ;
/* loop over all nodes and send an async control to each of them */
for ( j = 0 ; j < num_nodes ; j + + ) {
uint32_t pnn = nodes [ j ] ;
2018-04-30 12:36:43 +03:00
state = ctdb_control_send ( ctdb , pnn , srvid , opcode ,
2008-01-29 05:59:28 +03:00
0 , data , async_data , & timeout , NULL ) ;
if ( state = = NULL ) {
2008-02-04 12:07:15 +03:00
DEBUG ( DEBUG_ERR , ( __location__ " Failed to call async control %u \n " , ( unsigned ) opcode ) ) ;
2008-01-29 05:59:28 +03:00
talloc_free ( async_data ) ;
return - 1 ;
}
2018-04-30 12:36:43 +03:00
2008-01-29 05:59:28 +03:00
ctdb_client_async_add ( async_data , state ) ;
}
if ( ctdb_client_async_wait ( ctdb , async_data ) ! = 0 ) {
talloc_free ( async_data ) ;
return - 1 ;
}
talloc_free ( async_data ) ;
return 0 ;
}
uint32_t * list_of_vnnmap_nodes ( struct ctdb_context * ctdb ,
struct ctdb_vnn_map * vnn_map ,
TALLOC_CTX * mem_ctx ,
bool include_self )
{
2019-06-07 23:38:56 +03:00
unsigned int i , j , num_nodes ;
2008-01-29 05:59:28 +03:00
uint32_t * nodes ;
for ( i = num_nodes = 0 ; i < vnn_map - > size ; i + + ) {
if ( vnn_map - > map [ i ] = = ctdb - > pnn & & ! include_self ) {
continue ;
}
num_nodes + + ;
2018-04-30 12:36:43 +03:00
}
2008-01-29 05:59:28 +03:00
nodes = talloc_array ( mem_ctx , uint32_t , num_nodes ) ;
CTDB_NO_MEMORY_FATAL ( ctdb , nodes ) ;
for ( i = j = 0 ; i < vnn_map - > size ; i + + ) {
if ( vnn_map - > map [ i ] = = ctdb - > pnn & & ! include_self ) {
continue ;
}
nodes [ j + + ] = vnn_map - > map [ i ] ;
2018-04-30 12:36:43 +03:00
}
2008-01-29 05:59:28 +03:00
return nodes ;
}
2019-06-07 23:22:49 +03:00
/* Get list of nodes not including those with flags specified by mask */
2019-06-07 23:08:48 +03:00
static uint32_t * list_of_nodes ( struct ctdb_context * ctdb ,
struct ctdb_node_map_old * node_map ,
TALLOC_CTX * mem_ctx ,
uint32_t mask ,
2019-06-07 23:22:49 +03:00
bool include_self )
2013-02-19 07:29:06 +04:00
{
2019-06-07 23:38:56 +03:00
unsigned int i , j , num_nodes ;
2019-06-07 23:22:49 +03:00
uint32_t exclude_pnn ;
2013-02-19 07:29:06 +04:00
uint32_t * nodes ;
2019-06-07 23:22:49 +03:00
exclude_pnn = include_self ? CTDB_UNKNOWN_PNN : ctdb - > pnn ;
2013-02-19 07:29:06 +04:00
for ( i = num_nodes = 0 ; i < node_map - > num ; i + + ) {
if ( node_map - > nodes [ i ] . flags & mask ) {
continue ;
}
if ( node_map - > nodes [ i ] . pnn = = exclude_pnn ) {
continue ;
}
num_nodes + + ;
2018-04-30 12:36:43 +03:00
}
2013-02-19 07:29:06 +04:00
nodes = talloc_array ( mem_ctx , uint32_t , num_nodes ) ;
CTDB_NO_MEMORY_FATAL ( ctdb , nodes ) ;
for ( i = j = 0 ; i < node_map - > num ; i + + ) {
if ( node_map - > nodes [ i ] . flags & mask ) {
continue ;
}
if ( node_map - > nodes [ i ] . pnn = = exclude_pnn ) {
continue ;
}
nodes [ j + + ] = node_map - > nodes [ i ] . pnn ;
2018-04-30 12:36:43 +03:00
}
2013-02-19 07:29:06 +04:00
return nodes ;
}
2008-01-29 05:59:28 +03:00
uint32_t * list_of_active_nodes ( struct ctdb_context * ctdb ,
2015-10-29 09:22:48 +03:00
struct ctdb_node_map_old * node_map ,
2008-01-29 05:59:28 +03:00
TALLOC_CTX * mem_ctx ,
bool include_self )
{
2019-06-07 23:22:49 +03:00
return list_of_nodes ( ctdb ,
node_map ,
mem_ctx ,
NODE_FLAGS_INACTIVE ,
include_self ) ;
2008-01-29 05:59:28 +03:00
}
2008-02-29 04:37:42 +03:00
2008-11-19 06:43:46 +03:00
uint32_t * list_of_connected_nodes ( struct ctdb_context * ctdb ,
2015-10-29 09:22:48 +03:00
struct ctdb_node_map_old * node_map ,
2008-11-19 06:43:46 +03:00
TALLOC_CTX * mem_ctx ,
bool include_self )
{
2019-06-07 23:22:49 +03:00
return list_of_nodes ( ctdb ,
node_map ,
mem_ctx ,
NODE_FLAGS_DISCONNECTED ,
include_self ) ;
2008-11-19 06:43:46 +03:00
}
2008-05-06 04:02:27 +04:00
/*
get capabilities of a remote node
*/
struct ctdb_client_control_state *
ctdb_ctrl_getcapabilities_send ( struct ctdb_context * ctdb , TALLOC_CTX * mem_ctx , struct timeval timeout , uint32_t destnode )
{
2018-04-30 12:36:43 +03:00
return ctdb_control_send ( ctdb , destnode , 0 ,
CTDB_CONTROL_GET_CAPABILITIES , 0 , tdb_null ,
2008-05-06 04:02:27 +04:00
mem_ctx , & timeout , NULL ) ;
}
int ctdb_ctrl_getcapabilities_recv ( struct ctdb_context * ctdb , TALLOC_CTX * mem_ctx , struct ctdb_client_control_state * state , uint32_t * capabilities )
{
int ret ;
int32_t res ;
2008-05-06 09:42:59 +04:00
TDB_DATA outdata ;
2008-05-06 04:02:27 +04:00
2008-05-06 09:42:59 +04:00
ret = ctdb_control_recv ( ctdb , state , mem_ctx , & outdata , & res , NULL ) ;
if ( ( ret ! = 0 ) | | ( res ! = 0 ) ) {
2008-05-06 04:02:27 +04:00
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_ctrl_getcapabilities_recv failed \n " ) ) ;
return - 1 ;
}
if ( capabilities ) {
2008-05-06 09:42:59 +04:00
* capabilities = * ( ( uint32_t * ) outdata . dptr ) ;
2008-05-06 04:02:27 +04:00
}
return 0 ;
}
int ctdb_ctrl_getcapabilities ( struct ctdb_context * ctdb , struct timeval timeout , uint32_t destnode , uint32_t * capabilities )
{
struct ctdb_client_control_state * state ;
TALLOC_CTX * tmp_ctx = talloc_new ( NULL ) ;
int ret ;
state = ctdb_ctrl_getcapabilities_send ( ctdb , tmp_ctx , timeout , destnode ) ;
ret = ctdb_ctrl_getcapabilities_recv ( ctdb , tmp_ctx , state , capabilities ) ;
talloc_free ( tmp_ctx ) ;
return ret ;
}
2008-07-30 13:57:48 +04:00
2014-07-31 09:06:19 +04:00
static void get_capabilities_callback ( struct ctdb_context * ctdb ,
uint32_t node_pnn , int32_t res ,
TDB_DATA outdata , void * callback_data )
{
struct ctdb_node_capabilities * caps =
talloc_get_type ( callback_data ,
struct ctdb_node_capabilities ) ;
if ( ( outdata . dsize ! = sizeof ( uint32_t ) ) | | ( outdata . dptr = = NULL ) ) {
DEBUG ( DEBUG_ERR , ( __location__ " Invalid length/pointer for getcap callback : %u %p \n " , ( unsigned ) outdata . dsize , outdata . dptr ) ) ;
return ;
}
if ( node_pnn > = talloc_array_length ( caps ) ) {
DEBUG ( DEBUG_ERR ,
( __location__ " unexpected PNN %u \n " , node_pnn ) ) ;
return ;
}
caps [ node_pnn ] . retrieved = true ;
caps [ node_pnn ] . capabilities = * ( ( uint32_t * ) outdata . dptr ) ;
}
struct ctdb_node_capabilities *
ctdb_get_capabilities ( struct ctdb_context * ctdb ,
TALLOC_CTX * mem_ctx ,
struct timeval timeout ,
2015-10-29 09:22:48 +03:00
struct ctdb_node_map_old * nodemap )
2014-07-31 09:06:19 +04:00
{
uint32_t * nodes ;
uint32_t i , res ;
struct ctdb_node_capabilities * ret ;
2015-12-07 07:50:23 +03:00
nodes = list_of_active_nodes ( ctdb , nodemap , mem_ctx , true ) ;
2014-07-31 09:06:19 +04:00
ret = talloc_array ( mem_ctx , struct ctdb_node_capabilities ,
nodemap - > num ) ;
CTDB_NO_MEMORY_NULL ( ctdb , ret ) ;
/* Prepopulate the expected PNNs */
for ( i = 0 ; i < talloc_array_length ( ret ) ; i + + ) {
ret [ i ] . retrieved = false ;
}
res = ctdb_client_async_control ( ctdb , CTDB_CONTROL_GET_CAPABILITIES ,
nodes , 0 , timeout ,
false , tdb_null ,
get_capabilities_callback , NULL ,
ret ) ;
if ( res ! = 0 ) {
DEBUG ( DEBUG_ERR ,
( __location__ " Failed to read node capabilities. \n " ) ) ;
TALLOC_FREE ( ret ) ;
}
return ret ;
}
uint32_t *
ctdb_get_node_capabilities ( struct ctdb_node_capabilities * caps ,
uint32_t pnn )
{
if ( pnn < talloc_array_length ( caps ) & & caps [ pnn ] . retrieved ) {
return & caps [ pnn ] . capabilities ;
}
return NULL ;
}
bool ctdb_node_has_capabilities ( struct ctdb_node_capabilities * caps ,
uint32_t pnn ,
uint32_t capabilities_required )
{
uint32_t * capp = ctdb_get_node_capabilities ( caps , pnn ) ;
return ( capp ! = NULL ) & &
( ( * capp & capabilities_required ) = = capabilities_required ) ;
}
2018-04-30 12:32:13 +03:00
/*
recovery daemon ping to main daemon
*/
int ctdb_ctrl_recd_ping ( struct ctdb_context * ctdb )
2013-10-04 09:37:24 +04:00
{
2018-04-30 12:32:13 +03:00
int ret ;
int32_t res ;
2013-10-04 09:37:24 +04:00
2018-04-30 12:32:13 +03:00
ret = ctdb_control ( ctdb , CTDB_CURRENT_NODE , 0 , CTDB_CONTROL_RECD_PING , 0 , tdb_null ,
ctdb , NULL , & res , NULL , NULL ) ;
if ( ret ! = 0 | | res ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " Failed to send recd ping \n " ) ) ;
return - 1 ;
2013-10-04 09:37:24 +04:00
}
2018-04-30 12:32:13 +03:00
return 0 ;
2013-10-04 09:37:24 +04:00
}
2018-04-30 12:32:13 +03:00
/*
tell the main daemon how long it took to lock the reclock file
*/
int ctdb_ctrl_report_recd_lock_latency ( struct ctdb_context * ctdb , struct timeval timeout , double latency )
2013-10-04 09:37:24 +04:00
{
int ret ;
2018-04-30 12:32:13 +03:00
int32_t res ;
TDB_DATA data ;
data . dptr = ( uint8_t * ) & latency ;
data . dsize = sizeof ( latency ) ;
2013-10-04 09:37:24 +04:00
2018-04-30 12:32:13 +03:00
ret = ctdb_control ( ctdb , CTDB_CURRENT_NODE , 0 , CTDB_CONTROL_RECD_RECLOCK_LATENCY , 0 , data ,
ctdb , NULL , & res , NULL , NULL ) ;
if ( ret ! = 0 | | res ! = 0 ) {
DEBUG ( DEBUG_ERR , ( " Failed to send recd reclock latency \n " ) ) ;
return - 1 ;
2013-10-04 09:37:24 +04:00
}
2018-04-30 12:32:13 +03:00
return 0 ;
2013-10-04 09:37:24 +04:00
}
2018-04-30 12:32:13 +03:00
int ctdb_ctrl_set_ban ( struct ctdb_context * ctdb , struct timeval timeout ,
uint32_t destnode , struct ctdb_ban_state * bantime )
2013-10-04 09:38:04 +04:00
{
2018-04-30 12:32:13 +03:00
int ret ;
TDB_DATA data ;
int32_t res ;
2013-10-04 09:38:04 +04:00
2018-04-30 12:32:13 +03:00
data . dsize = sizeof ( * bantime ) ;
data . dptr = ( uint8_t * ) bantime ;
2013-10-04 09:38:04 +04:00
2018-04-30 12:32:13 +03:00
ret = ctdb_control ( ctdb , destnode , 0 ,
CTDB_CONTROL_SET_BAN_STATE , 0 , data ,
NULL , NULL , & res , & timeout , NULL ) ;
if ( ret ! = 0 | | res ! = 0 ) {
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_control for set ban state failed \n " ) ) ;
return - 1 ;
2013-10-04 09:38:04 +04:00
}
2018-04-30 12:32:13 +03:00
return 0 ;
2013-10-04 09:38:04 +04:00
}
2018-04-30 12:32:13 +03:00
struct ctdb_client_control_state *
ctdb_ctrl_updaterecord_send ( struct ctdb_context * ctdb , TALLOC_CTX * mem_ctx , struct timeval timeout , uint32_t destnode , struct ctdb_db_context * ctdb_db , TDB_DATA key , struct ctdb_ltdb_header * header , TDB_DATA data )
2013-10-04 09:38:04 +04:00
{
2018-04-30 12:32:13 +03:00
struct ctdb_client_control_state * handle ;
struct ctdb_marshall_buffer * m ;
struct ctdb_rec_data_old * rec ;
TDB_DATA outdata ;
2013-10-04 09:38:04 +04:00
2018-04-30 12:32:13 +03:00
m = talloc_zero ( mem_ctx , struct ctdb_marshall_buffer ) ;
if ( m = = NULL ) {
DEBUG ( DEBUG_ERR , ( " Failed to allocate marshall buffer for update record \n " ) ) ;
return NULL ;
2013-10-04 09:38:04 +04:00
}
2018-04-30 12:32:13 +03:00
m - > db_id = ctdb_db - > db_id ;
2013-10-04 09:38:04 +04:00
2018-04-30 12:32:13 +03:00
rec = ctdb_marshall_record ( m , 0 , key , header , data ) ;
if ( rec = = NULL ) {
DEBUG ( DEBUG_ERR , ( " Failed to marshall record for update record \n " ) ) ;
talloc_free ( m ) ;
return NULL ;
2013-10-04 09:38:04 +04:00
}
2018-04-30 12:32:13 +03:00
m = talloc_realloc_size ( mem_ctx , m , rec - > length + offsetof ( struct ctdb_marshall_buffer , data ) ) ;
if ( m = = NULL ) {
DEBUG ( DEBUG_CRIT , ( __location__ " Failed to expand recdata \n " ) ) ;
talloc_free ( m ) ;
return NULL ;
2013-10-04 09:38:04 +04:00
}
2018-04-30 12:32:13 +03:00
m - > count + + ;
memcpy ( ( uint8_t * ) m + offsetof ( struct ctdb_marshall_buffer , data ) , rec , rec - > length ) ;
2013-10-04 09:38:04 +04:00
2018-04-30 12:32:13 +03:00
outdata . dptr = ( uint8_t * ) m ;
outdata . dsize = talloc_get_size ( m ) ;
2013-10-04 09:38:04 +04:00
2018-04-30 12:32:13 +03:00
handle = ctdb_control_send ( ctdb , destnode , 0 ,
CTDB_CONTROL_UPDATE_RECORD , 0 , outdata ,
mem_ctx , & timeout , NULL ) ;
talloc_free ( m ) ;
return handle ;
2013-10-04 09:38:04 +04:00
}
2018-04-30 12:32:13 +03:00
int ctdb_ctrl_updaterecord_recv ( struct ctdb_context * ctdb , struct ctdb_client_control_state * state )
2013-10-04 09:38:04 +04:00
{
2018-04-30 12:32:13 +03:00
int ret ;
int32_t res ;
2013-10-04 09:38:04 +04:00
2018-04-30 12:32:13 +03:00
ret = ctdb_control_recv ( ctdb , state , state , NULL , & res , NULL ) ;
if ( ( ret ! = 0 ) | | ( res ! = 0 ) ) {
DEBUG ( DEBUG_ERR , ( __location__ " ctdb_ctrl_update_record_recv failed \n " ) ) ;
return - 1 ;
2013-10-04 09:38:04 +04:00
}
2013-09-23 12:30:04 +04:00
return 0 ;
}
2018-04-30 12:32:13 +03:00
int
ctdb_ctrl_updaterecord ( struct ctdb_context * ctdb , TALLOC_CTX * mem_ctx , struct timeval timeout , uint32_t destnode , struct ctdb_db_context * ctdb_db , TDB_DATA key , struct ctdb_ltdb_header * header , TDB_DATA data )
2013-09-23 12:30:04 +04:00
{
2018-04-30 12:32:13 +03:00
struct ctdb_client_control_state * state ;
2011-07-20 06:06:37 +04:00
state = ctdb_ctrl_updaterecord_send ( ctdb , mem_ctx , timeout , destnode , ctdb_db , key , header , data ) ;
return ctdb_ctrl_updaterecord_recv ( ctdb , state ) ;
}