2007-09-13 03:50:21 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1998-08-17 17:11:34 +04:00
file closing
Copyright ( C ) Andrew Tridgell 1992 - 1998
2007-02-07 00:05:34 +03:00
Copyright ( C ) Jeremy Allison 1992 - 2007.
2005-07-08 08:51:27 +04:00
Copyright ( C ) Volker Lendecke 2005
2007-09-13 03:50:21 +04:00
1998-08-17 17:11:34 +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
1998-08-17 17:11:34 +04:00
( at your option ) any later version .
2007-09-13 03:50:21 +04:00
1998-08-17 17:11:34 +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 .
2007-09-13 03:50:21 +04:00
1998-08-17 17:11:34 +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/>.
1998-08-17 17:11:34 +04:00
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2017-01-01 23:00:55 +03:00
# include "lib/util/server_id.h"
2010-05-04 12:28:48 +04:00
# include "printing.h"
2020-10-28 14:09:39 +03:00
# include "locking/share_mode_lock.h"
2011-03-22 18:57:01 +03:00
# include "smbd/smbd.h"
2010-07-04 20:25:02 +04:00
# include "smbd/globals.h"
2021-10-26 14:48:28 +03:00
# include "smbd/smbXsrv_open.h"
2013-03-20 13:01:43 +04:00
# include "smbd/scavenger.h"
2010-10-03 12:46:22 +04:00
# include "fake_file.h"
2011-02-26 02:56:46 +03:00
# include "transfer_file.h"
2011-03-24 15:46:20 +03:00
# include "auth.h"
2011-03-24 17:31:06 +03:00
# include "messages.h"
2011-11-24 17:11:28 +04:00
# include "../librpc/gen_ndr/open_files.h"
2020-03-09 13:31:00 +03:00
# include "lib/util/tevent_ntstatus.h"
1998-08-17 17:11:34 +04:00
/****************************************************************************
2003-01-03 21:50:13 +03:00
Run a file if it is a magic script .
1998-08-17 17:11:34 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-01-03 21:50:13 +03:00
2009-06-07 01:27:31 +04:00
static NTSTATUS check_magic ( struct files_struct * fsp )
1998-08-17 17:11:34 +04:00
{
2007-09-13 03:50:21 +04:00
int ret ;
2019-10-31 20:47:43 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
2007-09-13 03:50:21 +04:00
const char * magic_output = NULL ;
SMB_STRUCT_STAT st ;
int tmp_fd , outfd ;
TALLOC_CTX * ctx = NULL ;
const char * p ;
2007-12-22 04:13:02 +03:00
struct connection_struct * conn = fsp - > conn ;
2009-07-11 01:50:37 +04:00
char * fname = NULL ;
NTSTATUS status ;
2007-09-13 03:50:21 +04:00
2019-10-31 20:49:07 +03:00
if ( ! * lp_magic_script ( talloc_tos ( ) , lp_sub , SNUM ( conn ) ) ) {
2009-06-07 01:27:31 +04:00
return NT_STATUS_OK ;
2007-09-13 03:50:21 +04:00
}
2003-01-03 21:50:13 +03:00
2009-07-11 01:50:37 +04:00
DEBUG ( 5 , ( " checking magic for %s \n " , fsp_str_dbg ( fsp ) ) ) ;
ctx = talloc_stackframe ( ) ;
2009-07-21 22:35:17 +04:00
fname = fsp - > fsp_name - > base_name ;
2003-01-03 21:50:13 +03:00
2009-07-11 01:50:37 +04:00
if ( ! ( p = strrchr_m ( fname , ' / ' ) ) ) {
p = fname ;
2007-09-13 03:50:21 +04:00
} else {
p + + ;
}
2003-01-03 21:50:13 +03:00
2019-10-31 20:49:07 +03:00
if ( ! strequal ( lp_magic_script ( talloc_tos ( ) , lp_sub , SNUM ( conn ) ) , p ) ) {
2009-07-11 01:50:37 +04:00
status = NT_STATUS_OK ;
goto out ;
2001-04-13 04:37:00 +04:00
}
2019-10-31 20:47:43 +03:00
if ( * lp_magic_output ( talloc_tos ( ) , lp_sub , SNUM ( conn ) ) ) {
magic_output = lp_magic_output ( talloc_tos ( ) , lp_sub , SNUM ( conn ) ) ;
2007-09-13 03:50:21 +04:00
} else {
magic_output = talloc_asprintf ( ctx ,
" %s.out " ,
2009-07-11 01:50:37 +04:00
fname ) ;
2007-09-13 03:50:21 +04:00
}
if ( ! magic_output ) {
2009-07-11 01:50:37 +04:00
status = NT_STATUS_NO_MEMORY ;
goto out ;
2007-09-13 03:50:21 +04:00
}
2009-01-07 03:05:28 +03:00
/* Ensure we don't depend on user's PATH. */
2009-07-11 01:50:37 +04:00
p = talloc_asprintf ( ctx , " ./%s " , fname ) ;
2009-01-07 03:05:28 +03:00
if ( ! p ) {
2009-07-11 01:50:37 +04:00
status = NT_STATUS_NO_MEMORY ;
goto out ;
2009-01-07 03:05:28 +03:00
}
2009-07-11 01:50:37 +04:00
if ( chmod ( fname , 0755 ) = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
goto out ;
2009-01-07 03:05:28 +03:00
}
2016-10-12 18:55:15 +03:00
ret = smbrun ( p , & tmp_fd , NULL ) ;
2007-09-13 03:50:21 +04:00
DEBUG ( 3 , ( " Invoking magic command %s gave %d \n " ,
2009-01-07 03:05:28 +03:00
p , ret ) ) ;
2003-01-03 21:50:13 +03:00
2009-07-11 01:50:37 +04:00
unlink ( fname ) ;
2007-09-13 03:50:21 +04:00
if ( ret ! = 0 | | tmp_fd = = - 1 ) {
if ( tmp_fd ! = - 1 ) {
2003-01-03 21:50:13 +03:00
close ( tmp_fd ) ;
}
2009-07-11 01:50:37 +04:00
status = NT_STATUS_UNSUCCESSFUL ;
goto out ;
2007-09-13 03:50:21 +04:00
}
outfd = open ( magic_output , O_CREAT | O_EXCL | O_RDWR , 0600 ) ;
if ( outfd = = - 1 ) {
2009-06-07 01:27:31 +04:00
int err = errno ;
2007-09-13 03:50:21 +04:00
close ( tmp_fd ) ;
2009-07-11 01:50:37 +04:00
status = map_nt_error_from_unix ( err ) ;
goto out ;
2007-09-13 03:50:21 +04:00
}
2003-01-03 21:50:13 +03:00
2009-11-27 14:58:26 +03:00
if ( sys_fstat ( tmp_fd , & st , false ) = = - 1 ) {
2009-06-07 01:27:31 +04:00
int err = errno ;
2001-04-13 04:37:00 +04:00
close ( tmp_fd ) ;
close ( outfd ) ;
2009-07-11 01:50:37 +04:00
status = map_nt_error_from_unix ( err ) ;
goto out ;
2001-04-13 04:37:00 +04:00
}
2007-09-13 03:50:21 +04:00
2012-04-05 08:53:08 +04:00
if ( transfer_file ( tmp_fd , outfd , ( off_t ) st . st_ex_size ) = = ( off_t ) - 1 ) {
2009-06-13 00:55:18 +04:00
int err = errno ;
close ( tmp_fd ) ;
close ( outfd ) ;
2009-07-11 01:50:37 +04:00
status = map_nt_error_from_unix ( err ) ;
goto out ;
2009-06-13 00:55:18 +04:00
}
2007-09-13 03:50:21 +04:00
close ( tmp_fd ) ;
2009-06-07 01:27:31 +04:00
if ( close ( outfd ) = = - 1 ) {
2009-07-11 01:50:37 +04:00
status = map_nt_error_from_unix ( errno ) ;
goto out ;
2009-06-07 01:27:31 +04:00
}
2009-07-11 01:50:37 +04:00
status = NT_STATUS_OK ;
out :
2007-09-13 03:50:21 +04:00
TALLOC_FREE ( ctx ) ;
2009-07-11 01:50:37 +04:00
return status ;
1998-08-17 17:11:34 +04:00
}
2008-01-20 01:25:36 +03:00
/****************************************************************************
Delete all streams
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-03-05 01:07:04 +03:00
NTSTATUS delete_all_streams ( connection_struct * conn ,
const struct smb_filename * smb_fname )
2008-01-20 01:25:36 +03:00
{
2011-10-14 02:41:53 +04:00
struct stream_struct * stream_info = NULL ;
2019-08-14 22:39:50 +03:00
unsigned int i ;
2011-10-14 02:41:53 +04:00
unsigned int num_streams = 0 ;
2008-01-20 01:25:36 +03:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
NTSTATUS status ;
2021-04-28 16:33:02 +03:00
status = vfs_fstreaminfo ( smb_fname - > fsp , talloc_tos ( ) ,
2011-10-17 22:10:29 +04:00
& num_streams , & stream_info ) ;
2008-01-20 01:25:36 +03:00
if ( NT_STATUS_EQUAL ( status , NT_STATUS_NOT_IMPLEMENTED ) ) {
DEBUG ( 10 , ( " no streams around \n " ) ) ;
TALLOC_FREE ( frame ) ;
return NT_STATUS_OK ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
2021-04-28 16:33:02 +03:00
DEBUG ( 10 , ( " vfs_fstreaminfo failed: %s \n " ,
2008-01-20 01:25:36 +03:00
nt_errstr ( status ) ) ) ;
goto fail ;
}
DEBUG ( 10 , ( " delete_all_streams found %d streams \n " ,
num_streams ) ) ;
if ( num_streams = = 0 ) {
TALLOC_FREE ( frame ) ;
return NT_STATUS_OK ;
}
for ( i = 0 ; i < num_streams ; i + + ) {
int res ;
2013-04-15 13:25:41 +04:00
struct smb_filename * smb_fname_stream ;
2008-01-20 01:25:36 +03:00
if ( strequal ( stream_info [ i ] . name , " ::$DATA " ) ) {
continue ;
}
2021-01-20 17:27:48 +03:00
status = synthetic_pathref ( talloc_tos ( ) ,
conn - > cwd_fsp ,
smb_fname - > base_name ,
stream_info [ i ] . name ,
NULL ,
smb_fname - > twrp ,
( smb_fname - > flags &
~ SMB_FILENAME_POSIX_PATH ) ,
& smb_fname_stream ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-01-20 01:25:36 +03:00
DEBUG ( 0 , ( " talloc_aprintf failed \n " ) ) ;
2013-04-15 13:25:41 +04:00
status = NT_STATUS_NO_MEMORY ;
2008-01-20 01:25:36 +03:00
goto fail ;
}
2019-09-13 22:23:02 +03:00
res = SMB_VFS_UNLINKAT ( conn ,
conn - > cwd_fsp ,
smb_fname_stream ,
0 ) ;
2008-01-20 01:25:36 +03:00
if ( res = = - 1 ) {
status = map_nt_error_from_unix ( errno ) ;
DEBUG ( 10 , ( " Could not delete stream %s: %s \n " ,
2009-07-02 20:27:44 +04:00
smb_fname_str_dbg ( smb_fname_stream ) ,
strerror ( errno ) ) ) ;
TALLOC_FREE ( smb_fname_stream ) ;
2008-01-20 01:25:36 +03:00
break ;
}
2009-07-02 20:27:44 +04:00
TALLOC_FREE ( smb_fname_stream ) ;
2008-01-20 01:25:36 +03:00
}
fail :
TALLOC_FREE ( frame ) ;
return status ;
}
2019-08-14 22:40:07 +03:00
struct has_other_nonposix_opens_state {
files_struct * fsp ;
bool found_another ;
} ;
static bool has_other_nonposix_opens_fn (
struct share_mode_entry * e ,
bool * modified ,
void * private_data )
2018-09-08 17:58:36 +03:00
{
2019-08-14 22:40:07 +03:00
struct has_other_nonposix_opens_state * state = private_data ;
struct files_struct * fsp = state - > fsp ;
2018-09-08 17:58:36 +03:00
2019-08-14 22:40:07 +03:00
if ( e - > name_hash ! = fsp - > name_hash ) {
return false ;
}
if ( ( fsp - > posix_flags & FSP_POSIX_FLAGS_OPEN ) & &
( e - > flags & SHARE_MODE_FLAG_POSIX_OPEN ) ) {
return false ;
}
2020-09-28 11:35:32 +03:00
if ( e - > share_file_id = = fh_get_gen_id ( fsp - > fh ) ) {
2019-08-14 22:40:07 +03:00
struct server_id self = messaging_server_id (
fsp - > conn - > sconn - > msg_ctx ) ;
if ( server_id_equal ( & self , & e - > pid ) ) {
return false ;
2018-09-08 17:58:36 +03:00
}
}
2019-08-14 22:40:07 +03:00
if ( share_entry_stale_pid ( e ) ) {
return false ;
}
state - > found_another = true ;
return true ;
}
2018-09-08 17:58:36 +03:00
2019-08-14 22:40:07 +03:00
bool has_other_nonposix_opens ( struct share_mode_lock * lck ,
struct files_struct * fsp )
{
struct has_other_nonposix_opens_state state = { . fsp = fsp } ;
bool ok ;
ok = share_mode_forall_entries (
lck , has_other_nonposix_opens_fn , & state ) ;
if ( ! ok ) {
return false ;
}
return state . found_another ;
2018-09-08 17:58:36 +03:00
}
2022-08-30 08:31:41 +03:00
struct close_share_mode_lock_state {
struct share_mode_entry_prepare_state prepare_state ;
const char * object_type ;
struct files_struct * fsp ;
enum file_close_type close_type ;
bool delete_object ;
bool got_tokens ;
const struct security_unix_token * del_token ;
const struct security_token * del_nt_token ;
bool reset_delete_on_close ;
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
share_mode_entry_prepare_unlock_fn_t cleanup_fn ;
2022-08-30 08:31:41 +03:00
} ;
1999-12-13 16:27:58 +03:00
2022-08-30 08:31:41 +03:00
static void close_share_mode_lock_prepare ( struct share_mode_lock * lck ,
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
bool * keep_locked ,
void * private_data )
1998-08-17 17:11:34 +04:00
{
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
struct close_share_mode_lock_state * state =
( struct close_share_mode_lock_state * ) private_data ;
2022-08-30 08:31:41 +03:00
struct files_struct * fsp = state - > fsp ;
2012-06-01 18:13:22 +04:00
bool normal_close ;
2022-08-30 08:31:41 +03:00
bool ok ;
2001-08-22 04:29:40 +04:00
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
/*
* By default drop the g_lock again if we leave the
* tdb chainlock .
*/
* keep_locked = false ;
2022-09-13 02:30:48 +03:00
if ( fsp - > oplock_type ! = NO_OPLOCK ) {
ok = remove_share_oplock ( lck , fsp ) ;
if ( ! ok ) {
struct file_id_buf buf ;
DBG_ERR ( " failed to remove share oplock for "
2022-08-30 08:31:41 +03:00
" %s %s, %s, %s \n " ,
state - > object_type ,
2022-09-13 02:30:48 +03:00
fsp_str_dbg ( fsp ) , fsp_fnum_dbg ( fsp ) ,
file_id_str_buf ( fsp - > file_id , & buf ) ) ;
}
2016-08-11 00:42:07 +03:00
}
2020-04-02 16:39:32 +03:00
if ( fsp - > fsp_flags . write_time_forced ) {
2020-11-04 15:00:55 +03:00
NTTIME mtime = share_mode_changed_write_time ( lck ) ;
struct timespec ts = nt_time_to_full_timespec ( mtime ) ;
2019-12-02 18:30:50 +03:00
2022-08-30 08:31:41 +03:00
DBG_DEBUG ( " write time forced for %s %s \n " ,
state - > object_type , fsp_str_dbg ( fsp ) ) ;
2019-12-02 18:30:50 +03:00
set_close_write_time ( fsp , ts ) ;
2020-04-02 16:37:15 +03:00
} else if ( fsp - > fsp_flags . update_write_time_on_close ) {
2009-11-06 09:58:12 +03:00
/* Someone had a pending write. */
2019-12-02 18:30:50 +03:00
if ( is_omit_timespec ( & fsp - > close_write_time ) ) {
2022-08-30 08:31:41 +03:00
DBG_DEBUG ( " update to current time for %s %s \n " ,
state - > object_type , fsp_str_dbg ( fsp ) ) ;
2009-11-06 09:58:12 +03:00
/* Update to current time due to "normal" write. */
set_close_write_time ( fsp , timespec_current ( ) ) ;
2009-11-06 08:27:25 +03:00
} else {
2022-08-30 08:31:41 +03:00
DBG_DEBUG ( " write time pending for %s %s \n " ,
state - > object_type , fsp_str_dbg ( fsp ) ) ;
2009-11-06 09:58:12 +03:00
/* Update to time set on close call. */
set_close_write_time ( fsp , fsp - > close_write_time ) ;
2009-11-06 08:27:25 +03:00
}
2008-03-12 17:39:38 +03:00
}
2020-04-03 08:43:38 +03:00
if ( fsp - > fsp_flags . initial_delete_on_close & &
2011-01-26 01:23:19 +03:00
! is_delete_on_close_set ( lck , fsp - > name_hash ) ) {
2007-01-19 00:51:52 +03:00
/* Initial delete on close was set and no one else
* wrote a real delete on close . */
2020-04-03 08:48:08 +03:00
fsp - > fsp_flags . delete_on_close = true ;
2015-04-21 12:41:45 +03:00
set_delete_on_close_lck ( fsp , lck ,
2021-01-23 20:36:23 +03:00
fsp - > conn - > session_info - > security_token ,
fsp - > conn - > session_info - > unix_token ) ;
2007-01-19 00:51:52 +03:00
}
2022-08-30 08:31:41 +03:00
state - > delete_object = is_delete_on_close_set ( lck , fsp - > name_hash ) & &
2019-09-09 11:42:59 +03:00
! has_other_nonposix_opens ( lck , fsp ) ;
1998-08-17 17:11:34 +04:00
1998-09-11 23:14:27 +04:00
/*
* NT can set delete_on_close of the last open
* reference to a file .
*/
2022-08-30 08:31:41 +03:00
normal_close = ( state - > close_type = = NORMAL_CLOSE | | state - > close_type = = SHUTDOWN_CLOSE ) ;
2022-09-12 19:00:39 +03:00
if ( ! normal_close ) {
/*
2022-08-30 08:31:41 +03:00
* Never try to delete the file / directory for ERROR_CLOSE
2022-09-12 19:00:39 +03:00
*/
2022-08-30 08:31:41 +03:00
state - > delete_object = false ;
}
if ( ! state - > delete_object ) {
ok = del_share_mode ( lck , fsp ) ;
if ( ! ok ) {
DBG_ERR ( " Could not delete share entry for %s %s \n " ,
state - > object_type , fsp_str_dbg ( fsp ) ) ;
}
return ;
2022-09-12 19:00:39 +03:00
}
2012-06-01 18:13:22 +04:00
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
/*
* We ' re going to remove the file / directory
* so keep the g_lock after the tdb chainlock
* is left , so we hold the share_mode_lock
* also during the deletion
*/
* keep_locked = true ;
2022-08-30 08:31:41 +03:00
state - > got_tokens = get_delete_on_close_token ( lck , fsp - > name_hash ,
& state - > del_nt_token , & state - > del_token ) ;
if ( state - > close_type ! = ERROR_CLOSE ) {
SMB_ASSERT ( state - > got_tokens ) ;
}
}
static void close_share_mode_lock_cleanup ( struct share_mode_lock * lck ,
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
void * private_data )
2022-08-30 08:31:41 +03:00
{
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
struct close_share_mode_lock_state * state =
( struct close_share_mode_lock_state * ) private_data ;
2022-08-30 08:31:41 +03:00
struct files_struct * fsp = state - > fsp ;
bool ok ;
if ( state - > reset_delete_on_close ) {
reset_delete_on_close_lck ( fsp , lck ) ;
}
ok = del_share_mode ( lck , fsp ) ;
if ( ! ok ) {
DBG_ERR ( " Could not delete share entry for %s %s \n " ,
state - > object_type , fsp_str_dbg ( fsp ) ) ;
}
}
/****************************************************************************
Deal with removing a share mode on last close .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static NTSTATUS close_remove_share_mode ( files_struct * fsp ,
enum file_close_type close_type )
{
connection_struct * conn = fsp - > conn ;
struct close_share_mode_lock_state lck_state = { } ;
bool changed_user = false ;
NTSTATUS status = NT_STATUS_OK ;
NTSTATUS tmp_status ;
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
NTSTATUS ulstatus ;
2022-08-30 08:31:41 +03:00
struct file_id id ;
struct smb_filename * parent_fname = NULL ;
struct smb_filename * base_fname = NULL ;
int ret ;
/* Ensure any pending write time updates are done. */
if ( fsp - > update_write_time_event ) {
fsp_flush_write_time_update ( fsp ) ;
}
/*
* Lock the share entries , and determine if we should delete
* on close . If so delete whilst the lock is still in effect .
* This prevents race conditions with the file being created . JRA .
*/
lck_state = ( struct close_share_mode_lock_state ) {
. fsp = fsp ,
. object_type = " file " ,
. close_type = close_type ,
} ;
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
status = share_mode_entry_prepare_lock_del ( & lck_state . prepare_state ,
fsp - > file_id ,
close_share_mode_lock_prepare ,
& lck_state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_ERR ( " share_mode_entry_prepare_lock_del() failed for %s - %s \n " ,
fsp_str_dbg ( fsp ) , nt_errstr ( status ) ) ;
return status ;
}
2022-08-30 08:31:41 +03:00
/* Remove the oplock before potentially deleting the file. */
if ( fsp - > oplock_type ! = NO_OPLOCK ) {
release_file_oplock ( fsp ) ;
}
/*
* NT can set delete_on_close of the last open
* reference to a file .
*/
if ( ! lck_state . delete_object ) {
2013-06-21 17:11:55 +04:00
status = NT_STATUS_OK ;
goto done ;
2007-01-04 18:12:22 +03:00
}
2005-12-13 21:11:50 +03:00
2007-01-04 18:12:22 +03:00
/*
* Ok , we have to delete the file
*/
2022-08-30 08:31:41 +03:00
lck_state . cleanup_fn = close_share_mode_lock_cleanup ;
2005-12-13 21:11:50 +03:00
2007-01-04 18:12:22 +03:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. Delete on close was set "
2009-07-11 01:50:37 +04:00
" - deleting file. \n " , fsp_str_dbg ( fsp ) ) ) ;
2006-02-02 23:44:50 +03:00
2008-03-12 17:39:38 +03:00
/*
* Don ' t try to update the write time when we delete the file
*/
2020-04-02 16:37:15 +03:00
fsp - > fsp_flags . update_write_time_on_close = false ;
2008-03-12 17:39:38 +03:00
2022-08-30 08:31:41 +03:00
if ( lck_state . got_tokens & &
! unix_token_equal ( lck_state . del_token , get_current_utok ( conn ) ) )
{
2008-01-11 03:35:54 +03:00
/* Become the user who requested the delete. */
2006-02-02 23:44:50 +03:00
2008-01-11 03:35:54 +03:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. "
" Change user to uid %u \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ,
2022-08-30 08:31:41 +03:00
( unsigned int ) lck_state . del_token - > uid ) ) ;
2008-01-11 03:35:54 +03:00
if ( ! push_sec_ctx ( ) ) {
smb_panic ( " close_remove_share_mode: file %s. failed to push "
" sec_ctx. \n " ) ;
}
2006-02-02 23:44:50 +03:00
2022-08-30 08:31:41 +03:00
set_sec_ctx ( lck_state . del_token - > uid ,
lck_state . del_token - > gid ,
lck_state . del_token - > ngroups ,
lck_state . del_token - > groups ,
lck_state . del_nt_token ) ;
2008-01-11 03:35:54 +03:00
changed_user = true ;
}
2007-01-04 18:12:22 +03:00
/* We can only delete the file if the name we have is still valid and
hasn ' t been renamed . */
2007-07-17 04:09:56 +04:00
2009-10-03 00:45:38 +04:00
tmp_status = vfs_stat_fsp ( fsp ) ;
if ( ! NT_STATUS_IS_OK ( tmp_status ) ) {
2007-01-04 18:12:22 +03:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. Delete on close "
" was set and stat failed with error %s \n " ,
2009-10-03 00:45:38 +04:00
fsp_str_dbg ( fsp ) , nt_errstr ( tmp_status ) ) ) ;
2007-01-05 14:31:28 +03:00
/*
* Don ' t save the errno here , we ignore this error
*/
2007-01-04 18:12:22 +03:00
goto done ;
}
2009-07-11 01:50:37 +04:00
id = vfs_file_id_from_sbuf ( conn , & fsp - > fsp_name - > st ) ;
2007-05-29 13:30:34 +04:00
if ( ! file_id_equal ( & fsp - > file_id , & id ) ) {
2019-09-09 17:34:10 +03:00
struct file_id_buf ftmp1 , ftmp2 ;
2007-01-04 18:12:22 +03:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. Delete on close "
" was set and dev and/or inode does not match \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ) ) ;
2007-05-29 13:30:34 +04:00
DEBUG ( 5 , ( " close_remove_share_mode: file %s. stored file_id %s, "
" stat file_id %s \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ,
2019-09-09 17:34:10 +03:00
file_id_str_buf ( fsp - > file_id , & ftmp1 ) ,
file_id_str_buf ( id , & ftmp2 ) ) ) ;
2007-01-05 14:31:28 +03:00
/*
* Don ' t save the errno here , we ignore this error
*/
2007-01-04 18:12:22 +03:00
goto done ;
2001-11-13 04:35:20 +03:00
}
1998-09-11 23:14:27 +04:00
2008-01-20 01:25:36 +03:00
if ( ( conn - > fs_capabilities & FILE_NAMED_STREAMS )
2022-02-11 20:49:53 +03:00
& & ! fsp_is_alternate_stream ( fsp ) ) {
2008-01-20 01:25:36 +03:00
2016-03-05 01:07:04 +03:00
status = delete_all_streams ( conn , fsp - > fsp_name ) ;
2008-01-20 01:25:36 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 5 , ( " delete_all_streams failed: %s \n " ,
nt_errstr ( status ) ) ) ;
goto done ;
}
}
2020-04-01 12:59:10 +03:00
if ( fsp - > fsp_flags . kernel_share_modes_taken ) {
2018-01-11 01:56:08 +03:00
/*
* A file system sharemode could block the unlink ;
* remove filesystem sharemodes first .
*/
2021-09-21 01:50:08 +03:00
ret = SMB_VFS_FILESYSTEM_SHAREMODE ( fsp , 0 , 0 ) ;
if ( ret = = - 1 ) {
2021-09-21 01:55:32 +03:00
DBG_INFO ( " Removing file system sharemode for %s "
" failed: %s \n " ,
fsp_str_dbg ( fsp ) , strerror ( errno ) ) ;
2018-01-11 01:56:08 +03:00
}
2020-04-01 12:59:10 +03:00
fsp - > fsp_flags . kernel_share_modes_taken = false ;
2018-01-11 01:56:08 +03:00
}
2021-01-21 18:45:47 +03:00
status = parent_pathref ( talloc_tos ( ) ,
conn - > cwd_fsp ,
fsp - > fsp_name ,
& parent_fname ,
& base_fname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto done ;
}
2008-01-20 01:25:36 +03:00
2019-09-13 22:25:10 +03:00
ret = SMB_VFS_UNLINKAT ( conn ,
2021-01-21 18:45:47 +03:00
parent_fname - > fsp ,
base_fname ,
0 ) ;
TALLOC_FREE ( parent_fname ) ;
base_fname = NULL ;
2019-09-13 22:25:10 +03:00
if ( ret ! = 0 ) {
2007-01-04 18:12:22 +03:00
/*
* This call can potentially fail as another smbd may
* have had the file open with delete on close set and
* deleted it when its last reference to this file
* went away . Hence we log this but not at debug level
* zero .
*/
DEBUG ( 5 , ( " close_remove_share_mode: file %s. Delete on close "
" was set and unlink failed with error %s \n " ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) , strerror ( errno ) ) ) ;
2007-01-05 14:31:28 +03:00
status = map_nt_error_from_unix ( errno ) ;
2007-01-04 18:12:22 +03:00
}
2007-02-11 07:37:56 +03:00
/* As we now have POSIX opens which can unlink
* with other open files we may have taken
* this code path with more than one share mode
* entry - ensure we only delete once by resetting
* the delete on close flag . JRA .
*/
2020-04-03 08:48:08 +03:00
fsp - > fsp_flags . delete_on_close = false ;
2022-08-30 08:31:41 +03:00
lck_state . reset_delete_on_close = true ;
2007-02-11 07:37:56 +03:00
2007-01-04 18:12:22 +03:00
done :
2007-02-07 00:05:34 +03:00
2008-01-11 03:35:54 +03:00
if ( changed_user ) {
/* unbecome user. */
pop_sec_ctx ( ) ;
}
2020-04-01 12:59:10 +03:00
if ( fsp - > fsp_flags . kernel_share_modes_taken ) {
2016-05-16 00:24:08 +03:00
/* remove filesystem sharemodes */
2021-09-21 01:50:08 +03:00
ret = SMB_VFS_FILESYSTEM_SHAREMODE ( fsp , 0 , 0 ) ;
if ( ret = = - 1 ) {
2021-09-21 01:55:32 +03:00
DBG_INFO ( " Removing file system sharemode for "
" %s failed: %s \n " ,
fsp_str_dbg ( fsp ) , strerror ( errno ) ) ;
2016-05-16 00:24:08 +03:00
}
2013-06-13 17:23:07 +04:00
}
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
ulstatus = share_mode_entry_prepare_unlock ( & lck_state . prepare_state ,
lck_state . cleanup_fn ,
& lck_state ) ;
if ( ! NT_STATUS_IS_OK ( ulstatus ) ) {
DBG_ERR ( " share_mode_entry_prepare_unlock() failed for %s - %s \n " ,
fsp_str_dbg ( fsp ) , nt_errstr ( ulstatus ) ) ;
smb_panic ( " share_mode_entry_prepare_unlock() failed! " ) ;
2012-06-01 18:29:51 +04:00
}
2022-08-30 08:31:41 +03:00
if ( lck_state . delete_object ) {
2011-01-21 12:30:13 +03:00
/*
* Do the notification after we released the share
* mode lock . Inside notify_fname we take out another
* tdb lock . With ctdb also accessing our databases ,
* this can lead to deadlocks . Putting this notify
* after the TALLOC_FREE ( lck ) above we avoid locking
* two records simultaneously . Notifies are async and
* informational only , so calling the notify_fname
* without holding the share mode lock should not do
* any harm .
*/
notify_fname ( conn , NOTIFY_ACTION_REMOVED ,
FILE_NOTIFY_CHANGE_FILE_NAME ,
fsp - > fsp_name - > base_name ) ;
}
2007-01-05 14:31:28 +03:00
return status ;
2006-05-18 03:15:53 +04:00
}
2009-11-06 09:58:12 +03:00
void set_close_write_time ( struct files_struct * fsp , struct timespec ts )
2008-03-12 17:39:38 +03:00
{
DEBUG ( 6 , ( " close_write_time: %s " , time_to_asc ( convert_timespec_to_time_t ( ts ) ) ) ) ;
2019-12-02 18:30:50 +03:00
if ( is_omit_timespec ( & ts ) ) {
2008-03-12 17:39:38 +03:00
return ;
}
2020-04-02 16:39:32 +03:00
fsp - > fsp_flags . write_time_forced = false ;
2020-04-02 16:37:15 +03:00
fsp - > fsp_flags . update_write_time_on_close = true ;
2008-03-12 17:39:38 +03:00
fsp - > close_write_time = ts ;
}
2022-08-30 09:11:12 +03:00
static void update_write_time_on_close_share_mode_fn ( struct share_mode_lock * lck ,
void * private_data )
{
struct files_struct * fsp =
talloc_get_type_abort ( private_data ,
struct files_struct ) ;
NTTIME share_mtime = share_mode_changed_write_time ( lck ) ;
/*
* On close if we ' re changing the real file time we
* must update it in the open file db too .
*/
share_mode_set_old_write_time ( lck , fsp - > close_write_time ) ;
/*
* Close write times overwrite sticky write times
* so we must replace any sticky write time here .
*/
if ( ! null_nttime ( share_mtime ) ) {
share_mode_set_changed_write_time ( lck , fsp - > close_write_time ) ;
}
}
2008-03-12 17:39:38 +03:00
static NTSTATUS update_write_time_on_close ( struct files_struct * fsp )
{
2009-01-24 01:40:19 +03:00
struct smb_file_time ft ;
2008-03-12 17:39:38 +03:00
NTSTATUS status ;
2019-12-02 18:30:50 +03:00
init_smb_file_time ( & ft ) ;
2008-03-12 17:39:38 +03:00
2020-04-02 16:37:15 +03:00
if ( ! ( fsp - > fsp_flags . update_write_time_on_close ) ) {
2008-03-12 17:39:38 +03:00
return NT_STATUS_OK ;
}
2019-12-02 18:30:50 +03:00
if ( is_omit_timespec ( & fsp - > close_write_time ) ) {
2008-03-12 17:39:38 +03:00
fsp - > close_write_time = timespec_current ( ) ;
}
/* Ensure we have a valid stat struct for the source. */
2009-10-03 00:45:38 +04:00
status = vfs_stat_fsp ( fsp ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
2009-02-26 01:57:16 +03:00
}
2009-07-11 01:50:37 +04:00
if ( ! VALID_STAT ( fsp - > fsp_name - > st ) ) {
2008-03-12 17:39:38 +03:00
/* if it doesn't seem to be a real file */
2009-07-11 01:50:37 +04:00
return NT_STATUS_OK ;
2008-03-12 17:39:38 +03:00
}
2012-05-19 06:29:36 +04:00
/*
2022-08-30 09:11:12 +03:00
* We ' re being called after close_remove_share_mode ( ) inside
* close_normal_file ( ) so it ' s quite normal to not have an
* existing share . So just ignore the result of
* share_mode_do_locked_vfs_denied ( ) . . .
2012-05-19 06:29:36 +04:00
*/
2022-08-30 09:11:12 +03:00
share_mode_do_locked_vfs_denied ( fsp - > file_id ,
update_write_time_on_close_share_mode_fn ,
fsp ) ;
2009-11-06 09:58:12 +03:00
2009-01-24 01:40:19 +03:00
ft . mtime = fsp - > close_write_time ;
2011-11-16 05:41:48 +04:00
/* As this is a close based update, we are not directly changing the
2011-02-23 03:38:42 +03:00
file attributes from a client call , but indirectly from a write . */
2011-11-16 05:41:48 +04:00
status = smb_set_file_time ( fsp - > conn , fsp , fsp - > fsp_name , & ft , false ) ;
2008-03-12 17:39:38 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2011-02-23 03:38:42 +03:00
DEBUG ( 10 , ( " update_write_time_on_close: smb_set_file_time "
" on file %s returned %s \n " ,
fsp_str_dbg ( fsp ) ,
nt_errstr ( status ) ) ) ;
2009-07-11 01:50:37 +04:00
return status ;
2008-03-12 17:39:38 +03:00
}
2009-07-03 00:39:20 +04:00
return status ;
2008-03-12 17:39:38 +03:00
}
2009-06-07 01:20:44 +04:00
static NTSTATUS ntstatus_keeperror ( NTSTATUS s1 , NTSTATUS s2 )
{
if ( ! NT_STATUS_IS_OK ( s1 ) ) {
return s1 ;
}
return s2 ;
}
2020-03-17 13:17:46 +03:00
static void assert_no_pending_aio ( struct files_struct * fsp ,
enum file_close_type close_type )
2020-03-09 13:16:37 +03:00
{
2020-06-17 01:01:49 +03:00
struct smbXsrv_client * client = global_smbXsrv_client ;
size_t num_connections_alive ;
2020-03-09 13:16:37 +03:00
unsigned num_requests = fsp - > num_aio_requests ;
if ( num_requests = = 0 ) {
return ;
}
2020-06-17 01:01:49 +03:00
num_connections_alive = smbXsrv_client_valid_connections ( client ) ;
if ( close_type = = SHUTDOWN_CLOSE & & num_connections_alive = = 0 ) {
/*
* fsp - > aio_requests and the contents ( fsp - > aio_requests [ x ] )
* are both independently owned by fsp and are not in a
* talloc heirarchy . This allows the fsp - > aio_requests array to
* be reallocated independently of the array contents so it can
* grow on demand .
*
* This means we must ensure order of deallocation
* on a SHUTDOWN_CLOSE by deallocating the fsp - > aio_requests [ x ]
* contents first , as their destructors access the
* fsp - > aio_request array . If we don ' t deallocate them
* first , when fsp is deallocated fsp - > aio_requests
* could have been deallocated * before * its contents
* fsp - > aio_requests [ x ] , causing a crash .
*/
while ( fsp - > num_aio_requests ! = 0 ) {
2020-09-27 08:14:33 +03:00
/*
* NB . We * MUST * use
* talloc_free ( fsp - > aio_requests [ 0 ] ) ,
* and * NOT * TALLOC_FREE ( ) here , as
* TALLOC_FREE ( fsp - > aio_requests [ 0 ] )
* will overwrite any new contents of
* fsp - > aio_requests [ 0 ] that were
* copied into it via the destructor
* aio_del_req_from_fsp ( ) .
*
* BUG : https : //bugzilla.samba.org/show_bug.cgi?id=14515
*/
talloc_free ( fsp - > aio_requests [ 0 ] ) ;
2020-06-17 01:01:49 +03:00
}
return ;
}
2020-03-17 13:17:46 +03:00
DBG_ERR ( " fsp->num_aio_requests=%u \n " , num_requests ) ;
smb_panic ( " can not close with outstanding aio requests " ) ;
return ;
2020-03-09 13:16:37 +03:00
}
2006-05-18 03:15:53 +04:00
/****************************************************************************
Close a file .
close_type can be NORMAL_CLOSE = 0 , SHUTDOWN_CLOSE , ERROR_CLOSE .
printing and magic scripts are only run on normal close .
delete on close is done on normal and shutdown close .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-10-09 18:27:49 +04:00
static NTSTATUS close_normal_file ( struct smb_request * req , files_struct * fsp ,
enum file_close_type close_type )
2006-05-18 03:15:53 +04:00
{
2007-02-07 00:05:34 +03:00
NTSTATUS status = NT_STATUS_OK ;
2009-06-07 01:20:44 +04:00
NTSTATUS tmp ;
2006-05-18 03:15:53 +04:00
connection_struct * conn = fsp - > conn ;
2012-08-03 18:47:57 +04:00
bool is_durable = false ;
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
2020-12-17 03:38:28 +03:00
SMB_ASSERT ( fsp - > fsp_flags . is_fsa ) ;
2020-03-17 13:17:46 +03:00
assert_no_pending_aio ( fsp , close_type ) ;
2010-06-05 08:00:24 +04:00
2019-06-13 13:38:57 +03:00
while ( talloc_array_length ( fsp - > blocked_smb1_lock_reqs ) ! = 0 ) {
smbd_smb1_brl_finish_by_req (
fsp - > blocked_smb1_lock_reqs [ 0 ] ,
NT_STATUS_RANGE_NOT_LOCKED ) ;
}
2006-05-18 03:15:53 +04:00
/*
* If we ' re flushing on a close we can get a write
* error here , we must remember this .
*/
2012-08-03 18:47:57 +04:00
if ( NT_STATUS_IS_OK ( status ) & & fsp - > op ! = NULL ) {
is_durable = fsp - > op - > global - > durable ;
}
if ( close_type ! = SHUTDOWN_CLOSE ) {
is_durable = false ;
}
if ( is_durable ) {
DATA_BLOB new_cookie = data_blob_null ;
tmp = SMB_VFS_DURABLE_DISCONNECT ( fsp ,
fsp - > op - > global - > backend_cookie ,
fsp - > op ,
& new_cookie ) ;
if ( NT_STATUS_IS_OK ( tmp ) ) {
2013-02-13 17:26:43 +04:00
struct timeval tv ;
NTTIME now ;
if ( req ! = NULL ) {
tv = req - > request_time ;
} else {
tv = timeval_current ( ) ;
}
now = timeval_to_nttime ( & tv ) ;
2012-08-03 18:47:57 +04:00
data_blob_free ( & fsp - > op - > global - > backend_cookie ) ;
fsp - > op - > global - > backend_cookie = new_cookie ;
2013-02-19 02:21:24 +04:00
fsp - > op - > compat = NULL ;
2013-02-13 17:26:43 +04:00
tmp = smbXsrv_open_close ( fsp - > op , now ) ;
2013-02-12 20:44:51 +04:00
if ( ! NT_STATUS_IS_OK ( tmp ) ) {
DEBUG ( 1 , ( " Failed to update smbXsrv_open "
" record when disconnecting durable "
" handle for file %s: %s - "
" proceeding with normal close \n " ,
fsp_str_dbg ( fsp ) , nt_errstr ( tmp ) ) ) ;
}
2013-03-20 13:01:43 +04:00
scavenger_schedule_disconnected ( fsp ) ;
2013-02-12 20:44:51 +04:00
} else {
DEBUG ( 1 , ( " Failed to disconnect durable handle for "
" file %s: %s - proceeding with normal "
" close \n " , fsp_str_dbg ( fsp ) , nt_errstr ( tmp ) ) ) ;
2012-08-03 18:47:57 +04:00
}
if ( ! NT_STATUS_IS_OK ( tmp ) ) {
is_durable = false ;
}
}
if ( is_durable ) {
/*
* This is the case where we successfully disconnected
* a durable handle and closed the underlying file .
* In all other cases , we proceed with a genuine close .
*/
2013-02-12 20:44:51 +04:00
DEBUG ( 10 , ( " %s disconnected durable handle for file %s \n " ,
conn - > session_info - > unix_info - > unix_name ,
fsp_str_dbg ( fsp ) ) ) ;
2012-08-03 18:47:57 +04:00
return NT_STATUS_OK ;
}
if ( fsp - > op ! = NULL ) {
/*
* Make sure the handle is not marked as durable anymore
*/
fsp - > op - > global - > durable = false ;
}
2006-05-18 03:15:53 +04:00
/* If this is an old DOS or FCB open and we have multiple opens on
the same handle we only have one share mode . Ensure we only remove
the share mode on the last close . */
2020-09-28 11:37:36 +03:00
if ( fh_get_refcount ( fsp - > fh ) = = 1 ) {
2006-05-18 03:15:53 +04:00
/* Should we return on error here... ? */
2009-06-07 01:20:44 +04:00
tmp = close_remove_share_mode ( fsp , close_type ) ;
status = ntstatus_keeperror ( status , tmp ) ;
2006-05-18 03:15:53 +04:00
}
2019-07-01 16:35:19 +03:00
locking_close_file ( fsp , close_type ) ;
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
2001-12-17 00:04:19 +03:00
/*
2021-04-15 12:53:13 +03:00
* Ensure pending modtime is set before closing underlying fd .
2001-12-17 00:04:19 +03:00
*/
2009-06-07 01:20:44 +04:00
tmp = update_write_time_on_close ( fsp ) ;
if ( NT_STATUS_EQUAL ( tmp , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ) {
2021-04-15 12:53:13 +03:00
/*
* Someone renamed the file or a parent directory containing
* this file . We can ' t do anything about this , eat the error .
*/
2009-06-07 01:20:44 +04:00
tmp = NT_STATUS_OK ;
2009-02-26 01:57:16 +03:00
}
2021-04-15 12:53:13 +03:00
status = ntstatus_keeperror ( status , tmp ) ;
This is a big, rather ugly patch. Whilst investigating the files not truncated
when copying to a full disk problem, I discovered that we were not allowing
the delete on close flag to be set properly, this led to other things, and
after investigation of the proper delete on close semantics and their relationship
to the file_share_delete flag I discovered there were some cases where we
weren't doing the deny modes properly. And this after only 5 years working
on them..... :-) :-).
So here's the latest attempt. I realised the delete on close flag needs to
be set across all smbds with a dev/ino pair open - in addition, the delete
on close flag, allow share delete and delete access requested all need to
be stored in the share mode tdb.
The "delete_on_close" entry in the fsp struct is now redundant and should
really be removed. This may also mean we can get rid of the "iterate_fsp"
calls that I didn't like adding in the first place. Whilst doing this patch,
I also discovered we needed to do the se_map_generic() call for file opens
and POSIX ACL mapping, so I added that also.
This code, although ugly, now passes the deny mode torture tests plus the
delete on close tests I added. I do need to add one more multiple connection
delete on close test to make sure I got the semantics exactly right, plus we
should also (as Andrew suggested) move to random testing here.
The good news is that NT should now correctly delete the file on disk
full error when copying to a disk :-).
Jeremy.
(This used to be commit 51987684bd231c744da2e5f3705fd236d5616173)
2001-03-30 12:57:24 +04:00
2021-04-15 12:53:13 +03:00
tmp = fd_close ( fsp ) ;
2009-06-07 01:20:44 +04:00
status = ntstatus_keeperror ( status , tmp ) ;
2007-02-07 00:05:34 +03:00
2021-04-15 12:53:13 +03:00
/* check for magic scripts */
if ( close_type = = NORMAL_CLOSE ) {
tmp = check_magic ( fsp ) ;
status = ntstatus_keeperror ( status , tmp ) ;
}
1999-12-13 16:27:58 +03:00
DEBUG ( 2 , ( " %s closed file %s (numopen=%d) %s \n " ,
2011-07-15 09:55:31 +04:00
conn - > session_info - > unix_info - > unix_name , fsp_str_dbg ( fsp ) ,
2008-11-21 23:14:53 +03:00
conn - > num_files_open - 1 ,
2007-02-07 00:05:34 +03:00
nt_errstr ( status ) ) ) ;
1998-08-17 17:11:34 +04:00
2007-02-07 00:05:34 +03:00
return status ;
1998-08-17 17:11:34 +04:00
}
2009-12-04 00:01:10 +03:00
/****************************************************************************
2012-10-01 11:55:28 +04:00
Function used by reply_rmdir to delete an entire directory
2009-12-04 00:01:10 +03:00
tree recursively . Return True on ok , False on fail .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2022-03-03 20:34:45 +03:00
NTSTATUS recursive_rmdir ( TALLOC_CTX * ctx ,
2012-10-01 11:55:28 +04:00
connection_struct * conn ,
struct smb_filename * smb_dname )
2009-12-04 00:01:10 +03:00
{
const char * dname = NULL ;
char * talloced = NULL ;
long offset = 0 ;
2022-03-01 01:20:37 +03:00
struct smb_Dir * dir_hnd = NULL ;
2021-01-22 14:49:56 +03:00
struct files_struct * dirfsp = NULL ;
2019-09-13 22:29:41 +03:00
int retval ;
2022-03-03 20:34:45 +03:00
NTSTATUS status = NT_STATUS_OK ;
2009-12-04 00:01:10 +03:00
SMB_ASSERT ( ! is_ntfs_stream_smb_fname ( smb_dname ) ) ;
2022-03-01 01:34:48 +03:00
status = OpenDir ( talloc_tos ( ) ,
conn ,
smb_dname ,
NULL ,
0 ,
& dir_hnd ) ;
2022-03-01 01:20:37 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2022-03-03 20:34:45 +03:00
return status ;
2022-03-01 01:20:37 +03:00
}
2009-12-04 00:01:10 +03:00
2021-01-22 14:49:56 +03:00
dirfsp = dir_hnd_fetch_fsp ( dir_hnd ) ;
2022-09-06 10:07:31 +03:00
while ( ( dname = ReadDirName ( dir_hnd , & offset , NULL , & talloced ) ) ) {
2021-01-22 14:49:56 +03:00
struct smb_filename * atname = NULL ;
2009-12-04 00:01:10 +03:00
struct smb_filename * smb_dname_full = NULL ;
char * fullname = NULL ;
bool do_break = true ;
2021-01-22 14:00:56 +03:00
int unlink_flags = 0 ;
2009-12-04 00:01:10 +03:00
if ( ISDOT ( dname ) | | ISDOTDOT ( dname ) ) {
TALLOC_FREE ( talloced ) ;
continue ;
}
/* Construct the full name. */
fullname = talloc_asprintf ( ctx ,
" %s/%s " ,
smb_dname - > base_name ,
dname ) ;
if ( ! fullname ) {
2022-03-03 20:34:45 +03:00
status = NT_STATUS_NO_MEMORY ;
2009-12-04 00:01:10 +03:00
goto err_break ;
}
2016-03-19 07:19:38 +03:00
smb_dname_full = synthetic_smb_fname ( talloc_tos ( ) ,
fullname ,
NULL ,
NULL ,
2020-04-30 12:48:32 +03:00
smb_dname - > twrp ,
2016-03-19 07:19:38 +03:00
smb_dname - > flags ) ;
2013-04-15 13:27:35 +04:00
if ( smb_dname_full = = NULL ) {
2022-03-03 20:34:45 +03:00
status = NT_STATUS_NO_MEMORY ;
2009-12-04 00:01:10 +03:00
goto err_break ;
}
2021-01-22 14:01:56 +03:00
if ( SMB_VFS_LSTAT ( conn , smb_dname_full ) ! = 0 ) {
2022-03-03 20:34:45 +03:00
status = map_nt_error_from_unix ( errno ) ;
2009-12-04 00:01:10 +03:00
goto err_break ;
}
2021-01-22 14:01:56 +03:00
if ( smb_dname_full - > st . st_ex_mode & S_IFDIR ) {
2022-03-03 20:34:45 +03:00
status = recursive_rmdir ( ctx , conn , smb_dname_full ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-12-04 00:01:10 +03:00
goto err_break ;
}
2021-01-22 14:00:56 +03:00
unlink_flags = AT_REMOVEDIR ;
}
2021-01-22 14:49:56 +03:00
status = synthetic_pathref ( talloc_tos ( ) ,
dirfsp ,
dname ,
NULL ,
2021-06-07 19:14:03 +03:00
& smb_dname_full - > st ,
2021-01-22 14:49:56 +03:00
smb_dname_full - > twrp ,
smb_dname_full - > flags ,
& atname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto err_break ;
}
2021-06-04 23:32:40 +03:00
if ( ! is_visible_fsp ( atname - > fsp ) ) {
2021-06-03 03:42:27 +03:00
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( atname ) ;
continue ;
}
2021-01-22 14:00:56 +03:00
retval = SMB_VFS_UNLINKAT ( conn ,
2021-01-22 14:49:56 +03:00
dirfsp ,
atname ,
2021-01-22 14:00:56 +03:00
unlink_flags ) ;
if ( retval ! = 0 ) {
2022-03-03 20:34:45 +03:00
status = map_nt_error_from_unix ( errno ) ;
2021-01-22 14:00:56 +03:00
goto err_break ;
2009-12-04 00:01:10 +03:00
}
/* Successful iteration. */
do_break = false ;
err_break :
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( talloced ) ;
2021-01-22 14:49:56 +03:00
TALLOC_FREE ( atname ) ;
2009-12-04 00:01:10 +03:00
if ( do_break ) {
break ;
}
}
TALLOC_FREE ( dir_hnd ) ;
2022-03-03 20:34:45 +03:00
return status ;
2009-12-04 00:01:10 +03:00
}
/****************************************************************************
The internals of the rmdir code - called elsewhere .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2021-01-22 15:10:54 +03:00
static NTSTATUS rmdir_internals ( TALLOC_CTX * ctx , struct files_struct * fsp )
2009-12-04 00:01:10 +03:00
{
2021-01-22 15:10:54 +03:00
struct connection_struct * conn = fsp - > conn ;
2009-12-04 00:01:10 +03:00
struct smb_filename * smb_dname = fsp - > fsp_name ;
2021-01-22 16:40:36 +03:00
struct smb_filename * parent_fname = NULL ;
struct smb_filename * at_fname = NULL ;
2021-01-22 15:23:00 +03:00
const char * dname = NULL ;
char * talloced = NULL ;
long dirpos = 0 ;
struct smb_Dir * dir_hnd = NULL ;
2021-01-22 16:40:36 +03:00
struct files_struct * dirfsp = NULL ;
2021-01-22 15:14:28 +03:00
int unlink_flags = 0 ;
2021-01-22 16:40:36 +03:00
NTSTATUS status ;
2009-12-04 00:01:10 +03:00
int ret ;
SMB_ASSERT ( ! is_ntfs_stream_smb_fname ( smb_dname ) ) ;
2021-01-22 16:40:36 +03:00
status = parent_pathref ( talloc_tos ( ) ,
conn - > cwd_fsp ,
fsp - > fsp_name ,
& parent_fname ,
& at_fname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
/*
* Todo : use SMB_VFS_STATX ( ) once it ' s available .
*/
2009-12-04 00:01:10 +03:00
/* Might be a symlink. */
2021-01-22 15:10:54 +03:00
ret = SMB_VFS_LSTAT ( conn , smb_dname ) ;
if ( ret ! = 0 ) {
2021-01-22 16:40:36 +03:00
TALLOC_FREE ( parent_fname ) ;
2009-12-04 00:01:10 +03:00
return map_nt_error_from_unix ( errno ) ;
}
if ( S_ISLNK ( smb_dname - > st . st_ex_mode ) ) {
/* Is what it points to a directory ? */
2021-01-22 15:10:54 +03:00
ret = SMB_VFS_STAT ( conn , smb_dname ) ;
if ( ret ! = 0 ) {
2021-01-22 16:40:36 +03:00
TALLOC_FREE ( parent_fname ) ;
2009-12-04 00:01:10 +03:00
return map_nt_error_from_unix ( errno ) ;
}
if ( ! ( S_ISDIR ( smb_dname - > st . st_ex_mode ) ) ) {
2021-01-22 16:40:36 +03:00
TALLOC_FREE ( parent_fname ) ;
2009-12-04 00:01:10 +03:00
return NT_STATUS_NOT_A_DIRECTORY ;
}
} else {
2021-01-22 15:14:28 +03:00
unlink_flags = AT_REMOVEDIR ;
2009-12-04 00:01:10 +03:00
}
2021-01-22 16:40:36 +03:00
2021-01-22 15:14:28 +03:00
ret = SMB_VFS_UNLINKAT ( conn ,
2021-01-22 16:40:36 +03:00
parent_fname - > fsp ,
at_fname ,
2021-01-22 15:14:28 +03:00
unlink_flags ) ;
2009-12-04 00:01:10 +03:00
if ( ret = = 0 ) {
2021-01-22 16:40:36 +03:00
TALLOC_FREE ( parent_fname ) ;
2009-12-04 00:01:10 +03:00
notify_fname ( conn , NOTIFY_ACTION_REMOVED ,
FILE_NOTIFY_CHANGE_DIR_NAME ,
smb_dname - > base_name ) ;
return NT_STATUS_OK ;
}
2021-10-25 22:21:37 +03:00
if ( ! ( ( errno = = ENOTEMPTY ) | | ( errno = = EEXIST ) ) ) {
2021-01-22 15:23:00 +03:00
DEBUG ( 3 , ( " rmdir_internals: couldn't remove directory %s : "
" %s \n " , smb_fname_str_dbg ( smb_dname ) ,
strerror ( errno ) ) ) ;
2021-01-22 16:40:36 +03:00
TALLOC_FREE ( parent_fname ) ;
2021-01-22 15:23:00 +03:00
return map_nt_error_from_unix ( errno ) ;
}
2009-12-04 00:01:10 +03:00
2021-10-25 22:21:37 +03:00
/*
* Here we know the initial directory unlink failed with
* ENOTEMPTY or EEXIST so we know there are objects within .
* If we don ' t have permission to delete files non
* visible to the client just fail the directory delete .
*/
if ( ! lp_delete_veto_files ( SNUM ( conn ) ) ) {
2022-03-03 20:49:15 +03:00
status = NT_STATUS_DIRECTORY_NOT_EMPTY ;
2021-10-25 22:21:37 +03:00
goto err ;
}
2021-01-22 15:23:00 +03:00
/*
* Check to see if the only thing in this directory are
2021-10-25 22:21:37 +03:00
* files non - visible to the client . If not , fail the delete .
2021-01-22 15:23:00 +03:00
*/
2022-03-01 01:34:48 +03:00
status = OpenDir ( talloc_tos ( ) ,
conn ,
smb_dname ,
NULL ,
0 ,
& dir_hnd ) ;
2022-03-01 01:22:03 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
/*
* Note , we deliberately squash the error here
* to avoid leaking information about what we
* can ' t delete .
*/
2022-03-03 20:49:15 +03:00
status = NT_STATUS_DIRECTORY_NOT_EMPTY ;
2021-01-22 15:23:00 +03:00
goto err ;
}
2021-11-02 20:44:44 +03:00
dirfsp = dir_hnd_fetch_fsp ( dir_hnd ) ;
2022-09-06 10:07:31 +03:00
while ( ( dname = ReadDirName (
dir_hnd , & dirpos , NULL , & talloced ) ) ! = NULL ) {
2021-06-04 22:16:17 +03:00
struct smb_filename * smb_dname_full = NULL ;
struct smb_filename * direntry_fname = NULL ;
char * fullname = NULL ;
2021-11-04 05:02:36 +03:00
int retval ;
2021-06-04 22:16:17 +03:00
2021-06-03 03:56:59 +03:00
if ( ISDOT ( dname ) | | ISDOTDOT ( dname ) ) {
2009-12-04 00:01:10 +03:00
TALLOC_FREE ( talloced ) ;
2021-01-22 15:23:00 +03:00
continue ;
2009-12-04 00:01:10 +03:00
}
2021-06-04 22:16:17 +03:00
if ( IS_VETO_PATH ( conn , dname ) ) {
2021-01-22 15:23:00 +03:00
TALLOC_FREE ( talloced ) ;
continue ;
}
2021-06-04 22:16:17 +03:00
fullname = talloc_asprintf ( talloc_tos ( ) ,
" %s/%s " ,
smb_dname - > base_name ,
dname ) ;
if ( fullname = = NULL ) {
TALLOC_FREE ( talloced ) ;
2022-03-03 20:49:15 +03:00
status = NT_STATUS_NO_MEMORY ;
2021-06-04 22:16:17 +03:00
goto err ;
}
smb_dname_full = synthetic_smb_fname ( talloc_tos ( ) ,
fullname ,
NULL ,
NULL ,
smb_dname - > twrp ,
smb_dname - > flags ) ;
if ( smb_dname_full = = NULL ) {
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( fullname ) ;
2022-03-03 20:49:15 +03:00
status = NT_STATUS_NO_MEMORY ;
2021-06-04 22:16:17 +03:00
goto err ;
}
2021-11-04 05:02:36 +03:00
retval = SMB_VFS_LSTAT ( conn , smb_dname_full ) ;
if ( retval ! = 0 ) {
2022-03-03 20:49:15 +03:00
status = map_nt_error_from_unix ( errno ) ;
2021-01-22 15:23:00 +03:00
TALLOC_FREE ( talloced ) ;
2021-06-04 22:16:17 +03:00
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
2009-12-04 00:01:10 +03:00
goto err ;
}
2021-06-04 22:16:17 +03:00
if ( S_ISLNK ( smb_dname_full - > st . st_ex_mode ) ) {
2021-10-25 22:32:29 +03:00
/* Could it be an msdfs link ? */
if ( lp_host_msdfs ( ) & &
lp_msdfs_root ( SNUM ( conn ) ) ) {
struct smb_filename * smb_atname ;
smb_atname = synthetic_smb_fname ( talloc_tos ( ) ,
dname ,
NULL ,
& smb_dname_full - > st ,
fsp - > fsp_name - > twrp ,
fsp - > fsp_name - > flags ) ;
if ( smb_atname = = NULL ) {
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
2022-03-03 20:49:15 +03:00
status = NT_STATUS_NO_MEMORY ;
2021-10-25 22:32:29 +03:00
goto err ;
}
if ( is_msdfs_link ( fsp , smb_atname ) ) {
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( smb_atname ) ;
DBG_DEBUG ( " got msdfs link name %s "
" - can't delete directory %s \n " ,
dname ,
fsp_str_dbg ( fsp ) ) ;
2022-03-03 20:49:15 +03:00
status = NT_STATUS_DIRECTORY_NOT_EMPTY ;
2021-10-25 22:32:29 +03:00
goto err ;
}
TALLOC_FREE ( smb_atname ) ;
}
/* Not a DFS link - could it be a dangling symlink ? */
2021-11-04 05:02:36 +03:00
retval = SMB_VFS_STAT ( conn , smb_dname_full ) ;
if ( retval = = - 1 & & ( errno = = ENOENT | | errno = = ELOOP ) ) {
2021-10-25 22:32:29 +03:00
/*
* Dangling symlink .
* Allow delete as " delete veto files = yes "
*/
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
continue ;
}
DBG_DEBUG ( " got symlink name %s - "
" can't delete directory %s \n " ,
dname ,
fsp_str_dbg ( fsp ) ) ;
2021-06-04 22:16:17 +03:00
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
2022-03-03 20:49:15 +03:00
status = NT_STATUS_DIRECTORY_NOT_EMPTY ;
2021-10-25 22:32:29 +03:00
goto err ;
2021-06-04 22:16:17 +03:00
}
/* Not a symlink, get a pathref. */
status = synthetic_pathref ( talloc_tos ( ) ,
dirfsp ,
dname ,
NULL ,
& smb_dname_full - > st ,
smb_dname - > twrp ,
smb_dname - > flags ,
& direntry_fname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
goto err ;
}
2021-06-04 23:32:40 +03:00
if ( ! is_visible_fsp ( direntry_fname - > fsp ) ) {
2021-06-04 22:16:17 +03:00
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( direntry_fname ) ;
continue ;
}
2021-10-25 22:21:37 +03:00
/*
* We found a client visible name .
* We cannot delete this directory .
*/
DBG_DEBUG ( " got name %s - "
" can't delete directory %s \n " ,
dname ,
fsp_str_dbg ( fsp ) ) ;
2021-01-22 15:23:00 +03:00
TALLOC_FREE ( talloced ) ;
2021-06-04 22:16:17 +03:00
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( direntry_fname ) ;
2022-03-03 20:49:15 +03:00
status = NT_STATUS_DIRECTORY_NOT_EMPTY ;
2021-01-22 15:23:00 +03:00
goto err ;
}
2009-12-04 00:01:10 +03:00
2021-01-22 15:23:00 +03:00
/* Do a recursive delete. */
RewindDir ( dir_hnd , & dirpos ) ;
2021-01-22 16:40:36 +03:00
2022-09-06 10:07:31 +03:00
while ( ( dname = ReadDirName (
dir_hnd , & dirpos , NULL , & talloced ) ) ! = NULL ) {
2021-01-22 16:40:36 +03:00
struct smb_filename * direntry_fname = NULL ;
2021-01-22 15:23:00 +03:00
struct smb_filename * smb_dname_full = NULL ;
char * fullname = NULL ;
bool do_break = true ;
int retval ;
2009-12-04 00:01:10 +03:00
2021-01-22 15:23:00 +03:00
if ( ISDOT ( dname ) | | ISDOTDOT ( dname ) ) {
TALLOC_FREE ( talloced ) ;
continue ;
}
2009-12-04 00:01:10 +03:00
2021-01-22 15:23:00 +03:00
fullname = talloc_asprintf ( ctx ,
" %s/%s " ,
smb_dname - > base_name ,
dname ) ;
2021-01-22 15:10:54 +03:00
2021-01-22 15:23:00 +03:00
if ( fullname = = NULL ) {
2022-03-03 20:49:15 +03:00
status = NT_STATUS_NO_MEMORY ;
2021-01-22 15:23:00 +03:00
goto err_break ;
}
2021-01-22 15:14:28 +03:00
2021-01-22 15:23:00 +03:00
smb_dname_full = synthetic_smb_fname ( talloc_tos ( ) ,
fullname ,
NULL ,
NULL ,
smb_dname - > twrp ,
smb_dname - > flags ) ;
if ( smb_dname_full = = NULL ) {
2022-03-03 20:49:15 +03:00
status = NT_STATUS_NO_MEMORY ;
2021-01-22 15:23:00 +03:00
goto err_break ;
}
2021-01-22 16:40:36 +03:00
/*
* Todo : use SMB_VFS_STATX ( ) once that ' s available .
*/
2021-11-04 05:02:36 +03:00
retval = SMB_VFS_LSTAT ( conn , smb_dname_full ) ;
if ( retval ! = 0 ) {
2022-03-03 20:49:15 +03:00
status = map_nt_error_from_unix ( errno ) ;
2021-01-22 15:23:00 +03:00
goto err_break ;
}
2021-06-03 21:15:50 +03:00
/*
* We are only dealing with VETO ' ed objects
* here . If it ' s a symlink , just delete the
* link without caring what it is pointing
* to .
*/
if ( S_ISLNK ( smb_dname_full - > st . st_ex_mode ) ) {
direntry_fname = synthetic_smb_fname ( talloc_tos ( ) ,
dname ,
NULL ,
& smb_dname_full - > st ,
smb_dname - > twrp ,
smb_dname - > flags ) ;
if ( direntry_fname = = NULL ) {
2022-03-03 20:49:15 +03:00
status = NT_STATUS_NO_MEMORY ;
2021-06-03 21:15:50 +03:00
goto err_break ;
}
} else {
status = synthetic_pathref ( talloc_tos ( ) ,
dirfsp ,
dname ,
NULL ,
& smb_dname_full - > st ,
smb_dname - > twrp ,
smb_dname - > flags ,
& direntry_fname ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto err_break ;
}
2021-01-22 16:40:36 +03:00
2021-06-04 23:32:40 +03:00
if ( ! is_visible_fsp ( direntry_fname - > fsp ) ) {
2021-06-03 21:15:50 +03:00
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( talloced ) ;
TALLOC_FREE ( direntry_fname ) ;
continue ;
}
2021-06-03 03:51:25 +03:00
}
unlink_flags = 0 ;
if ( smb_dname_full - > st . st_ex_mode & S_IFDIR ) {
2022-03-03 20:34:45 +03:00
status = recursive_rmdir ( ctx , conn , smb_dname_full ) ;
2022-03-03 20:49:15 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2021-06-03 03:51:25 +03:00
goto err_break ;
}
unlink_flags = AT_REMOVEDIR ;
}
2021-01-22 15:23:00 +03:00
retval = SMB_VFS_UNLINKAT ( conn ,
2021-01-22 16:40:36 +03:00
dirfsp ,
direntry_fname ,
2021-06-03 19:36:08 +03:00
unlink_flags ) ;
2021-01-22 15:23:00 +03:00
if ( retval ! = 0 ) {
2022-03-03 20:49:15 +03:00
status = map_nt_error_from_unix ( errno ) ;
2021-01-22 15:23:00 +03:00
goto err_break ;
}
2009-12-04 00:01:10 +03:00
2021-01-22 15:23:00 +03:00
/* Successful iteration. */
do_break = false ;
err_break :
TALLOC_FREE ( fullname ) ;
TALLOC_FREE ( smb_dname_full ) ;
TALLOC_FREE ( talloced ) ;
2021-01-22 16:40:36 +03:00
TALLOC_FREE ( direntry_fname ) ;
2021-01-22 15:23:00 +03:00
if ( do_break ) {
break ;
2009-12-04 00:01:10 +03:00
}
}
2022-03-03 20:49:15 +03:00
/* If we get here, we know NT_STATUS_IS_OK(status) */
SMB_ASSERT ( NT_STATUS_IS_OK ( status ) ) ;
2021-01-22 15:23:00 +03:00
/* Retry the rmdir */
ret = SMB_VFS_UNLINKAT ( conn ,
2021-10-22 02:18:24 +03:00
parent_fname - > fsp ,
2021-01-22 16:40:36 +03:00
at_fname ,
2021-01-22 15:23:00 +03:00
AT_REMOVEDIR ) ;
2022-03-03 20:49:15 +03:00
if ( ret ! = 0 ) {
status = map_nt_error_from_unix ( errno ) ;
}
2021-01-22 16:40:36 +03:00
2009-12-04 00:01:10 +03:00
err :
2021-06-07 19:21:55 +03:00
TALLOC_FREE ( dir_hnd ) ;
2021-01-22 16:40:36 +03:00
TALLOC_FREE ( parent_fname ) ;
2009-12-04 00:01:10 +03:00
2022-03-03 20:49:15 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_NOTICE ( " couldn't remove directory %s : "
2009-12-04 00:01:10 +03:00
" %s \n " , smb_fname_str_dbg ( smb_dname ) ,
2022-03-03 20:49:15 +03:00
nt_errstr ( status ) ) ;
return status ;
2009-12-04 00:01:10 +03:00
}
notify_fname ( conn , NOTIFY_ACTION_REMOVED ,
FILE_NOTIFY_CHANGE_DIR_NAME ,
smb_dname - > base_name ) ;
2022-03-03 20:49:15 +03:00
return status ;
2009-12-04 00:01:10 +03:00
}
1998-08-17 17:11:34 +04:00
/****************************************************************************
Close a directory opened by an NT SMB call .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-10-09 18:27:49 +04:00
static NTSTATUS close_directory ( struct smb_request * req , files_struct * fsp ,
enum file_close_type close_type )
1998-08-17 17:11:34 +04:00
{
2022-09-12 20:49:09 +03:00
connection_struct * conn = fsp - > conn ;
2022-08-30 08:31:41 +03:00
struct close_share_mode_lock_state lck_state = { } ;
2022-09-12 20:49:09 +03:00
bool changed_user = false ;
2007-02-07 00:05:34 +03:00
NTSTATUS status = NT_STATUS_OK ;
2010-01-13 03:55:31 +03:00
NTSTATUS status1 = NT_STATUS_OK ;
2015-05-01 21:02:38 +03:00
NTSTATUS notify_status ;
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
NTSTATUS ulstatus ;
2015-05-01 21:02:38 +03:00
2020-12-17 03:38:28 +03:00
SMB_ASSERT ( fsp - > fsp_flags . is_fsa ) ;
2015-05-01 21:02:38 +03:00
if ( fsp - > conn - > sconn - > using_smb2 ) {
2020-06-19 22:38:59 +03:00
notify_status = NT_STATUS_NOTIFY_CLEANUP ;
2015-05-01 21:02:38 +03:00
} else {
notify_status = NT_STATUS_OK ;
}
2005-10-28 02:35:08 +04:00
2020-03-17 13:17:46 +03:00
assert_no_pending_aio ( fsp , close_type ) ;
2018-03-21 22:41:44 +03:00
1999-12-13 16:27:58 +03:00
/*
* NT can set delete_on_close of the last open
* reference to a directory also .
*/
2022-08-30 08:31:41 +03:00
lck_state = ( struct close_share_mode_lock_state ) {
. fsp = fsp ,
. object_type = " directory " ,
. close_type = close_type ,
} ;
2007-01-19 00:51:52 +03:00
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
status = share_mode_entry_prepare_lock_del ( & lck_state . prepare_state ,
fsp - > file_id ,
close_share_mode_lock_prepare ,
& lck_state ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DBG_ERR ( " share_mode_entry_prepare_lock_del() failed for %s - %s \n " ,
fsp_str_dbg ( fsp ) , nt_errstr ( status ) ) ;
return status ;
}
2007-01-19 00:51:52 +03:00
2022-08-30 08:31:41 +03:00
/*
* We don ' t have directory leases yet , so assert it in order
* to skip release_file_oplock ( ) .
*/
SMB_ASSERT ( fsp - > oplock_type = = NO_OPLOCK ) ;
2007-01-04 19:26:15 +03:00
2022-09-12 20:35:38 +03:00
/*
* NT can set delete_on_close of the last open
* reference to a file .
*/
2022-08-30 08:31:41 +03:00
if ( ! lck_state . delete_object ) {
2022-09-12 20:41:25 +03:00
status = NT_STATUS_OK ;
goto done ;
}
2022-09-12 20:35:38 +03:00
2022-09-12 20:41:25 +03:00
/*
* Ok , we have to delete the directory
*/
2022-08-30 08:31:41 +03:00
lck_state . cleanup_fn = close_share_mode_lock_cleanup ;
2022-09-12 20:35:38 +03:00
2022-08-30 08:31:41 +03:00
if ( lck_state . got_tokens & &
! unix_token_equal ( lck_state . del_token , get_current_utok ( conn ) ) )
{
2022-09-12 20:49:09 +03:00
/* Become the user who requested the delete. */
2006-02-02 23:44:50 +03:00
2022-09-12 20:49:09 +03:00
DBG_INFO ( " dir %s. Change user to uid %u \n " ,
fsp_str_dbg ( fsp ) ,
2022-08-30 08:31:41 +03:00
( unsigned int ) lck_state . del_token - > uid ) ;
2022-09-12 20:49:09 +03:00
if ( ! push_sec_ctx ( ) ) {
smb_panic ( " close_directory: failed to push sec_ctx. \n " ) ;
}
2022-08-30 08:31:41 +03:00
set_sec_ctx ( lck_state . del_token - > uid ,
lck_state . del_token - > gid ,
lck_state . del_token - > ngroups ,
lck_state . del_token - > groups ,
lck_state . del_nt_token ) ;
2022-09-12 20:49:09 +03:00
changed_user = true ;
}
2006-02-02 23:44:50 +03:00
2022-09-12 20:41:25 +03:00
if ( ( fsp - > conn - > fs_capabilities & FILE_NAMED_STREAMS )
& & ! is_ntfs_stream_smb_fname ( fsp - > fsp_name ) ) {
2012-02-01 01:26:23 +04:00
2022-09-12 20:41:25 +03:00
status = delete_all_streams ( fsp - > conn , fsp - > fsp_name ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 5 , ( " delete_all_streams failed: %s \n " ,
nt_errstr ( status ) ) ) ;
2022-09-12 20:49:09 +03:00
goto done ;
2012-02-01 01:26:23 +04:00
}
2022-09-12 20:41:25 +03:00
}
2012-02-01 01:26:23 +04:00
2022-09-12 20:41:25 +03:00
status = rmdir_internals ( talloc_tos ( ) , fsp ) ;
2006-02-02 23:44:50 +03:00
2022-09-12 20:41:25 +03:00
DEBUG ( 5 , ( " close_directory: %s. Delete on close was set - "
" deleting directory returned %s. \n " ,
fsp_str_dbg ( fsp ) , nt_errstr ( status ) ) ) ;
1999-12-13 16:27:58 +03:00
2022-09-12 20:41:25 +03:00
/*
* Ensure we remove any change notify requests that would
* now fail as the directory has been deleted .
*/
1999-12-13 16:27:58 +03:00
2022-09-12 20:41:25 +03:00
if ( NT_STATUS_IS_OK ( status ) ) {
notify_status = NT_STATUS_DELETE_PENDING ;
2022-09-12 20:15:58 +03:00
}
2012-06-02 16:06:29 +04:00
2022-09-12 20:41:25 +03:00
done :
2022-09-12 20:49:09 +03:00
if ( changed_user ) {
/* unbecome user. */
pop_sec_ctx ( ) ;
}
s3:smbd: make use of share_mode_entry_prepare_{lock_del,unlock}() in close_{remove_share_mode,directory}()
This gives a nice speed up...
The following test with 256 commections all looping with open/close
on the same inode (share root) is improved drastically:
smbtorture //127.0.0.1/m -Uroot%test smb2.bench.path-contention-shared \
--option='torture:bench_path=' \
--option="torture:timelimit=60" \
--option="torture:nprocs=256" \
--option="torture:qdepth=1"
From some like this:
open[num/s=11536,avslat=0.011450,minlat=0.000039,maxlat=0.052707]
close[num/s=11534,avslat=0.010878,minlat=0.000022,maxlat=0.052342]
to:
open[num/s=13225,avslat=0.010504,minlat=0.000042,maxlat=0.054023]
close[num/s=13223,avslat=0.008971,minlat=0.000022,maxlat=0.053838]
But this is only half of the solution, the next commits will
add a similar optimization to the open code, at the end we'll
perform like we did in Samba 4.12:
open[num/s=37680,avslat=0.003471,minlat=0.000040,maxlat=0.061411]
close[num/s=37678,avslat=0.003440,minlat=0.000022,maxlat=0.051536]
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15125
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
2022-08-30 08:31:41 +03:00
ulstatus = share_mode_entry_prepare_unlock ( & lck_state . prepare_state ,
lck_state . cleanup_fn ,
& lck_state ) ;
if ( ! NT_STATUS_IS_OK ( ulstatus ) ) {
DBG_ERR ( " share_mode_entry_prepare_unlock() failed for %s - %s \n " ,
fsp_str_dbg ( fsp ) , nt_errstr ( ulstatus ) ) ;
smb_panic ( " share_mode_entry_prepare_unlock() failed! " ) ;
2001-11-13 04:35:20 +03:00
}
1999-12-13 16:27:58 +03:00
2015-05-01 21:02:38 +03:00
remove_pending_change_notify_requests_by_fid ( fsp , notify_status ) ;
2010-01-13 03:55:31 +03:00
status1 = fd_close ( fsp ) ;
2008-11-27 03:47:14 +03:00
2010-01-13 03:55:31 +03:00
if ( ! NT_STATUS_IS_OK ( status1 ) ) {
2008-11-27 03:47:14 +03:00
DEBUG ( 0 , ( " Could not close dir! fname=%s, fd=%d, err=%d=%s \n " ,
2020-10-03 18:41:17 +03:00
fsp_str_dbg ( fsp ) , fsp_get_pathref_fd ( fsp ) , errno ,
2009-07-02 20:27:44 +04:00
strerror ( errno ) ) ) ;
2008-11-27 03:47:14 +03:00
}
2010-01-13 03:55:31 +03:00
if ( NT_STATUS_IS_OK ( status ) & & ! NT_STATUS_IS_OK ( status1 ) ) {
status = status1 ;
}
2007-02-07 00:05:34 +03:00
return status ;
2003-01-03 21:50:13 +03:00
}
1999-12-13 16:27:58 +03:00
/****************************************************************************
2022-02-09 20:03:33 +03:00
Rundown all SMB - related dependencies of a files struct
1999-12-13 16:27:58 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2022-02-09 20:03:33 +03:00
NTSTATUS close_file_smb ( struct smb_request * req ,
struct files_struct * fsp ,
enum file_close_type close_type )
1999-12-13 16:27:58 +03:00
{
2008-01-20 01:25:36 +03:00
NTSTATUS status ;
2020-12-17 23:31:35 +03:00
/*
* This fsp can never be an internal dirfsp . They must
* be explicitly closed by TALLOC_FREE of the dir handle .
*/
SMB_ASSERT ( ! fsp - > fsp_flags . is_dirfsp ) ;
2020-05-18 15:37:18 +03:00
2022-02-02 14:42:08 +03:00
/*
* Never call directly on a base fsp
*/
SMB_ASSERT ( fsp - > stream_fsp = = NULL ) ;
2020-12-23 13:50:34 +03:00
2020-12-23 14:10:37 +03:00
if ( fsp - > fake_file_handle ! = NULL ) {
2008-10-09 18:27:49 +04:00
status = close_fake_file ( req , fsp ) ;
2020-12-17 06:49:56 +03:00
} else if ( fsp - > print_file ! = NULL ) {
/* FIXME: return spool errors */
print_spool_end ( fsp , close_type ) ;
2021-12-28 14:25:40 +03:00
fd_close ( fsp ) ;
2020-12-17 06:49:56 +03:00
status = NT_STATUS_OK ;
2020-12-23 14:10:37 +03:00
} else if ( ! fsp - > fsp_flags . is_fsa ) {
if ( close_type = = NORMAL_CLOSE ) {
DBG_ERR ( " unexpected NORMAL_CLOSE for [%s] "
" is_fsa[%u] is_pathref[%u] is_directory[%u] \n " ,
fsp_str_dbg ( fsp ) ,
fsp - > fsp_flags . is_fsa ,
fsp - > fsp_flags . is_pathref ,
fsp - > fsp_flags . is_directory ) ;
}
SMB_ASSERT ( close_type ! = NORMAL_CLOSE ) ;
fd_close ( fsp ) ;
status = NT_STATUS_OK ;
} else if ( fsp - > fsp_flags . is_directory ) {
status = close_directory ( req , fsp , close_type ) ;
2008-01-20 01:25:36 +03:00
} else {
2008-10-09 18:27:49 +04:00
status = close_normal_file ( req , fsp , close_type ) ;
2008-01-20 01:25:36 +03:00
}
2022-02-11 11:37:35 +03:00
if ( fsp_is_alternate_stream ( fsp ) ) {
2022-02-02 14:42:08 +03:00
/*
* fsp was a stream , its base_fsp can ' t be a stream
* as well
*/
2022-02-11 11:37:35 +03:00
SMB_ASSERT ( ! fsp_is_alternate_stream ( fsp - > base_fsp ) ) ;
2022-02-01 19:21:24 +03:00
2022-02-02 14:42:08 +03:00
/*
* There ' s a 1 : 1 relationship between fsp and a base_fsp
*/
SMB_ASSERT ( fsp - > base_fsp - > stream_fsp = = fsp ) ;
2008-01-20 01:25:36 +03:00
/*
2022-02-02 14:42:08 +03:00
* Make base_fsp look standalone now
2008-01-20 01:25:36 +03:00
*/
2022-02-02 14:42:08 +03:00
fsp - > base_fsp - > stream_fsp = NULL ;
2008-01-20 01:25:36 +03:00
2022-02-02 14:42:08 +03:00
close_file_free ( req , & fsp - > base_fsp , close_type ) ;
2008-01-20 01:25:36 +03:00
}
2022-02-09 20:03:33 +03:00
fsp_unbind_smb ( req , fsp ) ;
2022-02-02 14:42:08 +03:00
2022-02-09 20:03:33 +03:00
return status ;
}
NTSTATUS close_file_free ( struct smb_request * req ,
struct files_struct * * _fsp ,
enum file_close_type close_type )
{
struct files_struct * fsp = * _fsp ;
NTSTATUS status ;
status = close_file_smb ( req , fsp , close_type ) ;
file_free ( req , fsp ) ;
* _fsp = NULL ;
2022-02-01 19:47:29 +03:00
2008-01-20 01:25:36 +03:00
return status ;
1999-12-13 16:27:58 +03:00
}
2008-05-27 22:23:59 +04:00
/****************************************************************************
Deal with an ( authorized ) message to close a file given the share mode
entry .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void msg_close_file ( struct messaging_context * msg_ctx ,
void * private_data ,
uint32_t msg_type ,
struct server_id server_id ,
DATA_BLOB * data )
{
files_struct * fsp = NULL ;
2018-02-07 13:40:58 +03:00
struct file_id id ;
2008-05-27 22:23:59 +04:00
struct share_mode_entry e ;
2011-12-12 18:45:07 +04:00
struct smbd_server_connection * sconn =
2011-12-14 12:50:24 +04:00
talloc_get_type_abort ( private_data ,
2011-12-12 18:45:07 +04:00
struct smbd_server_connection ) ;
2008-05-27 22:23:59 +04:00
2018-02-07 13:40:58 +03:00
message_to_share_mode_entry ( & id , & e , ( char * ) data - > data ) ;
2008-05-27 22:23:59 +04:00
if ( DEBUGLVL ( 10 ) ) {
2018-02-07 13:40:58 +03:00
char * sm_str = share_mode_str ( NULL , 0 , & id , & e ) ;
2008-05-27 22:23:59 +04:00
if ( ! sm_str ) {
smb_panic ( " talloc failed " ) ;
}
DEBUG ( 10 , ( " msg_close_file: got request to close share mode "
" entry %s \n " , sm_str ) ) ;
TALLOC_FREE ( sm_str ) ;
}
2018-02-07 13:40:58 +03:00
fsp = file_find_dif ( sconn , id , e . share_file_id ) ;
2008-05-27 22:23:59 +04:00
if ( ! fsp ) {
DEBUG ( 10 , ( " msg_close_file: failed to find file. \n " ) ) ;
return ;
}
2022-02-01 19:47:29 +03:00
close_file_free ( NULL , & fsp , NORMAL_CLOSE ) ;
2008-05-27 22:23:59 +04:00
}