From 276966bfbd5ea7c216916272e78815f6243b6d0f Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 10 Aug 2016 15:46:29 -0700 Subject: [PATCH 1/2] smbd: oplock: Factor out internals of remove_oplock() into new remove_oplock_under_lock(). Allows this to be called elsewhere. BUG: https://bugzilla.samba.org/show_bug.cgi?id=12139 Signed-off-by: Jeremy Allison --- source3/smbd/oplock.c | 44 ++++++++++++++++++++++++++++---------------- source3/smbd/proto.h | 1 + 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/source3/smbd/oplock.c b/source3/smbd/oplock.c index 9996b8b..3bdae9a 100644 --- a/source3/smbd/oplock.c +++ b/source3/smbd/oplock.c @@ -224,29 +224,15 @@ bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck) /**************************************************************************** Remove a file oplock. Copes with level II and exclusive. - Locks then unlocks the share mode lock. Client can decide to go directly - to none even if a "break-to-level II" was sent. ****************************************************************************/ -bool remove_oplock(files_struct *fsp) +bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck) { bool ret; - struct share_mode_lock *lck; - - DEBUG(10, ("remove_oplock called for %s\n", - fsp_str_dbg(fsp))); - - /* Remove the oplock flag from the sharemode. */ - lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); - if (lck == NULL) { - DEBUG(0,("remove_oplock: failed to lock share entry for " - "file %s\n", fsp_str_dbg(fsp))); - return False; - } ret = remove_share_oplock(lck, fsp); if (!ret) { - DEBUG(0,("remove_oplock: failed to remove share oplock for " + DEBUG(0,("failed to remove share oplock for " "file %s, %s, %s\n", fsp_str_dbg(fsp), fsp_fnum_dbg(fsp), file_id_string_tos(&fsp->file_id))); @@ -260,6 +246,32 @@ bool remove_oplock(files_struct *fsp) __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp), file_id_string_tos(&fsp->file_id))); } + return ret; +} + +/**************************************************************************** + Remove a file oplock. Copes with level II and exclusive. + Locks then unlocks the share mode lock. Client can decide to go directly + to none even if a "break-to-level II" was sent. +****************************************************************************/ + +bool remove_oplock(files_struct *fsp) +{ + bool ret; + struct share_mode_lock *lck; + + DEBUG(10, ("remove_oplock called for %s\n", + fsp_str_dbg(fsp))); + + /* Remove the oplock flag from the sharemode. */ + lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id); + if (lck == NULL) { + DEBUG(0,("remove_oplock: failed to lock share entry for " + "file %s\n", fsp_str_dbg(fsp))); + return False; + } + + ret = remove_oplock_under_lock(fsp, lck); TALLOC_FREE(lck); return ret; diff --git a/source3/smbd/proto.h b/source3/smbd/proto.h index abfb543..9100890 100644 --- a/source3/smbd/proto.h +++ b/source3/smbd/proto.h @@ -674,6 +674,7 @@ bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck); void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp); NTSTATUS set_file_oplock(files_struct *fsp); +bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck); bool remove_oplock(files_struct *fsp); bool downgrade_oplock(files_struct *fsp); bool fsp_lease_update(struct share_mode_lock *lck, -- 2.8.0.rc3.226.g39d4020 From 98beaae521544dc2aaf7b526acaedda48d7f41fd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 10 Aug 2016 15:47:17 -0700 Subject: [PATCH 2/2] s3: oplock: Fix race condition when closing an oplocked file. We must send the 'oplock released' message whilst the lock is held in the close path. Otherwise the messaged smbd can race with the share mode delete. BUG: https://bugzilla.samba.org/show_bug.cgi?id=12139 Signed-off-by: Jeremy Allison --- source3/smbd/close.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source3/smbd/close.c b/source3/smbd/close.c index 1cb5460..12e546f 100644 --- a/source3/smbd/close.c +++ b/source3/smbd/close.c @@ -269,6 +269,11 @@ static NTSTATUS close_remove_share_mode(files_struct *fsp, return NT_STATUS_INVALID_PARAMETER; } + /* Remove the oplock before potentially deleting the file. */ + if(fsp->oplock_type) { + remove_oplock(fsp); + } + if (fsp->write_time_forced) { DEBUG(10,("close_remove_share_mode: write time forced " "for file %s\n", @@ -733,11 +738,6 @@ static NTSTATUS close_normal_file(struct smb_request *req, files_struct *fsp, return NT_STATUS_OK; } - /* Remove the oplock before potentially deleting the file. */ - if(fsp->oplock_type) { - remove_oplock(fsp); - } - /* 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. */ -- 2.8.0.rc3.226.g39d4020