2012-11-07 19:22:07 +04:00
/*
2007-06-10 21:02:09 +04:00
Unix SMB / CIFS implementation .
Database interface wrapper around ctdbd
2009-12-11 12:35:50 +03:00
Copyright ( C ) Volker Lendecke 2007 - 2009
Copyright ( C ) Michael Adam 2009
2008-08-24 14:46:26 +04:00
2007-06-10 21:02:09 +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-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2007-06-10 21:02:09 +04:00
( at your option ) any later version .
2008-08-24 14:46:26 +04:00
2007-06-10 21:02:09 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2008-08-24 14:46:26 +04:00
2007-06-10 21:02:09 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2007-06-10 21:02:09 +04:00
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2012-03-11 00:33:11 +04:00
# include "lib/tdb_wrap/tdb_wrap.h"
2011-05-05 13:25:29 +04:00
# include "util_tdb.h"
2012-05-11 23:36:48 +04:00
# include "dbwrap/dbwrap.h"
2011-10-25 18:32:12 +04:00
# include "dbwrap/dbwrap_ctdb.h"
2011-10-12 13:48:55 +04:00
# include "dbwrap/dbwrap_rbt.h"
2011-10-13 18:50:57 +04:00
# include "lib/param/param.h"
2011-10-12 13:48:55 +04:00
2017-06-09 09:48:21 +03:00
# include "ctdb/include/ctdb_protocol.h"
2008-01-16 12:09:48 +03:00
# include "ctdbd_conn.h"
2011-07-07 19:42:08 +04:00
# include "dbwrap/dbwrap.h"
2011-08-24 16:53:42 +04:00
# include "dbwrap/dbwrap_private.h"
2011-08-16 17:51:58 +04:00
# include "dbwrap/dbwrap_ctdb.h"
2009-12-03 19:29:54 +03:00
# include "g_lock.h"
2011-03-24 17:31:06 +03:00
# include "messages.h"
2017-06-16 14:00:59 +03:00
# include "messages_ctdb.h"
2015-10-01 13:28:21 +03:00
# include "lib/cluster_support.h"
2016-12-21 10:38:25 +03:00
# include "lib/util/tevent_ntstatus.h"
2007-06-10 21:02:09 +04:00
2008-08-07 10:20:05 +04:00
struct db_ctdb_transaction_handle {
struct db_ctdb_ctx * ctx ;
2009-10-22 16:37:51 +04:00
/*
2011-09-23 13:58:35 +04:00
* we store the writes done under a transaction :
2009-10-22 16:37:51 +04:00
*/
2008-08-07 10:20:05 +04:00
struct ctdb_marshall_buffer * m_write ;
2008-08-08 10:44:24 +04:00
uint32_t nesting ;
bool nested_cancel ;
2009-12-03 19:29:54 +03:00
char * lock_name ;
2008-08-07 10:20:05 +04:00
} ;
2007-06-10 21:02:09 +04:00
struct db_ctdb_ctx {
2008-08-07 13:14:16 +04:00
struct db_context * db ;
2007-06-10 21:02:09 +04:00
struct tdb_wrap * wtdb ;
2012-05-11 23:53:13 +04:00
uint32_t db_id ;
2008-08-07 10:20:05 +04:00
struct db_ctdb_transaction_handle * transaction ;
2009-12-03 19:29:54 +03:00
struct g_lock_ctx * lock_ctx ;
2014-01-15 01:17:32 +04:00
/* thresholds for warning messages */
int warn_unlock_msecs ;
int warn_migrate_msecs ;
int warn_migrate_attempts ;
int warn_locktime_msecs ;
2007-06-10 21:02:09 +04:00
} ;
struct db_ctdb_rec {
struct db_ctdb_ctx * ctdb_ctx ;
struct ctdb_ltdb_header header ;
2010-03-05 18:46:36 +03:00
struct timeval lock_time ;
2007-06-10 21:02:09 +04:00
} ;
2016-12-21 10:38:25 +03:00
struct ctdb_async_ctx {
bool initialized ;
struct ctdbd_connection * async_conn ;
} ;
static struct ctdb_async_ctx ctdb_async_ctx ;
static int ctdb_async_ctx_init_internal ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
bool reinit )
{
int ret ;
if ( reinit ) {
TALLOC_FREE ( ctdb_async_ctx . async_conn ) ;
ctdb_async_ctx . initialized = false ;
}
if ( ctdb_async_ctx . initialized ) {
return 0 ;
}
become_root ( ) ;
2020-03-11 13:03:06 +03:00
ret = ctdbd_init_async_connection (
mem_ctx ,
lp_ctdbd_socket ( ) ,
lp_ctdb_timeout ( ) ,
& ctdb_async_ctx . async_conn ) ;
2016-12-21 10:38:25 +03:00
unbecome_root ( ) ;
2019-07-09 17:13:13 +03:00
if ( ret ! = 0 | | ctdb_async_ctx . async_conn = = NULL ) {
2016-12-21 10:38:25 +03:00
DBG_ERR ( " ctdbd_init_connection failed \n " ) ;
return EIO ;
}
2017-05-05 23:30:32 +03:00
ctdb_async_ctx . initialized = true ;
2016-12-21 10:38:25 +03:00
return 0 ;
}
static int ctdb_async_ctx_init ( TALLOC_CTX * mem_ctx , struct tevent_context * ev )
{
return ctdb_async_ctx_init_internal ( mem_ctx , ev , false ) ;
}
int ctdb_async_ctx_reinit ( TALLOC_CTX * mem_ctx , struct tevent_context * ev )
{
return ctdb_async_ctx_init_internal ( mem_ctx , ev , true ) ;
}
2008-08-07 00:22:23 +04:00
static NTSTATUS tdb_error_to_ntstatus ( struct tdb_context * tdb )
{
enum TDB_ERROR tret = tdb_error ( tdb ) ;
2011-11-29 18:57:10 +04:00
return map_nt_error_from_tdb ( tret ) ;
2008-08-07 00:22:23 +04:00
}
2012-11-08 14:37:30 +04:00
struct db_ctdb_ltdb_parse_state {
void ( * parser ) ( TDB_DATA key , struct ctdb_ltdb_header * header ,
TDB_DATA data , void * private_data ) ;
void * private_data ;
} ;
static int db_ctdb_ltdb_parser ( TDB_DATA key , TDB_DATA data ,
void * private_data )
{
struct db_ctdb_ltdb_parse_state * state =
( struct db_ctdb_ltdb_parse_state * ) private_data ;
if ( data . dsize < sizeof ( struct ctdb_ltdb_header ) ) {
return - 1 ;
}
dbwrap_ctdb: avoid smbd/ctdb deadlocks: check whether we can work locally in db_ctdb_parse_record()
If the same process tries to re-lock the same record
it has already locked, don't go to the ctdbd again.
There are situations where we already have a lock on a record
and then do a dbwrap_parse_record() on that record, for instance
in locking code:
do_lock()
-> grabs lock on brl record with brl_get_locks()
-> calls brl_lock()
-> calls brl_lock_posix or _windows_default()
-> calls contend_level2_oplocks_begin()
-> calls brl_locks_get_read_only()
-> calls dbwrap_parse_record on the same brl record as above
In the local (tdb) case, this is not a problem, because
identical fcntl locks in the same process don't contend each other,
but calling out to ctdb for this lets smbd and ctdb deadlock.
db_ctdb_fetch_lock() already correclty checks first
whether we can simply try to lock locally. But db_ctdb_parse_record()
failed to do so for empty records, i.e. records that only
consist of the ctdb record header. (These can be deleted records
but can also be freshly created and still empty records.)
This patch lets db_ctdb_parse_record() not skip local access
for empty records, hence fixing the deadlock.
PLAN: In the long run, it would be better to solve this
generically on the dbwrap_layer, i.e. root the notion of
an already locked record there, and skip any call to the
db (tdb or ctdb backend) if we have it. This would also
solve the problem for all calls like fetch_locked, parse_record
and possibly others. But this is the urgent fix for now.
Pair-Programmed-With: Volker Lendecke <vl@samba.org>
Signed-off-by: Michael Adam <obnox@samba.org>
Signed-off-by: Volker Lendecke <vl@samba.org>
Tested-by: Björn Baumbach <bb@sernet.de>
2014-02-28 18:49:30 +04:00
2012-11-08 14:37:30 +04:00
state - > parser (
key , ( struct ctdb_ltdb_header * ) data . dptr ,
make_tdb_data ( data . dptr + sizeof ( struct ctdb_ltdb_header ) ,
data . dsize - sizeof ( struct ctdb_ltdb_header ) ) ,
state - > private_data ) ;
return 0 ;
}
static NTSTATUS db_ctdb_ltdb_parse (
struct db_ctdb_ctx * db , TDB_DATA key ,
void ( * parser ) ( TDB_DATA key , struct ctdb_ltdb_header * header ,
TDB_DATA data , void * private_data ) ,
void * private_data )
{
struct db_ctdb_ltdb_parse_state state ;
int ret ;
state . parser = parser ;
state . private_data = private_data ;
ret = tdb_parse_record ( db - > wtdb - > tdb , key , db_ctdb_ltdb_parser ,
& state ) ;
if ( ret = = - 1 ) {
return NT_STATUS_NOT_FOUND ;
}
return NT_STATUS_OK ;
}
2008-08-07 10:20:05 +04:00
2009-10-22 18:27:45 +04:00
/*
* Store a record together with the ctdb record header
* in the local copy of the database .
*/
static NTSTATUS db_ctdb_ltdb_store ( struct db_ctdb_ctx * db ,
TDB_DATA key ,
struct ctdb_ltdb_header * header ,
2016-09-13 15:22:05 +03:00
const TDB_DATA * dbufs , int num_dbufs )
2009-10-22 18:27:45 +04:00
{
2016-09-13 15:22:05 +03:00
TDB_DATA recs [ num_dbufs + 1 ] ;
2009-10-22 18:27:45 +04:00
int ret ;
2016-08-10 22:12:06 +03:00
recs [ 0 ] = ( TDB_DATA ) { . dptr = ( uint8_t * ) header ,
. dsize = sizeof ( struct ctdb_ltdb_header ) } ;
2016-09-13 15:22:05 +03:00
memcpy ( & recs [ 1 ] , dbufs , sizeof ( TDB_DATA ) * num_dbufs ) ;
2009-10-22 18:27:45 +04:00
2016-09-13 15:22:05 +03:00
ret = tdb_storev ( db - > wtdb - > tdb , key , recs , num_dbufs + 1 , TDB_REPLACE ) ;
2009-10-22 18:27:45 +04:00
return ( ret = = 0 ) ? NT_STATUS_OK
: tdb_error_to_ntstatus ( db - > wtdb - > tdb ) ;
}
2008-08-07 10:20:05 +04:00
/*
form a ctdb_rec_data record from a key / data pair
*/
2015-10-29 09:30:30 +03:00
static struct ctdb_rec_data_old * db_ctdb_marshall_record ( TALLOC_CTX * mem_ctx , uint32_t reqid ,
2012-11-07 19:22:07 +04:00
TDB_DATA key ,
2008-08-07 10:20:05 +04:00
struct ctdb_ltdb_header * header ,
TDB_DATA data )
{
size_t length ;
2015-10-29 09:30:30 +03:00
struct ctdb_rec_data_old * d ;
2008-08-07 10:20:05 +04:00
2015-10-29 09:30:30 +03:00
length = offsetof ( struct ctdb_rec_data_old , data ) + key . dsize +
2012-11-07 19:25:31 +04:00
data . dsize + sizeof ( * header ) ;
2015-10-29 09:30:30 +03:00
d = ( struct ctdb_rec_data_old * ) talloc_size ( mem_ctx , length ) ;
2008-08-07 10:20:05 +04:00
if ( d = = NULL ) {
return NULL ;
}
d - > length = length ;
d - > reqid = reqid ;
d - > keylen = key . dsize ;
memcpy ( & d - > data [ 0 ] , key . dptr , key . dsize ) ;
2012-11-07 19:25:31 +04:00
d - > datalen = data . dsize + sizeof ( * header ) ;
memcpy ( & d - > data [ key . dsize ] , header , sizeof ( * header ) ) ;
memcpy ( & d - > data [ key . dsize + sizeof ( * header ) ] , data . dptr , data . dsize ) ;
2008-08-07 10:20:05 +04:00
return d ;
}
/* helper function for marshalling multiple records */
2012-11-07 19:22:07 +04:00
static struct ctdb_marshall_buffer * db_ctdb_marshall_add ( TALLOC_CTX * mem_ctx ,
2008-08-07 10:20:05 +04:00
struct ctdb_marshall_buffer * m ,
2016-06-28 07:35:43 +03:00
uint32_t db_id ,
2008-08-07 10:20:05 +04:00
uint32_t reqid ,
TDB_DATA key ,
struct ctdb_ltdb_header * header ,
TDB_DATA data )
{
2015-10-29 09:30:30 +03:00
struct ctdb_rec_data_old * r ;
2008-08-07 10:20:05 +04:00
size_t m_size , r_size ;
2009-03-05 00:05:17 +03:00
struct ctdb_marshall_buffer * m2 = NULL ;
2008-08-07 10:20:05 +04:00
2009-03-05 00:05:17 +03:00
r = db_ctdb_marshall_record ( talloc_tos ( ) , reqid , key , header , data ) ;
2008-08-07 10:20:05 +04:00
if ( r = = NULL ) {
talloc_free ( m ) ;
return NULL ;
}
if ( m = = NULL ) {
2008-08-24 14:43:36 +04:00
m = ( struct ctdb_marshall_buffer * ) talloc_zero_size (
mem_ctx , offsetof ( struct ctdb_marshall_buffer , data ) ) ;
2008-08-07 10:20:05 +04:00
if ( m = = NULL ) {
2009-03-05 00:05:17 +03:00
goto done ;
2008-08-07 10:20:05 +04:00
}
m - > db_id = db_id ;
}
m_size = talloc_get_size ( m ) ;
r_size = talloc_get_size ( r ) ;
2008-08-24 14:43:36 +04:00
m2 = ( struct ctdb_marshall_buffer * ) talloc_realloc_size (
mem_ctx , m , m_size + r_size ) ;
2008-08-07 10:20:05 +04:00
if ( m2 = = NULL ) {
talloc_free ( m ) ;
2009-03-05 00:05:17 +03:00
goto done ;
2008-08-07 10:20:05 +04:00
}
memcpy ( m_size + ( uint8_t * ) m2 , r , r_size ) ;
m2 - > count + + ;
2009-03-05 00:05:17 +03:00
done :
talloc_free ( r ) ;
2008-08-07 10:20:05 +04:00
return m2 ;
}
/* we've finished marshalling, return a data blob with the marshalled records */
static TDB_DATA db_ctdb_marshall_finish ( struct ctdb_marshall_buffer * m )
{
TDB_DATA data ;
data . dptr = ( uint8_t * ) m ;
data . dsize = talloc_get_size ( m ) ;
return data ;
}
2012-11-10 17:46:10 +04:00
/*
loop over a marshalling buffer
- pass r = = NULL to start
- loop the number of times indicated by m - > count
*/
2015-10-29 09:30:30 +03:00
static struct ctdb_rec_data_old * db_ctdb_marshall_loop_next_key (
struct ctdb_marshall_buffer * m , struct ctdb_rec_data_old * r , TDB_DATA * key )
2012-11-10 17:46:10 +04:00
{
if ( r = = NULL ) {
2015-10-29 09:30:30 +03:00
r = ( struct ctdb_rec_data_old * ) & m - > data [ 0 ] ;
2012-11-10 17:46:10 +04:00
} else {
2015-10-29 09:30:30 +03:00
r = ( struct ctdb_rec_data_old * ) ( r - > length + ( uint8_t * ) r ) ;
2012-11-10 17:46:10 +04:00
}
key - > dptr = & r - > data [ 0 ] ;
key - > dsize = r - > keylen ;
return r ;
}
2012-11-10 18:03:35 +04:00
static bool db_ctdb_marshall_buf_parse (
2015-10-29 09:30:30 +03:00
struct ctdb_rec_data_old * r , uint32_t * reqid ,
2012-11-10 18:03:35 +04:00
struct ctdb_ltdb_header * * header , TDB_DATA * data )
2008-08-07 10:20:05 +04:00
{
2012-11-10 18:03:35 +04:00
if ( r - > datalen < sizeof ( struct ctdb_ltdb_header ) ) {
return false ;
2008-08-07 10:20:05 +04:00
}
2012-11-10 18:03:35 +04:00
* reqid = r - > reqid ;
2008-08-24 14:46:26 +04:00
2012-11-10 18:03:35 +04:00
data - > dptr = & r - > data [ r - > keylen ] + sizeof ( struct ctdb_ltdb_header ) ;
data - > dsize = r - > datalen - sizeof ( struct ctdb_ltdb_header ) ;
2008-08-07 10:20:05 +04:00
2012-11-10 18:03:35 +04:00
* header = ( struct ctdb_ltdb_header * ) & r - > data [ r - > keylen ] ;
2008-08-07 10:20:05 +04:00
2012-11-10 18:03:35 +04:00
return true ;
2008-08-07 10:20:05 +04:00
}
2009-05-25 23:59:40 +04:00
/**
* CTDB transaction destructor
*/
2008-08-07 10:20:05 +04:00
static int db_ctdb_transaction_destructor ( struct db_ctdb_transaction_handle * h )
{
2009-10-28 03:50:15 +03:00
NTSTATUS status ;
2008-08-07 10:20:05 +04:00
2017-12-03 22:47:02 +03:00
status = g_lock_unlock ( h - > ctx - > lock_ctx ,
string_term_tdb_data ( h - > lock_name ) ) ;
2009-10-28 03:54:04 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2011-09-05 19:07:37 +04:00
DEBUG ( 0 , ( " g_lock_unlock failed for %s: %s \n " , h - > lock_name ,
nt_errstr ( status ) ) ) ;
2009-10-28 03:54:04 +03:00
return - 1 ;
}
2008-08-07 10:20:05 +04:00
return 0 ;
}
2009-05-25 23:59:40 +04:00
/**
* CTDB dbwrap API : transaction_start function
* starts a transaction on a persistent database
*/
2008-08-07 10:20:05 +04:00
static int db_ctdb_transaction_start ( struct db_context * db )
{
struct db_ctdb_transaction_handle * h ;
2009-12-03 19:29:54 +03:00
NTSTATUS status ;
2008-08-07 10:20:05 +04:00
struct db_ctdb_ctx * ctx = talloc_get_type_abort ( db - > private_data ,
struct db_ctdb_ctx ) ;
if ( ! db - > persistent ) {
DEBUG ( 0 , ( " transactions not supported on non-persistent database 0x%08x \n " ,
ctx - > db_id ) ) ;
return - 1 ;
}
if ( ctx - > transaction ) {
2008-08-08 10:44:24 +04:00
ctx - > transaction - > nesting + + ;
2011-08-15 01:47:47 +04:00
DEBUG ( 5 , ( __location__ " transaction start on db 0x%08x: nesting %d -> %d \n " ,
ctx - > db_id , ctx - > transaction - > nesting - 1 , ctx - > transaction - > nesting ) ) ;
2008-08-08 10:44:24 +04:00
return 0 ;
2008-08-07 10:20:05 +04:00
}
h = talloc_zero ( db , struct db_ctdb_transaction_handle ) ;
if ( h = = NULL ) {
2012-11-07 19:22:07 +04:00
DEBUG ( 0 , ( __location__ " oom for transaction handle \n " ) ) ;
2008-08-07 10:20:05 +04:00
return - 1 ;
}
h - > ctx = ctx ;
2009-12-03 19:29:54 +03:00
h - > lock_name = talloc_asprintf ( h , " transaction_db_0x%08x " ,
( unsigned int ) ctx - > db_id ) ;
if ( h - > lock_name = = NULL ) {
DEBUG ( 0 , ( " talloc_asprintf failed \n " ) ) ;
TALLOC_FREE ( h ) ;
return - 1 ;
}
/*
* Wait a day , i . e . forever . . .
*/
2017-12-03 22:47:02 +03:00
status = g_lock_lock ( ctx - > lock_ctx , string_term_tdb_data ( h - > lock_name ) ,
G_LOCK_WRITE , timeval_set ( 86400 , 0 ) ) ;
2009-12-03 19:29:54 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " g_lock_lock failed: %s \n " , nt_errstr ( status ) ) ) ;
TALLOC_FREE ( h ) ;
2008-08-07 10:20:05 +04:00
return - 1 ;
}
talloc_set_destructor ( h , db_ctdb_transaction_destructor ) ;
ctx - > transaction = h ;
2011-08-15 01:47:47 +04:00
DEBUG ( 5 , ( __location__ " transaction started on db 0x%08x \n " , ctx - > db_id ) ) ;
2008-08-07 10:56:47 +04:00
2008-08-07 10:20:05 +04:00
return 0 ;
}
2012-11-12 15:13:39 +04:00
static bool parse_newest_in_marshall_buffer (
struct ctdb_marshall_buffer * buf , TDB_DATA key ,
void ( * parser ) ( TDB_DATA key , struct ctdb_ltdb_header * header ,
TDB_DATA data , void * private_data ) ,
void * private_data )
2009-12-03 19:29:54 +03:00
{
2015-10-29 09:30:30 +03:00
struct ctdb_rec_data_old * rec = NULL ;
2012-11-10 18:03:35 +04:00
struct ctdb_ltdb_header * h = NULL ;
2009-12-03 19:29:54 +03:00
TDB_DATA data ;
2016-04-11 16:45:49 +03:00
uint32_t i ;
2009-12-03 19:29:54 +03:00
if ( buf = = NULL ) {
return false ;
}
/*
* Walk the list of records written during this
* transaction . If we want to read one we have already
* written , return the last written sample . Thus we do not do
* a " break; " for the first hit , this record might have been
* overwritten later .
*/
for ( i = 0 ; i < buf - > count ; i + + ) {
2012-11-10 18:03:35 +04:00
TDB_DATA tkey ;
2009-12-03 19:29:54 +03:00
uint32_t reqid ;
2012-11-10 18:03:35 +04:00
rec = db_ctdb_marshall_loop_next_key ( buf , rec , & tkey ) ;
2009-12-03 19:29:54 +03:00
if ( rec = = NULL ) {
return false ;
}
2012-11-10 18:03:35 +04:00
if ( ! tdb_data_equal ( key , tkey ) ) {
continue ;
}
if ( ! db_ctdb_marshall_buf_parse ( rec , & reqid , & h , & data ) ) {
return false ;
2009-12-03 19:29:54 +03:00
}
}
2012-11-10 18:03:35 +04:00
if ( h = = NULL ) {
2009-12-03 19:29:54 +03:00
return false ;
}
2012-11-12 15:13:39 +04:00
parser ( key , h , data , private_data ) ;
return true ;
}
struct pull_newest_from_marshall_buffer_state {
struct ctdb_ltdb_header * pheader ;
TALLOC_CTX * mem_ctx ;
TDB_DATA * pdata ;
} ;
static void pull_newest_from_marshall_buffer_parser (
TDB_DATA key , struct ctdb_ltdb_header * header ,
TDB_DATA data , void * private_data )
{
struct pull_newest_from_marshall_buffer_state * state =
( struct pull_newest_from_marshall_buffer_state * ) private_data ;
2009-12-03 19:29:54 +03:00
2012-11-12 15:13:39 +04:00
if ( state - > pheader ! = NULL ) {
memcpy ( state - > pheader , header , sizeof ( * state - > pheader ) ) ;
2009-12-03 19:29:54 +03:00
}
2012-11-12 15:13:39 +04:00
if ( state - > pdata ! = NULL ) {
state - > pdata - > dsize = data . dsize ;
state - > pdata - > dptr = ( uint8_t * ) talloc_memdup (
state - > mem_ctx , data . dptr , data . dsize ) ;
}
}
static bool pull_newest_from_marshall_buffer ( struct ctdb_marshall_buffer * buf ,
TDB_DATA key ,
struct ctdb_ltdb_header * pheader ,
TALLOC_CTX * mem_ctx ,
TDB_DATA * pdata )
{
struct pull_newest_from_marshall_buffer_state state ;
state . pheader = pheader ;
state . mem_ctx = mem_ctx ;
state . pdata = pdata ;
2008-08-07 10:20:05 +04:00
2012-11-12 15:13:39 +04:00
if ( ! parse_newest_in_marshall_buffer (
buf , key , pull_newest_from_marshall_buffer_parser ,
& state ) ) {
return false ;
}
if ( ( pdata ! = NULL ) & & ( pdata - > dsize ! = 0 ) & & ( pdata - > dptr = = NULL ) ) {
/* ENOMEM */
return false ;
}
2009-12-03 19:29:54 +03:00
return true ;
}
2008-08-07 10:20:05 +04:00
2016-09-12 18:30:55 +03:00
static NTSTATUS db_ctdb_storev_transaction ( struct db_record * rec ,
const TDB_DATA * dbufs , int num_dbufs ,
int flag ) ;
2008-08-07 10:20:05 +04:00
static NTSTATUS db_ctdb_delete_transaction ( struct db_record * rec ) ;
static struct db_record * db_ctdb_fetch_locked_transaction ( struct db_ctdb_ctx * ctx ,
TALLOC_CTX * mem_ctx ,
TDB_DATA key )
{
struct db_record * result ;
TDB_DATA ctdb_data ;
if ( ! ( result = talloc ( mem_ctx , struct db_record ) ) ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
return NULL ;
}
2013-02-01 11:48:00 +04:00
result - > db = ctx - > db ;
2008-08-07 10:20:05 +04:00
result - > private_data = ctx - > transaction ;
result - > key . dsize = key . dsize ;
2012-05-11 23:53:13 +04:00
result - > key . dptr = ( uint8_t * ) talloc_memdup ( result , key . dptr ,
key . dsize ) ;
2008-08-07 10:20:05 +04:00
if ( result - > key . dptr = = NULL ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2016-09-12 18:30:55 +03:00
result - > storev = db_ctdb_storev_transaction ;
2008-08-07 10:20:05 +04:00
result - > delete_rec = db_ctdb_delete_transaction ;
2019-07-09 17:42:46 +03:00
if ( ctx - > transaction = = NULL ) {
DEBUG ( 0 , ( " no transaction available \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2009-12-03 19:29:54 +03:00
if ( pull_newest_from_marshall_buffer ( ctx - > transaction - > m_write , key ,
NULL , result , & result - > value ) ) {
2020-01-11 02:52:31 +03:00
result - > value_valid = true ;
2009-12-03 19:29:54 +03:00
return result ;
}
2015-03-12 17:23:17 +03:00
ctdb_data = tdb_fetch ( ctx - > wtdb - > tdb , key ) ;
2008-08-07 10:20:05 +04:00
if ( ctdb_data . dptr = = NULL ) {
/* create the record */
result - > value = tdb_null ;
2020-01-17 05:47:02 +03:00
result - > value_valid = true ;
2008-08-07 10:20:05 +04:00
return result ;
}
result - > value . dsize = ctdb_data . dsize - sizeof ( struct ctdb_ltdb_header ) ;
result - > value . dptr = NULL ;
if ( ( result - > value . dsize ! = 0 )
2012-05-11 23:53:13 +04:00
& & ! ( result - > value . dptr = ( uint8_t * ) talloc_memdup (
2008-08-07 10:20:05 +04:00
result , ctdb_data . dptr + sizeof ( struct ctdb_ltdb_header ) ,
result - > value . dsize ) ) ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
TALLOC_FREE ( result ) ;
2020-01-11 02:55:29 +03:00
return NULL ;
2008-08-07 10:20:05 +04:00
}
2019-10-24 17:41:47 +03:00
result - > value_valid = true ;
2008-08-07 10:20:05 +04:00
SAFE_FREE ( ctdb_data . dptr ) ;
return result ;
}
2008-09-15 08:27:50 +04:00
static int db_ctdb_record_destructor ( struct db_record * * recp )
2008-08-07 13:14:16 +04:00
{
2008-09-15 08:27:50 +04:00
struct db_record * rec = talloc_get_type_abort ( * recp , struct db_record ) ;
2008-08-07 13:14:16 +04:00
struct db_ctdb_transaction_handle * h = talloc_get_type_abort (
rec - > private_data , struct db_ctdb_transaction_handle ) ;
2008-08-07 15:33:00 +04:00
int ret = h - > ctx - > db - > transaction_commit ( h - > ctx - > db ) ;
if ( ret ! = 0 ) {
DEBUG ( 0 , ( __location__ " transaction_commit failed \n " ) ) ;
}
2008-08-07 13:14:16 +04:00
return 0 ;
}
/*
auto - create a transaction for persistent databases
*/
static struct db_record * db_ctdb_fetch_locked_persistent ( struct db_ctdb_ctx * ctx ,
TALLOC_CTX * mem_ctx ,
TDB_DATA key )
{
int res ;
2008-09-15 08:27:50 +04:00
struct db_record * rec , * * recp ;
2008-08-07 13:14:16 +04:00
res = db_ctdb_transaction_start ( ctx - > db ) ;
if ( res = = - 1 ) {
return NULL ;
}
rec = db_ctdb_fetch_locked_transaction ( ctx , mem_ctx , key ) ;
if ( rec = = NULL ) {
2012-11-07 19:22:07 +04:00
ctx - > db - > transaction_cancel ( ctx - > db ) ;
2008-08-07 13:14:16 +04:00
return NULL ;
}
/* destroy this transaction when we release the lock */
2008-09-15 08:27:50 +04:00
recp = talloc ( rec , struct db_record * ) ;
if ( recp = = NULL ) {
ctx - > db - > transaction_cancel ( ctx - > db ) ;
2008-09-15 08:51:35 +04:00
talloc_free ( rec ) ;
2008-09-15 08:27:50 +04:00
return NULL ;
}
* recp = rec ;
talloc_set_destructor ( recp , db_ctdb_record_destructor ) ;
2008-08-07 13:14:16 +04:00
return rec ;
}
2008-08-07 10:20:05 +04:00
/*
stores a record inside a transaction
*/
2009-12-11 14:30:57 +03:00
static NTSTATUS db_ctdb_transaction_store ( struct db_ctdb_transaction_handle * h ,
TDB_DATA key , TDB_DATA data )
2008-08-07 10:20:05 +04:00
{
TALLOC_CTX * tmp_ctx = talloc_new ( h ) ;
TDB_DATA rec ;
struct ctdb_ltdb_header header ;
2009-12-03 19:29:54 +03:00
ZERO_STRUCT ( header ) ;
2008-08-07 10:20:05 +04:00
/* we need the header so we can update the RSN */
2009-12-03 19:29:54 +03:00
if ( ! pull_newest_from_marshall_buffer ( h - > m_write , key , & header ,
NULL , NULL ) ) {
2015-03-12 17:23:17 +03:00
rec = tdb_fetch ( h - > ctx - > wtdb - > tdb , key ) ;
2009-12-03 19:29:54 +03:00
if ( rec . dptr ! = NULL ) {
memcpy ( & header , rec . dptr ,
sizeof ( struct ctdb_ltdb_header ) ) ;
rec . dsize - = sizeof ( struct ctdb_ltdb_header ) ;
/*
* a special case , we are writing the same
* data that is there now
*/
if ( data . dsize = = rec . dsize & &
memcmp ( data . dptr ,
rec . dptr + sizeof ( struct ctdb_ltdb_header ) ,
data . dsize ) = = 0 ) {
SAFE_FREE ( rec . dptr ) ;
talloc_free ( tmp_ctx ) ;
2009-12-11 14:30:57 +03:00
return NT_STATUS_OK ;
2009-12-03 19:29:54 +03:00
}
2008-08-08 03:58:15 +04:00
}
2008-08-07 10:20:05 +04:00
SAFE_FREE ( rec . dptr ) ;
}
2017-06-16 18:11:48 +03:00
header . dmaster = get_my_vnn ( ) ;
2008-08-07 10:20:05 +04:00
header . rsn + + ;
2008-08-08 03:58:15 +04:00
h - > m_write = db_ctdb_marshall_add ( h , h - > m_write , h - > ctx - > db_id , 0 , key , & header , data ) ;
if ( h - > m_write = = NULL ) {
DEBUG ( 0 , ( __location__ " Failed to add to marshalling record \n " ) ) ;
talloc_free ( tmp_ctx ) ;
2009-12-11 14:30:57 +03:00
return NT_STATUS_NO_MEMORY ;
2008-08-07 10:20:05 +04:00
}
2008-08-24 14:46:26 +04:00
2008-08-07 10:20:05 +04:00
talloc_free ( tmp_ctx ) ;
2009-12-11 14:30:57 +03:00
return NT_STATUS_OK ;
2008-08-07 10:20:05 +04:00
}
/*
a record store inside a transaction
*/
2016-09-12 18:30:55 +03:00
static NTSTATUS db_ctdb_storev_transaction (
struct db_record * rec , const TDB_DATA * dbufs , int num_dbufs , int flag )
2008-08-07 10:20:05 +04:00
{
struct db_ctdb_transaction_handle * h = talloc_get_type_abort (
rec - > private_data , struct db_ctdb_transaction_handle ) ;
2009-12-11 14:30:57 +03:00
NTSTATUS status ;
2016-09-12 18:30:55 +03:00
TDB_DATA data ;
data = dbwrap_merge_dbufs ( rec , dbufs , num_dbufs ) ;
if ( data . dptr = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2008-08-07 10:20:05 +04:00
2009-12-11 14:30:57 +03:00
status = db_ctdb_transaction_store ( h , rec - > key , data ) ;
2016-09-12 18:30:55 +03:00
TALLOC_FREE ( data . dptr ) ;
2009-12-11 14:30:57 +03:00
return status ;
2008-08-07 10:20:05 +04:00
}
2012-11-07 19:22:07 +04:00
/*
2008-08-07 10:20:05 +04:00
a record delete inside a transaction
*/
static NTSTATUS db_ctdb_delete_transaction ( struct db_record * rec )
{
struct db_ctdb_transaction_handle * h = talloc_get_type_abort (
rec - > private_data , struct db_ctdb_transaction_handle ) ;
2009-12-11 14:30:57 +03:00
NTSTATUS status ;
2008-08-07 10:20:05 +04:00
2009-12-11 14:30:57 +03:00
status = db_ctdb_transaction_store ( h , rec - > key , tdb_null ) ;
return status ;
2008-08-07 10:20:05 +04:00
}
2012-11-08 15:00:11 +04:00
static void db_ctdb_fetch_db_seqnum_parser (
TDB_DATA key , struct ctdb_ltdb_header * header ,
TDB_DATA data , void * private_data )
{
uint64_t * seqnum = ( uint64_t * ) private_data ;
if ( data . dsize ! = sizeof ( uint64_t ) ) {
* seqnum = 0 ;
return ;
}
memcpy ( seqnum , data . dptr , sizeof ( * seqnum ) ) ;
}
2009-12-11 16:07:28 +03:00
/**
* Fetch the db sequence number of a persistent db directly from the db .
*/
static NTSTATUS db_ctdb_fetch_db_seqnum_from_db ( struct db_ctdb_ctx * db ,
uint64_t * seqnum )
{
NTSTATUS status ;
TDB_DATA key ;
if ( seqnum = = NULL ) {
return NT_STATUS_INVALID_PARAMETER ;
}
2012-11-08 15:00:11 +04:00
key = string_term_tdb_data ( CTDB_DB_SEQNUM_KEY ) ;
2009-12-11 16:07:28 +03:00
2012-11-08 15:00:11 +04:00
status = db_ctdb_ltdb_parse (
db , key , db_ctdb_fetch_db_seqnum_parser , seqnum ) ;
2009-12-12 02:30:37 +03:00
2012-11-08 15:00:11 +04:00
if ( NT_STATUS_IS_OK ( status ) ) {
return NT_STATUS_OK ;
}
if ( NT_STATUS_EQUAL ( status , NT_STATUS_NOT_FOUND ) ) {
2009-12-11 16:07:28 +03:00
* seqnum = 0 ;
2012-11-08 15:00:11 +04:00
return NT_STATUS_OK ;
2009-12-11 16:07:28 +03:00
}
return status ;
}
/**
* Store the database sequence number inside a transaction .
*/
static NTSTATUS db_ctdb_store_db_seqnum ( struct db_ctdb_transaction_handle * h ,
uint64_t seqnum )
{
NTSTATUS status ;
2020-01-20 16:06:11 +03:00
TDB_DATA key = string_term_tdb_data ( CTDB_DB_SEQNUM_KEY ) ;
TDB_DATA data = { . dptr = ( uint8_t * ) & seqnum , . dsize = sizeof ( seqnum ) } ;
2009-12-11 16:07:28 +03:00
status = db_ctdb_transaction_store ( h , key , data ) ;
return status ;
}
2008-08-07 10:20:05 +04:00
/*
commit a transaction
*/
static int db_ctdb_transaction_commit ( struct db_context * db )
{
struct db_ctdb_ctx * ctx = talloc_get_type_abort ( db - > private_data ,
struct db_ctdb_ctx ) ;
NTSTATUS rets ;
2016-04-21 14:31:39 +03:00
int32_t status ;
2008-08-07 10:20:05 +04:00
struct db_ctdb_transaction_handle * h = ctx - > transaction ;
2009-12-11 16:07:28 +03:00
uint64_t old_seqnum , new_seqnum ;
int ret ;
2008-08-07 10:20:05 +04:00
if ( h = = NULL ) {
DEBUG ( 0 , ( __location__ " transaction commit with no open transaction on db 0x%08x \n " , ctx - > db_id ) ) ;
return - 1 ;
}
2008-08-08 10:44:24 +04:00
if ( h - > nested_cancel ) {
db - > transaction_cancel ( db ) ;
DEBUG ( 5 , ( __location__ " Failed transaction commit after nested cancel \n " ) ) ;
return - 1 ;
}
if ( h - > nesting ! = 0 ) {
h - > nesting - - ;
2011-08-15 01:47:47 +04:00
DEBUG ( 5 , ( __location__ " transaction commit on db 0x%08x: nesting %d -> %d \n " ,
ctx - > db_id , ctx - > transaction - > nesting + 1 , ctx - > transaction - > nesting ) ) ;
2008-08-08 10:44:24 +04:00
return 0 ;
}
2010-01-14 01:53:54 +03:00
if ( h - > m_write = = NULL ) {
/*
* No changes were made , so don ' t change the seqnum ,
* don ' t push to other node , just exit with success .
*/
ret = 0 ;
goto done ;
}
2011-08-15 01:47:47 +04:00
DEBUG ( 5 , ( __location__ " transaction commit on db 0x%08x \n " , ctx - > db_id ) ) ;
2008-08-07 10:20:05 +04:00
2009-12-11 16:07:28 +03:00
/*
* As the last db action before committing , bump the database sequence
* number . Note that this undoes all changes to the seqnum records
* performed under the transaction . This record is not meant to be
* modified by user interaction . It is for internal use only . . .
*/
rets = db_ctdb_fetch_db_seqnum_from_db ( ctx , & old_seqnum ) ;
if ( ! NT_STATUS_IS_OK ( rets ) ) {
DEBUG ( 1 , ( __location__ " failed to fetch the db sequence number "
" in transaction commit on db 0x%08x \n " , ctx - > db_id ) ) ;
ret = - 1 ;
goto done ;
}
new_seqnum = old_seqnum + 1 ;
rets = db_ctdb_store_db_seqnum ( h , new_seqnum ) ;
if ( ! NT_STATUS_IS_OK ( rets ) ) {
DEBUG ( 1 , ( __location__ " failed to store the db sequence number "
" in transaction commit on db 0x%08x \n " , ctx - > db_id ) ) ;
ret = - 1 ;
goto done ;
}
2008-08-07 10:20:05 +04:00
again :
/* tell ctdbd to commit to the other nodes */
2017-06-16 14:00:59 +03:00
ret = ctdbd_control_local ( messaging_ctdb_connection ( ) ,
2017-06-16 18:11:48 +03:00
CTDB_CONTROL_TRANS3_COMMIT ,
2015-10-03 06:42:05 +03:00
h - > ctx - > db_id , 0 ,
db_ctdb_marshall_finish ( h - > m_write ) ,
NULL , NULL , & status ) ;
if ( ( ret ! = 0 ) | | status ! = 0 ) {
2009-12-03 19:29:54 +03:00
/*
2009-12-11 16:07:28 +03:00
* The TRANS3_COMMIT control should only possibly fail when a
* recovery has been running concurrently . In any case , the db
* will be the same on all nodes , either the new copy or the
* old copy . This can be detected by comparing the old and new
* local sequence numbers .
*/
rets = db_ctdb_fetch_db_seqnum_from_db ( ctx , & new_seqnum ) ;
if ( ! NT_STATUS_IS_OK ( rets ) ) {
DEBUG ( 1 , ( __location__ " failed to refetch db sequence "
" number after failed TRANS3_COMMIT \n " ) ) ;
ret = - 1 ;
goto done ;
}
if ( new_seqnum = = old_seqnum ) {
/* Recovery prevented all our changes: retry. */
goto again ;
2012-11-07 19:39:16 +04:00
}
if ( new_seqnum ! = ( old_seqnum + 1 ) ) {
2009-12-11 16:07:28 +03:00
DEBUG ( 0 , ( __location__ " ERROR: new_seqnum[%lu] != "
" old_seqnum[%lu] + (0 or 1) after failed "
" TRANS3_COMMIT - this should not happen! \n " ,
( unsigned long ) new_seqnum ,
( unsigned long ) old_seqnum ) ) ;
ret = - 1 ;
goto done ;
}
/*
* Recovery propagated our changes to all nodes , completing
* our commit for us - succeed .
2009-12-03 19:29:54 +03:00
*/
2008-08-07 10:20:05 +04:00
}
2009-12-11 16:07:28 +03:00
ret = 0 ;
2009-12-03 19:29:54 +03:00
done :
2008-08-07 10:20:05 +04:00
h - > ctx - > transaction = NULL ;
talloc_free ( h ) ;
2010-01-14 01:51:34 +03:00
return ret ;
2008-08-07 10:20:05 +04:00
}
/*
cancel a transaction
*/
static int db_ctdb_transaction_cancel ( struct db_context * db )
{
struct db_ctdb_ctx * ctx = talloc_get_type_abort ( db - > private_data ,
struct db_ctdb_ctx ) ;
struct db_ctdb_transaction_handle * h = ctx - > transaction ;
if ( h = = NULL ) {
DEBUG ( 0 , ( __location__ " transaction cancel with no open transaction on db 0x%08x \n " , ctx - > db_id ) ) ;
return - 1 ;
}
2008-08-08 10:44:24 +04:00
if ( h - > nesting ! = 0 ) {
h - > nesting - - ;
h - > nested_cancel = true ;
2011-08-15 01:47:47 +04:00
DEBUG ( 5 , ( __location__ " transaction cancel on db 0x%08x: nesting %d -> %d \n " ,
ctx - > db_id , ctx - > transaction - > nesting + 1 , ctx - > transaction - > nesting ) ) ;
2008-08-08 10:44:24 +04:00
return 0 ;
}
2008-08-07 10:56:47 +04:00
DEBUG ( 5 , ( __location__ " Cancel transaction on db 0x%08x \n " , ctx - > db_id ) ) ;
2008-08-07 10:20:05 +04:00
ctx - > transaction = NULL ;
talloc_free ( h ) ;
return 0 ;
}
2016-09-12 18:30:55 +03:00
static NTSTATUS db_ctdb_storev ( struct db_record * rec ,
const TDB_DATA * dbufs , int num_dbufs , int flag )
2007-06-10 21:02:09 +04:00
{
struct db_ctdb_rec * crec = talloc_get_type_abort (
rec - > private_data , struct db_ctdb_rec ) ;
2016-09-12 18:30:55 +03:00
NTSTATUS status ;
2007-06-10 21:02:09 +04:00
2016-09-12 18:30:55 +03:00
status = db_ctdb_ltdb_store ( crec - > ctdb_ctx , rec - > key , & ( crec - > header ) ,
2016-09-13 15:22:05 +03:00
dbufs , num_dbufs ) ;
2016-09-12 18:30:55 +03:00
return status ;
2007-06-10 21:02:09 +04:00
}
2008-01-16 12:09:48 +03:00
2010-12-22 16:16:07 +03:00
static NTSTATUS db_ctdb_send_schedule_for_deletion ( struct db_record * rec )
{
2015-10-13 21:34:24 +03:00
NTSTATUS status = NT_STATUS_OK ;
2015-10-03 06:42:05 +03:00
int ret ;
2010-12-22 16:16:07 +03:00
struct ctdb_control_schedule_for_deletion * dd ;
TDB_DATA indata ;
2016-04-21 14:31:39 +03:00
int32_t cstatus ;
2010-12-22 16:16:07 +03:00
struct db_ctdb_rec * crec = talloc_get_type_abort (
rec - > private_data , struct db_ctdb_rec ) ;
2016-04-11 17:01:07 +03:00
struct db_ctdb_ctx * ctx = crec - > ctdb_ctx ;
2010-12-22 16:16:07 +03:00
indata . dsize = offsetof ( struct ctdb_control_schedule_for_deletion , key ) + rec - > key . dsize ;
indata . dptr = talloc_zero_array ( crec , uint8_t , indata . dsize ) ;
if ( indata . dptr = = NULL ) {
DEBUG ( 0 , ( __location__ " talloc failed! \n " ) ) ;
return NT_STATUS_NO_MEMORY ;
}
dd = ( struct ctdb_control_schedule_for_deletion * ) ( void * ) indata . dptr ;
2016-04-11 17:01:07 +03:00
dd - > db_id = ctx - > db_id ;
2010-12-22 16:16:07 +03:00
dd - > hdr = crec - > header ;
dd - > keylen = rec - > key . dsize ;
memcpy ( dd - > key , rec - > key . dptr , rec - > key . dsize ) ;
2017-06-16 14:00:59 +03:00
ret = ctdbd_control_local ( messaging_ctdb_connection ( ) ,
2015-10-03 06:42:05 +03:00
CTDB_CONTROL_SCHEDULE_FOR_DELETION ,
crec - > ctdb_ctx - > db_id ,
CTDB_CTRL_FLAG_NOREPLY , /* flags */
indata ,
2020-03-30 13:44:59 +03:00
NULL , /* mem_ctx */
2015-10-03 06:42:05 +03:00
NULL , /* outdata */
& cstatus ) ;
2010-12-22 16:16:07 +03:00
talloc_free ( indata . dptr ) ;
2015-10-03 06:42:05 +03:00
if ( ( ret ! = 0 ) | | cstatus ! = 0 ) {
2010-12-22 16:16:07 +03:00
DEBUG ( 1 , ( __location__ " Error sending local control "
2016-04-21 14:31:39 +03:00
" SCHEDULE_FOR_DELETION: %s, cstatus = % " PRIi32 " \n " ,
2015-10-03 06:42:05 +03:00
strerror ( ret ) , cstatus ) ) ;
if ( ret ! = 0 ) {
2015-10-13 21:34:24 +03:00
status = map_nt_error_from_unix ( ret ) ;
} else {
2010-12-22 16:16:07 +03:00
status = NT_STATUS_UNSUCCESSFUL ;
}
}
return status ;
}
2007-06-10 21:02:09 +04:00
static NTSTATUS db_ctdb_delete ( struct db_record * rec )
{
2010-12-22 16:16:07 +03:00
NTSTATUS status ;
2007-06-10 21:02:09 +04:00
/*
* We have to store the header with empty data . TODO : Fix the
* tdb - level cleanup
*/
2016-09-12 18:30:55 +03:00
status = db_ctdb_storev ( rec , & tdb_null , 1 , 0 ) ;
2010-12-22 16:16:07 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
2007-06-10 21:02:09 +04:00
2010-12-22 16:16:07 +03:00
status = db_ctdb_send_schedule_for_deletion ( rec ) ;
return status ;
2007-06-10 21:02:09 +04:00
}
static int db_ctdb_record_destr ( struct db_record * data )
{
struct db_ctdb_rec * crec = talloc_get_type_abort (
data - > private_data , struct db_ctdb_rec ) ;
2010-03-05 18:46:36 +03:00
int threshold ;
2013-05-06 12:56:12 +04:00
int ret ;
struct timeval before ;
double timediff ;
2007-06-10 21:02:09 +04:00
2007-08-29 15:46:44 +04:00
DEBUG ( 10 , ( DEBUGLEVEL > 10
2007-11-07 21:06:30 +03:00
? " Unlocking db %u key %s \n "
: " Unlocking db %u key %.20s \n " ,
2007-08-29 15:46:44 +04:00
( int ) crec - > ctdb_ctx - > db_id ,
2008-10-18 18:16:57 +04:00
hex_encode_talloc ( data , ( unsigned char * ) data - > key . dptr ,
2007-06-10 21:02:09 +04:00
data - > key . dsize ) ) ) ;
2013-05-06 12:56:12 +04:00
before = timeval_current ( ) ;
ret = tdb_chainunlock ( crec - > ctdb_ctx - > wtdb - > tdb , data - > key ) ;
timediff = timeval_elapsed ( & before ) ;
timediff * = 1000 ; /* get us milliseconds */
2014-01-15 01:17:32 +04:00
if ( timediff > crec - > ctdb_ctx - > warn_unlock_msecs ) {
2013-05-06 12:56:12 +04:00
char * key ;
key = hex_encode_talloc ( talloc_tos ( ) ,
( unsigned char * ) data - > key . dptr ,
data - > key . dsize ) ;
DEBUG ( 0 , ( " tdb_chainunlock on db %s, key %s took %f milliseconds \n " ,
tdb_name ( crec - > ctdb_ctx - > wtdb - > tdb ) , key ,
timediff ) ) ;
TALLOC_FREE ( key ) ;
}
if ( ret ! = 0 ) {
DEBUG ( 0 , ( " tdb_chainunlock failed \n " ) ) ;
return - 1 ;
}
2007-06-10 21:02:09 +04:00
2014-01-15 01:17:32 +04:00
threshold = crec - > ctdb_ctx - > warn_locktime_msecs ;
2010-03-05 18:46:36 +03:00
if ( threshold ! = 0 ) {
2014-01-15 01:23:04 +04:00
timediff = timeval_elapsed ( & crec - > lock_time ) * 1000 ;
if ( timediff > threshold ) {
2012-08-31 00:16:24 +04:00
const char * key ;
key = hex_encode_talloc ( data ,
( unsigned char * ) data - > key . dptr ,
data - > key . dsize ) ;
2014-01-15 01:23:04 +04:00
DEBUG ( 0 , ( " Held tdb lock on db %s, key %s "
" %f milliseconds \n " ,
tdb_name ( crec - > ctdb_ctx - > wtdb - > tdb ) ,
key , timediff ) ) ;
2010-03-05 18:46:36 +03:00
}
}
2007-06-10 21:02:09 +04:00
return 0 ;
}
2012-06-29 12:51:37 +04:00
/**
* Check whether we have a valid local copy of the given record ,
* either for reading or for writing .
*/
2012-11-12 16:27:07 +04:00
static bool db_ctdb_can_use_local_hdr ( const struct ctdb_ltdb_header * hdr ,
2016-04-11 17:31:25 +03:00
uint32_t my_vnn , bool read_only )
2012-02-03 03:53:27 +04:00
{
2016-04-11 17:31:25 +03:00
if ( hdr - > dmaster ! = my_vnn ) {
2012-02-03 03:53:27 +04:00
/* If we're not dmaster, it must be r/o copy. */
return read_only & & ( hdr - > flags & CTDB_REC_RO_HAVE_READONLY ) ;
}
2012-06-29 12:55:32 +04:00
/*
* If we want write access , no one may have r / o copies .
*/
2012-02-03 03:53:27 +04:00
return read_only | | ! ( hdr - > flags & CTDB_REC_RO_HAVE_DELEGATIONS ) ;
}
2016-04-11 17:31:25 +03:00
static bool db_ctdb_can_use_local_copy ( TDB_DATA ctdb_data , uint32_t my_vnn ,
bool read_only )
2012-11-12 16:27:07 +04:00
{
2013-05-14 23:02:15 +04:00
if ( ctdb_data . dptr = = NULL ) {
2012-11-12 16:27:07 +04:00
return false ;
2013-05-14 23:02:15 +04:00
}
2012-11-12 16:27:07 +04:00
2013-05-14 23:02:15 +04:00
if ( ctdb_data . dsize < sizeof ( struct ctdb_ltdb_header ) ) {
2012-11-12 16:27:07 +04:00
return false ;
2013-05-14 23:02:15 +04:00
}
2012-11-12 16:27:07 +04:00
return db_ctdb_can_use_local_hdr (
2016-04-11 17:31:25 +03:00
( struct ctdb_ltdb_header * ) ctdb_data . dptr , my_vnn , read_only ) ;
2012-11-12 16:27:07 +04:00
}
2008-08-05 13:32:20 +04:00
static struct db_record * fetch_locked_internal ( struct db_ctdb_ctx * ctx ,
TALLOC_CTX * mem_ctx ,
2012-03-27 16:31:04 +04:00
TDB_DATA key ,
bool tryonly )
2007-06-10 21:02:09 +04:00
{
struct db_record * result ;
struct db_ctdb_rec * crec ;
TDB_DATA ctdb_data ;
2013-02-01 15:49:52 +04:00
int migrate_attempts ;
struct timeval migrate_start ;
2013-05-13 19:02:17 +04:00
struct timeval chainlock_start ;
struct timeval ctdb_start_time ;
double chainlock_time = 0 ;
double ctdb_time = 0 ;
2013-02-01 15:49:52 +04:00
int duration_msecs ;
2012-03-27 16:31:04 +04:00
int lockret ;
2015-10-03 06:42:05 +03:00
int ret ;
2007-06-10 21:02:09 +04:00
if ( ! ( result = talloc ( mem_ctx , struct db_record ) ) ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
return NULL ;
}
2011-06-07 05:44:43 +04:00
if ( ! ( crec = talloc_zero ( result , struct db_ctdb_rec ) ) ) {
2007-06-10 21:02:09 +04:00
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2012-07-31 11:30:29 +04:00
result - > db = ctx - > db ;
2007-06-10 21:02:09 +04:00
result - > private_data = ( void * ) crec ;
crec - > ctdb_ctx = ctx ;
result - > key . dsize = key . dsize ;
2012-05-11 23:53:13 +04:00
result - > key . dptr = ( uint8_t * ) talloc_memdup ( result , key . dptr ,
key . dsize ) ;
2007-06-10 21:02:09 +04:00
if ( result - > key . dptr = = NULL ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2013-02-01 15:49:52 +04:00
migrate_attempts = 0 ;
GetTimeOfDay ( & migrate_start ) ;
2007-06-10 21:02:09 +04:00
/*
* Do a blocking lock on the record
*/
again :
2007-08-29 15:46:44 +04:00
if ( DEBUGLEVEL > = 10 ) {
2008-10-18 18:16:57 +04:00
char * keystr = hex_encode_talloc ( result , key . dptr , key . dsize ) ;
2007-08-29 15:46:44 +04:00
DEBUG ( 10 , ( DEBUGLEVEL > 10
? " Locking db %u key %s \n "
2007-11-07 21:06:30 +03:00
: " Locking db %u key %.20s \n " ,
2007-08-29 15:46:44 +04:00
( int ) crec - > ctdb_ctx - > db_id , keystr ) ) ;
TALLOC_FREE ( keystr ) ;
}
2008-08-24 14:46:26 +04:00
2013-05-13 19:02:17 +04:00
GetTimeOfDay ( & chainlock_start ) ;
2012-03-27 16:31:04 +04:00
lockret = tryonly
? tdb_chainlock_nonblock ( ctx - > wtdb - > tdb , key )
: tdb_chainlock ( ctx - > wtdb - > tdb , key ) ;
2013-05-13 19:02:17 +04:00
chainlock_time + = timeval_elapsed ( & chainlock_start ) ;
2012-03-27 16:31:04 +04:00
if ( lockret ! = 0 ) {
2007-06-10 21:02:09 +04:00
DEBUG ( 3 , ( " tdb_chainlock failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2016-09-12 18:30:55 +03:00
result - > storev = db_ctdb_storev ;
2008-08-07 13:14:16 +04:00
result - > delete_rec = db_ctdb_delete ;
2007-06-10 21:02:09 +04:00
talloc_set_destructor ( result , db_ctdb_record_destr ) ;
2015-03-12 17:23:17 +03:00
ctdb_data = tdb_fetch ( ctx - > wtdb - > tdb , key ) ;
2007-06-10 21:02:09 +04:00
/*
* See if we have a valid record and we are the dmaster . If so , we can
* take the shortcut and just return it .
*/
2017-06-16 18:11:48 +03:00
if ( ! db_ctdb_can_use_local_copy ( ctdb_data , get_my_vnn ( ) , false ) ) {
2007-06-10 21:02:09 +04:00
SAFE_FREE ( ctdb_data . dptr ) ;
tdb_chainunlock ( ctx - > wtdb - > tdb , key ) ;
talloc_set_destructor ( result , NULL ) ;
2012-03-27 16:31:04 +04:00
if ( tryonly & & ( migrate_attempts ! = 0 ) ) {
DEBUG ( 5 , ( " record migrated away again \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2008-01-16 12:09:48 +03:00
migrate_attempts + = 1 ;
2016-04-11 17:07:12 +03:00
DEBUG ( 10 , ( " ctdb_data.dptr = %p, dmaster = % " PRIu32 " "
" (% " PRIu32 " ) % " PRIu32 " \n " ,
2007-06-10 21:02:09 +04:00
ctdb_data . dptr , ctdb_data . dptr ?
2016-04-11 17:07:12 +03:00
( ( struct ctdb_ltdb_header * ) ctdb_data . dptr ) - > dmaster :
UINT32_MAX ,
2017-06-16 18:11:48 +03:00
get_my_vnn ( ) ,
2012-03-30 17:15:29 +04:00
ctdb_data . dptr ?
( ( struct ctdb_ltdb_header * ) ctdb_data . dptr ) - > flags : 0 ) ) ;
2007-06-10 21:02:09 +04:00
2013-05-13 19:02:17 +04:00
GetTimeOfDay ( & ctdb_start_time ) ;
2017-06-16 14:00:59 +03:00
ret = ctdbd_migrate ( messaging_ctdb_connection ( ) , ctx - > db_id ,
2017-06-16 18:11:48 +03:00
key ) ;
2013-05-13 19:02:17 +04:00
ctdb_time + = timeval_elapsed ( & ctdb_start_time ) ;
2015-10-03 06:42:05 +03:00
if ( ret ! = 0 ) {
2017-05-08 13:16:16 +03:00
DEBUG ( 5 , ( " ctdbd_migrate failed: %s \n " ,
2015-10-03 06:42:05 +03:00
strerror ( ret ) ) ) ;
2007-06-10 21:02:09 +04:00
TALLOC_FREE ( result ) ;
return NULL ;
}
/* now its migrated, try again */
goto again ;
}
2013-02-01 15:49:52 +04:00
{
double duration ;
duration = timeval_elapsed ( & migrate_start ) ;
/*
* Convert the duration to milliseconds to avoid a
* floating - point division of
* lp_parm_int ( " migrate_duration " ) by 1000.
*/
duration_msecs = duration * 1000 ;
}
2014-01-15 01:17:32 +04:00
if ( ( migrate_attempts > ctx - > warn_migrate_attempts ) | |
( duration_msecs > ctx - > warn_migrate_msecs ) ) {
2013-05-13 20:20:43 +04:00
int chain = 0 ;
if ( tdb_get_flags ( ctx - > wtdb - > tdb ) & TDB_INCOMPATIBLE_HASH ) {
chain = tdb_jenkins_hash ( & key ) %
tdb_hash_size ( ctx - > wtdb - > tdb ) ;
}
DEBUG ( 0 , ( " db_ctdb_fetch_locked for %s key %s, chain %d "
" needed %d attempts, %d milliseconds, "
2014-03-06 10:27:36 +04:00
" chainlock: %f ms, CTDB %f ms \n " ,
2013-05-13 20:20:43 +04:00
tdb_name ( ctx - > wtdb - > tdb ) ,
2012-04-03 15:20:39 +04:00
hex_encode_talloc ( talloc_tos ( ) ,
( unsigned char * ) key . dptr ,
key . dsize ) ,
2013-05-13 20:20:43 +04:00
chain ,
2013-05-13 19:02:17 +04:00
migrate_attempts , duration_msecs ,
2014-03-06 10:27:36 +04:00
chainlock_time * 1000.0 ,
ctdb_time * 1000.0 ) ) ;
2008-01-16 12:09:48 +03:00
}
2010-03-05 18:46:36 +03:00
GetTimeOfDay ( & crec - > lock_time ) ;
2007-06-10 21:02:09 +04:00
memcpy ( & crec - > header , ctdb_data . dptr , sizeof ( crec - > header ) ) ;
result - > value . dsize = ctdb_data . dsize - sizeof ( crec - > header ) ;
result - > value . dptr = NULL ;
2018-09-04 13:47:42 +03:00
if ( result - > value . dsize ! = 0 ) {
result - > value . dptr = talloc_memdup (
result , ctdb_data . dptr + sizeof ( crec - > header ) ,
result - > value . dsize ) ;
if ( result - > value . dptr = = NULL ) {
DBG_ERR ( " talloc failed \n " ) ;
TALLOC_FREE ( result ) ;
2020-01-11 02:55:29 +03:00
return NULL ;
2018-09-04 13:47:42 +03:00
}
2007-06-10 21:02:09 +04:00
}
2019-10-24 17:41:47 +03:00
result - > value_valid = true ;
2007-06-10 21:02:09 +04:00
SAFE_FREE ( ctdb_data . dptr ) ;
return result ;
}
2008-08-05 13:32:20 +04:00
static struct db_record * db_ctdb_fetch_locked ( struct db_context * db ,
TALLOC_CTX * mem_ctx ,
TDB_DATA key )
{
struct db_ctdb_ctx * ctx = talloc_get_type_abort ( db - > private_data ,
struct db_ctdb_ctx ) ;
2008-08-07 13:14:16 +04:00
if ( ctx - > transaction ! = NULL ) {
return db_ctdb_fetch_locked_transaction ( ctx , mem_ctx , key ) ;
}
if ( db - > persistent ) {
return db_ctdb_fetch_locked_persistent ( ctx , mem_ctx , key ) ;
}
2012-03-27 16:31:04 +04:00
return fetch_locked_internal ( ctx , mem_ctx , key , false ) ;
}
2012-11-12 16:03:56 +04:00
struct db_ctdb_parse_record_state {
void ( * parser ) ( TDB_DATA key , TDB_DATA data , void * private_data ) ;
void * private_data ;
2016-04-11 17:31:25 +03:00
uint32_t my_vnn ;
2012-11-12 16:42:23 +04:00
bool ask_for_readonly_copy ;
2012-11-12 16:36:48 +04:00
bool done ;
2016-08-08 17:58:51 +03:00
bool empty_record ;
2012-11-12 16:03:56 +04:00
} ;
static void db_ctdb_parse_record_parser (
TDB_DATA key , struct ctdb_ltdb_header * header ,
TDB_DATA data , void * private_data )
{
struct db_ctdb_parse_record_state * state =
( struct db_ctdb_parse_record_state * ) private_data ;
state - > parser ( key , data , state - > private_data ) ;
}
2012-11-12 16:36:48 +04:00
static void db_ctdb_parse_record_parser_nonpersistent (
TDB_DATA key , struct ctdb_ltdb_header * header ,
TDB_DATA data , void * private_data )
{
struct db_ctdb_parse_record_state * state =
( struct db_ctdb_parse_record_state * ) private_data ;
2016-04-11 17:31:25 +03:00
if ( db_ctdb_can_use_local_hdr ( header , state - > my_vnn , true ) ) {
2016-08-08 17:58:51 +03:00
/*
* A record consisting only of the ctdb header can be
* a validly created empty record or a tombstone
* record of a deleted record ( not vacuumed yet ) . Mark
* it accordingly .
*/
state - > empty_record = ( data . dsize = = 0 ) ;
if ( ! state - > empty_record ) {
state - > parser ( key , data , state - > private_data ) ;
}
2012-11-12 16:36:48 +04:00
state - > done = true ;
2012-11-12 16:42:23 +04:00
} else {
/*
* We found something in the db , so it seems that this record ,
* while not usable locally right now , is popular . Ask for a
* R / O copy .
*/
state - > ask_for_readonly_copy = true ;
2012-11-12 16:36:48 +04:00
}
}
2017-02-23 20:28:32 +03:00
static NTSTATUS db_ctdb_try_parse_local_record ( struct db_ctdb_ctx * ctx ,
TDB_DATA key ,
struct db_ctdb_parse_record_state * state )
2011-12-08 18:56:35 +04:00
{
NTSTATUS status ;
2012-11-12 16:03:56 +04:00
if ( ctx - > transaction ! = NULL ) {
struct db_ctdb_transaction_handle * h = ctx - > transaction ;
bool found ;
/*
* Transactions only happen for persistent db ' s .
*/
found = parse_newest_in_marshall_buffer (
2017-02-23 20:28:32 +03:00
h - > m_write , key , db_ctdb_parse_record_parser , state ) ;
2012-11-12 16:03:56 +04:00
if ( found ) {
return NT_STATUS_OK ;
}
}
2017-02-23 20:28:32 +03:00
if ( ctx - > db - > persistent ) {
2012-11-12 16:03:56 +04:00
/*
* Persistent db , but not found in the transaction buffer
*/
return db_ctdb_ltdb_parse (
2017-02-23 20:28:32 +03:00
ctx , key , db_ctdb_parse_record_parser , state ) ;
2012-11-12 16:03:56 +04:00
}
2017-02-23 20:28:32 +03:00
state - > done = false ;
state - > ask_for_readonly_copy = false ;
2012-11-12 16:36:48 +04:00
status = db_ctdb_ltdb_parse (
2017-02-23 20:28:32 +03:00
ctx , key , db_ctdb_parse_record_parser_nonpersistent , state ) ;
if ( NT_STATUS_IS_OK ( status ) & & state - > done ) {
if ( state - > empty_record ) {
2016-08-08 17:58:51 +03:00
/*
* We know authoritatively , that this is an empty
* record . Since ctdb does not distinguish between empty
* and deleted records , this can be a record stored as
* empty or a not - yet - vacuumed tombstone record of a
* deleted record . Now Samba right now can live without
* empty records , so we can safely report this record
* as non - existing .
*
* See bugs 10008 and 12005.
*/
return NT_STATUS_NOT_FOUND ;
}
2012-11-12 16:36:48 +04:00
return NT_STATUS_OK ;
}
2017-02-23 20:28:32 +03:00
return NT_STATUS_MORE_PROCESSING_REQUIRED ;
}
static NTSTATUS db_ctdb_parse_record ( struct db_context * db , TDB_DATA key ,
void ( * parser ) ( TDB_DATA key ,
TDB_DATA data ,
void * private_data ) ,
void * private_data )
{
struct db_ctdb_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_ctdb_ctx ) ;
struct db_ctdb_parse_record_state state ;
NTSTATUS status ;
int ret ;
state . parser = parser ;
state . private_data = private_data ;
2017-06-16 18:11:48 +03:00
state . my_vnn = get_my_vnn ( ) ;
2017-02-23 20:28:32 +03:00
state . empty_record = false ;
status = db_ctdb_try_parse_local_record ( ctx , key , & state ) ;
if ( ! NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) ) {
return status ;
}
2017-06-16 14:00:59 +03:00
ret = ctdbd_parse ( messaging_ctdb_connection ( ) , ctx - > db_id , key ,
2015-10-03 06:42:05 +03:00
state . ask_for_readonly_copy , parser , private_data ) ;
if ( ret ! = 0 ) {
2016-04-20 14:27:07 +03:00
if ( ret = = ENOENT ) {
/*
* This maps to
* NT_STATUS_OBJECT_NAME_NOT_FOUND . Our upper
* layers expect NT_STATUS_NOT_FOUND for " no
* record around " . We need to convert dbwrap
* to 0 / errno away from NTSTATUS . . . : - )
*/
return NT_STATUS_NOT_FOUND ;
}
2015-10-03 06:42:05 +03:00
return map_nt_error_from_unix ( ret ) ;
}
return NT_STATUS_OK ;
2011-12-08 18:56:35 +04:00
}
2016-12-21 10:38:25 +03:00
static void db_ctdb_parse_record_done ( struct tevent_req * subreq ) ;
static struct tevent_req * db_ctdb_parse_record_send (
TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct db_context * db ,
TDB_DATA key ,
void ( * parser ) ( TDB_DATA key ,
TDB_DATA data ,
void * private_data ) ,
void * private_data ,
enum dbwrap_req_state * req_state )
{
struct db_ctdb_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_ctdb_ctx ) ;
struct tevent_req * req = NULL ;
struct tevent_req * subreq = NULL ;
struct db_ctdb_parse_record_state * state = NULL ;
NTSTATUS status ;
req = tevent_req_create ( mem_ctx , & state ,
struct db_ctdb_parse_record_state ) ;
if ( req = = NULL ) {
* req_state = DBWRAP_REQ_ERROR ;
return NULL ;
}
* state = ( struct db_ctdb_parse_record_state ) {
. parser = parser ,
. private_data = private_data ,
2017-06-16 18:11:48 +03:00
. my_vnn = get_my_vnn ( ) ,
2016-12-21 10:38:25 +03:00
. empty_record = false ,
} ;
status = db_ctdb_try_parse_local_record ( ctx , key , state ) ;
if ( ! NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) ) {
if ( tevent_req_nterror ( req , status ) ) {
* req_state = DBWRAP_REQ_ERROR ;
return tevent_req_post ( req , ev ) ;
}
* req_state = DBWRAP_REQ_DONE ;
tevent_req_done ( req ) ;
return tevent_req_post ( req , ev ) ;
}
subreq = ctdbd_parse_send ( state ,
ev ,
ctdb_async_ctx . async_conn ,
ctx - > db_id ,
key ,
state - > ask_for_readonly_copy ,
parser ,
private_data ,
req_state ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
* req_state = DBWRAP_REQ_ERROR ;
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , db_ctdb_parse_record_done , req ) ;
return req ;
}
static void db_ctdb_parse_record_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
int ret ;
ret = ctdbd_parse_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( ret ! = 0 ) {
if ( ret = = ENOENT ) {
/*
* This maps to NT_STATUS_OBJECT_NAME_NOT_FOUND . Our
* upper layers expect NT_STATUS_NOT_FOUND for " no
* record around " . We need to convert dbwrap to 0/errno
* away from NTSTATUS . . . : - )
*/
tevent_req_nterror ( req , NT_STATUS_NOT_FOUND ) ;
return ;
}
tevent_req_nterror ( req , map_nt_error_from_unix ( ret ) ) ;
return ;
}
tevent_req_done ( req ) ;
return ;
}
static NTSTATUS db_ctdb_parse_record_recv ( struct tevent_req * req )
{
return tevent_req_simple_recv_ntstatus ( req ) ;
}
2007-06-10 21:02:09 +04:00
struct traverse_state {
struct db_context * db ;
int ( * fn ) ( struct db_record * rec , void * private_data ) ;
void * private_data ;
2012-06-12 12:10:36 +04:00
int count ;
2007-06-10 21:02:09 +04:00
} ;
static void traverse_callback ( TDB_DATA key , TDB_DATA data , void * private_data )
{
struct traverse_state * state = ( struct traverse_state * ) private_data ;
2018-09-09 19:35:26 +03:00
struct db_record * rec = NULL ;
TALLOC_CTX * tmp_ctx = NULL ;
tmp_ctx = talloc_new ( state - > db ) ;
if ( tmp_ctx = = NULL ) {
DBG_ERR ( " talloc_new failed \n " ) ;
return ;
}
2007-06-10 21:02:09 +04:00
/* we have to give them a locked record to prevent races */
rec = db_ctdb_fetch_locked ( state - > db , tmp_ctx , key ) ;
2018-09-09 19:35:26 +03:00
if ( rec ! = NULL & & rec - > value . dsize > 0 ) {
2007-06-10 21:02:09 +04:00
state - > fn ( rec , state - > private_data ) ;
2018-09-09 19:51:43 +03:00
state - > count + + ;
2007-06-10 21:02:09 +04:00
}
talloc_free ( tmp_ctx ) ;
}
2008-01-16 12:09:48 +03:00
static int traverse_persistent_callback ( TDB_CONTEXT * tdb , TDB_DATA kbuf , TDB_DATA dbuf ,
void * private_data )
{
struct traverse_state * state = ( struct traverse_state * ) private_data ;
struct db_record * rec ;
TALLOC_CTX * tmp_ctx = talloc_new ( state - > db ) ;
int ret = 0 ;
2011-09-20 06:33:31 +04:00
/*
* Skip the __db_sequence_number__ key :
* This is used for persistent transactions internally .
*/
if ( kbuf . dsize = = strlen ( CTDB_DB_SEQNUM_KEY ) + 1 & &
2011-10-12 14:03:42 +04:00
strcmp ( ( const char * ) kbuf . dptr , CTDB_DB_SEQNUM_KEY ) = = 0 )
2011-09-20 06:33:31 +04:00
{
goto done ;
}
2008-01-16 12:09:48 +03:00
/* we have to give them a locked record to prevent races */
rec = db_ctdb_fetch_locked ( state - > db , tmp_ctx , kbuf ) ;
if ( rec & & rec - > value . dsize > 0 ) {
ret = state - > fn ( rec , state - > private_data ) ;
}
2011-09-20 06:33:31 +04:00
done :
2008-01-16 12:09:48 +03:00
talloc_free ( tmp_ctx ) ;
return ret ;
}
2011-09-22 15:58:24 +04:00
/* wrapper to use traverse_persistent_callback with dbwrap */
static int traverse_persistent_callback_dbwrap ( struct db_record * rec , void * data )
{
return traverse_persistent_callback ( NULL , rec - > key , rec - > value , data ) ;
}
2016-04-05 18:30:11 +03:00
static int db_ctdbd_traverse ( uint32_t db_id ,
void ( * fn ) ( TDB_DATA key , TDB_DATA data ,
void * private_data ) ,
void * private_data )
{
struct ctdbd_connection * conn ;
int ret ;
become_root ( ) ;
ret = ctdbd_init_connection ( talloc_tos ( ) , lp_ctdbd_socket ( ) ,
lp_ctdb_timeout ( ) , & conn ) ;
unbecome_root ( ) ;
if ( ret ! = 0 ) {
DBG_WARNING ( " ctdbd_init_connection failed: %s \n " ,
strerror ( ret ) ) ;
return ret ;
}
ret = ctdbd_traverse ( conn , db_id , fn , private_data ) ;
TALLOC_FREE ( conn ) ;
if ( ret ! = 0 ) {
DBG_WARNING ( " ctdbd_traverse failed: %s \n " ,
strerror ( ret ) ) ;
return ret ;
}
return 0 ;
}
2011-09-22 15:58:24 +04:00
2007-06-10 21:02:09 +04:00
static int db_ctdb_traverse ( struct db_context * db ,
int ( * fn ) ( struct db_record * rec ,
void * private_data ) ,
void * private_data )
{
2015-10-03 06:42:05 +03:00
int ret ;
2007-06-10 21:02:09 +04:00
struct db_ctdb_ctx * ctx = talloc_get_type_abort ( db - > private_data ,
struct db_ctdb_ctx ) ;
struct traverse_state state ;
2018-09-09 19:50:14 +03:00
state = ( struct traverse_state ) {
. db = db ,
. fn = fn ,
. private_data = private_data ,
} ;
2007-06-10 21:02:09 +04:00
2008-01-16 12:09:48 +03:00
if ( db - > persistent ) {
2011-09-22 15:58:24 +04:00
struct tdb_context * ltdb = ctx - > wtdb - > tdb ;
2008-01-16 12:09:48 +03:00
/* for persistent databases we don't need to do a ctdb traverse,
we can do a faster local traverse */
2011-09-22 15:58:24 +04:00
ret = tdb_traverse ( ltdb , traverse_persistent_callback , & state ) ;
if ( ret < 0 ) {
return ret ;
}
if ( ctx - > transaction & & ctx - > transaction - > m_write ) {
2011-10-14 18:11:06 +04:00
/*
* we now have to handle keys not yet
* present at transaction start
*/
2011-09-22 15:58:24 +04:00
struct db_context * newkeys = db_open_rbt ( talloc_tos ( ) ) ;
struct ctdb_marshall_buffer * mbuf = ctx - > transaction - > m_write ;
2015-10-29 09:30:30 +03:00
struct ctdb_rec_data_old * rec = NULL ;
2016-04-11 16:45:49 +03:00
uint32_t i ;
2011-10-14 18:11:06 +04:00
int count = 0 ;
2015-10-03 06:42:05 +03:00
NTSTATUS status ;
2011-10-14 18:11:06 +04:00
if ( newkeys = = NULL ) {
return - 1 ;
}
2011-09-22 15:58:24 +04:00
for ( i = 0 ; i < mbuf - > count ; i + + ) {
TDB_DATA key ;
2012-11-10 17:46:10 +04:00
rec = db_ctdb_marshall_loop_next_key (
mbuf , rec , & key ) ;
2011-09-22 15:58:24 +04:00
SMB_ASSERT ( rec ! = NULL ) ;
if ( ! tdb_exists ( ltdb , key ) ) {
dbwrap_store ( newkeys , key , tdb_null , 0 ) ;
}
}
status = dbwrap_traverse ( newkeys ,
traverse_persistent_callback_dbwrap ,
2011-10-12 13:48:55 +04:00
& state ,
2011-10-14 18:11:06 +04:00
& count ) ;
2011-09-22 15:58:24 +04:00
talloc_free ( newkeys ) ;
2011-10-14 18:11:06 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return - 1 ;
}
ret + = count ;
2011-09-22 15:58:24 +04:00
}
return ret ;
2008-01-16 12:09:48 +03:00
}
2016-04-05 18:30:11 +03:00
ret = db_ctdbd_traverse ( ctx - > db_id , traverse_callback , & state ) ;
2015-10-03 06:42:05 +03:00
if ( ret ! = 0 ) {
2012-06-12 12:10:36 +04:00
return - 1 ;
}
return state . count ;
2007-06-10 21:02:09 +04:00
}
2016-09-12 18:30:55 +03:00
static NTSTATUS db_ctdb_storev_deny ( struct db_record * rec ,
const TDB_DATA * dbufs , int num_dbufs , int flag )
2007-06-10 21:02:09 +04:00
{
return NT_STATUS_MEDIA_WRITE_PROTECTED ;
}
static NTSTATUS db_ctdb_delete_deny ( struct db_record * rec )
{
return NT_STATUS_MEDIA_WRITE_PROTECTED ;
}
static void traverse_read_callback ( TDB_DATA key , TDB_DATA data , void * private_data )
{
struct traverse_state * state = ( struct traverse_state * ) private_data ;
struct db_record rec ;
2013-02-04 16:09:46 +04:00
ZERO_STRUCT ( rec ) ;
2013-02-01 11:48:00 +04:00
rec . db = state - > db ;
2007-06-10 21:02:09 +04:00
rec . key = key ;
rec . value = data ;
2016-09-12 18:30:55 +03:00
rec . storev = db_ctdb_storev_deny ;
2007-06-10 21:02:09 +04:00
rec . delete_rec = db_ctdb_delete_deny ;
2013-02-01 11:48:00 +04:00
rec . private_data = NULL ;
2020-01-08 21:24:24 +03:00
rec . value_valid = true ;
2007-06-10 21:02:09 +04:00
state - > fn ( & rec , state - > private_data ) ;
2012-06-12 12:10:36 +04:00
state - > count + + ;
2007-06-10 21:02:09 +04:00
}
2008-01-16 12:09:48 +03:00
static int traverse_persistent_callback_read ( TDB_CONTEXT * tdb , TDB_DATA kbuf , TDB_DATA dbuf ,
void * private_data )
{
struct traverse_state * state = ( struct traverse_state * ) private_data ;
struct db_record rec ;
2011-09-20 06:33:31 +04:00
/*
* Skip the __db_sequence_number__ key :
* This is used for persistent transactions internally .
*/
if ( kbuf . dsize = = strlen ( CTDB_DB_SEQNUM_KEY ) + 1 & &
2011-10-12 14:04:50 +04:00
strcmp ( ( const char * ) kbuf . dptr , CTDB_DB_SEQNUM_KEY ) = = 0 )
2011-09-20 06:33:31 +04:00
{
return 0 ;
}
2013-02-04 16:10:34 +04:00
ZERO_STRUCT ( rec ) ;
2013-02-01 11:48:00 +04:00
rec . db = state - > db ;
2008-01-16 12:09:48 +03:00
rec . key = kbuf ;
rec . value = dbuf ;
2020-01-08 21:24:24 +03:00
rec . value_valid = true ;
2016-09-12 18:30:55 +03:00
rec . storev = db_ctdb_storev_deny ;
2008-01-16 12:09:48 +03:00
rec . delete_rec = db_ctdb_delete_deny ;
2013-02-01 11:48:00 +04:00
rec . private_data = NULL ;
2008-01-16 12:09:48 +03:00
if ( rec . value . dsize < = sizeof ( struct ctdb_ltdb_header ) ) {
/* a deleted record */
return 0 ;
}
rec . value . dsize - = sizeof ( struct ctdb_ltdb_header ) ;
rec . value . dptr + = sizeof ( struct ctdb_ltdb_header ) ;
2012-06-12 12:10:36 +04:00
state - > count + + ;
2008-01-16 12:09:48 +03:00
return state - > fn ( & rec , state - > private_data ) ;
}
2007-06-10 21:02:09 +04:00
static int db_ctdb_traverse_read ( struct db_context * db ,
int ( * fn ) ( struct db_record * rec ,
void * private_data ) ,
void * private_data )
{
2015-10-03 06:42:05 +03:00
int ret ;
2007-06-10 21:02:09 +04:00
struct db_ctdb_ctx * ctx = talloc_get_type_abort ( db - > private_data ,
struct db_ctdb_ctx ) ;
struct traverse_state state ;
2018-09-09 19:48:13 +03:00
state = ( struct traverse_state ) {
. db = db ,
. fn = fn ,
. private_data = private_data ,
} ;
2007-06-10 21:02:09 +04:00
2008-01-16 12:09:48 +03:00
if ( db - > persistent ) {
/* for persistent databases we don't need to do a ctdb traverse,
we can do a faster local traverse */
2018-09-10 13:50:01 +03:00
int nrecs ;
nrecs = tdb_traverse_read ( ctx - > wtdb - > tdb ,
traverse_persistent_callback_read ,
& state ) ;
if ( nrecs = = - 1 ) {
return - 1 ;
}
return state . count ;
2008-01-16 12:09:48 +03:00
}
2016-04-05 18:30:11 +03:00
ret = db_ctdbd_traverse ( ctx - > db_id , traverse_read_callback , & state ) ;
2015-10-03 06:42:05 +03:00
if ( ret ! = 0 ) {
2012-06-12 12:10:36 +04:00
return - 1 ;
}
return state . count ;
2007-06-10 21:02:09 +04:00
}
static int db_ctdb_get_seqnum ( struct db_context * db )
{
struct db_ctdb_ctx * ctx = talloc_get_type_abort ( db - > private_data ,
struct db_ctdb_ctx ) ;
return tdb_get_seqnum ( ctx - > wtdb - > tdb ) ;
}
2015-09-20 17:26:06 +03:00
static size_t db_ctdb_id ( struct db_context * db , uint8_t * id , size_t idlen )
2012-02-15 17:57:01 +04:00
{
struct db_ctdb_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_ctdb_ctx ) ;
2015-09-20 17:26:06 +03:00
if ( idlen > = sizeof ( ctx - > db_id ) ) {
memcpy ( id , & ctx - > db_id , sizeof ( ctx - > db_id ) ) ;
}
return sizeof ( ctx - > db_id ) ;
2012-02-15 17:57:01 +04:00
}
2007-06-10 21:02:09 +04:00
struct db_context * db_open_ctdb ( TALLOC_CTX * mem_ctx ,
2016-04-24 18:37:07 +03:00
struct messaging_context * msg_ctx ,
2007-06-10 21:02:09 +04:00
const char * name ,
int hash_size , int tdb_flags ,
2012-01-16 15:50:44 +04:00
int open_flags , mode_t mode ,
2014-01-28 15:53:24 +04:00
enum dbwrap_lock_order lock_order ,
uint64_t dbwrap_flags )
2007-06-10 21:02:09 +04:00
{
struct db_context * result ;
struct db_ctdb_ctx * db_ctdb ;
char * db_path ;
2011-10-13 18:50:57 +04:00
struct loadparm_context * lp_ctx ;
2017-07-11 21:36:35 +03:00
TDB_DATA data ;
2020-03-30 13:54:00 +03:00
TDB_DATA outdata = { 0 } ;
2017-07-19 05:04:35 +03:00
bool persistent = ( tdb_flags & TDB_CLEAR_IF_FIRST ) = = 0 ;
2016-04-21 14:31:39 +03:00
int32_t cstatus ;
2015-10-03 06:42:05 +03:00
int ret ;
2007-06-10 21:02:09 +04:00
if ( ! lp_clustering ( ) ) {
DEBUG ( 10 , ( " Clustering disabled -- no ctdb \n " ) ) ;
return NULL ;
}
2011-06-07 05:44:43 +04:00
if ( ! ( result = talloc_zero ( mem_ctx , struct db_context ) ) ) {
2007-06-10 21:02:09 +04:00
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2011-06-07 05:38:41 +04:00
if ( ! ( db_ctdb = talloc ( result , struct db_ctdb_ctx ) ) ) {
2007-06-10 21:02:09 +04:00
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2013-02-01 12:14:16 +04:00
result - > name = talloc_strdup ( result , name ) ;
if ( result - > name = = NULL ) {
DEBUG ( 0 , ( " talloc failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2008-08-07 10:56:47 +04:00
db_ctdb - > transaction = NULL ;
2008-08-07 13:14:16 +04:00
db_ctdb - > db = result ;
2009-12-03 19:29:54 +03:00
2017-06-16 14:00:59 +03:00
ret = ctdbd_db_attach ( messaging_ctdb_connection ( ) , name ,
2017-06-16 18:11:48 +03:00
& db_ctdb - > db_id , persistent ) ;
2015-10-03 06:42:05 +03:00
if ( ret ! = 0 ) {
DEBUG ( 0 , ( " ctdbd_db_attach failed for %s: %s \n " , name ,
strerror ( ret ) ) ) ;
2007-06-10 21:02:09 +04:00
TALLOC_FREE ( result ) ;
return NULL ;
}
2017-07-11 21:36:35 +03:00
if ( tdb_flags & TDB_SEQNUM ) {
data . dptr = ( uint8_t * ) & db_ctdb - > db_id ;
data . dsize = sizeof ( db_ctdb - > db_id ) ;
2017-06-16 14:00:59 +03:00
ret = ctdbd_control_local ( messaging_ctdb_connection ( ) ,
2017-06-16 18:11:48 +03:00
CTDB_CONTROL_ENABLE_SEQNUM ,
2017-07-11 21:36:35 +03:00
0 , 0 , data ,
NULL , NULL , & cstatus ) ;
if ( ( ret ! = 0 ) | | cstatus ! = 0 ) {
DBG_ERR ( " ctdb_control for enable seqnum "
" failed: %s \n " , strerror ( ret ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
}
2017-06-16 14:00:59 +03:00
db_path = ctdbd_dbpath ( messaging_ctdb_connection ( ) , db_ctdb ,
2017-06-16 18:11:48 +03:00
db_ctdb - > db_id ) ;
2018-03-05 01:39:05 +03:00
if ( db_path = = NULL ) {
DBG_ERR ( " ctdbd_dbpath failed \n " ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2008-01-16 12:09:48 +03:00
2017-07-11 21:41:43 +03:00
result - > persistent = persistent ;
2012-03-15 14:10:35 +04:00
result - > lock_order = lock_order ;
2007-06-10 21:02:09 +04:00
2017-07-11 22:35:17 +03:00
data . dptr = ( uint8_t * ) & db_ctdb - > db_id ;
data . dsize = sizeof ( db_ctdb - > db_id ) ;
2017-06-16 14:00:59 +03:00
ret = ctdbd_control_local ( messaging_ctdb_connection ( ) ,
2017-06-16 18:11:48 +03:00
CTDB_CONTROL_DB_OPEN_FLAGS ,
2020-03-30 13:54:00 +03:00
0 , 0 , data , NULL , & outdata , & cstatus ) ;
2017-07-11 22:35:17 +03:00
if ( ret ! = 0 ) {
DBG_ERR ( " ctdb control for db_open_flags "
" failed: %s \n " , strerror ( ret ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
2020-03-30 13:54:00 +03:00
if ( cstatus ! = 0 | | outdata . dsize ! = sizeof ( int ) ) {
2017-07-11 22:35:17 +03:00
DBG_ERR ( " ctdb_control for db_open_flags failed \n " ) ;
2020-03-30 13:54:00 +03:00
TALLOC_FREE ( outdata . dptr ) ;
2017-07-11 22:35:17 +03:00
TALLOC_FREE ( result ) ;
return NULL ;
}
2020-03-30 13:54:00 +03:00
tdb_flags = * ( int * ) outdata . dptr ;
TALLOC_FREE ( outdata . dptr ) ;
2007-06-10 21:02:09 +04:00
2016-12-21 10:38:25 +03:00
if ( ! result - > persistent ) {
ret = ctdb_async_ctx_init ( NULL , messaging_tevent_context ( msg_ctx ) ) ;
if ( ret ! = 0 ) {
DBG_ERR ( " ctdb_async_ctx_init failed: %s \n " , strerror ( ret ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
}
2014-01-29 00:24:22 +04:00
if ( ! result - > persistent & &
( dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS ) )
{
TDB_DATA indata ;
indata = make_tdb_data ( ( uint8_t * ) & db_ctdb - > db_id ,
sizeof ( db_ctdb - > db_id ) ) ;
2015-10-03 06:42:05 +03:00
ret = ctdbd_control_local (
2017-06-16 14:00:59 +03:00
messaging_ctdb_connection ( ) ,
2017-06-16 18:11:48 +03:00
CTDB_CONTROL_SET_DB_READONLY , 0 , 0 ,
2016-04-11 17:01:07 +03:00
indata , NULL , NULL , & cstatus ) ;
2015-10-03 06:42:05 +03:00
if ( ( ret ! = 0 ) | | ( cstatus ! = 0 ) ) {
2014-01-29 00:24:22 +04:00
DEBUG ( 1 , ( " CTDB_CONTROL_SET_DB_READONLY failed: "
2016-04-21 14:31:39 +03:00
" %s, % " PRIi32 " \n " , strerror ( ret ) , cstatus ) ) ;
2014-01-29 00:24:22 +04:00
TALLOC_FREE ( result ) ;
return NULL ;
}
}
2012-06-27 17:24:39 +04:00
lp_ctx = loadparm_init_s3 ( db_path , loadparm_s3_helpers ( ) ) ;
2011-10-13 18:50:57 +04:00
2014-03-26 18:32:58 +04:00
if ( hash_size = = 0 ) {
hash_size = lpcfg_tdb_hash_size ( lp_ctx , db_path ) ;
}
2014-03-26 18:41:03 +04:00
db_ctdb - > wtdb = tdb_wrap_open ( db_ctdb , db_path , hash_size ,
lpcfg_tdb_flags ( lp_ctx , tdb_flags ) ,
O_RDWR , 0 ) ;
2011-10-13 18:50:57 +04:00
talloc_unlink ( db_path , lp_ctx ) ;
2007-06-10 21:02:09 +04:00
if ( db_ctdb - > wtdb = = NULL ) {
DEBUG ( 0 , ( " Could not open tdb %s: %s \n " , db_path , strerror ( errno ) ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
talloc_free ( db_path ) ;
2015-10-06 10:57:59 +03:00
/* honor permissions if user has specified O_CREAT */
if ( open_flags & O_CREAT ) {
2015-10-03 06:42:05 +03:00
int fd ;
2015-10-06 10:57:59 +03:00
fd = tdb_fd ( db_ctdb - > wtdb - > tdb ) ;
ret = fchmod ( fd , mode ) ;
if ( ret = = - 1 ) {
2015-10-21 21:07:57 +03:00
DBG_WARNING ( " fchmod failed: %s \n " ,
2015-10-06 10:57:59 +03:00
strerror ( errno ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
}
2009-12-03 19:29:54 +03:00
if ( result - > persistent ) {
2016-04-24 18:37:07 +03:00
db_ctdb - > lock_ctx = g_lock_ctx_init ( db_ctdb , msg_ctx ) ;
2009-12-03 19:29:54 +03:00
if ( db_ctdb - > lock_ctx = = NULL ) {
DEBUG ( 0 , ( " g_lock_ctx_init failed \n " ) ) ;
TALLOC_FREE ( result ) ;
return NULL ;
}
}
2014-01-15 01:17:32 +04:00
db_ctdb - > warn_unlock_msecs = lp_parm_int ( - 1 , " ctdb " ,
" unlock_warn_threshold " , 5 ) ;
db_ctdb - > warn_migrate_attempts = lp_parm_int ( - 1 , " ctdb " ,
" migrate_attempts " , 10 ) ;
db_ctdb - > warn_migrate_msecs = lp_parm_int ( - 1 , " ctdb " ,
" migrate_duration " , 5000 ) ;
db_ctdb - > warn_locktime_msecs = lp_ctdb_locktime_warn_threshold ( ) ;
2007-06-10 21:02:09 +04:00
result - > private_data = ( void * ) db_ctdb ;
result - > fetch_locked = db_ctdb_fetch_locked ;
2011-12-08 18:56:35 +04:00
result - > parse_record = db_ctdb_parse_record ;
2016-12-21 10:38:25 +03:00
result - > parse_record_send = db_ctdb_parse_record_send ;
result - > parse_record_recv = db_ctdb_parse_record_recv ;
2007-06-10 21:02:09 +04:00
result - > traverse = db_ctdb_traverse ;
result - > traverse_read = db_ctdb_traverse_read ;
result - > get_seqnum = db_ctdb_get_seqnum ;
2008-08-07 10:20:05 +04:00
result - > transaction_start = db_ctdb_transaction_start ;
result - > transaction_commit = db_ctdb_transaction_commit ;
result - > transaction_cancel = db_ctdb_transaction_cancel ;
2012-02-15 17:57:01 +04:00
result - > id = db_ctdb_id ;
2007-06-10 21:02:09 +04:00
DEBUG ( 3 , ( " db_open_ctdb: opened database '%s' with dbid 0x%x \n " ,
name , db_ctdb - > db_id ) ) ;
return result ;
}