#include "smbd/smbd.h"
#include "smbd/globals.h"
#include "messages.h"
+#include "locking/leases_db.h"
#include "../librpc/gen_ndr/ndr_open_files.h"
/*
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.
****************************************************************************/
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--;
const struct share_mode_entry *e)
{
if (e->op_type == LEASE_OPLOCK) {
- return d->leases[e->lease_idx].current_state;
+ NTSTATUS status;
+ uint32_t current_state;
+
+ status = leases_db_get(
+ &e->client_guid,
+ &e->lease_key,
+ &d->id,
+ ¤t_state,
+ NULL, /* breaking */
+ NULL, /* breaking_to_requested */
+ NULL, /* breaking_to_required */
+ NULL, /* lease_version */
+ NULL); /* epoch */
+ SMB_ASSERT(NT_STATUS_IS_OK(status));
+ return current_state;
}
return map_oplock_to_lease_type(e->op_type);
}
struct share_mode_lock *lck;
uint16_t old_epoch = lease->lease.lease_epoch;
- /*
- * This function runs without any specific impersonation
- * and must not call any SMB_VFS operations!
- */
-
fsp = file_find_one_fsp_from_lease_key(lease->sconn,
&lease->lease.lease_key);
if (fsp == NULL) {
const struct GUID *client_guid,
struct fsp_lease *lease)
{
- struct share_mode_data *d = lck->data;
- int idx;
- struct share_mode_lease *l = NULL;
+ uint32_t current_state;
+ bool breaking;
+ uint16_t lease_version, epoch;
+ NTSTATUS status;
- idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
- if (idx == -1) {
- DEBUG(1, ("%s: Could not find lease entry\n", __func__));
+ status = leases_db_get(client_guid,
+ &lease->lease.lease_key,
+ &lck->data->id,
+ ¤t_state,
+ &breaking,
+ NULL, /* breaking_to_requested */
+ NULL, /* breaking_to_required */
+ &lease_version,
+ &epoch);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_WARNING("Could not find lease entry: %s\n",
+ nt_errstr(status));
TALLOC_FREE(lease->timeout);
lease->lease.lease_state = SMB2_LEASE_NONE;
lease->lease.lease_epoch += 1;
return false;
}
- l = &d->leases[idx];
-
DEBUG(10,("%s: refresh lease state\n", __func__));
/* Ensure we're in sync with current lease state. */
- if (lease->lease.lease_epoch != l->epoch) {
+ if (lease->lease.lease_epoch != epoch) {
DEBUG(10,("%s: cancel outdated timeout\n", __func__));
TALLOC_FREE(lease->timeout);
}
- lease->lease.lease_epoch = l->epoch;
- lease->lease.lease_state = l->current_state;
+ lease->lease.lease_epoch = epoch;
+ lease->lease.lease_state = current_state;
- if (l->breaking) {
+ if (breaking) {
lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
if (lease->timeout == NULL) {
DEBUG(10,("%s: setup timeout handler\n", __func__));
- /*
- * lease_timeout_handler() only accesses locking.tdb
- * so we don't use any impersonation and use
- * the raw tevent context.
- */
- lease->timeout = tevent_add_timer(lease->sconn->raw_ev_ctx,
+ lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
lease, t,
lease_timeout_handler,
lease);
return NULL;
}
-static NTSTATUS downgrade_share_lease(struct smbd_server_connection *sconn,
- struct share_mode_lock *lck,
- const struct smb2_lease_key *key,
- uint32_t new_lease_state,
- struct share_mode_lease **_l)
+NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
+ uint32_t num_file_ids,
+ const struct file_id *ids,
+ const struct smb2_lease_key *key,
+ uint32_t lease_state)
{
- struct share_mode_data *d = lck->data;
- struct share_mode_lease *l;
+ struct smbd_server_connection *sconn = xconn->client->sconn;
+ const struct GUID *client_guid = NULL;
+ struct share_mode_lock *lck;
+ const struct file_id id = ids[0];
+ uint32_t current_state, breaking_to_requested, breaking_to_required;
+ bool breaking;
+ uint16_t lease_version, epoch;
+ NTSTATUS status;
uint32_t i;
- *_l = NULL;
+ DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
+ file_id_string_tos(&id), (unsigned)lease_state));
- for (i=0; i<d->num_leases; i++) {
- if (smb2_lease_equal(&sconn->client->connections->smb2.client.guid,
- key,
- &d->leases[i].client_guid,
- &d->leases[i].lease_key)) {
- break;
- }
- }
- if (i == d->num_leases) {
- DEBUG(10, ("lease not found\n"));
- return NT_STATUS_INVALID_PARAMETER;
+ lck = get_existing_share_mode_lock(talloc_tos(), id);
+ if (lck == NULL) {
+ return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
- l = &d->leases[i];
+ client_guid = &sconn->client->connections->smb2.client.guid;
- if (!l->breaking) {
+ status = leases_db_get(client_guid,
+ key,
+ &id,
+ ¤t_state,
+ &breaking,
+ &breaking_to_requested,
+ &breaking_to_required,
+ &lease_version,
+ &epoch);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_WARNING("leases_db_get returned %s\n",
+ nt_errstr(status));
+ TALLOC_FREE(lck);
+ return status;
+ }
+
+ if (!breaking) {
DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
"but we're not in breaking state\n",
- l->current_state, new_lease_state);
+ current_state, lease_state);
+ TALLOC_FREE(lck);
return NT_STATUS_UNSUCCESSFUL;
}
/*
- * Can't upgrade anything: l->breaking_to_requested (and l->current_state)
+ * Can't upgrade anything: breaking_to_requested (and current_state)
* must be a strict bitwise superset of new_lease_state
*/
- if ((new_lease_state & l->breaking_to_requested) != new_lease_state) {
+ if ((lease_state & breaking_to_requested) != lease_state) {
DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
"- expected %"PRIu32"\n",
- l->current_state, new_lease_state,
- l->breaking_to_requested);
+ current_state, lease_state,
+ breaking_to_requested);
+ TALLOC_FREE(lck);
return NT_STATUS_REQUEST_NOT_ACCEPTED;
}
- if (l->current_state != new_lease_state) {
- l->current_state = new_lease_state;
- d->modified = true;
+ if (current_state != lease_state) {
+ current_state = lease_state;
}
- if ((new_lease_state & ~l->breaking_to_required) != 0) {
+ status = NT_STATUS_OK;
+
+ if ((lease_state & ~breaking_to_required) != 0) {
+ struct downgrade_lease_additional_state *state;
+
DBG_INFO("lease state %"PRIu32" not fully broken from "
"%"PRIu32" to %"PRIu32"\n",
- new_lease_state,
- l->current_state,
- l->breaking_to_required);
- l->breaking_to_requested = l->breaking_to_required;
- if (l->current_state & (~SMB2_LEASE_READ)) {
+ lease_state,
+ current_state,
+ breaking_to_required);
+
+ breaking_to_requested = breaking_to_required;
+
+ if (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;
+ breaking_to_requested |= SMB2_LEASE_READ;
}
- d->modified = true;
- *_l = l;
- return NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
- }
-
- DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
- "expected %"PRIu32"\n",
- l->current_state,
- new_lease_state,
- l->breaking_to_requested);
-
- l->breaking_to_requested = 0;
- l->breaking_to_required = 0;
- l->breaking = false;
-
- d->modified = true;
-
- return NT_STATUS_OK;
-}
-
-NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
- uint32_t num_file_ids,
- const struct file_id *ids,
- const struct smb2_lease_key *key,
- uint32_t lease_state)
-{
- struct smbd_server_connection *sconn = xconn->client->sconn;
- struct share_mode_lock *lck;
- struct share_mode_lease *l = NULL;
- const struct file_id id = ids[0];
- uint32_t i;
- NTSTATUS status;
-
- DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
- file_id_string_tos(&id), (unsigned)lease_state));
-
- lck = get_existing_share_mode_lock(talloc_tos(), id);
- if (lck == NULL) {
- return NT_STATUS_OBJECT_NAME_NOT_FOUND;
- }
- status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
-
- DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
- file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
-
- if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
- struct downgrade_lease_additional_state *state;
state = talloc_zero(xconn,
struct downgrade_lease_additional_state);
}
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;
- if (l->lease_version > 1) {
- state->new_epoch = l->epoch;
+ state->lease_key = *key;
+ state->break_from = current_state;
+ state->break_to = breaking_to_requested;
+ if (lease_version > 1) {
+ state->new_epoch = epoch;
}
- if (state->break_flags == 0) {
+ if (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
* we need to store NONE state in the
* database.
*/
- l->current_state = 0;
- l->breaking_to_requested = 0;
- l->breaking_to_required = 0;
- l->breaking = false;
-
- lck->data->modified = true;
+ current_state = 0;
+ breaking_to_requested = 0;
+ breaking_to_required = 0;
+ breaking = false;
+
+ {
+ NTSTATUS set_status;
+
+ set_status = leases_db_set(
+ &sconn->client->connections->
+ smb2.client.guid,
+ key,
+ current_state,
+ breaking,
+ breaking_to_requested,
+ breaking_to_required,
+ lease_version,
+ epoch);
+
+ if (!NT_STATUS_IS_OK(set_status)) {
+ DBG_DEBUG("leases_db_set failed: %s\n",
+ nt_errstr(set_status));
+ return set_status;
+ }
+ }
}
tevent_schedule_immediate(state->im,
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",
+ current_state,
+ lease_state,
+ breaking_to_requested);
+
+ breaking_to_requested = 0;
+ breaking_to_required = 0;
+ breaking = false;
+ }
+
+ {
+ NTSTATUS set_status;
+
+ set_status = leases_db_set(
+ client_guid,
+ key,
+ current_state,
+ breaking,
+ breaking_to_requested,
+ breaking_to_required,
+ lease_version,
+ epoch);
+
+ if (!NT_STATUS_IS_OK(set_status)) {
+ DBG_DEBUG("leases_db_set failed: %s\n",
+ nt_errstr(set_status));
+ TALLOC_FREE(lck);
+ return set_status;
+ }
}
+ DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
+ file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
+
+ /*
+ * No, we did not modify the share mode array. We did modify
+ * the leases_db. But without this we don't notify a lease
+ * break waiter via dbwrap_watch_record. We need to make
+ * leases_db watched too.
+ */
+ lck->data->modified = true;
+
{
struct downgrade_lease_fsps_state state = {
.id = id, .lck = lck, .key = key,
{
files_struct *fsp = (files_struct *)private_data;
- /*
- * Note this function doesn't run under any specific impersonation and
- * is not expected to call any SMB_VFS operation!
- */
-
SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
/* Remove the timed event handler. */
"around\n"));
}
- /*
- * For now we keep the logic and use the
- * raw event context. We're called from
- * the messaging system from a raw event context.
- * Also oplock_timeout_handler doesn't invoke
- * SMB_VFS calls.
- */
fsp->oplock_timeout =
- tevent_add_timer(fsp->conn->sconn->raw_ev_ctx, fsp,
+ tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
oplock_timeout_handler, fsp);
}
if (fsp->oplock_type == LEASE_OPLOCK) {
+ const struct GUID *client_guid = fsp_client_guid(fsp);
struct share_mode_lock *lck;
- int idx;
+ uint32_t current_state;
+ uint32_t breaking_to_requested, breaking_to_required;
+ bool breaking;
+ uint16_t lease_version, epoch;
+ NTSTATUS status;
lck = get_existing_share_mode_lock(
talloc_tos(), fsp->file_id);
return;
}
- idx = find_share_mode_lease(
- lck->data,
- fsp_client_guid(fsp),
- &fsp->lease->lease.lease_key);
- if (idx != -1) {
- struct share_mode_lease *l;
- l = &lck->data->leases[idx];
-
- break_from = l->current_state;
- break_to &= l->current_state;
-
- if (l->breaking) {
- break_to &= l->breaking_to_required;
- if (l->breaking_to_required != break_to) {
- /*
- * Note we don't increment the epoch
- * here, which might be a bug in
- * Windows too...
- */
- l->breaking_to_required = break_to;
- lck->data->modified = true;
- }
- break_needed = false;
- } else if (l->current_state == break_to) {
- break_needed = false;
- } else if (l->current_state == SMB2_LEASE_READ) {
- l->current_state = SMB2_LEASE_NONE;
- /* Need to increment the epoch */
- l->epoch += 1;
- lck->data->modified = true;
- } else {
- l->breaking = true;
- l->breaking_to_required = break_to;
- l->breaking_to_requested = break_to;
- /* Need to increment the epoch */
- l->epoch += 1;
- lck->data->modified = true;
+ status = leases_db_get(client_guid,
+ &fsp->lease->lease.lease_key,
+ &id,
+ ¤t_state,
+ &breaking,
+ &breaking_to_requested,
+ &breaking_to_required,
+ &lease_version,
+ &epoch);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_WARNING("leases_db_get returned %s\n",
+ nt_errstr(status));
+ TALLOC_FREE(lck);
+ return;
+ }
+
+ break_from = current_state;
+ break_to &= current_state;
+
+ if (breaking) {
+ break_to &= breaking_to_required;
+ if (breaking_to_required != break_to) {
+ /*
+ * Note we don't increment the epoch
+ * here, which might be a bug in
+ * Windows too...
+ */
+ breaking_to_required = break_to;
}
+ break_needed = false;
+ } else if (current_state == break_to) {
+ break_needed = false;
+ } else if (current_state == SMB2_LEASE_READ) {
+ current_state = SMB2_LEASE_NONE;
+ /* Need to increment the epoch */
+ epoch += 1;
+ } else {
+ breaking = true;
+ breaking_to_required = break_to;
+ breaking_to_requested = break_to;
+ /* Need to increment the epoch */
+ epoch += 1;
+ }
- /* Ensure we're in sync with current lease state. */
- fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
+ {
+ NTSTATUS set_status;
+
+ set_status = leases_db_set(
+ client_guid,
+ &fsp->lease->lease.lease_key,
+ current_state,
+ breaking,
+ breaking_to_requested,
+ breaking_to_required,
+ lease_version,
+ epoch);
+
+ if (!NT_STATUS_IS_OK(set_status)) {
+ DBG_DEBUG("leases_db_set failed: %s\n",
+ nt_errstr(set_status));
+ return;
+ }
}
+ /* Ensure we're in sync with current lease state. */
+ fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
+
TALLOC_FREE(lck);
}
* anyway, so we postpone this into an immediate event.
*/
- state = talloc_zero(sconn, struct break_to_none_state);
+ state = talloc(sconn, struct break_to_none_state);
if (state == NULL) {
DEBUG(1, ("talloc failed\n"));
return;
}
- state->sconn = sconn;
- state->id = fsp->file_id;
+ *state = (struct break_to_none_state) {
+ .sconn = sconn, .id = fsp->file_id,
+ };
if (fsp->oplock_type == LEASE_OPLOCK) {
state->client_guid = *fsp_client_guid(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,
(uint8_t *)msg, sizeof(msg));
}
+static bool do_break_lease_to_none(struct share_mode_lock *lck,
+ struct share_mode_entry *e,
+ void *private_data)
+{
+ struct break_to_none_state *state = talloc_get_type_abort(
+ private_data, struct break_to_none_state);
+ uint32_t current_state = 0;
+ bool our_own;
+ NTSTATUS status;
+
+ DBG_DEBUG("lease_key=%"PRIu64"/%"PRIu64"\n",
+ e->lease_key.data[0],
+ e->lease_key.data[1]);
+
+ status = leases_db_get(&e->client_guid,
+ &e->lease_key,
+ &state->id,
+ ¤t_state,
+ NULL, /* breaking */
+ NULL, /* breaking_to_requested */
+ NULL, /* breaking_to_required */
+ NULL, /* lease_version */
+ NULL); /* epoch */
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_WARNING("leases_db_get failed: %s\n",
+ nt_errstr(status));
+ return false;
+ }
+
+ if ((current_state & SMB2_LEASE_READ) == 0) {
+ return false;
+ }
+
+ our_own = smb2_lease_equal(&state->client_guid,
+ &state->lease_key,
+ &e->client_guid,
+ &e->lease_key);
+ if (our_own) {
+ DEBUG(10, ("Don't break our own lease\n"));
+ return false;
+ }
+
+ DBG_DEBUG("Breaking %"PRIu64"/%"PRIu64" to none\n",
+ e->lease_key.data[0],
+ e->lease_key.data[1]);
+
+ send_break_to_none(state->sconn->msg_ctx, &state->id, e);
+
+ return false;
+}
+
static void do_break_to_none(struct tevent_context *ctx,
struct tevent_immediate *im,
void *private_data)
uint32_t i;
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!
- */
+ bool ok;
lck = get_existing_share_mode_lock(talloc_tos(), state->id);
if (lck == NULL) {
* separately.
*/
- for (i=0; i<d->num_leases; i++) {
- struct share_mode_lease *l = &d->leases[i];
- struct share_mode_entry *e = NULL;
- uint32_t j;
-
- if ((l->current_state & SMB2_LEASE_READ) == 0) {
- continue;
- }
- if (smb2_lease_equal(&state->client_guid,
- &state->lease_key,
- &l->client_guid,
- &l->lease_key)) {
- DEBUG(10, ("Don't break our own lease\n"));
- continue;
- }
-
- for (j=0; j<d->num_share_modes; j++) {
- e = &d->share_modes[j];
-
- if (!is_valid_share_mode_entry(e)) {
- continue;
- }
- if (e->lease_idx == i) {
- break;
- }
- }
- if (j == d->num_share_modes) {
- DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
- i));
- continue;
- }
-
- DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
- "%"PRIu32"\n", i, j));
-
- send_break_to_none(state->sconn->msg_ctx, &state->id, e);
+ ok = share_mode_forall_leases(lck, do_break_lease_to_none, state);
+ if (!ok) {
+ DBG_WARNING("share_mode_forall_leases failed\n");
}
for(i = 0; i < d->num_share_modes; i++) {
/* 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;