Revert "smbd: explain that/why we use the raw tevent_context for do_break_to_none()"
[samba.git] / source3 / smbd / oplock.c
index 295ae1ff72df2a68c6ac08a813f79ec958d570b6..3feddaa90b871be3a608a175fad8392102550e3e 100644 (file)
@@ -25,7 +25,6 @@
 #include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "messages.h"
-#include "../librpc/gen_ndr/open_files.h"
 #include "../librpc/gen_ndr/ndr_open_files.h"
 
 /*
@@ -59,13 +58,10 @@ NTSTATUS set_file_oplock(files_struct *fsp)
        bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
                        (koplocks != NULL);
 
-       if (fsp->oplock_type == LEVEL_II_OPLOCK) {
-               if (use_kernel &&
-                   !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
-                       DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
-                                  "don't support them\n"));
-                       return NT_STATUS_NOT_SUPPORTED;
-               }
+       if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
+               DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
+                          "don't support them\n"));
+               return NT_STATUS_NOT_SUPPORTED;
        }
 
        if ((fsp->oplock_type != NO_OPLOCK) &&
@@ -91,6 +87,32 @@ NTSTATUS set_file_oplock(files_struct *fsp)
        return NT_STATUS_OK;
 }
 
+static void release_fsp_kernel_oplock(files_struct *fsp)
+{
+       struct smbd_server_connection *sconn = fsp->conn->sconn;
+       struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
+       bool use_kernel;
+
+       if (koplocks == NULL) {
+               return;
+       }
+       use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
+       if (!use_kernel) {
+               return;
+       }
+       if (fsp->oplock_type == NO_OPLOCK) {
+               return;
+       }
+       if (fsp->oplock_type == LEASE_OPLOCK) {
+               /*
+                * For leases we don't touch kernel oplocks at all
+                */
+               return;
+       }
+
+       koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
+}
+
 /****************************************************************************
  Attempt to release an oplock on a file. Decrements oplock count.
 ****************************************************************************/
