1996-05-04 11:50:46 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
Locking functions
2000-04-28 01:12:33 +04:00
Copyright ( C ) Andrew Tridgell 1992 - 2000
2006-04-10 19:33:04 +04:00
Copyright ( C ) Jeremy Allison 1992 - 2006
2005-07-08 08:51:27 +04:00
Copyright ( C ) Volker Lendecke 2005
2011-05-28 12:24:20 +04:00
1996-05-04 11:50:46 +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
1996-05-04 11:50:46 +04:00
( at your option ) any later version .
2011-05-28 12:24:20 +04:00
1996-05-04 11:50:46 +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 .
2011-05-28 12:24:20 +04:00
1996-05-04 11:50:46 +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/>.
1996-08-15 19:11:34 +04:00
Revision History :
12 aug 96 : Erik . Devriendt @ te6 . siemens . be
added support for shared memory implementation of share mode locking
1997-05-20 04:32:51 +04:00
May 1997. Jeremy Allison ( jallison @ whistle . com ) . Modified share mode
locking to deal with multiple share modes per open file .
1997-10-07 22:46:19 +04:00
September 1997. Jeremy Allison ( jallison @ whistle . com ) . Added oplock
support .
2011-12-15 18:48:33 +04:00
rewritten completely to use new tdb code . Tridge , Dec ' 99
2000-04-28 01:12:33 +04:00
Added POSIX locking support . Jeremy Allison ( jeremy @ valinux . com ) , Apr . 2000.
2006-04-10 19:33:04 +04:00
Added Unix Extensions POSIX locking support . Jeremy Allison Mar 2006.
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2011-03-23 14:43:17 +03:00
# include "locking/proto.h"
2010-07-04 20:18:58 +04:00
# include "smbd/globals.h"
2011-07-07 19:42:08 +04:00
# include "dbwrap/dbwrap.h"
2011-07-06 18:40:21 +04:00
# include "dbwrap/dbwrap_open.h"
2010-10-12 08:27:50 +04:00
# include "../libcli/security/security.h"
2011-02-25 01:05:57 +03:00
# include "serverid.h"
2011-03-24 17:31:06 +03:00
# include "messages.h"
2011-05-05 13:25:29 +04:00
# include "util_tdb.h"
2011-11-24 17:11:28 +04:00
# include "../librpc/gen_ndr/ndr_open_files.h"
1996-05-04 11:50:46 +04:00
2005-04-27 22:32:37 +04:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_LOCKING
2007-07-17 04:50:48 +04:00
# define NO_LOCKING_COUNT (-1)
2000-04-12 21:59:09 +04:00
/****************************************************************************
2006-04-10 19:33:04 +04:00
Debugging aids : - ) .
2000-04-12 21:59:09 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-04-10 19:33:04 +04:00
const char * lock_type_name ( enum brl_type lock_type )
2000-04-12 21:59:09 +04:00
{
2006-04-10 19:33:04 +04:00
switch ( lock_type ) {
case READ_LOCK :
return " READ " ;
case WRITE_LOCK :
return " WRITE " ;
2006-07-29 23:14:24 +04:00
case PENDING_READ_LOCK :
return " PENDING_READ " ;
case PENDING_WRITE_LOCK :
return " PENDING_WRITE " ;
2006-04-10 19:33:04 +04:00
default :
return " other " ;
}
}
const char * lock_flav_name ( enum brl_flavour lock_flav )
{
return ( lock_flav = = WINDOWS_LOCK ) ? " WINDOWS_LOCK " : " POSIX_LOCK " ;
2000-04-12 21:59:09 +04:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
1998-07-23 04:10:26 +04:00
Utility function called to see if a file region is locked .
2006-04-10 19:33:04 +04:00
Called in the read / write codepath .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-12 21:59:09 +04:00
2009-03-14 00:15:28 +03:00
void init_strict_lock_struct ( files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64_t smblctx ,
2009-03-14 00:15:28 +03:00
br_off start ,
br_off size ,
enum brl_type lock_type ,
struct lock_struct * plock )
{
SMB_ASSERT ( lock_type = = READ_LOCK | | lock_type = = WRITE_LOCK ) ;
2010-05-07 17:20:50 +04:00
plock - > context . smblctx = smblctx ;
2009-03-14 00:15:28 +03:00
plock - > context . tid = fsp - > conn - > cnum ;
2011-12-15 14:50:43 +04:00
plock - > context . pid = messaging_server_id ( fsp - > conn - > sconn - > msg_ctx ) ;
2009-03-14 00:15:28 +03:00
plock - > start = start ;
plock - > size = size ;
plock - > fnum = fsp - > fnum ;
plock - > lock_type = lock_type ;
plock - > lock_flav = lp_posix_cifsu_locktype ( fsp ) ;
}
bool strict_lock_default ( files_struct * fsp , struct lock_struct * plock )
1996-05-04 11:50:46 +04:00
{
2006-11-11 20:05:11 +03:00
int strict_locking = lp_strict_locking ( fsp - > conn - > params ) ;
2009-03-14 00:15:28 +03:00
bool ret = False ;
if ( plock - > size = = 0 ) {
return True ;
2006-04-10 19:33:04 +04:00
}
1996-05-04 11:50:46 +04:00
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) | | ! strict_locking ) {
2009-03-14 00:15:28 +03:00
return True ;
2006-04-10 19:33:04 +04:00
}
1998-07-28 22:26:47 +04:00
2004-12-11 03:30:28 +03:00
if ( strict_locking = = Auto ) {
2009-03-14 00:15:28 +03:00
if ( EXCLUSIVE_OPLOCK_TYPE ( fsp - > oplock_type ) & & ( plock - > lock_type = = READ_LOCK | | plock - > lock_type = = WRITE_LOCK ) ) {
2009-07-11 01:50:37 +04:00
DEBUG ( 10 , ( " is_locked: optimisation - exclusive oplock on file %s \n " , fsp_str_dbg ( fsp ) ) ) ;
2009-03-14 00:15:28 +03:00
ret = True ;
2005-09-30 21:13:37 +04:00
} else if ( ( fsp - > oplock_type = = LEVEL_II_OPLOCK ) & &
2009-03-14 00:15:28 +03:00
( plock - > lock_type = = READ_LOCK ) ) {
2009-07-11 01:50:37 +04:00
DEBUG ( 10 , ( " is_locked: optimisation - level II oplock on file %s \n " , fsp_str_dbg ( fsp ) ) ) ;
2009-03-14 00:15:28 +03:00
ret = True ;
2004-12-11 03:30:28 +03:00
} else {
2009-11-16 11:40:47 +03:00
struct byte_range_lock * br_lck ;
br_lck = brl_get_locks_readonly ( fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
2009-03-14 00:15:28 +03:00
return True ;
2006-04-10 19:33:04 +04:00
}
2009-03-14 00:15:28 +03:00
ret = brl_locktest ( br_lck ,
2010-05-07 17:20:50 +04:00
plock - > context . smblctx ,
2009-03-14 00:15:28 +03:00
plock - > context . pid ,
plock - > start ,
plock - > size ,
plock - > lock_type ,
plock - > lock_flav ) ;
2004-12-11 03:30:28 +03:00
}
2004-12-10 01:10:40 +03:00
} else {
2009-11-16 11:40:47 +03:00
struct byte_range_lock * br_lck ;
br_lck = brl_get_locks_readonly ( fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
2009-03-14 00:15:28 +03:00
return True ;
2006-04-10 19:33:04 +04:00
}
2009-03-14 00:15:28 +03:00
ret = brl_locktest ( br_lck ,
2010-05-07 17:20:50 +04:00
plock - > context . smblctx ,
2009-03-14 00:15:28 +03:00
plock - > context . pid ,
plock - > start ,
plock - > size ,
plock - > lock_type ,
plock - > lock_flav ) ;
2004-12-10 01:10:40 +03:00
}
2001-06-30 05:59:48 +04:00
2009-03-14 00:15:28 +03:00
DEBUG ( 10 , ( " strict_lock_default: flavour = %s brl start=%.0f "
" len=%.0f %s for fnum %d file %s \n " ,
lock_flav_name ( plock - > lock_flav ) ,
( double ) plock - > start , ( double ) plock - > size ,
ret ? " unlocked " : " locked " ,
2009-07-11 01:50:37 +04:00
plock - > fnum , fsp_str_dbg ( fsp ) ) ) ;
1996-05-04 11:50:46 +04:00
2006-04-10 19:33:04 +04:00
return ret ;
}
2009-03-14 00:15:28 +03:00
void strict_unlock_default ( files_struct * fsp , struct lock_struct * plock )
{
}
2006-04-10 19:33:04 +04:00
/****************************************************************************
Find out if a lock could be granted - return who is blocking us if we can ' t .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-12 21:59:09 +04:00
2006-04-10 19:33:04 +04:00
NTSTATUS query_lock ( files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64_t * psmblctx ,
2008-10-14 03:59:36 +04:00
uint64_t * pcount ,
uint64_t * poffset ,
2006-04-10 19:33:04 +04:00
enum brl_type * plock_type ,
enum brl_flavour lock_flav )
{
struct byte_range_lock * br_lck = NULL ;
2000-04-12 21:59:09 +04:00
2006-07-11 22:01:26 +04:00
if ( ! fsp - > can_lock ) {
return fsp - > is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE ;
2001-06-30 05:59:48 +04:00
}
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2006-04-10 19:33:04 +04:00
return NT_STATUS_OK ;
}
2009-11-16 11:40:47 +03:00
br_lck = brl_get_locks_readonly ( fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
return NT_STATUS_NO_MEMORY ;
}
2009-11-16 11:40:47 +03:00
return brl_lockquery ( br_lck ,
2010-05-07 17:20:50 +04:00
psmblctx ,
2011-12-15 14:50:43 +04:00
messaging_server_id ( fsp - > conn - > sconn - > msg_ctx ) ,
2006-04-10 19:33:04 +04:00
poffset ,
pcount ,
plock_type ,
lock_flav ) ;
2000-04-12 21:59:09 +04:00
}
1996-05-04 11:50:46 +04:00
2009-02-10 08:51:29 +03:00
static void increment_current_lock_count ( files_struct * fsp ,
enum brl_flavour lock_flav )
{
if ( lock_flav = = WINDOWS_LOCK & &
fsp - > current_lock_count ! = NO_LOCKING_COUNT ) {
/* blocking ie. pending, locks also count here,
* as this is an efficiency counter to avoid checking
* the lock db . on close . JRA . */
fsp - > current_lock_count + + ;
} else {
/* Notice that this has had a POSIX lock request.
* We can ' t count locks after this so forget them .
*/
fsp - > current_lock_count = NO_LOCKING_COUNT ;
}
}
static void decrement_current_lock_count ( files_struct * fsp ,
enum brl_flavour lock_flav )
{
if ( lock_flav = = WINDOWS_LOCK & &
fsp - > current_lock_count ! = NO_LOCKING_COUNT ) {
SMB_ASSERT ( fsp - > current_lock_count > 0 ) ;
fsp - > current_lock_count - - ;
}
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
1998-07-23 04:10:26 +04:00
Utility function called by locking requests .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-12 21:59:09 +04:00
2007-05-14 17:01:28 +04:00
struct byte_range_lock * do_lock ( struct messaging_context * msg_ctx ,
files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64_t smblctx ,
2008-10-14 03:59:36 +04:00
uint64_t count ,
uint64_t offset ,
2006-04-10 19:33:04 +04:00
enum brl_type lock_type ,
enum brl_flavour lock_flav ,
2007-10-19 04:40:25 +04:00
bool blocking_lock ,
2007-05-20 00:57:12 +04:00
NTSTATUS * perr ,
2010-05-07 17:20:50 +04:00
uint64_t * psmblctx ,
2009-02-10 08:51:29 +03:00
struct blocking_lock_record * blr )
1996-05-04 11:50:46 +04:00
{
2006-04-10 19:33:04 +04:00
struct byte_range_lock * br_lck = NULL ;
1999-12-21 12:25:59 +03:00
2010-04-29 03:42:04 +04:00
/* silently return ok on print files as we don't do locking there */
if ( fsp - > print_file ) {
* perr = NT_STATUS_OK ;
return NULL ;
}
2006-07-11 22:01:26 +04:00
if ( ! fsp - > can_lock ) {
2006-07-18 05:05:51 +04:00
* perr = fsp - > is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE ;
return NULL ;
2006-04-10 19:33:04 +04:00
}
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2006-07-18 05:05:51 +04:00
* perr = NT_STATUS_OK ;
return NULL ;
2006-04-10 19:33:04 +04:00
}
1999-12-21 12:25:59 +03:00
2001-08-24 08:55:28 +04:00
/* NOTE! 0 byte long ranges ARE allowed and should be stored */
2009-02-10 08:51:29 +03:00
DEBUG ( 10 , ( " do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
" blocking_lock=%s requested for fnum %d file %s \n " ,
2006-04-10 19:33:04 +04:00
lock_flav_name ( lock_flav ) , lock_type_name ( lock_type ) ,
2009-02-10 08:51:29 +03:00
( double ) offset , ( double ) count , blocking_lock ? " true " :
2009-07-11 01:50:37 +04:00
" false " , fsp - > fnum , fsp_str_dbg ( fsp ) ) ) ;
2006-04-10 19:33:04 +04:00
2008-01-06 18:15:45 +03:00
br_lck = brl_get_locks ( talloc_tos ( ) , fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
2006-07-18 05:05:51 +04:00
* perr = NT_STATUS_NO_MEMORY ;
return NULL ;
2000-01-13 15:09:36 +03:00
}
1999-12-21 12:25:59 +03:00
2007-05-14 17:01:28 +04:00
* perr = brl_lock ( msg_ctx ,
br_lck ,
2010-05-07 17:20:50 +04:00
smblctx ,
2011-12-15 14:50:43 +04:00
messaging_server_id ( fsp - > conn - > sconn - > msg_ctx ) ,
2006-04-10 19:33:04 +04:00
offset ,
2010-05-07 17:20:50 +04:00
count ,
2006-04-10 19:33:04 +04:00
lock_type ,
lock_flav ,
2007-05-20 00:57:12 +04:00
blocking_lock ,
2010-05-07 17:20:50 +04:00
psmblctx ,
2009-02-10 08:51:29 +03:00
blr ) ;
2006-04-10 19:33:04 +04:00
2009-02-10 08:51:29 +03:00
DEBUG ( 10 , ( " do_lock: returning status=%s \n " , nt_errstr ( * perr ) ) ) ;
2007-03-07 00:59:51 +03:00
2009-02-10 08:51:29 +03:00
increment_current_lock_count ( fsp , lock_flav ) ;
2006-07-18 05:05:51 +04:00
return br_lck ;
1996-05-04 11:50:46 +04:00
}
2002-03-13 23:28:19 +03:00
/****************************************************************************
2006-07-18 01:09:02 +04:00
Utility function called by unlocking requests .
2002-03-13 23:28:19 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-14 17:01:28 +04:00
NTSTATUS do_unlock ( struct messaging_context * msg_ctx ,
files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64_t smblctx ,
2008-10-14 03:59:36 +04:00
uint64_t count ,
uint64_t offset ,
2006-07-18 01:09:02 +04:00
enum brl_flavour lock_flav )
2002-03-13 23:28:19 +03:00
{
2007-10-19 04:40:25 +04:00
bool ok = False ;
2006-07-18 01:09:02 +04:00
struct byte_range_lock * br_lck = NULL ;
2011-05-28 12:24:20 +04:00
2006-07-18 01:09:02 +04:00
if ( ! fsp - > can_lock ) {
return fsp - > is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE ;
2006-04-10 19:33:04 +04:00
}
2011-05-28 12:24:20 +04:00
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2006-07-18 01:09:02 +04:00
return NT_STATUS_OK ;
}
2011-05-28 12:24:20 +04:00
2006-07-18 01:09:02 +04:00
DEBUG ( 10 , ( " do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s \n " ,
2009-07-11 01:50:37 +04:00
( double ) offset , ( double ) count , fsp - > fnum ,
fsp_str_dbg ( fsp ) ) ) ;
2002-03-13 23:49:57 +03:00
2008-01-06 18:15:45 +03:00
br_lck = brl_get_locks ( talloc_tos ( ) , fsp ) ;
2006-07-18 01:09:02 +04:00
if ( ! br_lck ) {
return NT_STATUS_NO_MEMORY ;
}
2006-04-10 19:33:04 +04:00
2007-05-14 17:01:28 +04:00
ok = brl_unlock ( msg_ctx ,
br_lck ,
2010-05-07 17:20:50 +04:00
smblctx ,
2011-12-15 14:50:43 +04:00
messaging_server_id ( fsp - > conn - > sconn - > msg_ctx ) ,
2006-07-18 01:09:02 +04:00
offset ,
count ,
lock_flav ) ;
2011-05-28 12:24:20 +04:00
2006-07-18 01:09:02 +04:00
TALLOC_FREE ( br_lck ) ;
2006-04-10 19:33:04 +04:00
2006-07-18 01:09:02 +04:00
if ( ! ok ) {
DEBUG ( 10 , ( " do_unlock: returning ERRlock. \n " ) ) ;
return NT_STATUS_RANGE_NOT_LOCKED ;
2002-03-13 23:28:19 +03:00
}
2006-07-18 01:09:02 +04:00
2009-02-10 08:51:29 +03:00
decrement_current_lock_count ( fsp , lock_flav ) ;
2006-07-18 01:09:02 +04:00
return NT_STATUS_OK ;
2002-03-13 23:28:19 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2006-07-18 01:09:02 +04:00
Cancel any pending blocked locks .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-04-13 03:01:11 +04:00
2006-07-18 01:09:02 +04:00
NTSTATUS do_lock_cancel ( files_struct * fsp ,
2010-05-07 17:20:50 +04:00
uint64 smblctx ,
2008-10-14 03:59:36 +04:00
uint64_t count ,
uint64_t offset ,
2009-02-10 08:51:29 +03:00
enum brl_flavour lock_flav ,
struct blocking_lock_record * blr )
1996-05-04 11:50:46 +04:00
{
2007-10-19 04:40:25 +04:00
bool ok = False ;
2006-04-10 19:33:04 +04:00
struct byte_range_lock * br_lck = NULL ;
2009-02-10 08:51:29 +03:00
2006-07-11 22:01:26 +04:00
if ( ! fsp - > can_lock ) {
2006-07-18 01:09:02 +04:00
return fsp - > is_directory ?
NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE ;
2006-04-10 19:33:04 +04:00
}
2011-05-28 12:24:20 +04:00
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2006-07-18 01:09:02 +04:00
return NT_STATUS_DOS ( ERRDOS , ERRcancelviolation ) ;
2000-04-26 00:30:58 +04:00
}
2006-07-18 01:09:02 +04:00
DEBUG ( 10 , ( " do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s \n " ,
2009-07-11 01:50:37 +04:00
( double ) offset , ( double ) count , fsp - > fnum ,
fsp_str_dbg ( fsp ) ) ) ;
2000-04-12 21:59:09 +04:00
2008-01-06 18:15:45 +03:00
br_lck = brl_get_locks ( talloc_tos ( ) , fsp ) ;
2006-04-10 19:33:04 +04:00
if ( ! br_lck ) {
return NT_STATUS_NO_MEMORY ;
}
2003-04-05 00:38:12 +04:00
2006-07-18 01:09:02 +04:00
ok = brl_lock_cancel ( br_lck ,
2010-05-07 17:20:50 +04:00
smblctx ,
2011-12-15 14:50:43 +04:00
messaging_server_id ( fsp - > conn - > sconn - > msg_ctx ) ,
2006-04-10 19:33:04 +04:00
offset ,
count ,
2009-02-10 08:51:29 +03:00
lock_flav ,
blr ) ;
2006-07-11 22:01:26 +04:00
TALLOC_FREE ( br_lck ) ;
2006-04-10 19:33:04 +04:00
1999-12-21 12:25:59 +03:00
if ( ! ok ) {
2006-07-18 01:09:02 +04:00
DEBUG ( 10 , ( " do_lock_cancel: returning ERRcancelviolation. \n " ) ) ;
return NT_STATUS_DOS ( ERRDOS , ERRcancelviolation ) ;
1999-12-21 12:25:59 +03:00
}
2006-04-10 19:33:04 +04:00
2009-02-10 08:51:29 +03:00
decrement_current_lock_count ( fsp , lock_flav ) ;
2001-09-04 11:13:01 +04:00
return NT_STATUS_OK ;
1996-05-04 11:50:46 +04:00
}
2000-04-26 00:30:58 +04:00
/****************************************************************************
Remove any locks on this fd . Called from file_close ( ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-14 17:01:28 +04:00
void locking_close_file ( struct messaging_context * msg_ctx ,
2010-05-07 12:20:26 +04:00
files_struct * fsp ,
enum file_close_type close_type )
2000-04-26 00:30:58 +04:00
{
2006-04-10 19:33:04 +04:00
struct byte_range_lock * br_lck ;
2000-04-26 00:30:58 +04:00
2006-11-11 20:05:11 +03:00
if ( ! lp_locking ( fsp - > conn - > params ) ) {
2000-04-26 00:30:58 +04:00
return ;
2006-04-10 19:33:04 +04:00
}
2000-04-26 00:30:58 +04:00
2007-03-07 00:59:51 +03:00
/* If we have not outstanding locks or pending
* locks then we don ' t need to look in the lock db .
*/
if ( fsp - > current_lock_count = = 0 ) {
return ;
}
2008-01-06 18:15:45 +03:00
br_lck = brl_get_locks ( talloc_tos ( ) , fsp ) ;
2006-07-18 01:09:02 +04:00
2006-07-11 22:01:26 +04:00
if ( br_lck ) {
2010-05-07 12:20:26 +04:00
cancel_pending_lock_requests_by_fid ( fsp , br_lck , close_type ) ;
2007-05-14 17:01:28 +04:00
brl_close_fnum ( msg_ctx , br_lck ) ;
2006-07-11 22:01:26 +04:00
TALLOC_FREE ( br_lck ) ;
2000-04-26 00:30:58 +04:00
}
}
2002-01-08 03:52:36 +03:00
/*******************************************************************
Print out a share mode .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-05-27 22:23:59 +04:00
char * share_mode_str ( TALLOC_CTX * ctx , int num , const struct share_mode_entry * e )
2002-01-08 03:52:36 +03:00
{
2011-12-14 13:35:56 +04:00
return talloc_asprintf ( ctx , " share_mode_entry[%d]: "
2005-09-30 21:13:37 +04:00
" pid = %s, share_access = 0x%x, private_options = 0x%x, "
2011-12-25 14:35:07 +04:00
" access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %llu, "
2011-01-26 01:01:52 +03:00
" uid = %u, flags = %u, file_id %s, name_hash = 0x%x " ,
2006-01-27 22:54:39 +03:00
num ,
procid_str_static ( & e - > pid ) ,
2005-07-08 08:51:27 +04:00
e - > share_access , e - > private_options ,
2010-04-13 08:40:28 +04:00
e - > access_mask , ( unsigned long long ) e - > op_mid ,
2012-01-07 02:25:06 +04:00
e - > op_type , ( unsigned long long ) e - > share_file_id ,
2007-02-07 00:05:34 +03:00
( unsigned int ) e - > uid , ( unsigned int ) e - > flags ,
2011-01-26 01:01:52 +03:00
file_id_string_tos ( & e - > id ) ,
( unsigned int ) e - > name_hash ) ;
2002-01-08 03:52:36 +03:00
}
2012-01-13 02:46:45 +04:00
/*******************************************************************
Fetch a share mode where we know one MUST exist . This call reference
counts it internally to allow for nested lock fetches .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-02-19 17:23:56 +04:00
struct share_mode_lock * get_existing_share_mode_lock ( TALLOC_CTX * mem_ctx ,
const struct file_id id )
2012-01-09 17:30:53 +04:00
{
2012-02-19 17:27:49 +04:00
return get_share_mode_lock ( mem_ctx , id , NULL , NULL , NULL ) ;
2012-01-09 17:30:53 +04:00
}
2005-12-13 01:07:36 +03:00
/*******************************************************************
Sets the service name and filename for rename .
2005-12-13 21:11:50 +03:00
At this point we emit " file renamed " messages to all
process id ' s that have this file open .
Based on an initial code idea from SATOH Fumiyasu < fumiya @ samba . gr . jp >
2005-12-13 01:07:36 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool rename_share_filename ( struct messaging_context * msg_ctx ,
2007-05-14 17:01:28 +04:00
struct share_mode_lock * lck ,
2005-12-13 01:07:36 +03:00
const char * servicepath ,
2011-01-26 01:01:52 +03:00
uint32_t orig_name_hash ,
uint32_t new_name_hash ,
2009-07-09 03:22:44 +04:00
const struct smb_filename * smb_fname_dst )
2005-12-13 01:07:36 +03:00
{
2012-01-10 16:56:37 +04:00
struct share_mode_data * d = lck - > data ;
2005-12-13 21:11:50 +03:00
size_t sp_len ;
2009-07-09 03:22:44 +04:00
size_t bn_len ;
size_t sn_len ;
2005-12-13 21:11:50 +03:00
size_t msg_len ;
2006-01-13 01:17:54 +03:00
char * frm = NULL ;
2005-12-13 21:11:50 +03:00
int i ;
2009-07-09 03:22:44 +04:00
bool strip_two_chars = false ;
bool has_stream = smb_fname_dst - > stream_name ! = NULL ;
2005-12-13 21:11:50 +03:00
DEBUG ( 10 , ( " rename_share_filename: servicepath %s newname %s \n " ,
2009-07-09 03:22:44 +04:00
servicepath , smb_fname_dst - > base_name ) ) ;
2005-12-13 21:11:50 +03:00
2005-12-13 01:07:36 +03:00
/*
* rename_internal_fsp ( ) and rename_internals ( ) add ' . / ' to
* head of newname if newname does not contain a ' / ' .
*/
2009-07-09 03:22:44 +04:00
if ( smb_fname_dst - > base_name [ 0 ] & &
smb_fname_dst - > base_name [ 1 ] & &
smb_fname_dst - > base_name [ 0 ] = = ' . ' & &
smb_fname_dst - > base_name [ 1 ] = = ' / ' ) {
strip_two_chars = true ;
2005-12-13 01:07:36 +03:00
}
2012-01-10 16:56:37 +04:00
d - > servicepath = talloc_strdup ( d , servicepath ) ;
d - > base_name = talloc_strdup ( d , smb_fname_dst - > base_name +
2009-07-09 03:22:44 +04:00
( strip_two_chars ? 2 : 0 ) ) ;
2012-01-10 16:56:37 +04:00
d - > stream_name = talloc_strdup ( d , smb_fname_dst - > stream_name ) ;
if ( d - > base_name = = NULL | |
( has_stream & & d - > stream_name = = NULL ) | |
d - > servicepath = = NULL ) {
2005-12-13 01:07:36 +03:00
DEBUG ( 0 , ( " rename_share_filename: talloc failed \n " ) ) ;
return False ;
}
2012-01-10 16:56:37 +04:00
d - > modified = True ;
2005-12-13 21:11:50 +03:00
2012-01-10 16:56:37 +04:00
sp_len = strlen ( d - > servicepath ) ;
bn_len = strlen ( d - > base_name ) ;
sn_len = has_stream ? strlen ( d - > stream_name ) : 0 ;
2005-12-13 21:11:50 +03:00
2009-07-09 03:22:44 +04:00
msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
sn_len + 1 ;
2005-12-13 21:11:50 +03:00
/* Set up the name changed message. */
2012-01-10 16:56:37 +04:00
frm = talloc_array ( d , char , msg_len ) ;
2005-12-13 21:11:50 +03:00
if ( ! frm ) {
return False ;
}
2006-01-13 01:17:54 +03:00
2012-01-10 16:56:37 +04:00
push_file_id_24 ( frm , & d - > id ) ;
2005-12-13 21:11:50 +03:00
2006-06-16 05:35:08 +04:00
DEBUG ( 10 , ( " rename_share_filename: msg_len = %u \n " , ( unsigned int ) msg_len ) ) ;
2005-12-13 21:11:50 +03:00
2011-05-04 01:01:20 +04:00
strlcpy ( & frm [ 24 ] ,
2012-01-10 16:56:37 +04:00
d - > servicepath ? d - > servicepath : " " ,
2011-05-04 01:01:20 +04:00
sp_len + 1 ) ;
strlcpy ( & frm [ 24 + sp_len + 1 ] ,
2012-01-10 16:56:37 +04:00
d - > base_name ? d - > base_name : " " ,
2011-05-04 01:01:20 +04:00
bn_len + 1 ) ;
strlcpy ( & frm [ 24 + sp_len + 1 + bn_len + 1 ] ,
2012-01-10 16:56:37 +04:00
d - > stream_name ? d - > stream_name : " " ,
2011-05-04 01:01:20 +04:00
sn_len + 1 ) ;
2005-12-13 21:11:50 +03:00
/* Send the messages. */
2012-01-10 16:56:37 +04:00
for ( i = 0 ; i < d - > num_share_modes ; i + + ) {
struct share_mode_entry * se = & d - > share_modes [ i ] ;
2005-12-13 21:11:50 +03:00
if ( ! is_valid_share_mode_entry ( se ) ) {
continue ;
}
2011-01-26 01:01:52 +03:00
/* If this is a hardlink to the inode
with a different name , skip this . */
if ( se - > name_hash ! = orig_name_hash ) {
continue ;
}
se - > name_hash = new_name_hash ;
2005-12-13 21:11:50 +03:00
/* But not to ourselves... */
if ( procid_is_me ( & se - > pid ) ) {
continue ;
}
2012-05-07 17:23:10 +04:00
if ( share_mode_stale_pid ( d , i ) ) {
continue ;
}
2009-07-09 03:22:44 +04:00
DEBUG ( 10 , ( " rename_share_filename: sending rename message to "
" pid %s file_id %s sharepath %s base_name %s "
" stream_name %s \n " ,
2007-05-29 13:30:34 +04:00
procid_str_static ( & se - > pid ) ,
2012-01-10 16:56:37 +04:00
file_id_string_tos ( & d - > id ) ,
d - > servicepath , d - > base_name ,
has_stream ? d - > stream_name : " " ) ) ;
2005-12-13 21:11:50 +03:00
2007-05-27 13:29:26 +04:00
messaging_send_buf ( msg_ctx , se - > pid , MSG_SMB_FILE_RENAME ,
( uint8 * ) frm , msg_len ) ;
2005-12-13 21:11:50 +03:00
}
2005-12-13 01:07:36 +03:00
return True ;
}
2008-04-01 13:40:23 +04:00
void get_file_infos ( struct file_id id ,
2011-01-26 00:57:38 +03:00
uint32_t name_hash ,
2008-04-01 13:40:23 +04:00
bool * delete_on_close ,
struct timespec * write_time )
2008-03-12 17:32:47 +03:00
{
struct share_mode_lock * lck ;
2008-04-01 13:40:23 +04:00
if ( delete_on_close ) {
* delete_on_close = false ;
}
if ( write_time ) {
ZERO_STRUCTP ( write_time ) ;
}
2008-03-12 17:32:47 +03:00
2009-07-09 03:22:44 +04:00
if ( ! ( lck = fetch_share_mode_unlocked ( talloc_tos ( ) , id ) ) ) {
2008-04-01 13:40:23 +04:00
return ;
2008-03-12 17:32:47 +03:00
}
2008-04-01 13:40:23 +04:00
if ( delete_on_close ) {
2011-01-26 01:23:19 +03:00
* delete_on_close = is_delete_on_close_set ( lck , name_hash ) ;
2008-03-12 17:32:47 +03:00
}
2008-04-01 13:40:23 +04:00
if ( write_time ) {
struct timespec wt ;
2008-03-12 17:32:47 +03:00
2012-01-10 16:56:37 +04:00
wt = lck - > data - > changed_write_time ;
2008-04-01 13:40:23 +04:00
if ( null_timespec ( wt ) ) {
2012-01-10 16:56:37 +04:00
wt = lck - > data - > old_write_time ;
2008-04-01 13:40:23 +04:00
}
* write_time = wt ;
2007-05-27 13:57:05 +04:00
}
2008-04-01 13:40:23 +04:00
2007-05-27 13:57:05 +04:00
TALLOC_FREE ( lck ) ;
1998-10-23 07:34:50 +04:00
}
2007-10-19 04:40:25 +04:00
bool is_valid_share_mode_entry ( const struct share_mode_entry * e )
1998-10-23 07:34:50 +04:00
{
2005-09-30 21:13:37 +04:00
int num_props = 0 ;
num_props + = ( ( e - > op_type = = NO_OPLOCK ) ? 1 : 0 ) ;
num_props + = ( EXCLUSIVE_OPLOCK_TYPE ( e - > op_type ) ? 1 : 0 ) ;
num_props + = ( LEVEL_II_OPLOCK_TYPE ( e - > op_type ) ? 1 : 0 ) ;
2012-06-01 17:15:07 +04:00
if ( ( num_props > 1 ) & & serverid_exists ( & e - > pid ) ) {
2012-05-14 16:57:34 +04:00
smb_panic ( " Invalid share mode entry " ) ;
}
2005-09-30 21:13:37 +04:00
return ( num_props ! = 0 ) ;
1998-10-23 07:34:50 +04:00
}
2007-10-19 04:40:25 +04:00
bool is_deferred_open_entry ( const struct share_mode_entry * e )
2005-09-30 21:13:37 +04:00
{
return ( e - > op_type = = DEFERRED_OPEN_ENTRY ) ;
}
2001-08-21 05:25:45 +04:00
2012-05-07 14:57:07 +04:00
/*
* In case d - > share_modes [ i ] conflicts with something or otherwise is
* being used , we need to make sure the corresponding process still
* exists . This routine checks it and potentially removes the entry
* from d - > share_modes . Modifies d - > num_share_modes , watch out in
* routines iterating over that array .
*/
bool share_mode_stale_pid ( struct share_mode_data * d , unsigned i )
{
struct share_mode_entry * e ;
if ( i > d - > num_share_modes ) {
DEBUG ( 1 , ( " Asking for index %u, only %u around \n " ,
i , ( unsigned ) d - > num_share_modes ) ) ;
return false ;
}
e = & d - > share_modes [ i ] ;
if ( serverid_exists ( & e - > pid ) ) {
DEBUG ( 10 , ( " PID %s (index %u out of %u) still exists \n " ,
procid_str_static ( & e - > pid ) , i ,
( unsigned ) d - > num_share_modes ) ) ;
return false ;
}
DEBUG ( 10 , ( " PID %s (index %u out of %u) does not exist anymore \n " ,
procid_str_static ( & e - > pid ) , i ,
( unsigned ) d - > num_share_modes ) ) ;
* e = d - > share_modes [ d - > num_share_modes - 1 ] ;
d - > num_share_modes - = 1 ;
2012-05-22 23:27:06 +04:00
if ( d - > num_share_modes = = 0 & &
d - > num_delete_tokens ) {
/*
* We cannot have any delete tokens
* if there are no valid share modes .
*/
TALLOC_FREE ( d - > delete_tokens ) ;
d - > num_delete_tokens = 0 ;
}
2012-05-07 14:57:07 +04:00
d - > modified = true ;
return true ;
}
1999-12-13 16:27:58 +03:00
/*******************************************************************
2005-09-30 21:13:37 +04:00
Fill a share mode entry .
1999-12-13 16:27:58 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-08-21 05:25:45 +04:00
2005-09-30 21:13:37 +04:00
static void fill_share_mode_entry ( struct share_mode_entry * e ,
files_struct * fsp ,
2010-04-13 08:40:28 +04:00
uid_t uid , uint64_t mid , uint16 op_type )
1999-12-13 16:27:58 +03:00
{
2005-09-30 21:13:37 +04:00
ZERO_STRUCTP ( e ) ;
2011-12-15 14:50:43 +04:00
e - > pid = messaging_server_id ( fsp - > conn - > sconn - > msg_ctx ) ;
2005-09-30 21:13:37 +04:00
e - > share_access = fsp - > share_access ;
e - > private_options = fsp - > fh - > private_options ;
e - > access_mask = fsp - > access_mask ;
e - > op_mid = mid ;
e - > op_type = op_type ;
e - > time . tv_sec = fsp - > open_time . tv_sec ;
e - > time . tv_usec = fsp - > open_time . tv_usec ;
2007-05-29 13:30:34 +04:00
e - > id = fsp - > file_id ;
e - > share_file_id = fsp - > fh - > gen_id ;
2006-06-21 06:31:12 +04:00
e - > uid = ( uint32 ) uid ;
2007-02-07 00:05:34 +03:00
e - > flags = fsp - > posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0 ;
2011-01-26 01:01:52 +03:00
e - > name_hash = fsp - > name_hash ;
2001-08-22 04:29:40 +04:00
}
2005-09-30 21:13:37 +04:00
static void fill_deferred_open_entry ( struct share_mode_entry * e ,
const struct timeval request_time ,
2010-07-04 22:14:27 +04:00
struct file_id id ,
struct server_id pid ,
uint64_t mid )
1998-10-23 07:34:50 +04:00
{
2005-09-30 21:13:37 +04:00
ZERO_STRUCTP ( e ) ;
2010-07-04 22:14:27 +04:00
e - > pid = pid ;
2005-09-30 21:13:37 +04:00
e - > op_mid = mid ;
e - > op_type = DEFERRED_OPEN_ENTRY ;
e - > time . tv_sec = request_time . tv_sec ;
e - > time . tv_usec = request_time . tv_usec ;
2007-05-29 13:30:34 +04:00
e - > id = id ;
2006-06-21 06:31:12 +04:00
e - > uid = ( uint32 ) - 1 ;
2007-02-07 00:05:34 +03:00
e - > flags = 0 ;
2005-09-30 21:13:37 +04:00
}
1998-10-23 07:34:50 +04:00
2012-01-10 16:56:37 +04:00
static void add_share_mode_entry ( struct share_mode_data * d ,
2005-09-30 21:13:37 +04:00
const struct share_mode_entry * entry )
{
2012-01-10 16:56:37 +04:00
ADD_TO_ARRAY ( d , struct share_mode_entry , * entry ,
& d - > share_modes , & d - > num_share_modes ) ;
d - > modified = True ;
2005-09-30 21:13:37 +04:00
}
1999-12-13 16:27:58 +03:00
2005-09-30 21:13:37 +04:00
void set_share_mode ( struct share_mode_lock * lck , files_struct * fsp ,
2010-04-13 08:40:28 +04:00
uid_t uid , uint64_t mid , uint16 op_type )
2005-09-30 21:13:37 +04:00
{
struct share_mode_entry entry ;
2006-06-21 06:31:12 +04:00
fill_share_mode_entry ( & entry , fsp , uid , mid , op_type ) ;
2012-01-10 16:56:37 +04:00
add_share_mode_entry ( lck - > data , & entry ) ;
2005-09-30 21:13:37 +04:00
}
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
2010-04-13 08:40:28 +04:00
void add_deferred_open ( struct share_mode_lock * lck , uint64_t mid ,
2005-09-30 21:13:37 +04:00
struct timeval request_time ,
2010-07-04 22:14:27 +04:00
struct server_id pid , struct file_id id )
2005-09-30 21:13:37 +04:00
{
struct share_mode_entry entry ;
2010-07-04 22:14:27 +04:00
fill_deferred_open_entry ( & entry , request_time , id , pid , mid ) ;
2012-01-10 16:56:37 +04:00
add_share_mode_entry ( lck - > data , & entry ) ;
1997-10-20 12:46:00 +04:00
}
1997-05-20 04:32:51 +04:00
2004-06-08 20:14:31 +04:00
/*******************************************************************
2005-09-30 21:13:37 +04:00
Check if two share mode entries are identical , ignoring oplock
2006-05-18 03:15:53 +04:00
and mid info and desired_access . ( Removed paranoia test - it ' s
not automatically a logic error if they are identical . JRA . )
2004-06-08 20:14:31 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool share_modes_identical ( struct share_mode_entry * e1 ,
2005-09-30 21:13:37 +04:00
struct share_mode_entry * e2 )
2004-06-08 20:14:31 +04:00
{
2006-05-18 03:15:53 +04:00
/* We used to check for e1->share_access == e2->share_access here
as well as the other fields but 2 different DOS or FCB opens
sharing the same share mode entry may validly differ in
fsp - > share_access field . */
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
return ( procid_equal ( & e1 - > pid , & e2 - > pid ) & &
2007-05-29 13:30:34 +04:00
file_id_equal ( & e1 - > id , & e2 - > id ) & &
2005-09-30 21:13:37 +04:00
e1 - > share_file_id = = e2 - > share_file_id ) ;
2004-06-08 20:14:31 +04:00
}
2007-10-19 04:40:25 +04:00
static bool deferred_open_identical ( struct share_mode_entry * e1 ,
2005-09-30 21:13:37 +04:00
struct share_mode_entry * e2 )
{
return ( procid_equal ( & e1 - > pid , & e2 - > pid ) & &
( e1 - > op_mid = = e2 - > op_mid ) & &
2007-05-29 13:30:34 +04:00
file_id_equal ( & e1 - > id , & e2 - > id ) ) ;
2005-09-30 21:13:37 +04:00
}
2004-06-08 20:14:31 +04:00
2012-01-10 16:56:37 +04:00
static struct share_mode_entry * find_share_mode_entry ( struct share_mode_data * d ,
2005-09-30 21:13:37 +04:00
struct share_mode_entry * entry )
{
int i ;
2004-06-08 20:14:31 +04:00
2012-01-10 16:56:37 +04:00
for ( i = 0 ; i < d - > num_share_modes ; i + + ) {
struct share_mode_entry * e = & d - > share_modes [ i ] ;
2005-09-30 21:13:37 +04:00
if ( is_valid_share_mode_entry ( entry ) & &
is_valid_share_mode_entry ( e ) & &
share_modes_identical ( e , entry ) ) {
return e ;
}
if ( is_deferred_open_entry ( entry ) & &
is_deferred_open_entry ( e ) & &
deferred_open_identical ( e , entry ) ) {
return e ;
}
}
return NULL ;
}
2004-06-08 20:14:31 +04:00
/*******************************************************************
2005-09-30 21:13:37 +04:00
Del the share mode of a file for this process . Return the number of
entries left .
2004-06-08 20:14:31 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool del_share_mode ( struct share_mode_lock * lck , files_struct * fsp )
2004-06-08 20:14:31 +04:00
{
2005-09-30 21:13:37 +04:00
struct share_mode_entry entry , * e ;
2006-06-21 06:31:12 +04:00
/* Don't care about the pid owner being correct here - just a search. */
fill_share_mode_entry ( & entry , fsp , ( uid_t ) - 1 , 0 , NO_OPLOCK ) ;
2004-06-08 20:14:31 +04:00
2012-01-10 16:56:37 +04:00
e = find_share_mode_entry ( lck - > data , & entry ) ;
2005-09-30 21:13:37 +04:00
if ( e = = NULL ) {
return False ;
2004-06-08 20:14:31 +04:00
}
2012-01-10 16:56:37 +04:00
* e = lck - > data - > share_modes [ lck - > data - > num_share_modes - 1 ] ;
lck - > data - > num_share_modes - = 1 ;
lck - > data - > modified = True ;
2005-09-30 21:13:37 +04:00
return True ;
2004-06-08 20:14:31 +04:00
}
2010-07-04 22:14:27 +04:00
void del_deferred_open_entry ( struct share_mode_lock * lck , uint64_t mid ,
struct server_id pid )
2005-09-30 21:13:37 +04:00
{
struct share_mode_entry entry , * e ;
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
fill_deferred_open_entry ( & entry , timeval_zero ( ) ,
2012-01-10 16:56:37 +04:00
lck - > data - > id , pid , mid ) ;
2004-06-08 20:14:31 +04:00
2012-01-10 16:56:37 +04:00
e = find_share_mode_entry ( lck - > data , & entry ) ;
2005-09-30 21:13:37 +04:00
if ( e = = NULL ) {
return ;
}
2012-01-10 16:56:37 +04:00
* e = lck - > data - > share_modes [ lck - > data - > num_share_modes - 1 ] ;
lck - > data - > num_share_modes - = 1 ;
lck - > data - > modified = True ;
2004-06-08 20:14:31 +04:00
}
/*******************************************************************
2005-09-30 21:13:37 +04:00
Remove an oplock mid and mode entry from a share mode .
2004-06-08 20:14:31 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool remove_share_oplock ( struct share_mode_lock * lck , files_struct * fsp )
2004-06-08 20:14:31 +04:00
{
2005-09-30 21:13:37 +04:00
struct share_mode_entry entry , * e ;
2004-06-08 20:14:31 +04:00
2006-06-21 06:31:12 +04:00
/* Don't care about the pid owner being correct here - just a search. */
fill_share_mode_entry ( & entry , fsp , ( uid_t ) - 1 , 0 , NO_OPLOCK ) ;
2004-06-08 20:14:31 +04:00
2012-01-10 16:56:37 +04:00
e = find_share_mode_entry ( lck - > data , & entry ) ;
2005-09-30 21:13:37 +04:00
if ( e = = NULL ) {
return False ;
}
2004-06-08 20:14:31 +04:00
2011-04-16 03:30:17 +04:00
if ( EXCLUSIVE_OPLOCK_TYPE ( e - > op_type ) ) {
2008-12-18 04:23:13 +03:00
/*
* Going from exclusive or batch ,
* we always go through FAKE_LEVEL_II
* first .
*/
2011-04-16 03:30:17 +04:00
if ( ! EXCLUSIVE_OPLOCK_TYPE ( fsp - > oplock_type ) ) {
smb_panic ( " remove_share_oplock: logic error " ) ;
}
2008-12-18 04:23:13 +03:00
e - > op_type = FAKE_LEVEL_II_OPLOCK ;
} else {
e - > op_type = NO_OPLOCK ;
}
2012-01-10 16:56:37 +04:00
lck - > data - > modified = True ;
2005-09-30 21:13:37 +04:00
return True ;
}
2004-06-08 20:14:31 +04:00
2005-09-30 21:13:37 +04:00
/*******************************************************************
Downgrade a oplock type from exclusive to level II .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-06-08 20:14:31 +04:00
2007-10-19 04:40:25 +04:00
bool downgrade_share_oplock ( struct share_mode_lock * lck , files_struct * fsp )
2005-09-30 21:13:37 +04:00
{
struct share_mode_entry entry , * e ;
2004-06-08 20:14:31 +04:00
2006-06-21 06:31:12 +04:00
/* Don't care about the pid owner being correct here - just a search. */
fill_share_mode_entry ( & entry , fsp , ( uid_t ) - 1 , 0 , NO_OPLOCK ) ;
2004-06-08 20:14:31 +04:00
2012-01-10 16:56:37 +04:00
e = find_share_mode_entry ( lck - > data , & entry ) ;
2005-09-30 21:13:37 +04:00
if ( e = = NULL ) {
return False ;
2004-06-08 20:14:31 +04:00
}
2005-09-30 21:13:37 +04:00
e - > op_type = LEVEL_II_OPLOCK ;
2012-01-10 16:56:37 +04:00
lck - > data - > modified = True ;
2005-09-30 21:13:37 +04:00
return True ;
2004-06-08 20:14:31 +04:00
}
2006-02-02 23:44:50 +03:00
/*************************************************************************
2011-02-11 10:49:15 +03:00
Return a talloced copy of a struct security_unix_token . NULL on fail .
2006-02-02 23:44:50 +03:00
( Should this be in locking . c . . . . ? ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-02-11 10:49:15 +03:00
static struct security_unix_token * copy_unix_token ( TALLOC_CTX * ctx , const struct security_unix_token * tok )
2006-02-02 23:44:50 +03:00
{
2011-02-11 10:49:15 +03:00
struct security_unix_token * cpy ;
2006-02-02 23:44:50 +03:00
2011-06-07 05:38:41 +04:00
cpy = talloc ( ctx , struct security_unix_token ) ;
2006-02-02 23:44:50 +03:00
if ( ! cpy ) {
return NULL ;
}
cpy - > uid = tok - > uid ;
cpy - > gid = tok - > gid ;
cpy - > ngroups = tok - > ngroups ;
if ( tok - > ngroups ) {
/* Make this a talloc child of cpy. */
2011-05-28 12:38:52 +04:00
cpy - > groups = ( gid_t * ) talloc_memdup (
cpy , tok - > groups , tok - > ngroups * sizeof ( gid_t ) ) ;
2006-02-02 23:44:50 +03:00
if ( ! cpy - > groups ) {
2011-05-28 12:39:25 +04:00
TALLOC_FREE ( cpy ) ;
2006-02-02 23:44:50 +03:00
return NULL ;
}
}
return cpy ;
}
/****************************************************************************
2011-01-26 01:23:19 +03:00
Adds a delete on close token .
2006-02-02 23:44:50 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-01-10 16:56:37 +04:00
static bool add_delete_on_close_token ( struct share_mode_data * d ,
2011-01-26 01:23:19 +03:00
uint32_t name_hash ,
2012-04-05 01:54:02 +04:00
const struct security_token * nt_tok ,
2011-02-11 10:49:15 +03:00
const struct security_unix_token * tok )
2006-02-02 23:44:50 +03:00
{
2011-11-21 20:12:00 +04:00
struct delete_token * tmp , * dtl ;
2006-02-02 23:44:50 +03:00
2012-01-10 16:56:37 +04:00
tmp = talloc_realloc ( d , d - > delete_tokens , struct delete_token ,
d - > num_delete_tokens + 1 ) ;
2011-11-21 20:12:00 +04:00
if ( tmp = = NULL ) {
2011-01-26 01:23:19 +03:00
return false ;
}
2012-01-10 16:56:37 +04:00
d - > delete_tokens = tmp ;
dtl = & d - > delete_tokens [ d - > num_delete_tokens ] ;
2011-01-26 01:23:19 +03:00
dtl - > name_hash = name_hash ;
2012-04-05 01:54:02 +04:00
dtl - > delete_nt_token = dup_nt_token ( d - > delete_tokens , nt_tok ) ;
if ( dtl - > delete_nt_token = = NULL ) {
return false ;
}
2012-01-10 16:56:37 +04:00
dtl - > delete_token = copy_unix_token ( d - > delete_tokens , tok ) ;
2011-01-26 01:23:19 +03:00
if ( dtl - > delete_token = = NULL ) {
return false ;
}
2012-01-10 16:56:37 +04:00
d - > num_delete_tokens + = 1 ;
d - > modified = true ;
2011-01-26 01:23:19 +03:00
return true ;
2006-02-02 23:44:50 +03:00
}
2005-09-30 21:13:37 +04:00
/****************************************************************************
Sets the delete on close flag over all share modes on this file .
Modify the share mode entry for all files open
on this device and inode to tell other smbds we have
changed the delete on close flag . This will be noticed
in the close code , the last closer will delete the file
if flag is set .
2011-02-11 10:49:15 +03:00
This makes a copy of any struct security_unix_token into the
2007-01-19 00:51:52 +03:00
lck entry . This function is used when the lock is already granted .
2005-09-30 21:13:37 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-06-08 20:14:31 +04:00
2011-01-26 01:23:19 +03:00
void set_delete_on_close_lck ( files_struct * fsp ,
struct share_mode_lock * lck ,
bool delete_on_close ,
2012-04-05 01:54:02 +04:00
const struct security_token * nt_tok ,
2011-02-11 10:49:15 +03:00
const struct security_unix_token * tok )
2007-01-19 00:51:52 +03:00
{
2012-01-10 16:56:37 +04:00
struct share_mode_data * d = lck - > data ;
2011-11-21 20:12:00 +04:00
int i ;
2011-01-26 01:23:19 +03:00
bool ret ;
if ( delete_on_close ) {
2012-04-05 01:54:02 +04:00
SMB_ASSERT ( nt_tok ! = NULL ) ;
2011-01-26 01:23:19 +03:00
SMB_ASSERT ( tok ! = NULL ) ;
} else {
2012-04-05 01:54:02 +04:00
SMB_ASSERT ( nt_tok = = NULL ) ;
2011-01-26 01:23:19 +03:00
SMB_ASSERT ( tok = = NULL ) ;
}
2012-01-10 16:56:37 +04:00
for ( i = 0 ; i < d - > num_delete_tokens ; i + + ) {
struct delete_token * dt = & d - > delete_tokens [ i ] ;
2011-11-21 20:12:00 +04:00
if ( dt - > name_hash = = fsp - > name_hash ) {
2012-01-10 16:56:37 +04:00
d - > modified = true ;
2011-01-26 01:23:19 +03:00
if ( delete_on_close = = false ) {
/* Delete this entry. */
2012-04-05 01:54:02 +04:00
TALLOC_FREE ( dt - > delete_nt_token ) ;
2011-11-21 20:12:00 +04:00
TALLOC_FREE ( dt - > delete_token ) ;
2012-01-10 16:56:37 +04:00
* dt = d - > delete_tokens [
d - > num_delete_tokens - 1 ] ;
d - > num_delete_tokens - = 1 ;
2012-04-07 02:35:55 +04:00
} else {
/* Replace this token with the
given tok . */
TALLOC_FREE ( dt - > delete_nt_token ) ;
dt - > delete_nt_token = dup_nt_token ( dt , nt_tok ) ;
SMB_ASSERT ( dt - > delete_nt_token ! = NULL ) ;
TALLOC_FREE ( dt - > delete_token ) ;
dt - > delete_token = copy_unix_token ( dt , tok ) ;
SMB_ASSERT ( dt - > delete_token ! = NULL ) ;
2011-01-26 01:23:19 +03:00
}
2012-04-07 02:35:55 +04:00
return ;
2007-01-19 00:51:52 +03:00
}
}
2011-01-26 01:23:19 +03:00
if ( ! delete_on_close ) {
/* Nothing to delete - not found. */
return ;
}
2012-04-05 01:54:02 +04:00
ret = add_delete_on_close_token ( lck - > data , fsp - > name_hash , nt_tok , tok ) ;
2011-01-26 01:23:19 +03:00
SMB_ASSERT ( ret ) ;
2007-01-19 00:51:52 +03:00
}
2012-04-05 01:54:02 +04:00
bool set_delete_on_close ( files_struct * fsp , bool delete_on_close ,
const struct security_token * nt_tok ,
const struct security_unix_token * tok )
2004-06-08 20:14:31 +04:00
{
2005-09-30 21:13:37 +04:00
struct share_mode_lock * lck ;
2011-05-28 12:24:20 +04:00
2005-09-30 21:13:37 +04:00
DEBUG ( 10 , ( " set_delete_on_close: %s delete on close flag for "
" fnum = %d, file %s \n " ,
delete_on_close ? " Adding " : " Removing " , fsp - > fnum ,
2009-07-11 01:50:37 +04:00
fsp_str_dbg ( fsp ) ) ) ;
2004-06-08 20:14:31 +04:00
2012-02-19 17:23:56 +04:00
lck = get_existing_share_mode_lock ( talloc_tos ( ) , fsp - > file_id ) ;
2005-09-30 21:13:37 +04:00
if ( lck = = NULL ) {
2004-06-08 20:14:31 +04:00
return False ;
}
2006-02-02 23:44:50 +03:00
2012-04-05 01:54:02 +04:00
if ( delete_on_close ) {
set_delete_on_close_lck ( fsp , lck , true ,
nt_tok ,
tok ) ;
} else {
set_delete_on_close_lck ( fsp , lck , false ,
NULL ,
NULL ) ;
}
2006-02-01 07:14:07 +03:00
2007-01-20 00:46:12 +03:00
if ( fsp - > is_directory ) {
2009-07-11 01:50:37 +04:00
SMB_ASSERT ( ! is_ntfs_stream_smb_fname ( fsp - > fsp_name ) ) ;
2010-07-07 00:43:14 +04:00
send_stat_cache_delete_message ( fsp - > conn - > sconn - > msg_ctx ,
fsp - > fsp_name - > base_name ) ;
2007-01-20 00:46:12 +03:00
}
2006-02-20 20:59:58 +03:00
TALLOC_FREE ( lck ) ;
2010-01-13 03:04:44 +03:00
fsp - > delete_on_close = delete_on_close ;
2005-09-30 21:13:37 +04:00
return True ;
}
2001-08-21 05:25:45 +04:00
2012-04-05 01:54:02 +04:00
/****************************************************************************
Return the NT token and UNIX token if there ' s a match . Return true if
found , false if not .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool get_delete_on_close_token ( struct share_mode_lock * lck ,
uint32_t name_hash ,
const struct security_token * * pp_nt_tok ,
const struct security_unix_token * * pp_tok )
2011-01-26 01:23:19 +03:00
{
2011-11-21 20:12:00 +04:00
int i ;
2011-01-26 01:23:19 +03:00
2011-01-26 01:59:44 +03:00
DEBUG ( 10 , ( " get_delete_on_close_token: name_hash = 0x%x \n " ,
2011-01-26 01:23:19 +03:00
( unsigned int ) name_hash ) ) ;
2012-01-10 16:56:37 +04:00
for ( i = 0 ; i < lck - > data - > num_delete_tokens ; i + + ) {
struct delete_token * dt = & lck - > data - > delete_tokens [ i ] ;
2011-01-26 01:59:44 +03:00
DEBUG ( 10 , ( " get_delete_on_close_token: dtl->name_hash = 0x%x \n " ,
2011-11-21 20:12:00 +04:00
( unsigned int ) dt - > name_hash ) ) ;
if ( dt - > name_hash = = name_hash ) {
2012-04-05 01:54:02 +04:00
if ( pp_nt_tok ) {
* pp_nt_tok = dt - > delete_nt_token ;
}
if ( pp_tok ) {
* pp_tok = dt - > delete_token ;
}
return true ;
2011-01-26 01:23:19 +03:00
}
}
2012-04-05 01:54:02 +04:00
return false ;
2011-01-26 01:23:19 +03:00
}
bool is_delete_on_close_set ( struct share_mode_lock * lck , uint32_t name_hash )
{
2012-04-05 01:54:02 +04:00
return get_delete_on_close_token ( lck , name_hash , NULL , NULL ) ;
2011-01-26 01:23:19 +03:00
}
2009-07-08 04:27:50 +04:00
bool set_sticky_write_time ( struct file_id fileid , struct timespec write_time )
2008-03-12 17:32:47 +03:00
{
struct share_mode_lock * lck ;
2008-09-06 06:00:48 +04:00
DEBUG ( 5 , ( " set_sticky_write_time: %s id=%s \n " ,
2009-11-03 12:59:18 +03:00
timestring ( talloc_tos ( ) ,
2008-03-12 17:32:47 +03:00
convert_timespec_to_time_t ( write_time ) ) ,
2008-09-06 06:00:48 +04:00
file_id_string_tos ( & fileid ) ) ) ;
2008-03-12 17:32:47 +03:00
2012-02-19 17:23:56 +04:00
lck = get_existing_share_mode_lock ( talloc_tos ( ) , fileid ) ;
2008-03-12 17:32:47 +03:00
if ( lck = = NULL ) {
return False ;
}
2012-01-10 16:56:37 +04:00
if ( timespec_compare ( & lck - > data - > changed_write_time , & write_time ) ! = 0 ) {
lck - > data - > modified = True ;
lck - > data - > changed_write_time = write_time ;
2008-03-12 17:32:47 +03:00
}
TALLOC_FREE ( lck ) ;
return True ;
}
2008-09-06 06:00:48 +04:00
bool set_write_time ( struct file_id fileid , struct timespec write_time )
{
struct share_mode_lock * lck ;
2008-09-06 07:37:00 +04:00
DEBUG ( 5 , ( " set_write_time: %s id=%s \n " ,
2009-11-03 12:59:18 +03:00
timestring ( talloc_tos ( ) ,
2008-09-06 06:00:48 +04:00
convert_timespec_to_time_t ( write_time ) ) ,
file_id_string_tos ( & fileid ) ) ) ;
2012-02-19 17:23:56 +04:00
lck = get_existing_share_mode_lock ( talloc_tos ( ) , fileid ) ;
2008-09-06 06:00:48 +04:00
if ( lck = = NULL ) {
return False ;
}
2012-01-10 16:56:37 +04:00
if ( timespec_compare ( & lck - > data - > old_write_time , & write_time ) ! = 0 ) {
lck - > data - > modified = True ;
lck - > data - > old_write_time = write_time ;
2008-09-06 06:00:48 +04:00
}
TALLOC_FREE ( lck ) ;
return True ;
}