2012-02-15 15:17:33 +01:00
/*
Unix SMB / CIFS implementation .
Watch dbwrap record changes
Copyright ( C ) Volker Lendecke 2012
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
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
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 .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
# include "system/filesys.h"
2017-01-01 20:00:55 +00:00
# include "lib/util/server_id.h"
2012-05-11 22:11:42 +02:00
# include "dbwrap/dbwrap.h"
2012-02-15 15:17:33 +01:00
# include "dbwrap_watch.h"
# include "dbwrap_open.h"
# include "lib/util/util_tdb.h"
# include "lib/util/tevent_ntstatus.h"
2016-03-10 14:37:12 +01:00
# include "server_id_watch.h"
2016-07-12 15:59:56 +02:00
# include "lib/dbwrap/dbwrap_private.h"
2012-02-15 15:17:33 +01:00
2019-11-18 12:37:21 +01:00
struct dbwrap_watcher {
/*
* Process watching this record
*/
struct server_id pid ;
2019-11-18 13:23:52 +01:00
/*
* Individual instance inside the waiter , incremented each
* time a watcher is created
*/
uint64_t instance ;
2019-11-18 12:37:21 +01:00
} ;
2019-11-18 13:23:52 +01:00
# define DBWRAP_WATCHER_BUF_LENGTH (SERVER_ID_BUF_LENGTH + sizeof(uint64_t))
2019-11-18 12:37:21 +01:00
2016-07-12 15:59:56 +02:00
/*
* Watched records contain a header of :
*
2020-04-28 16:55:55 +02:00
* [ uint32 ] num_records
2019-11-18 12:37:21 +01:00
* 0 [ DBWRAP_WATCHER_BUF_LENGTH ] \
* 1 [ DBWRAP_WATCHER_BUF_LENGTH ] |
2016-07-12 15:59:56 +02:00
* . . | - Array of watchers
2019-11-18 12:37:21 +01:00
* ( num_records - 1 ) [ DBWRAP_WATCHER_BUF_LENGTH ] /
2016-07-12 15:59:56 +02:00
*
* [ Remainder of record . . . . ]
*
* If this header is absent then this is a
* fresh record of length zero ( no watchers ) .
*/
2019-11-18 21:46:55 +01:00
static bool dbwrap_watch_rec_parse (
TDB_DATA data ,
uint8_t * * pwatchers ,
size_t * pnum_watchers ,
TDB_DATA * pdata )
2016-07-12 15:59:56 +02:00
{
2017-07-01 18:13:44 +02:00
size_t num_watchers ;
2019-11-18 21:46:55 +01:00
if ( data . dsize = = 0 ) {
/* Fresh record */
if ( pwatchers ! = NULL ) {
* pwatchers = NULL ;
}
if ( pnum_watchers ! = NULL ) {
* pnum_watchers = 0 ;
}
if ( pdata ! = NULL ) {
* pdata = ( TDB_DATA ) { . dptr = NULL } ;
}
return true ;
}
2016-07-12 15:59:56 +02:00
if ( data . dsize < sizeof ( uint32_t ) ) {
2019-11-18 21:46:55 +01:00
/* Invalid record */
2017-07-01 18:13:44 +02:00
return false ;
2016-07-12 15:59:56 +02:00
}
num_watchers = IVAL ( data . dptr , 0 ) ;
data . dptr + = sizeof ( uint32_t ) ;
data . dsize - = sizeof ( uint32_t ) ;
2019-11-18 12:37:21 +01:00
if ( num_watchers > data . dsize / DBWRAP_WATCHER_BUF_LENGTH ) {
2016-07-12 15:59:56 +02:00
/* Invalid record */
2017-07-01 18:13:44 +02:00
return false ;
2016-07-12 15:59:56 +02:00
}
2019-11-18 21:46:55 +01:00
if ( pwatchers ! = NULL ) {
* pwatchers = data . dptr ;
}
if ( pnum_watchers ! = NULL ) {
* pnum_watchers = num_watchers ;
}
if ( pdata ! = NULL ) {
2019-11-18 12:37:21 +01:00
size_t watchers_len = num_watchers * DBWRAP_WATCHER_BUF_LENGTH ;
2019-11-18 21:46:55 +01:00
* pdata = ( TDB_DATA ) {
2017-07-01 18:13:44 +02:00
. dptr = data . dptr + watchers_len ,
. dsize = data . dsize - watchers_len
} ;
2016-07-12 15:59:56 +02:00
}
2017-07-01 18:13:44 +02:00
return true ;
2016-07-12 15:59:56 +02:00
}
2019-11-18 12:37:21 +01:00
static void dbwrap_watcher_get ( struct dbwrap_watcher * w ,
const uint8_t buf [ DBWRAP_WATCHER_BUF_LENGTH ] )
{
server_id_get ( & w - > pid , buf ) ;
2019-11-18 13:23:52 +01:00
w - > instance = BVAL ( buf , SERVER_ID_BUF_LENGTH ) ;
2019-11-18 12:37:21 +01:00
}
static void dbwrap_watcher_put ( uint8_t buf [ DBWRAP_WATCHER_BUF_LENGTH ] ,
const struct dbwrap_watcher * w )
{
server_id_put ( buf , w - > pid ) ;
2019-11-18 13:23:52 +01:00
SBVAL ( buf , SERVER_ID_BUF_LENGTH , w - > instance ) ;
2019-11-18 12:37:21 +01:00
}
2019-11-18 21:46:55 +01:00
static void dbwrap_watch_log_invalid_record (
struct db_context * db , TDB_DATA key , TDB_DATA value )
2017-07-01 18:13:44 +02:00
{
2019-11-18 21:46:55 +01:00
DBG_ERR ( " Found invalid record in %s \n " , dbwrap_name ( db ) ) ;
dump_data ( 1 , key . dptr , key . dsize ) ;
dump_data ( 1 , value . dptr , value . dsize ) ;
2016-07-12 15:59:56 +02:00
}
struct db_watched_ctx {
struct db_context * backend ;
struct messaging_context * msg ;
} ;
struct db_watched_subrec {
struct db_record * subrec ;
2019-11-18 21:46:55 +01:00
struct dbwrap_watcher added ;
2016-07-12 15:59:56 +02:00
} ;
2017-06-27 18:40:28 +02:00
static NTSTATUS dbwrap_watched_subrec_storev (
struct db_record * rec , struct db_watched_subrec * subrec ,
const TDB_DATA * dbufs , int num_dbufs , int flags ) ;
static NTSTATUS dbwrap_watched_subrec_delete (
struct db_record * rec , struct db_watched_subrec * subrec ) ;
2016-09-12 17:30:55 +02:00
static NTSTATUS dbwrap_watched_storev ( struct db_record * rec ,
const TDB_DATA * dbufs , int num_dbufs ,
2017-06-27 18:40:28 +02:00
int flags ) ;
2016-07-12 15:59:56 +02:00
static NTSTATUS dbwrap_watched_delete ( struct db_record * rec ) ;
2019-05-24 14:49:47 +02:00
static void dbwrap_watched_subrec_wakeup (
struct db_record * rec , struct db_watched_subrec * subrec ) ;
2019-11-18 21:46:55 +01:00
static int db_watched_subrec_destructor ( struct db_watched_subrec * s ) ;
2016-07-12 15:59:56 +02:00
static struct db_record * dbwrap_watched_fetch_locked (
struct db_context * db , TALLOC_CTX * mem_ctx , TDB_DATA key )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
struct db_record * rec ;
struct db_watched_subrec * subrec ;
TDB_DATA subrec_value ;
2017-07-01 18:13:44 +02:00
bool ok ;
2016-07-12 15:59:56 +02:00
rec = talloc_zero ( mem_ctx , struct db_record ) ;
if ( rec = = NULL ) {
return NULL ;
}
subrec = talloc_zero ( rec , struct db_watched_subrec ) ;
if ( subrec = = NULL ) {
TALLOC_FREE ( rec ) ;
return NULL ;
}
2019-11-18 21:46:55 +01:00
talloc_set_destructor ( subrec , db_watched_subrec_destructor ) ;
2016-07-12 15:59:56 +02:00
rec - > private_data = subrec ;
subrec - > subrec = dbwrap_fetch_locked ( ctx - > backend , subrec , key ) ;
if ( subrec - > subrec = = NULL ) {
TALLOC_FREE ( rec ) ;
return NULL ;
}
rec - > db = db ;
rec - > key = dbwrap_record_get_key ( subrec - > subrec ) ;
2016-09-12 17:30:55 +02:00
rec - > storev = dbwrap_watched_storev ;
2016-07-12 15:59:56 +02:00
rec - > delete_rec = dbwrap_watched_delete ;
subrec_value = dbwrap_record_get_value ( subrec - > subrec ) ;
2019-11-18 21:46:55 +01:00
ok = dbwrap_watch_rec_parse ( subrec_value , NULL , NULL , & rec - > value ) ;
2019-08-13 14:18:05 +02:00
if ( ! ok ) {
2019-11-18 21:46:55 +01:00
dbwrap_watch_log_invalid_record ( db , rec - > key , subrec_value ) ;
/* wipe invalid data */
rec - > value = ( TDB_DATA ) { . dptr = NULL , . dsize = 0 } ;
2019-08-13 14:18:05 +02:00
}
2019-11-18 21:46:55 +01:00
rec - > value_valid = true ;
2019-08-13 14:18:05 +02:00
2019-11-18 21:46:55 +01:00
return rec ;
}
2019-08-13 14:18:05 +02:00
2019-11-18 21:46:55 +01:00
struct dbwrap_watched_add_watcher_state {
struct dbwrap_watcher w ;
NTSTATUS status ;
} ;
static void dbwrap_watched_add_watcher (
struct db_record * rec ,
TDB_DATA value ,
void * private_data )
{
struct dbwrap_watched_add_watcher_state * state = private_data ;
size_t num_watchers = 0 ;
bool ok ;
uint8_t num_watchers_buf [ 4 ] ;
uint8_t add_buf [ DBWRAP_WATCHER_BUF_LENGTH ] ;
TDB_DATA dbufs [ 4 ] = {
{
. dptr = num_watchers_buf ,
. dsize = sizeof ( num_watchers_buf ) ,
} ,
{ 0 } , /* filled in with existing watchers */
{
. dptr = add_buf ,
. dsize = sizeof ( add_buf ) ,
} ,
{ 0 } , /* filled in with existing data */
} ;
dbwrap_watcher_put ( add_buf , & state - > w ) ;
ok = dbwrap_watch_rec_parse (
value , & dbufs [ 1 ] . dptr , & num_watchers , & dbufs [ 3 ] ) ;
if ( ! ok ) {
struct db_context * db = dbwrap_record_get_db ( rec ) ;
TDB_DATA key = dbwrap_record_get_key ( rec ) ;
dbwrap_watch_log_invalid_record ( db , key , value ) ;
/* wipe invalid data */
num_watchers = 0 ;
dbufs [ 3 ] = ( TDB_DATA ) { . dptr = NULL , . dsize = 0 } ;
2019-08-13 14:18:05 +02:00
}
2019-11-18 21:46:55 +01:00
dbufs [ 1 ] . dsize = num_watchers * DBWRAP_WATCHER_BUF_LENGTH ;
if ( num_watchers > = UINT32_MAX ) {
DBG_DEBUG ( " Can't handle %zu watchers \n " ,
num_watchers + 1 ) ;
state - > status = NT_STATUS_INSUFFICIENT_RESOURCES ;
return ;
}
num_watchers + = 1 ;
SIVAL ( num_watchers_buf , 0 , num_watchers ) ;
state - > status = dbwrap_record_storev ( rec , dbufs , ARRAY_SIZE ( dbufs ) , 0 ) ;
}
static int db_watched_subrec_destructor ( struct db_watched_subrec * s )
{
struct dbwrap_watched_add_watcher_state state = { . w = s - > added } ;
struct db_context * backend = dbwrap_record_get_db ( s - > subrec ) ;
NTSTATUS status ;
if ( s - > added . pid . pid = = 0 ) {
return 0 ;
}
status = dbwrap_do_locked (
backend , s - > subrec - > key , dbwrap_watched_add_watcher , & state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_WARNING ( " dbwrap_do_locked failed: %s \n " ,
nt_errstr ( status ) ) ;
return 0 ;
}
if ( ! NT_STATUS_IS_OK ( state . status ) ) {
DBG_WARNING ( " dbwrap_watched_add_watcher failed: %s \n " ,
nt_errstr ( state . status ) ) ;
return 0 ;
}
return 0 ;
2016-07-12 15:59:56 +02:00
}
s3:dbwrap_watch: avoid recursion into dbwrap_do_locked() from dbwrap_watched_do_locked_{storev,delete}()
This avoids a lot of overhead!
Using smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 500000
under valgrind --tool=callgrind...
This change replaces this:
6,877,542,529 PROGRAM TOTALS
590,000,773 lib/tdb/common/lock.c:tdb_lock_list
479,000,608 lib/tdb/common/lock.c:tdb_unlock
446,500,532 lib/tdb/common/io.c:tdb_read
364,000,824 lib/tdb/common/hash.c:tdb_jenkins_hash
285,000,532 lib/tdb/common/io.c:tdb_write
262,054,669 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
193,000,176 lib/tdb/common/tdb.c:tdb_find
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
140,000,196 lib/tdb/common/lock.c:tdb_lock
130,000,858 lib/util/debug.c:debuglevel_get_class
128,003,722 lib/talloc/talloc.c:_talloc_free
128,000,118 lib/tdb/common/tdb.c:tdb_parse_record
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
108,500,168 lib/tdb/common/io.c:tdb_ofs_read
102,500,000 lib/tdb/common/io.c:tdb_parse_data
by this:
5,706,522,398 PROGRAM TOTALS
434,000,617 lib/tdb/common/lock.c:tdb_lock_list
389,500,494 lib/tdb/common/io.c:tdb_read
359,000,488 lib/tdb/common/lock.c:tdb_unlock
285,000,532 lib/tdb/common/io.c:tdb_write
237,554,655 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
208,000,668 lib/tdb/common/hash.c:tdb_jenkins_hash
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
136,000,132 lib/tdb/common/tdb.c:tdb_find
130,000,858 lib/util/debug.c:debuglevel_get_class
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
112,000,168 lib/tdb/common/lock.c:tdb_lock
94,500,154 lib/tdb/common/io.c:tdb_ofs_read
94,000,188 /nptl/pthread_mutex_unlock.c:__pthread_mutex_unlock_full
86,000,086 lib/dbwrap/dbwrap.c:dbwrap_lock_order_lock
83,000,083 lib/dbwrap/dbwrap_tdb.c:db_tdb_do_locked
time smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 5000000
gives:
902834 locks/sec
real 0m11,103s
user 0m8,233s
sys 0m2,868s
vs.
1037262 locks/sec
real 0m9,685s
user 0m6,788s
sys 0m2,896s
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Jul 8 11:02:39 UTC 2020 on sn-devel-184
2020-05-14 13:32:47 +02:00
struct dbwrap_watched_subrec_wakeup_state {
struct messaging_context * msg_ctx ;
} ;
static void dbwrap_watched_subrec_wakeup_fn (
struct db_record * rec ,
TDB_DATA value ,
void * private_data ) ;
2017-06-27 18:40:28 +02:00
struct dbwrap_watched_do_locked_state {
struct db_context * db ;
2019-10-23 11:34:47 +02:00
void ( * fn ) ( struct db_record * rec ,
TDB_DATA value ,
void * private_data ) ;
2017-06-27 18:40:28 +02:00
void * private_data ;
struct db_watched_subrec subrec ;
s3:dbwrap_watch: avoid recursion into dbwrap_do_locked() from dbwrap_watched_do_locked_{storev,delete}()
This avoids a lot of overhead!
Using smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 500000
under valgrind --tool=callgrind...
This change replaces this:
6,877,542,529 PROGRAM TOTALS
590,000,773 lib/tdb/common/lock.c:tdb_lock_list
479,000,608 lib/tdb/common/lock.c:tdb_unlock
446,500,532 lib/tdb/common/io.c:tdb_read
364,000,824 lib/tdb/common/hash.c:tdb_jenkins_hash
285,000,532 lib/tdb/common/io.c:tdb_write
262,054,669 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
193,000,176 lib/tdb/common/tdb.c:tdb_find
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
140,000,196 lib/tdb/common/lock.c:tdb_lock
130,000,858 lib/util/debug.c:debuglevel_get_class
128,003,722 lib/talloc/talloc.c:_talloc_free
128,000,118 lib/tdb/common/tdb.c:tdb_parse_record
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
108,500,168 lib/tdb/common/io.c:tdb_ofs_read
102,500,000 lib/tdb/common/io.c:tdb_parse_data
by this:
5,706,522,398 PROGRAM TOTALS
434,000,617 lib/tdb/common/lock.c:tdb_lock_list
389,500,494 lib/tdb/common/io.c:tdb_read
359,000,488 lib/tdb/common/lock.c:tdb_unlock
285,000,532 lib/tdb/common/io.c:tdb_write
237,554,655 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
208,000,668 lib/tdb/common/hash.c:tdb_jenkins_hash
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
136,000,132 lib/tdb/common/tdb.c:tdb_find
130,000,858 lib/util/debug.c:debuglevel_get_class
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
112,000,168 lib/tdb/common/lock.c:tdb_lock
94,500,154 lib/tdb/common/io.c:tdb_ofs_read
94,000,188 /nptl/pthread_mutex_unlock.c:__pthread_mutex_unlock_full
86,000,086 lib/dbwrap/dbwrap.c:dbwrap_lock_order_lock
83,000,083 lib/dbwrap/dbwrap_tdb.c:db_tdb_do_locked
time smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 5000000
gives:
902834 locks/sec
real 0m11,103s
user 0m8,233s
sys 0m2,868s
vs.
1037262 locks/sec
real 0m9,685s
user 0m6,788s
sys 0m2,896s
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Jul 8 11:02:39 UTC 2020 on sn-devel-184
2020-05-14 13:32:47 +02:00
/*
* This contains the initial value we got
* passed to dbwrap_watched_do_locked_fn ( )
*
* It ' s only used in order to pass it
* to dbwrap_watched_subrec_wakeup_fn ( )
* in dbwrap_watched_do_locked_ { storev , delete } ( )
*
* It gets cleared after the first call to
* dbwrap_watched_subrec_wakeup_fn ( ) as we
* only need to wakeup once per dbwrap_do_locked ( ) .
*/
TDB_DATA wakeup_value ;
2017-06-27 18:40:28 +02:00
NTSTATUS status ;
} ;
static NTSTATUS dbwrap_watched_do_locked_storev (
struct db_record * rec , const TDB_DATA * dbufs , int num_dbufs ,
int flags )
{
struct dbwrap_watched_do_locked_state * state = rec - > private_data ;
struct db_watched_subrec * subrec = & state - > subrec ;
s3:dbwrap_watch: avoid recursion into dbwrap_do_locked() from dbwrap_watched_do_locked_{storev,delete}()
This avoids a lot of overhead!
Using smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 500000
under valgrind --tool=callgrind...
This change replaces this:
6,877,542,529 PROGRAM TOTALS
590,000,773 lib/tdb/common/lock.c:tdb_lock_list
479,000,608 lib/tdb/common/lock.c:tdb_unlock
446,500,532 lib/tdb/common/io.c:tdb_read
364,000,824 lib/tdb/common/hash.c:tdb_jenkins_hash
285,000,532 lib/tdb/common/io.c:tdb_write
262,054,669 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
193,000,176 lib/tdb/common/tdb.c:tdb_find
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
140,000,196 lib/tdb/common/lock.c:tdb_lock
130,000,858 lib/util/debug.c:debuglevel_get_class
128,003,722 lib/talloc/talloc.c:_talloc_free
128,000,118 lib/tdb/common/tdb.c:tdb_parse_record
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
108,500,168 lib/tdb/common/io.c:tdb_ofs_read
102,500,000 lib/tdb/common/io.c:tdb_parse_data
by this:
5,706,522,398 PROGRAM TOTALS
434,000,617 lib/tdb/common/lock.c:tdb_lock_list
389,500,494 lib/tdb/common/io.c:tdb_read
359,000,488 lib/tdb/common/lock.c:tdb_unlock
285,000,532 lib/tdb/common/io.c:tdb_write
237,554,655 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
208,000,668 lib/tdb/common/hash.c:tdb_jenkins_hash
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
136,000,132 lib/tdb/common/tdb.c:tdb_find
130,000,858 lib/util/debug.c:debuglevel_get_class
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
112,000,168 lib/tdb/common/lock.c:tdb_lock
94,500,154 lib/tdb/common/io.c:tdb_ofs_read
94,000,188 /nptl/pthread_mutex_unlock.c:__pthread_mutex_unlock_full
86,000,086 lib/dbwrap/dbwrap.c:dbwrap_lock_order_lock
83,000,083 lib/dbwrap/dbwrap_tdb.c:db_tdb_do_locked
time smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 5000000
gives:
902834 locks/sec
real 0m11,103s
user 0m8,233s
sys 0m2,868s
vs.
1037262 locks/sec
real 0m9,685s
user 0m6,788s
sys 0m2,896s
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Jul 8 11:02:39 UTC 2020 on sn-devel-184
2020-05-14 13:32:47 +02:00
struct db_watched_ctx * ctx = talloc_get_type_abort (
state - > db - > private_data , struct db_watched_ctx ) ;
struct dbwrap_watched_subrec_wakeup_state wakeup_state = {
. msg_ctx = ctx - > msg ,
} ;
2017-06-27 18:40:28 +02:00
NTSTATUS status ;
s3:dbwrap_watch: avoid recursion into dbwrap_do_locked() from dbwrap_watched_do_locked_{storev,delete}()
This avoids a lot of overhead!
Using smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 500000
under valgrind --tool=callgrind...
This change replaces this:
6,877,542,529 PROGRAM TOTALS
590,000,773 lib/tdb/common/lock.c:tdb_lock_list
479,000,608 lib/tdb/common/lock.c:tdb_unlock
446,500,532 lib/tdb/common/io.c:tdb_read
364,000,824 lib/tdb/common/hash.c:tdb_jenkins_hash
285,000,532 lib/tdb/common/io.c:tdb_write
262,054,669 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
193,000,176 lib/tdb/common/tdb.c:tdb_find
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
140,000,196 lib/tdb/common/lock.c:tdb_lock
130,000,858 lib/util/debug.c:debuglevel_get_class
128,003,722 lib/talloc/talloc.c:_talloc_free
128,000,118 lib/tdb/common/tdb.c:tdb_parse_record
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
108,500,168 lib/tdb/common/io.c:tdb_ofs_read
102,500,000 lib/tdb/common/io.c:tdb_parse_data
by this:
5,706,522,398 PROGRAM TOTALS
434,000,617 lib/tdb/common/lock.c:tdb_lock_list
389,500,494 lib/tdb/common/io.c:tdb_read
359,000,488 lib/tdb/common/lock.c:tdb_unlock
285,000,532 lib/tdb/common/io.c:tdb_write
237,554,655 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
208,000,668 lib/tdb/common/hash.c:tdb_jenkins_hash
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
136,000,132 lib/tdb/common/tdb.c:tdb_find
130,000,858 lib/util/debug.c:debuglevel_get_class
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
112,000,168 lib/tdb/common/lock.c:tdb_lock
94,500,154 lib/tdb/common/io.c:tdb_ofs_read
94,000,188 /nptl/pthread_mutex_unlock.c:__pthread_mutex_unlock_full
86,000,086 lib/dbwrap/dbwrap.c:dbwrap_lock_order_lock
83,000,083 lib/dbwrap/dbwrap_tdb.c:db_tdb_do_locked
time smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 5000000
gives:
902834 locks/sec
real 0m11,103s
user 0m8,233s
sys 0m2,868s
vs.
1037262 locks/sec
real 0m9,685s
user 0m6,788s
sys 0m2,896s
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Jul 8 11:02:39 UTC 2020 on sn-devel-184
2020-05-14 13:32:47 +02:00
/*
* Wakeup only needs to happen once .
* so we clear state - > wakeup_value after the first run
*/
dbwrap_watched_subrec_wakeup_fn ( rec , state - > wakeup_value , & wakeup_state ) ;
state - > wakeup_value = ( TDB_DATA ) { . dsize = 0 , } ;
2017-06-27 18:40:28 +02:00
status = dbwrap_watched_subrec_storev ( rec , subrec , dbufs , num_dbufs ,
flags ) ;
return status ;
}
static NTSTATUS dbwrap_watched_do_locked_delete ( struct db_record * rec )
{
struct dbwrap_watched_do_locked_state * state = rec - > private_data ;
struct db_watched_subrec * subrec = & state - > subrec ;
s3:dbwrap_watch: avoid recursion into dbwrap_do_locked() from dbwrap_watched_do_locked_{storev,delete}()
This avoids a lot of overhead!
Using smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 500000
under valgrind --tool=callgrind...
This change replaces this:
6,877,542,529 PROGRAM TOTALS
590,000,773 lib/tdb/common/lock.c:tdb_lock_list
479,000,608 lib/tdb/common/lock.c:tdb_unlock
446,500,532 lib/tdb/common/io.c:tdb_read
364,000,824 lib/tdb/common/hash.c:tdb_jenkins_hash
285,000,532 lib/tdb/common/io.c:tdb_write
262,054,669 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
193,000,176 lib/tdb/common/tdb.c:tdb_find
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
140,000,196 lib/tdb/common/lock.c:tdb_lock
130,000,858 lib/util/debug.c:debuglevel_get_class
128,003,722 lib/talloc/talloc.c:_talloc_free
128,000,118 lib/tdb/common/tdb.c:tdb_parse_record
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
108,500,168 lib/tdb/common/io.c:tdb_ofs_read
102,500,000 lib/tdb/common/io.c:tdb_parse_data
by this:
5,706,522,398 PROGRAM TOTALS
434,000,617 lib/tdb/common/lock.c:tdb_lock_list
389,500,494 lib/tdb/common/io.c:tdb_read
359,000,488 lib/tdb/common/lock.c:tdb_unlock
285,000,532 lib/tdb/common/io.c:tdb_write
237,554,655 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
208,000,668 lib/tdb/common/hash.c:tdb_jenkins_hash
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
136,000,132 lib/tdb/common/tdb.c:tdb_find
130,000,858 lib/util/debug.c:debuglevel_get_class
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
112,000,168 lib/tdb/common/lock.c:tdb_lock
94,500,154 lib/tdb/common/io.c:tdb_ofs_read
94,000,188 /nptl/pthread_mutex_unlock.c:__pthread_mutex_unlock_full
86,000,086 lib/dbwrap/dbwrap.c:dbwrap_lock_order_lock
83,000,083 lib/dbwrap/dbwrap_tdb.c:db_tdb_do_locked
time smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 5000000
gives:
902834 locks/sec
real 0m11,103s
user 0m8,233s
sys 0m2,868s
vs.
1037262 locks/sec
real 0m9,685s
user 0m6,788s
sys 0m2,896s
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Jul 8 11:02:39 UTC 2020 on sn-devel-184
2020-05-14 13:32:47 +02:00
struct db_watched_ctx * ctx = talloc_get_type_abort (
state - > db - > private_data , struct db_watched_ctx ) ;
struct dbwrap_watched_subrec_wakeup_state wakeup_state = {
. msg_ctx = ctx - > msg ,
} ;
2017-06-27 18:40:28 +02:00
NTSTATUS status ;
s3:dbwrap_watch: avoid recursion into dbwrap_do_locked() from dbwrap_watched_do_locked_{storev,delete}()
This avoids a lot of overhead!
Using smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 500000
under valgrind --tool=callgrind...
This change replaces this:
6,877,542,529 PROGRAM TOTALS
590,000,773 lib/tdb/common/lock.c:tdb_lock_list
479,000,608 lib/tdb/common/lock.c:tdb_unlock
446,500,532 lib/tdb/common/io.c:tdb_read
364,000,824 lib/tdb/common/hash.c:tdb_jenkins_hash
285,000,532 lib/tdb/common/io.c:tdb_write
262,054,669 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
193,000,176 lib/tdb/common/tdb.c:tdb_find
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
140,000,196 lib/tdb/common/lock.c:tdb_lock
130,000,858 lib/util/debug.c:debuglevel_get_class
128,003,722 lib/talloc/talloc.c:_talloc_free
128,000,118 lib/tdb/common/tdb.c:tdb_parse_record
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
108,500,168 lib/tdb/common/io.c:tdb_ofs_read
102,500,000 lib/tdb/common/io.c:tdb_parse_data
by this:
5,706,522,398 PROGRAM TOTALS
434,000,617 lib/tdb/common/lock.c:tdb_lock_list
389,500,494 lib/tdb/common/io.c:tdb_read
359,000,488 lib/tdb/common/lock.c:tdb_unlock
285,000,532 lib/tdb/common/io.c:tdb_write
237,554,655 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
208,000,668 lib/tdb/common/hash.c:tdb_jenkins_hash
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
136,000,132 lib/tdb/common/tdb.c:tdb_find
130,000,858 lib/util/debug.c:debuglevel_get_class
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
112,000,168 lib/tdb/common/lock.c:tdb_lock
94,500,154 lib/tdb/common/io.c:tdb_ofs_read
94,000,188 /nptl/pthread_mutex_unlock.c:__pthread_mutex_unlock_full
86,000,086 lib/dbwrap/dbwrap.c:dbwrap_lock_order_lock
83,000,083 lib/dbwrap/dbwrap_tdb.c:db_tdb_do_locked
time smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 5000000
gives:
902834 locks/sec
real 0m11,103s
user 0m8,233s
sys 0m2,868s
vs.
1037262 locks/sec
real 0m9,685s
user 0m6,788s
sys 0m2,896s
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Jul 8 11:02:39 UTC 2020 on sn-devel-184
2020-05-14 13:32:47 +02:00
/*
* Wakeup only needs to happen once .
* so we clear state - > wakeup_value after the first run
*/
dbwrap_watched_subrec_wakeup_fn ( rec , state - > wakeup_value , & wakeup_state ) ;
state - > wakeup_value = ( TDB_DATA ) { . dsize = 0 , } ;
2017-06-27 18:40:28 +02:00
status = dbwrap_watched_subrec_delete ( rec , subrec ) ;
return status ;
}
2019-10-23 11:34:47 +02:00
static void dbwrap_watched_do_locked_fn (
struct db_record * subrec ,
TDB_DATA subrec_value ,
void * private_data )
2017-06-27 18:40:28 +02:00
{
struct dbwrap_watched_do_locked_state * state =
( struct dbwrap_watched_do_locked_state * ) private_data ;
2019-11-18 21:46:55 +01:00
TDB_DATA value = { 0 } ;
struct db_record rec = {
2019-10-23 11:34:47 +02:00
. db = state - > db ,
. key = dbwrap_record_get_key ( subrec ) ,
2020-04-20 14:41:18 +05:30
. value_valid = true ,
2017-06-27 18:40:28 +02:00
. storev = dbwrap_watched_do_locked_storev ,
. delete_rec = dbwrap_watched_do_locked_delete ,
. private_data = state
} ;
2019-11-18 21:46:55 +01:00
bool ok ;
2017-06-27 18:40:28 +02:00
state - > subrec = ( struct db_watched_subrec ) {
. subrec = subrec
} ;
s3:dbwrap_watch: avoid recursion into dbwrap_do_locked() from dbwrap_watched_do_locked_{storev,delete}()
This avoids a lot of overhead!
Using smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 500000
under valgrind --tool=callgrind...
This change replaces this:
6,877,542,529 PROGRAM TOTALS
590,000,773 lib/tdb/common/lock.c:tdb_lock_list
479,000,608 lib/tdb/common/lock.c:tdb_unlock
446,500,532 lib/tdb/common/io.c:tdb_read
364,000,824 lib/tdb/common/hash.c:tdb_jenkins_hash
285,000,532 lib/tdb/common/io.c:tdb_write
262,054,669 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
193,000,176 lib/tdb/common/tdb.c:tdb_find
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
140,000,196 lib/tdb/common/lock.c:tdb_lock
130,000,858 lib/util/debug.c:debuglevel_get_class
128,003,722 lib/talloc/talloc.c:_talloc_free
128,000,118 lib/tdb/common/tdb.c:tdb_parse_record
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
108,500,168 lib/tdb/common/io.c:tdb_ofs_read
102,500,000 lib/tdb/common/io.c:tdb_parse_data
by this:
5,706,522,398 PROGRAM TOTALS
434,000,617 lib/tdb/common/lock.c:tdb_lock_list
389,500,494 lib/tdb/common/io.c:tdb_read
359,000,488 lib/tdb/common/lock.c:tdb_unlock
285,000,532 lib/tdb/common/io.c:tdb_write
237,554,655 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
208,000,668 lib/tdb/common/hash.c:tdb_jenkins_hash
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
136,000,132 lib/tdb/common/tdb.c:tdb_find
130,000,858 lib/util/debug.c:debuglevel_get_class
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
112,000,168 lib/tdb/common/lock.c:tdb_lock
94,500,154 lib/tdb/common/io.c:tdb_ofs_read
94,000,188 /nptl/pthread_mutex_unlock.c:__pthread_mutex_unlock_full
86,000,086 lib/dbwrap/dbwrap.c:dbwrap_lock_order_lock
83,000,083 lib/dbwrap/dbwrap_tdb.c:db_tdb_do_locked
time smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 5000000
gives:
902834 locks/sec
real 0m11,103s
user 0m8,233s
sys 0m2,868s
vs.
1037262 locks/sec
real 0m9,685s
user 0m6,788s
sys 0m2,896s
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Jul 8 11:02:39 UTC 2020 on sn-devel-184
2020-05-14 13:32:47 +02:00
state - > wakeup_value = subrec_value ;
2017-06-27 18:40:28 +02:00
2019-11-18 21:46:55 +01:00
ok = dbwrap_watch_rec_parse ( subrec_value , NULL , NULL , & value ) ;
if ( ! ok ) {
dbwrap_watch_log_invalid_record ( rec . db , rec . key , subrec_value ) ;
/* wipe invalid data */
value = ( TDB_DATA ) { . dptr = NULL , . dsize = 0 } ;
}
state - > fn ( & rec , value , state - > private_data ) ;
db_watched_subrec_destructor ( & state - > subrec ) ;
2017-06-27 18:40:28 +02:00
}
static NTSTATUS dbwrap_watched_do_locked ( struct db_context * db , TDB_DATA key ,
void ( * fn ) ( struct db_record * rec ,
2019-10-23 11:34:47 +02:00
TDB_DATA value ,
2017-06-27 18:40:28 +02:00
void * private_data ) ,
void * private_data )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
struct dbwrap_watched_do_locked_state state = {
. db = db , . fn = fn , . private_data = private_data
} ;
NTSTATUS status ;
status = dbwrap_do_locked (
ctx - > backend , key , dbwrap_watched_do_locked_fn , & state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_DEBUG ( " dbwrap_do_locked returned %s \n " , nt_errstr ( status ) ) ;
return status ;
}
DBG_DEBUG ( " dbwrap_watched_do_locked_fn returned %s \n " ,
nt_errstr ( state . status ) ) ;
return state . status ;
}
2019-11-18 21:46:55 +01:00
static void dbwrap_watched_subrec_wakeup_fn (
struct db_record * rec ,
TDB_DATA value ,
void * private_data )
{
struct dbwrap_watched_subrec_wakeup_state * state = private_data ;
uint8_t * watchers ;
size_t num_watchers = 0 ;
size_t i ;
bool ok ;
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
ok = dbwrap_watch_rec_parse ( value , & watchers , & num_watchers , NULL ) ;
if ( ! ok ) {
struct db_context * db = dbwrap_record_get_db ( rec ) ;
TDB_DATA key = dbwrap_record_get_key ( rec ) ;
dbwrap_watch_log_invalid_record ( db , key , value ) ;
return ;
}
dbwrap_watch: Don't alert ourselves, fix raw.oplock.batch26 race
This fixes the following flaky test:
UNEXPECTED(failure): samba3.raw.oplock.batch26(nt4_dc)
REASON: Exception: Exception: (../../source4/torture/raw/oplock.c:3718): wrong value for break_info.count got 0x2 - should be 0x1
You can reproduce it with two small msleeps, which means it's a race
condition:
diff --git a/source3/smbd/open.c b/source3/smbd/open.c
index 20b5a3e294c..126c7fc021d 100644
--- a/source3/smbd/open.c
+++ b/source3/smbd/open.c
@@ -1917,6 +1917,14 @@ NTSTATUS send_break_message(struct messaging_context *msg_ctx,
DATA_BLOB blob;
NTSTATUS status;
+ {
+ static bool sent = false;
+ if (sent) {
+ smb_msleep(500);
+ }
+ sent = true;
+ }
+
if (DEBUGLVL(10)) {
struct server_id_buf buf;
DBG_DEBUG("Sending break message to %s\n",
diff --git a/source3/smbd/oplock.c b/source3/smbd/oplock.c
index b3da84b1269..d9c4dbb9487 100644
--- a/source3/smbd/oplock.c
+++ b/source3/smbd/oplock.c
@@ -858,6 +858,8 @@ static void process_oplock_break_message(struct messaging_context *msg_ctx,
uint16_t break_to;
bool break_needed = true;
+ smb_msleep(100);
+
msg = talloc(talloc_tos(), struct oplock_break_message);
if (msg == NULL) {
DBG_WARNING("talloc failed\n");
15a8af075a2 introduced a bug where we immediately wake up ourselves
after doing a watch_send, leading to two inter-smbd oplock break
messages for this case. In theory, this should not matter, as in the
oplock break handler in the destination smbd we check
(fsp->sent_oplock_break != NO_BREAK_SENT)
so that the break does not get sent twice. However, with the above two
sleeps the oplock holding client could send out its oplock downgrade
while the second inter-smbd break messages was on its way.
The real fix would be to note in the share mode array that the
inter-smbd message has already been sent, but as other users of
dbwrap_watched_watch_send might also be affected by this bug, this fix
should be sufficient to get rid of this flaky test.
Unfortunately, dbwrap_watch.c is now pretty complex and needs some
serious refactoring to become understandable again. But that's
something for another day, sorry.
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2019-09-30 11:39:11 +02:00
2019-11-18 21:46:55 +01:00
if ( num_watchers = = 0 ) {
DBG_DEBUG ( " No watchers \n " ) ;
return ;
dbwrap_watch: Don't alert ourselves, fix raw.oplock.batch26 race
This fixes the following flaky test:
UNEXPECTED(failure): samba3.raw.oplock.batch26(nt4_dc)
REASON: Exception: Exception: (../../source4/torture/raw/oplock.c:3718): wrong value for break_info.count got 0x2 - should be 0x1
You can reproduce it with two small msleeps, which means it's a race
condition:
diff --git a/source3/smbd/open.c b/source3/smbd/open.c
index 20b5a3e294c..126c7fc021d 100644
--- a/source3/smbd/open.c
+++ b/source3/smbd/open.c
@@ -1917,6 +1917,14 @@ NTSTATUS send_break_message(struct messaging_context *msg_ctx,
DATA_BLOB blob;
NTSTATUS status;
+ {
+ static bool sent = false;
+ if (sent) {
+ smb_msleep(500);
+ }
+ sent = true;
+ }
+
if (DEBUGLVL(10)) {
struct server_id_buf buf;
DBG_DEBUG("Sending break message to %s\n",
diff --git a/source3/smbd/oplock.c b/source3/smbd/oplock.c
index b3da84b1269..d9c4dbb9487 100644
--- a/source3/smbd/oplock.c
+++ b/source3/smbd/oplock.c
@@ -858,6 +858,8 @@ static void process_oplock_break_message(struct messaging_context *msg_ctx,
uint16_t break_to;
bool break_needed = true;
+ smb_msleep(100);
+
msg = talloc(talloc_tos(), struct oplock_break_message);
if (msg == NULL) {
DBG_WARNING("talloc failed\n");
15a8af075a2 introduced a bug where we immediately wake up ourselves
after doing a watch_send, leading to two inter-smbd oplock break
messages for this case. In theory, this should not matter, as in the
oplock break handler in the destination smbd we check
(fsp->sent_oplock_break != NO_BREAK_SENT)
so that the break does not get sent twice. However, with the above two
sleeps the oplock holding client could send out its oplock downgrade
while the second inter-smbd break messages was on its way.
The real fix would be to note in the share mode array that the
inter-smbd message has already been sent, but as other users of
dbwrap_watched_watch_send might also be affected by this bug, this fix
should be sufficient to get rid of this flaky test.
Unfortunately, dbwrap_watch.c is now pretty complex and needs some
serious refactoring to become understandable again. But that's
something for another day, sorry.
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
2019-09-30 11:39:11 +02:00
}
2019-11-18 21:46:55 +01:00
for ( i = 0 ; i < num_watchers ; i + + ) {
2019-11-18 12:37:21 +01:00
struct dbwrap_watcher watcher ;
2016-07-12 15:59:56 +02:00
struct server_id_buf tmp ;
2019-11-18 13:36:58 +01:00
uint8_t instance_buf [ 8 ] ;
2019-11-18 21:46:55 +01:00
NTSTATUS status ;
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
dbwrap_watcher_get (
& watcher , watchers + i * DBWRAP_WATCHER_BUF_LENGTH ) ;
2016-07-12 15:59:56 +02:00
2019-11-18 13:23:52 +01:00
DBG_DEBUG ( " Alerting %s:% " PRIu64 " \n " ,
server_id_str_buf ( watcher . pid , & tmp ) ,
watcher . instance ) ;
2019-11-18 12:37:21 +01:00
2019-11-18 13:36:58 +01:00
SBVAL ( instance_buf , 0 , watcher . instance ) ;
status = messaging_send_buf (
2019-11-18 21:46:55 +01:00
state - > msg_ctx ,
2019-11-18 12:37:21 +01:00
watcher . pid ,
MSG_DBWRAP_MODIFIED ,
2019-11-18 13:36:58 +01:00
instance_buf ,
sizeof ( instance_buf ) ) ;
2016-07-12 15:59:56 +02:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2019-11-18 21:46:55 +01:00
DBG_DEBUG ( " messaging_send_buf to %s failed: %s \n " ,
2019-11-18 12:37:21 +01:00
server_id_str_buf ( watcher . pid , & tmp ) ,
2016-07-12 15:59:56 +02:00
nt_errstr ( status ) ) ;
}
}
}
2019-11-18 21:46:55 +01:00
static void dbwrap_watched_subrec_wakeup (
struct db_record * rec , struct db_watched_subrec * subrec )
2016-07-12 15:59:56 +02:00
{
2019-11-18 21:46:55 +01:00
struct db_context * backend = dbwrap_record_get_db ( subrec - > subrec ) ;
struct db_context * db = dbwrap_record_get_db ( rec ) ;
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
struct dbwrap_watched_subrec_wakeup_state state = {
. msg_ctx = ctx - > msg ,
2019-08-13 14:18:05 +02:00
} ;
2019-11-18 21:46:55 +01:00
NTSTATUS status ;
2019-08-13 14:18:05 +02:00
s3:dbwrap_watch: avoid recursion into dbwrap_do_locked() from dbwrap_watched_do_locked_{storev,delete}()
This avoids a lot of overhead!
Using smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 500000
under valgrind --tool=callgrind...
This change replaces this:
6,877,542,529 PROGRAM TOTALS
590,000,773 lib/tdb/common/lock.c:tdb_lock_list
479,000,608 lib/tdb/common/lock.c:tdb_unlock
446,500,532 lib/tdb/common/io.c:tdb_read
364,000,824 lib/tdb/common/hash.c:tdb_jenkins_hash
285,000,532 lib/tdb/common/io.c:tdb_write
262,054,669 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
193,000,176 lib/tdb/common/tdb.c:tdb_find
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
140,000,196 lib/tdb/common/lock.c:tdb_lock
130,000,858 lib/util/debug.c:debuglevel_get_class
128,003,722 lib/talloc/talloc.c:_talloc_free
128,000,118 lib/tdb/common/tdb.c:tdb_parse_record
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
108,500,168 lib/tdb/common/io.c:tdb_ofs_read
102,500,000 lib/tdb/common/io.c:tdb_parse_data
by this:
5,706,522,398 PROGRAM TOTALS
434,000,617 lib/tdb/common/lock.c:tdb_lock_list
389,500,494 lib/tdb/common/io.c:tdb_read
359,000,488 lib/tdb/common/lock.c:tdb_unlock
285,000,532 lib/tdb/common/io.c:tdb_write
237,554,655 /x86_64/multiarch/memmove-vec-unaligned-erms.S:__memcpy_avx_unaligned_erms
208,000,668 lib/tdb/common/hash.c:tdb_jenkins_hash
206,500,496 lib/tdb/common/mutex.c:tdb_mutex_lock
160,000,256 lib/talloc/talloc.c:_talloc_get_type_abort
148,500,297 lib/tdb/common/tdb.c:tdb_storev
136,000,132 lib/tdb/common/tdb.c:tdb_find
130,000,858 lib/util/debug.c:debuglevel_get_class
126,000,576 lib/tdb/common/lock.c:tdb_brlock.part.3
121,000,272 lib/tdb/common/mutex.c:tdb_mutex_unlock
118,000,225 /nptl/pthread_mutex_lock.c:__pthread_mutex_lock_full
112,750,222 lib/tdb/common/freelist.c:tdb_allocate_from_freelist
112,000,168 lib/tdb/common/lock.c:tdb_lock
94,500,154 lib/tdb/common/io.c:tdb_ofs_read
94,000,188 /nptl/pthread_mutex_unlock.c:__pthread_mutex_unlock_full
86,000,086 lib/dbwrap/dbwrap.c:dbwrap_lock_order_lock
83,000,083 lib/dbwrap/dbwrap_tdb.c:db_tdb_do_locked
time smbtorture3 //foo/bar -U% local-g-lock-ping-pong -o 5000000
gives:
902834 locks/sec
real 0m11,103s
user 0m8,233s
sys 0m2,868s
vs.
1037262 locks/sec
real 0m9,685s
user 0m6,788s
sys 0m2,896s
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Volker Lendecke <vl@samba.org>
Autobuild-Date(master): Wed Jul 8 11:02:39 UTC 2020 on sn-devel-184
2020-05-14 13:32:47 +02:00
if ( rec - > storev = = dbwrap_watched_do_locked_storev ) {
/*
* This is handled in the caller ,
* as we need to avoid recursion
* into dbwrap_do_locked ( ) .
*/
return ;
}
2019-11-18 21:46:55 +01:00
status = dbwrap_do_locked (
backend ,
subrec - > subrec - > key ,
dbwrap_watched_subrec_wakeup_fn ,
& state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_DEBUG ( " dbwrap_record_modify failed: %s \n " ,
nt_errstr ( status ) ) ;
2017-07-01 18:13:44 +02:00
}
2016-07-12 15:59:56 +02:00
}
2017-06-27 18:40:28 +02:00
static NTSTATUS dbwrap_watched_subrec_storev (
struct db_record * rec , struct db_watched_subrec * subrec ,
const TDB_DATA * dbufs , int num_dbufs , int flags )
2016-07-12 15:59:56 +02:00
{
2019-11-18 21:46:55 +01:00
uint8_t num_watchers_buf [ 4 ] = { 0 } ;
TDB_DATA my_dbufs [ num_dbufs + 1 ] ;
2016-09-12 17:30:55 +02:00
NTSTATUS status ;
2019-05-24 14:49:47 +02:00
dbwrap_watched_subrec_wakeup ( rec , subrec ) ;
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
/*
* Watchers only informed once , set num_watchers to 0
*/
my_dbufs [ 0 ] = ( TDB_DATA ) {
. dptr = num_watchers_buf , . dsize = sizeof ( num_watchers_buf ) ,
} ;
if ( num_dbufs ! = 0 ) {
memcpy ( my_dbufs + 1 , dbufs , num_dbufs * sizeof ( * dbufs ) ) ;
}
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
status = dbwrap_record_storev (
subrec - > subrec , my_dbufs , ARRAY_SIZE ( my_dbufs ) , flags ) ;
2016-09-12 17:30:55 +02:00
return status ;
2016-07-12 15:59:56 +02:00
}
2017-06-27 18:40:28 +02:00
static NTSTATUS dbwrap_watched_storev ( struct db_record * rec ,
const TDB_DATA * dbufs , int num_dbufs ,
int flags )
2016-07-12 15:59:56 +02:00
{
struct db_watched_subrec * subrec = talloc_get_type_abort (
rec - > private_data , struct db_watched_subrec ) ;
2017-06-27 18:40:28 +02:00
NTSTATUS status ;
status = dbwrap_watched_subrec_storev ( rec , subrec , dbufs , num_dbufs ,
flags ) ;
return status ;
}
static NTSTATUS dbwrap_watched_subrec_delete (
struct db_record * rec , struct db_watched_subrec * subrec )
{
NTSTATUS status ;
2016-07-12 15:59:56 +02:00
2019-05-24 14:49:47 +02:00
dbwrap_watched_subrec_wakeup ( rec , subrec ) ;
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
/*
* Watchers were informed , we can throw away the record now
*/
status = dbwrap_record_delete ( subrec - > subrec ) ;
2017-06-27 18:40:28 +02:00
return status ;
}
static NTSTATUS dbwrap_watched_delete ( struct db_record * rec )
{
struct db_watched_subrec * subrec = talloc_get_type_abort (
rec - > private_data , struct db_watched_subrec ) ;
NTSTATUS status ;
status = dbwrap_watched_subrec_delete ( rec , subrec ) ;
return status ;
2016-07-12 15:59:56 +02:00
}
struct dbwrap_watched_traverse_state {
int ( * fn ) ( struct db_record * rec , void * private_data ) ;
void * private_data ;
} ;
static int dbwrap_watched_traverse_fn ( struct db_record * rec ,
void * private_data )
{
struct dbwrap_watched_traverse_state * state = private_data ;
struct db_record prec = * rec ;
2017-07-01 18:13:44 +02:00
bool ok ;
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
ok = dbwrap_watch_rec_parse ( rec - > value , NULL , NULL , & prec . value ) ;
if ( ! ok ) {
2016-07-12 15:59:56 +02:00
return 0 ;
}
2019-10-24 16:41:47 +02:00
prec . value_valid = true ;
2017-07-01 18:13:44 +02:00
2016-07-12 15:59:56 +02:00
return state - > fn ( & prec , state - > private_data ) ;
}
static int dbwrap_watched_traverse ( struct db_context * db ,
int ( * fn ) ( struct db_record * rec ,
void * private_data ) ,
void * private_data )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
struct dbwrap_watched_traverse_state state = {
. fn = fn , . private_data = private_data } ;
NTSTATUS status ;
int ret ;
status = dbwrap_traverse (
ctx - > backend , dbwrap_watched_traverse_fn , & state , & ret ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return - 1 ;
}
return ret ;
}
static int dbwrap_watched_traverse_read ( struct db_context * db ,
int ( * fn ) ( struct db_record * rec ,
void * private_data ) ,
void * private_data )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
struct dbwrap_watched_traverse_state state = {
. fn = fn , . private_data = private_data } ;
NTSTATUS status ;
int ret ;
status = dbwrap_traverse_read (
ctx - > backend , dbwrap_watched_traverse_fn , & state , & ret ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return - 1 ;
}
return ret ;
}
static int dbwrap_watched_get_seqnum ( struct db_context * db )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
return dbwrap_get_seqnum ( ctx - > backend ) ;
}
static int dbwrap_watched_transaction_start ( struct db_context * db )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
return dbwrap_transaction_start ( ctx - > backend ) ;
}
static int dbwrap_watched_transaction_commit ( struct db_context * db )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
return dbwrap_transaction_commit ( ctx - > backend ) ;
}
static int dbwrap_watched_transaction_cancel ( struct db_context * db )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
return dbwrap_transaction_cancel ( ctx - > backend ) ;
}
struct dbwrap_watched_parse_record_state {
2019-11-18 21:46:55 +01:00
struct db_context * db ;
2016-07-12 15:59:56 +02:00
void ( * parser ) ( TDB_DATA key , TDB_DATA data , void * private_data ) ;
void * private_data ;
2019-11-18 21:46:55 +01:00
bool ok ;
2016-07-12 15:59:56 +02:00
} ;
static void dbwrap_watched_parse_record_parser ( TDB_DATA key , TDB_DATA data ,
void * private_data )
{
struct dbwrap_watched_parse_record_state * state = private_data ;
2019-11-18 21:46:55 +01:00
TDB_DATA userdata ;
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
state - > ok = dbwrap_watch_rec_parse ( data , NULL , NULL , & userdata ) ;
if ( ! state - > ok ) {
dbwrap_watch_log_invalid_record ( state - > db , key , data ) ;
2016-07-12 15:59:56 +02:00
return ;
}
2017-07-01 18:13:44 +02:00
2019-11-18 21:46:55 +01:00
state - > parser ( key , userdata , state - > private_data ) ;
2016-07-12 15:59:56 +02:00
}
static NTSTATUS dbwrap_watched_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_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
struct dbwrap_watched_parse_record_state state = {
2019-11-18 21:46:55 +01:00
. db = db ,
2016-08-07 17:56:51 +10:00
. parser = parser ,
. private_data = private_data ,
2016-07-12 15:59:56 +02:00
} ;
NTSTATUS status ;
status = dbwrap_parse_record (
ctx - > backend , key , dbwrap_watched_parse_record_parser , & state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
2019-11-18 21:46:55 +01:00
if ( ! state . ok ) {
2016-07-12 15:59:56 +02:00
return NT_STATUS_NOT_FOUND ;
}
return NT_STATUS_OK ;
}
2016-12-26 10:15:11 +01:00
static void dbwrap_watched_parse_record_done ( struct tevent_req * subreq ) ;
static struct tevent_req * dbwrap_watched_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_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
struct tevent_req * req = NULL ;
struct tevent_req * subreq = NULL ;
struct dbwrap_watched_parse_record_state * state = NULL ;
req = tevent_req_create ( mem_ctx , & state ,
struct dbwrap_watched_parse_record_state ) ;
if ( req = = NULL ) {
* req_state = DBWRAP_REQ_ERROR ;
return NULL ;
}
* state = ( struct dbwrap_watched_parse_record_state ) {
. parser = parser ,
. private_data = private_data ,
2019-11-18 21:46:55 +01:00
. ok = true ,
2016-12-26 10:15:11 +01:00
} ;
subreq = dbwrap_parse_record_send ( state ,
ev ,
ctx - > backend ,
key ,
dbwrap_watched_parse_record_parser ,
state ,
req_state ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
* req_state = DBWRAP_REQ_ERROR ;
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , dbwrap_watched_parse_record_done , req ) ;
return req ;
}
static void dbwrap_watched_parse_record_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct dbwrap_watched_parse_record_state * state = tevent_req_data (
req , struct dbwrap_watched_parse_record_state ) ;
NTSTATUS status ;
status = dbwrap_parse_record_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2019-11-18 21:46:55 +01:00
if ( ! state - > ok ) {
2016-12-26 10:15:11 +01:00
tevent_req_nterror ( req , NT_STATUS_NOT_FOUND ) ;
return ;
}
tevent_req_done ( req ) ;
return ;
}
static NTSTATUS dbwrap_watched_parse_record_recv ( struct tevent_req * req )
{
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
tevent_req_received ( req ) ;
return status ;
}
tevent_req_received ( req ) ;
return NT_STATUS_OK ;
}
2016-07-12 15:59:56 +02:00
static int dbwrap_watched_exists ( struct db_context * db , TDB_DATA key )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
return dbwrap_exists ( ctx - > backend , key ) ;
}
static size_t dbwrap_watched_id ( struct db_context * db , uint8_t * id ,
size_t idlen )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
return dbwrap_db_id ( ctx - > backend , id , idlen ) ;
}
struct db_context * db_open_watched ( TALLOC_CTX * mem_ctx ,
2018-08-16 11:25:54 +02:00
struct db_context * * backend ,
2016-07-12 15:59:56 +02:00
struct messaging_context * msg )
{
struct db_context * db ;
struct db_watched_ctx * ctx ;
db = talloc_zero ( mem_ctx , struct db_context ) ;
if ( db = = NULL ) {
return NULL ;
}
ctx = talloc_zero ( db , struct db_watched_ctx ) ;
if ( ctx = = NULL ) {
TALLOC_FREE ( db ) ;
return NULL ;
}
db - > private_data = ctx ;
ctx - > msg = msg ;
2018-08-16 11:25:54 +02:00
ctx - > backend = talloc_move ( ctx , backend ) ;
db - > lock_order = ctx - > backend - > lock_order ;
ctx - > backend - > lock_order = DBWRAP_LOCK_ORDER_NONE ;
2016-07-12 15:59:56 +02:00
db - > fetch_locked = dbwrap_watched_fetch_locked ;
2017-06-27 18:40:28 +02:00
db - > do_locked = dbwrap_watched_do_locked ;
2016-07-12 15:59:56 +02:00
db - > traverse = dbwrap_watched_traverse ;
db - > traverse_read = dbwrap_watched_traverse_read ;
db - > get_seqnum = dbwrap_watched_get_seqnum ;
db - > transaction_start = dbwrap_watched_transaction_start ;
db - > transaction_commit = dbwrap_watched_transaction_commit ;
db - > transaction_cancel = dbwrap_watched_transaction_cancel ;
db - > parse_record = dbwrap_watched_parse_record ;
2016-12-26 10:15:11 +01:00
db - > parse_record_send = dbwrap_watched_parse_record_send ;
db - > parse_record_recv = dbwrap_watched_parse_record_recv ;
2016-07-12 15:59:56 +02:00
db - > exists = dbwrap_watched_exists ;
db - > id = dbwrap_watched_id ;
db - > name = dbwrap_name ( ctx - > backend ) ;
return db ;
}
struct dbwrap_watched_watch_state {
struct db_context * db ;
2019-11-18 13:43:13 +01:00
TDB_DATA key ;
2019-11-18 21:46:55 +01:00
struct dbwrap_watcher watcher ;
2016-07-12 15:59:56 +02:00
struct server_id blocker ;
bool blockerdead ;
} ;
static bool dbwrap_watched_msg_filter ( struct messaging_rec * rec ,
void * private_data ) ;
static void dbwrap_watched_watch_done ( struct tevent_req * subreq ) ;
static void dbwrap_watched_watch_blocker_died ( struct tevent_req * subreq ) ;
static int dbwrap_watched_watch_state_destructor (
struct dbwrap_watched_watch_state * state ) ;
struct tevent_req * dbwrap_watched_watch_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct db_record * rec ,
struct server_id blocker )
{
struct db_context * db = dbwrap_record_get_db ( rec ) ;
struct db_watched_ctx * ctx = talloc_get_type_abort (
db - > private_data , struct db_watched_ctx ) ;
2017-06-27 18:40:28 +02:00
struct db_watched_subrec * subrec = NULL ;
2016-07-12 15:59:56 +02:00
struct tevent_req * req , * subreq ;
struct dbwrap_watched_watch_state * state ;
2019-11-18 13:23:52 +01:00
static uint64_t instance = 1 ;
2016-07-12 15:59:56 +02:00
req = tevent_req_create ( mem_ctx , & state ,
struct dbwrap_watched_watch_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > db = db ;
state - > blocker = blocker ;
if ( ctx - > msg = = NULL ) {
tevent_req_nterror ( req , NT_STATUS_NOT_SUPPORTED ) ;
return tevent_req_post ( req , ev ) ;
}
2017-06-27 18:40:28 +02:00
/*
* Figure out whether we ' re called as part of do_locked . If
* so , we can ' t use talloc_get_type_abort , the
* db_watched_subrec is stack - allocated in that case .
*/
if ( rec - > storev = = dbwrap_watched_storev ) {
subrec = talloc_get_type_abort ( rec - > private_data ,
struct db_watched_subrec ) ;
}
if ( rec - > storev = = dbwrap_watched_do_locked_storev ) {
struct dbwrap_watched_do_locked_state * do_locked_state ;
do_locked_state = rec - > private_data ;
subrec = & do_locked_state - > subrec ;
}
if ( subrec = = NULL ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER ) ;
return tevent_req_post ( req , ev ) ;
}
2019-11-18 21:46:55 +01:00
if ( subrec - > added . pid . pid ! = 0 ) {
2019-11-17 16:45:45 +01:00
tevent_req_nterror ( req , NT_STATUS_REQUEST_NOT_ACCEPTED ) ;
return tevent_req_post ( req , ev ) ;
}
2017-06-27 18:40:28 +02:00
2019-11-18 12:37:21 +01:00
state - > watcher = ( struct dbwrap_watcher ) {
. pid = messaging_server_id ( ctx - > msg ) ,
2019-11-18 13:23:52 +01:00
. instance = instance + + ,
2019-11-18 12:37:21 +01:00
} ;
2019-11-18 21:46:55 +01:00
subrec - > added = state - > watcher ;
2016-07-12 15:59:56 +02:00
2019-11-18 13:43:13 +01:00
state - > key = tdb_data_talloc_copy ( state , rec - > key ) ;
if ( tevent_req_nomem ( state - > key . dptr , req ) ) {
2016-07-12 15:59:56 +02:00
return tevent_req_post ( req , ev ) ;
}
subreq = messaging_filtered_read_send (
state , ev , ctx - > msg , dbwrap_watched_msg_filter , state ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , dbwrap_watched_watch_done , req ) ;
talloc_set_destructor ( state , dbwrap_watched_watch_state_destructor ) ;
if ( blocker . pid ! = 0 ) {
2020-01-14 13:03:45 +01:00
subreq = server_id_watch_send ( state , ev , blocker ) ;
2016-07-12 15:59:56 +02:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback (
subreq , dbwrap_watched_watch_blocker_died , req ) ;
}
return req ;
}
static void dbwrap_watched_watch_blocker_died ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct dbwrap_watched_watch_state * state = tevent_req_data (
req , struct dbwrap_watched_watch_state ) ;
int ret ;
ret = server_id_watch_recv ( subreq , NULL ) ;
TALLOC_FREE ( subreq ) ;
if ( ret ! = 0 ) {
tevent_req_nterror ( req , map_nt_error_from_unix ( ret ) ) ;
return ;
}
state - > blockerdead = true ;
tevent_req_done ( req ) ;
}
2019-11-18 21:46:55 +01:00
static void dbwrap_watched_watch_state_destructor_fn (
struct db_record * rec ,
TDB_DATA value ,
void * private_data )
2016-07-12 15:59:56 +02:00
{
2019-11-18 21:46:55 +01:00
struct dbwrap_watched_watch_state * state = talloc_get_type_abort (
private_data , struct dbwrap_watched_watch_state ) ;
uint8_t * watchers ;
size_t num_watchers = 0 ;
2017-07-01 18:13:44 +02:00
size_t i ;
2019-11-18 21:46:55 +01:00
bool ok ;
NTSTATUS status ;
uint8_t num_watchers_buf [ 4 ] ;
TDB_DATA dbufs [ 4 ] = {
{
. dptr = num_watchers_buf ,
. dsize = sizeof ( num_watchers_buf ) ,
} ,
{ 0 } , /* watchers "before" state->w */
{ 0 } , /* watchers "behind" state->w */
{ 0 } , /* filled in with data */
} ;
ok = dbwrap_watch_rec_parse (
value , & watchers , & num_watchers , & dbufs [ 3 ] ) ;
if ( ! ok ) {
status = dbwrap_record_delete ( rec ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_DEBUG ( " dbwrap_record_delete failed: %s \n " ,
nt_errstr ( status ) ) ;
}
return ;
}
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
for ( i = 0 ; i < num_watchers ; i + + ) {
2019-11-18 12:37:21 +01:00
struct dbwrap_watcher watcher ;
2019-11-18 21:46:55 +01:00
dbwrap_watcher_get (
& watcher , watchers + i * DBWRAP_WATCHER_BUF_LENGTH ) ;
if ( ( state - > watcher . instance = = watcher . instance ) & &
server_id_equal ( & state - > watcher . pid , & watcher . pid ) ) {
2016-07-12 15:59:56 +02:00
break ;
}
}
2019-11-18 21:46:55 +01:00
if ( i = = num_watchers ) {
2016-09-20 10:52:46 -07:00
struct server_id_buf buf ;
2019-11-18 21:46:55 +01:00
DBG_DEBUG ( " Watcher %s:% " PRIu64 " not found \n " ,
server_id_str_buf ( state - > watcher . pid , & buf ) ,
state - > watcher . instance ) ;
return ;
2016-07-12 15:59:56 +02:00
}
2019-11-18 21:46:55 +01:00
if ( i > 0 ) {
dbufs [ 1 ] = ( TDB_DATA ) {
. dptr = watchers ,
. dsize = i * DBWRAP_WATCHER_BUF_LENGTH ,
} ;
}
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
if ( i < ( num_watchers - 1 ) ) {
size_t behind = ( num_watchers - 1 - i ) ;
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
dbufs [ 2 ] = ( TDB_DATA ) {
. dptr = watchers + ( i + 1 ) * DBWRAP_WATCHER_BUF_LENGTH ,
. dsize = behind * DBWRAP_WATCHER_BUF_LENGTH ,
} ;
2016-07-12 15:59:56 +02:00
}
2019-11-18 21:46:55 +01:00
num_watchers - = 1 ;
2016-07-12 15:59:56 +02:00
2019-11-18 21:46:55 +01:00
if ( ( num_watchers = = 0 ) & & ( dbufs [ 3 ] . dsize = = 0 ) ) {
status = dbwrap_record_delete ( rec ) ;
2016-07-12 15:59:56 +02:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2019-11-18 21:46:55 +01:00
DBG_DEBUG ( " dbwrap_record_delete() failed: %s \n " ,
nt_errstr ( status ) ) ;
2016-07-12 15:59:56 +02:00
}
2019-11-18 21:46:55 +01:00
return ;
2016-07-12 15:59:56 +02:00
}
2019-11-18 21:46:55 +01:00
SIVAL ( num_watchers_buf , 0 , num_watchers ) ;
status = dbwrap_record_storev ( rec , dbufs , ARRAY_SIZE ( dbufs ) , 0 ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_DEBUG ( " dbwrap_record_storev() failed: %s \n " ,
nt_errstr ( status ) ) ;
}
}
static int dbwrap_watched_watch_state_destructor (
struct dbwrap_watched_watch_state * state )
{
struct db_watched_ctx * ctx = talloc_get_type_abort (
state - > db - > private_data , struct db_watched_ctx ) ;
NTSTATUS status ;
status = dbwrap_do_locked (
ctx - > backend ,
state - > key ,
dbwrap_watched_watch_state_destructor_fn ,
state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_DEBUG ( " dbwrap_do_locked failed: %s \n " ,
nt_errstr ( status ) ) ;
}
2016-07-12 15:59:56 +02:00
return 0 ;
}
static bool dbwrap_watched_msg_filter ( struct messaging_rec * rec ,
void * private_data )
{
struct dbwrap_watched_watch_state * state = talloc_get_type_abort (
private_data , struct dbwrap_watched_watch_state ) ;
2019-11-18 13:36:58 +01:00
uint64_t instance ;
2016-07-12 15:59:56 +02:00
if ( rec - > msg_type ! = MSG_DBWRAP_MODIFIED ) {
return false ;
}
if ( rec - > num_fds ! = 0 ) {
return false ;
}
2019-11-18 13:36:58 +01:00
if ( rec - > buf . length ! = sizeof ( instance ) ) {
DBG_DEBUG ( " Got size %zu, expected %zu \n " ,
rec - > buf . length ,
sizeof ( instance ) ) ;
2016-07-12 15:59:56 +02:00
return false ;
}
2019-11-18 13:36:58 +01:00
instance = BVAL ( rec - > buf . data , 0 ) ;
if ( instance ! = state - > watcher . instance ) {
DBG_DEBUG ( " Got instance % " PRIu64 " , expected % " PRIu64 " \n " ,
instance ,
state - > watcher . instance ) ;
return false ;
}
2016-07-12 15:59:56 +02:00
2019-11-18 13:36:58 +01:00
return true ;
2016-07-12 15:59:56 +02:00
}
static void dbwrap_watched_watch_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
2019-11-18 21:46:55 +01:00
struct dbwrap_watched_watch_state * state = tevent_req_data (
req , struct dbwrap_watched_watch_state ) ;
2016-07-12 15:59:56 +02:00
struct messaging_rec * rec ;
int ret ;
ret = messaging_filtered_read_recv ( subreq , talloc_tos ( ) , & rec ) ;
TALLOC_FREE ( subreq ) ;
if ( ret ! = 0 ) {
tevent_req_nterror ( req , map_nt_error_from_unix ( ret ) ) ;
return ;
}
2019-11-18 21:46:55 +01:00
/*
* No need to remove ourselves anymore , we ' ve been removed by
* dbwrap_watched_subrec_wakeup ( ) .
*/
talloc_set_destructor ( state , NULL ) ;
2016-07-12 15:59:56 +02:00
tevent_req_done ( req ) ;
}
NTSTATUS dbwrap_watched_watch_recv ( struct tevent_req * req ,
bool * blockerdead ,
struct server_id * blocker )
{
struct dbwrap_watched_watch_state * state = tevent_req_data (
req , struct dbwrap_watched_watch_state ) ;
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
return status ;
}
if ( blockerdead ! = NULL ) {
* blockerdead = state - > blockerdead ;
}
if ( blocker ! = NULL ) {
* blocker = state - > blocker ;
}
return NT_STATUS_OK ;
}
2017-07-01 08:27:57 +02:00