@@ -98,14 +120,8 @@ NTSTATUS set_file_oplock(files_struct *fsp)
 static void release_file_oplock(files_struct *fsp)
 {
        struct smbd_server_connection *sconn = fsp->conn->sconn;
-       struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
-       bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
-                       (koplocks != NULL);
 
-       if ((fsp->oplock_type != NO_OPLOCK) &&
-           use_kernel) {
-               koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
-       }
+       release_fsp_kernel_oplock(fsp);
 
        if (fsp->oplock_type == LEVEL_II_OPLOCK) {
                sconn->oplocks.level_II_open--;
@@ -351,6 +367,12 @@ static void lease_timeout_handler(struct tevent_context *ctx,
                return;
        }
 
+       /*
+        * Paranoia check: There can only be one fsp_lease per lease
+        * key
+        */
+       SMB_ASSERT(fsp->lease == lease);
+
        lck = get_existing_share_mode_lock(
                        talloc_tos(), fsp->file_id);
        if (lck == NULL) {
@@ -403,11 +425,7 @@ bool fsp_lease_update(struct share_mode_lock *lck,
        struct share_mode_lease *l = NULL;
 
        idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
-       if (idx != -1) {
-               l = &d->leases[idx];
-       }
-
-       if (l == NULL) {
+       if (idx == -1) {
                DEBUG(1, ("%s: Could not find lease entry\n", __func__));
                TALLOC_FREE(lease->timeout);
                lease->lease.lease_state = SMB2_LEASE_NONE;
@@ -416,6 +434,8 @@ bool fsp_lease_update(struct share_mode_lock *lck,
                return false;
        }
 
+       l = &d->leases[idx];
+
        DEBUG(10,("%s: refresh lease state\n", __func__));
 
        /* Ensure we're in sync with current lease state. */
@@ -525,8 +545,10 @@ NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
 {
        struct smbd_server_connection *sconn = xconn->client->sconn;
        struct share_mode_lock *lck;
+       struct share_mode_data *d = NULL;
        struct share_mode_lease *l = NULL;
        const struct file_id id = ids[0];
+       int idx;
        uint32_t i;
        NTSTATUS status;
 
@@ -537,14 +559,65 @@ NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
        if (lck == NULL) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
-       status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
+       d = lck->data;
 
-       DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
-                  file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
+       idx = find_share_mode_lease(
+               d, &sconn->client->connections->smb2.client.guid, key);
+       if (idx == -1) {
+               DEBUG(10, ("lease not found\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+       l = &d->leases[idx];
+
+       if (!l->breaking) {
+               DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
+                           "but we're not in breaking state\n",
+                           l->current_state, lease_state);
+               TALLOC_FREE(lck);
+               return NT_STATUS_UNSUCCESSFUL;
+       }
 
-       if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
+       /*
+        * Can't upgrade anything: l->breaking_to_requested (and l->current_state)
+        * must be a strict bitwise superset of new_lease_state
+        */
+       if ((lease_state & l->breaking_to_requested) != lease_state) {
+               DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
+                           "- expected %"PRIu32"\n",
+                           l->current_state, lease_state,
+                           l->breaking_to_requested);
+               TALLOC_FREE(lck);
+               return NT_STATUS_REQUEST_NOT_ACCEPTED;
+       }
+
+       if (l->current_state != lease_state) {
+               l->current_state = lease_state;
+               d->modified = true;
+       }
+
+       status = NT_STATUS_OK;
+
+       d->modified = true;
+
+       if ((lease_state & ~l->breaking_to_required) != 0) {
                struct downgrade_lease_additional_state *state;
 
+               DBG_INFO("lease state %"PRIu32" not fully broken from "
+                        "%"PRIu32" to %"PRIu32"\n",
+                        lease_state,
+                        l->current_state,
+                        l->breaking_to_required);
+
+               l->breaking_to_requested = l->breaking_to_required;
+
+               if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
+                       /*
+                        * Here we break in steps, as windows does
+                        * see the breaking3 and v2_breaking3 tests.
+                        */
+                       l->breaking_to_requested |= SMB2_LEASE_READ;
+               }
+
                state = talloc_zero(xconn,
                                    struct downgrade_lease_additional_state);
                if (state == NULL) {
@@ -560,9 +633,6 @@ NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
                }
 
                state->xconn = xconn;
-               if (l->current_state & (~SMB2_LEASE_READ)) {
-                       state->break_flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
-               }
                state->lease_key = l->lease_key;
                state->break_from = l->current_state;
                state->break_to = l->breaking_to_requested;
@@ -570,7 +640,10 @@ NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
                        state->new_epoch = l->epoch;
                }
 
-               if (state->break_flags == 0) {
+               if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
+                       state->break_flags =
+                               SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
+               } else {
                        /*
                         * This is an async break without
                         * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
@@ -590,8 +663,25 @@ NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
                                          xconn->client->raw_ev_ctx,
                                          downgrade_lease_additional_trigger,
                                          state);
+
+               status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
+       } else {
+               DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
+                         "expected %"PRIu32"\n",
+                         l->current_state,
+                         lease_state,
+                         l->breaking_to_requested);
+
+               l->breaking_to_requested = 0;
+               l->breaking_to_required = 0;
+               l->breaking = false;
+
+               d->modified = true;
        }
 
+       DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
+                  file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
+
        {
                struct downgrade_lease_fsps_state state = {
                        .id = id, .lck = lck, .key = key,
@@ -747,20 +837,6 @@ static void oplock_timeout_handler(struct tevent_context *ctx,
 
 static void add_oplock_timeout_handler(files_struct *fsp)
 {
-       struct smbd_server_connection *sconn = fsp->conn->sconn;
-       struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
-       bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
-                       (koplocks != NULL);
-
-       /*
-        * If kernel oplocks already notifies smbds when an oplock break times
-        * out, just return.
-        */
-       if (use_kernel &&
-           (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
-               return;
-       }
-
        if (fsp->oplock_timeout != NULL) {
                DEBUG(0, ("Logic problem -- have an oplock event hanging "
                          "around\n"));
@@ -877,7 +953,7 @@ static void process_oplock_break_message(struct messaging_context *msg_ctx,
 
        use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
                        (koplocks != NULL);
-       if (use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
+       if (use_kernel) {
                DEBUG(10, ("Kernel oplocks don't allow level2\n"));
                break_to &= ~SMB2_LEASE_READ;
        }
@@ -1069,6 +1145,23 @@ static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
        add_oplock_timeout_handler(fsp);
 }
 
+static bool file_has_read_oplocks(struct files_struct *fsp)
+{
+       struct byte_range_lock *brl;
+       uint32_t num_read_oplocks = 0;
+
+       brl = brl_get_locks_readonly(fsp);
+       if (brl == NULL) {
+               return false;
+       }
+
+       num_read_oplocks = brl_num_read_oplocks(brl);
+
+       DBG_DEBUG("num_read_oplocks = %"PRIu32"\n", num_read_oplocks);
+
+       return (num_read_oplocks != 0);
+}
+
 struct break_to_none_state {
        struct smbd_server_connection *sconn;
        struct file_id id;
@@ -1091,8 +1184,7 @@ static void contend_level2_oplocks_begin_default(files_struct *fsp,
        struct smbd_server_connection *sconn = fsp->conn->sconn;
        struct tevent_immediate *im;
        struct break_to_none_state *state;
-       struct byte_range_lock *brl;
-       uint32_t num_read_oplocks = 0;
+       bool has_read_oplocks;
 
        /*
         * If this file is level II oplocked then we need
@@ -1109,14 +1201,8 @@ static void contend_level2_oplocks_begin_default(files_struct *fsp,
                return;
        }
 
-       brl = brl_get_locks_readonly(fsp);
-       if (brl != NULL) {
-               num_read_oplocks = brl_num_read_oplocks(brl);
-       }
-
-       DEBUG(10, ("num_read_oplocks = %"PRIu32"\n", num_read_oplocks));
-
-       if (num_read_oplocks == 0) {
+       has_read_oplocks = file_has_read_oplocks(fsp);
+       if (!has_read_oplocks) {
                DEBUG(10, ("No read oplocks around\n"));
                return;
        }
@@ -1150,15 +1236,7 @@ static void contend_level2_oplocks_begin_default(files_struct *fsp,
                TALLOC_FREE(state);
                return;
        }
-
-       /*
-        * do_break_to_none() only operates on the
-        * locking.tdb and sends network packets to
-        * the client. That doesn't require any
-        * impersonation, so we just use the
-        * raw tevent context here.
-        */
-       tevent_schedule_immediate(im, sconn->raw_ev_ctx, do_break_to_none, state);
+       tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
 }
 
 static void send_break_to_none(struct messaging_context *msg_ctx,
@@ -1185,11 +1263,6 @@ static void do_break_to_none(struct tevent_context *ctx,
        struct share_mode_lock *lck;
        struct share_mode_data *d;
 
-       /*
-        * Note this function doesn't run under any specific impersonation and
-        * is not expected to call any SMB_VFS operation!
-        */
-
        lck = get_existing_share_mode_lock(talloc_tos(), state->id);
        if (lck == NULL) {
                DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
@@ -1298,31 +1371,13 @@ done:
 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
                                  enum level2_contention_type type)
 {
-       struct smbd_server_connection *sconn = fsp->conn->sconn;
-       struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
-       bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
-                       (koplocks != NULL);
-
-       if (use_kernel && koplocks->ops->contend_level2_oplocks_begin) {
-               koplocks->ops->contend_level2_oplocks_begin(fsp, type);
-               return;
-       }
-
        contend_level2_oplocks_begin_default(fsp, type);
 }
 
 void smbd_contend_level2_oplocks_end(files_struct *fsp,
                                enum level2_contention_type type)
 {
-       struct smbd_server_connection *sconn = fsp->conn->sconn;
-       struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
-       bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
-                       (koplocks != NULL);
-
-       /* Only kernel oplocks implement this so far */
-       if (use_kernel && koplocks->ops->contend_level2_oplocks_end) {
-               koplocks->ops->contend_level2_oplocks_end(fsp, type);
-       }
+       return;
 }
 
 /****************************************************************************
@@ -1401,7 +1456,7 @@ void init_kernel_oplocks(struct smbd_server_connection *sconn)
 
        /* only initialize once */
        if (koplocks == NULL) {
-#if HAVE_KERNEL_OPLOCKS_LINUX
+#ifdef HAVE_KERNEL_OPLOCKS_LINUX
                koplocks = linux_init_kernel_oplocks(sconn);
 #endif
                sconn->oplocks.kernel_ops = koplocks;