smbd: Set SHARE_MODE_HAS_READ_LEASE when downgrading an oplock
[samba.git] / source3 / locking / locking.c
index 037a987666876a85805383663822e6ecea94ea52..e173034165c9f6f5a11576df2f87333587b05f0d 100644 (file)
@@ -66,10 +66,6 @@ const char *lock_type_name(enum brl_type lock_type)
                        return "READ";
                case WRITE_LOCK:
                        return "WRITE";
-               case PENDING_READ_LOCK:
-                       return "PENDING_READ";
-               case PENDING_WRITE_LOCK:
-                       return "PENDING_WRITE";
                default:
                        return "other";
        }
@@ -104,7 +100,7 @@ void init_strict_lock_struct(files_struct *fsp,
         plock->lock_flav = lp_posix_cifsu_locktype(fsp);
 }
 
-bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
+bool strict_lock_check_default(files_struct *fsp, struct lock_struct *plock)
 {
        struct byte_range_lock *br_lck;
        int strict_locking = lp_strict_locking(fsp->conn->params);
@@ -150,6 +146,9 @@ bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
                 * autocleanup. This is the slow path anyway.
                 */
                br_lck = brl_get_locks(talloc_tos(), fsp);
+               if (br_lck == NULL) {
+                       return true;
+               }
                ret = brl_locktest(br_lck, plock);
                TALLOC_FREE(br_lck);
        }
@@ -164,10 +163,6 @@ bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
        return ret;
 }
 
-void strict_unlock_default(files_struct *fsp, struct lock_struct *plock)
-{
-}
-
 /****************************************************************************
  Find out if a lock could be granted - return who is blocking us if we can't.
 ****************************************************************************/
@@ -235,76 +230,123 @@ static void decrement_current_lock_count(files_struct *fsp,
  Utility function called by locking requests.
 ****************************************************************************/
 
-struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
-                       files_struct *fsp,
-                       uint64_t smblctx,
-                       uint64_t count,
-                       uint64_t offset,
-                       enum brl_type lock_type,
-                       enum brl_flavour lock_flav,
-                       bool blocking_lock,
-                       NTSTATUS *perr,
-                       uint64_t *psmblctx)
+struct do_lock_state {
+       struct files_struct *fsp;
+       uint64_t smblctx;
+       uint64_t count;
+       uint64_t offset;
+       enum brl_type lock_type;
+       enum brl_flavour lock_flav;
+
+       struct server_id blocker_pid;
+       uint64_t blocker_smblctx;
+       NTSTATUS status;
+};
+
+static void do_lock_fn(
+       struct db_record *rec,
+       bool *modified_dependent,
+       void *private_data)
 {
+       struct do_lock_state *state = private_data;
        struct byte_range_lock *br_lck = NULL;
 
+       br_lck = brl_get_locks(talloc_tos(), state->fsp);
+       if (br_lck == NULL) {
+               state->status = NT_STATUS_NO_MEMORY;
+               return;
+       }
+
+       state->status = brl_lock(
+               br_lck,
+               state->smblctx,
+               messaging_server_id(state->fsp->conn->sconn->msg_ctx),
+               state->offset,
+               state->count,
+               state->lock_type,
+               state->lock_flav,
+               &state->blocker_pid,
+               &state->blocker_smblctx);
+
+       TALLOC_FREE(br_lck);
+}
+
+NTSTATUS do_lock(files_struct *fsp,
+                uint64_t smblctx,
+                uint64_t count,
+                uint64_t offset,
+                enum brl_type lock_type,
+                enum brl_flavour lock_flav,
+                struct server_id *pblocker_pid,
+                uint64_t *psmblctx)
+{
+       struct do_lock_state state = {
+               .fsp = fsp,
+               .smblctx = smblctx,
+               .count = count,
+               .offset = offset,
+               .lock_type = lock_type,
+               .lock_flav = lock_flav,
+       };
+       NTSTATUS status;
+
        /* silently return ok on print files as we don't do locking there */
        if (fsp->print_file) {
-               *perr = NT_STATUS_OK;
-               return NULL;
+               return NT_STATUS_OK;
        }
 
        if (!fsp->can_lock) {
-               *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
-               return NULL;
+               if (fsp->is_directory) {
+                       return NT_STATUS_INVALID_DEVICE_REQUEST;
+               }
+               return NT_STATUS_INVALID_HANDLE;
        }
 
        if (!lp_locking(fsp->conn->params)) {
-               *perr = NT_STATUS_OK;
-               return NULL;
+               return NT_STATUS_OK;
        }
 
        /* NOTE! 0 byte long ranges ARE allowed and should be stored  */
 
-       DEBUG(10,("do_lock: lock flavour %s lock type %s start=%ju len=%ju "
-               "blocking_lock=%s requested for %s file %s\n",
-               lock_flav_name(lock_flav), lock_type_name(lock_type),
-               (uintmax_t)offset, (uintmax_t)count, blocking_lock ? "true" :
-               "false", fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
+       DBG_DEBUG("lock flavour %s lock type %s start=%"PRIu64" len=%"PRIu64" "
+                 "requested for %s file %s\n",
+                 lock_flav_name(lock_flav),
+                 lock_type_name(lock_type),
+                 offset,
+                 count,
+                 fsp_fnum_dbg(fsp),
+                 fsp_str_dbg(fsp));
 
-       br_lck = brl_get_locks(talloc_tos(), fsp);
-       if (!br_lck) {
-               *perr = NT_STATUS_NO_MEMORY;
-               return NULL;
+       status = share_mode_do_locked(fsp->file_id, do_lock_fn, &state);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_DEBUG("share_mode_do_locked returned %s\n",
+                         nt_errstr(status));
+               return status;
        }
 
-       *perr = brl_lock(msg_ctx,
-                       br_lck,
-                       smblctx,
-                       messaging_server_id(fsp->conn->sconn->msg_ctx),
-                       offset,
-                       count,
-                       lock_type,
-                       lock_flav,
-                       blocking_lock,
-                       psmblctx);
+       if (psmblctx != NULL) {
+               *psmblctx = state.blocker_smblctx;
+       }
+       if (pblocker_pid != NULL) {
+               *pblocker_pid = state.blocker_pid;
+       }
 
-       DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr)));
+       DBG_DEBUG("returning status=%s\n", nt_errstr(state.status));
 
        increment_current_lock_count(fsp, lock_flav);
-       return br_lck;
+
+       return state.status;
 }
 
 /****************************************************************************
  Utility function called by unlocking requests.
 ****************************************************************************/
 
-NTSTATUS do_unlock(struct messaging_context *msg_ctx,
-                       files_struct *fsp,
-                       uint64_t smblctx,
-                       uint64_t count,
-                       uint64_t offset,
-                       enum brl_flavour lock_flav)
+NTSTATUS do_unlock(files_struct *fsp,
+                  uint64_t smblctx,
+                  uint64_t count,
+                  uint64_t offset,
+                  enum brl_flavour lock_flav)
 {
        bool ok = False;
        struct byte_range_lock *br_lck = NULL;
@@ -317,17 +359,19 @@ NTSTATUS do_unlock(struct messaging_context *msg_ctx,
                return NT_STATUS_OK;
        }
 
-       DEBUG(10, ("do_unlock: unlock start=%ju len=%ju requested for %s file "
-                  "%s\n", (uintmax_t)offset, (uintmax_t)count,
-                  fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
+       DBG_DEBUG("unlock start=%"PRIu64" len=%"PRIu64" requested for %s file "
+                 "%s\n",
+                 offset,
+                 count,
+                 fsp_fnum_dbg(fsp),
+                 fsp_str_dbg(fsp));
 
        br_lck = brl_get_locks(talloc_tos(), fsp);
        if (!br_lck) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       ok = brl_unlock(msg_ctx,
-                       br_lck,
+       ok = brl_unlock(br_lck,
                        smblctx,
                        messaging_server_id(fsp->conn->sconn->msg_ctx),
                        offset,
@@ -345,61 +389,11 @@ NTSTATUS do_unlock(struct messaging_context *msg_ctx,
        return NT_STATUS_OK;
 }
 
-/****************************************************************************
- Cancel any pending blocked locks.
-****************************************************************************/
-
-NTSTATUS do_lock_cancel(files_struct *fsp,
-                       uint64_t smblctx,
-                       uint64_t count,
-                       uint64_t offset,
-                       enum brl_flavour lock_flav)
-{
-       bool ok = False;
-       struct byte_range_lock *br_lck = NULL;
-
-       if (!fsp->can_lock) {
-               return fsp->is_directory ?
-                       NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
-       }
-
-       if (!lp_locking(fsp->conn->params)) {
-               return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
-       }
-
-       DEBUG(10, ("do_lock_cancel: cancel start=%ju len=%ju requested for "
-                  "%s file %s\n", (uintmax_t)offset, (uintmax_t)count,
-                  fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
-
-       br_lck = brl_get_locks(talloc_tos(), fsp);
-       if (!br_lck) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       ok = brl_lock_cancel(br_lck,
-                       smblctx,
-                       messaging_server_id(fsp->conn->sconn->msg_ctx),
-                       offset,
-                       count,
-                       lock_flav);
-
-       TALLOC_FREE(br_lck);
-
-       if (!ok) {
-               DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
-               return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
-       }
-
-       decrement_current_lock_count(fsp, lock_flav);
-       return NT_STATUS_OK;
-}
-
 /****************************************************************************
  Remove any locks on this fd. Called from file_close().
 ****************************************************************************/
 
-void locking_close_file(struct messaging_context *msg_ctx,
-                       files_struct *fsp,
+void locking_close_file(files_struct *fsp,
                        enum file_close_type close_type)
 {
        struct byte_range_lock *br_lck;
@@ -419,8 +413,13 @@ void locking_close_file(struct messaging_context *msg_ctx,
        br_lck = brl_get_locks(talloc_tos(),fsp);
 
        if (br_lck) {
-               cancel_pending_lock_requests_by_fid(fsp, br_lck, close_type);
-               brl_close_fnum(msg_ctx, br_lck);
+               /*
+                * Unlocks must trigger dbwrap_watch watchers,
+                * normally in smbd_do_unlocking. Here it's done
+                * implictly, we're closing the file and thus remove a
+                * share mode. This will wake the waiters.
+                */
+               brl_close_fnum(br_lck);
                TALLOC_FREE(br_lck);
        }
 }
@@ -429,7 +428,9 @@ void locking_close_file(struct messaging_context *msg_ctx,
  Print out a share mode.
 ********************************************************************/
 
-char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
+char *share_mode_str(TALLOC_CTX *ctx, int num,
+                    const struct file_id *id,
+                    const struct share_mode_entry *e)
 {
        struct server_id_buf tmp;
 
@@ -443,7 +444,7 @@ char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
                 e->access_mask, (unsigned long long)e->op_mid,
                 e->op_type, (unsigned long long)e->share_file_id,
                 (unsigned int)e->uid, (unsigned int)e->flags,
-                file_id_string_tos(&e->id),
+                file_id_string_tos(id),
                 (unsigned int)e->name_hash);
 }
 
@@ -458,6 +459,32 @@ struct share_mode_lock *get_existing_share_mode_lock(TALLOC_CTX *mem_ctx,
        return get_share_mode_lock(mem_ctx, id, NULL, NULL, NULL);
 }
 
+static bool rename_lease_fn(struct share_mode_lock *lck,
+                           struct share_mode_entry *e,
+                           void *private_data)
+{
+       struct share_mode_data *d = lck->data;
+       NTSTATUS status;
+
+       status = leases_db_rename(&e->client_guid,
+                                 &e->lease_key,
+                                 &d->id,
+                                 d->servicepath,
+                                 d->base_name,
+                                 d->stream_name);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               /* Any error recovery possible here ? */
+               DBG_WARNING("Failed to rename lease key for "
+                           "renamed file %s:%s. %s\n",
+                           d->base_name,
+                           d->stream_name,
+                           nt_errstr(status));
+       }
+
+       return false;
+}
+
 /*******************************************************************
  Sets the service name and filename for rename.
  At this point we emit "file renamed" messages to all
@@ -474,15 +501,15 @@ bool rename_share_filename(struct messaging_context *msg_ctx,
                        const struct smb_filename *smb_fname_dst)
 {
        struct share_mode_data *d = lck->data;
-       size_t sp_len;
-       size_t bn_len;
-       size_t sn_len;
-       size_t msg_len;
-       char *frm = NULL;
+       struct file_rename_message msg = {
+               .id = id,
+               .servicepath = servicepath,
+               .base_name = smb_fname_dst->base_name,
+               .stream_name = smb_fname_dst->stream_name,
+       };
        uint32_t i;
-       bool strip_two_chars = false;
-       bool has_stream = smb_fname_dst->stream_name != NULL;
        struct server_id self_pid = messaging_server_id(msg_ctx);
+       bool ok;
 
        DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
                   servicepath, smb_fname_dst->base_name));
@@ -491,56 +518,27 @@ bool rename_share_filename(struct messaging_context *msg_ctx,
         * rename_internal_fsp() and rename_internals() add './' to
         * head of newname if newname does not contain a '/'.
         */
-       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;
-       }
-
-       d->servicepath = talloc_strdup(d, servicepath);
-       d->base_name = talloc_strdup(d, smb_fname_dst->base_name +
-                                      (strip_two_chars ? 2 : 0));
-       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) {
-               DEBUG(0, ("rename_share_filename: talloc failed\n"));
-               return False;
-       }
-       d->modified = True;
-
-       sp_len = strlen(d->servicepath);
-       bn_len = strlen(d->base_name);
-       sn_len = has_stream ? strlen(d->stream_name) : 0;
 
-       msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
-           sn_len + 1;
-
-       /* Set up the name changed message. */
-       frm = talloc_array(d, char, msg_len);
-       if (!frm) {
-               return False;
+       if (strncmp(msg.base_name, "./", 2) == 0) {
+               msg.base_name += 2;
        }
 
-       push_file_id_24(frm, &id);
-
-       DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
-
-       strlcpy(&frm[24],
-               d->servicepath ? d->servicepath : "",
-               sp_len+1);
-       strlcpy(&frm[24 + sp_len + 1],
-               d->base_name ? d->base_name : "",
-               bn_len+1);
-       strlcpy(&frm[24 + sp_len + 1 + bn_len + 1],
-               d->stream_name ? d->stream_name : "",
-               sn_len+1);
+       d->servicepath = talloc_strdup(d, msg.servicepath);
+       d->base_name = talloc_strdup(d, msg.base_name);
+       d->stream_name = talloc_strdup(d, msg.stream_name);
+       if ((d->servicepath == NULL) ||
+           (d->base_name == NULL) ||
+           ((msg.stream_name != NULL) && (d->stream_name == NULL))) {
+               DBG_WARNING("talloc failed\n");
+               return false;
+       }
+       d->modified = True;
 
        /* Send the messages. */
        for (i=0; i<d->num_share_modes; i++) {
                struct share_mode_entry *se = &d->share_modes[i];
-               struct server_id_buf tmp;
+               DATA_BLOB blob;
+               enum ndr_err_code ndr_err;
 
                if (!is_valid_share_mode_entry(se)) {
                        continue;
@@ -563,40 +561,36 @@ bool rename_share_filename(struct messaging_context *msg_ctx,
                        continue;
                }
 
-               DEBUG(10,("rename_share_filename: sending rename message to "
-                         "pid %s file_id %s sharepath %s base_name %s "
-                         "stream_name %s\n",
-                         server_id_str_buf(se->pid, &tmp),
-                         file_id_string_tos(&id),
-                         d->servicepath, d->base_name,
-                       has_stream ? d->stream_name : ""));
-
-               messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
-                                  (uint8_t *)frm, msg_len);
-       }
+               msg.share_file_id = se->share_file_id;
+
+               ndr_err = ndr_push_struct_blob(
+                       &blob,
+                       talloc_tos(),
+                       &msg,
+                       (ndr_push_flags_fn_t)ndr_push_file_rename_message);
+               if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+                       DBG_DEBUG("ndr_push_file_rename_message failed: %s\n",
+                                 ndr_errstr(ndr_err));
+                       return false;
+               }
+               if (DEBUGLEVEL >= 10) {
+                       struct server_id_buf tmp;
+                       DBG_DEBUG("sending rename message to %s\n",
+                                 server_id_str_buf(se->pid, &tmp));
+                       NDR_PRINT_DEBUG(file_rename_message, &msg);
+               }
 
-       for (i=0; i<d->num_leases; i++) {
-               /* Update the filename in leases_db. */
-               NTSTATUS status;
-               struct share_mode_lease *l;
+               messaging_send(msg_ctx, se->pid, MSG_SMB_FILE_RENAME, &blob);
 
-               l = &d->leases[i];
+               TALLOC_FREE(blob.data);
+       }
 
-               status = leases_db_rename(&l->client_guid,
-                                       &l->lease_key,
-                                       &id,
-                                       d->servicepath,
-                                       d->base_name,
-                                       d->stream_name);
-               if (!NT_STATUS_IS_OK(status)) {
-                       /* Any error recovery possible here ? */
-                       DEBUG(1,("Failed to rename lease key for "
-                               "renamed file %s:%s. %s\n",
-                               d->base_name,
-                               d->stream_name,
-                               nt_errstr(status)));
-                       continue;
-               }
+       ok = share_mode_forall_leases(lck, rename_lease_fn, NULL);
+       if (!ok) {
+               /*
+                * Ignore error here. Not sure what to do..
+                */
+               DBG_WARNING("share_mode_forall_leases failed\n");
        }
 
        return True;
@@ -659,10 +653,7 @@ bool is_valid_share_mode_entry(const struct share_mode_entry *e)
 static void remove_share_mode_lease(struct share_mode_data *d,
                                    struct share_mode_entry *e)
 {
-       struct GUID client_guid;
-       struct smb2_lease_key lease_key;
        uint16_t op_type;
-       uint32_t lease_idx;
        uint32_t i;
 
        op_type = e->op_type;
@@ -679,18 +670,20 @@ static void remove_share_mode_lease(struct share_mode_data *d,
         * it, remove it.
         */
 
-       lease_idx = e->lease_idx;
-       e->lease_idx = UINT32_MAX;
-
        for (i=0; i<d->num_share_modes; i++) {
-               if (d->share_modes[i].stale) {
+               struct share_mode_entry *e2 = &d->share_modes[i];
+
+               if (e2->stale) {
                        continue;
                }
-               if (e == &d->share_modes[i]) {
+               if (e == e2) {
                        /* Not ourselves. */
                        continue;
                }
-               if (d->share_modes[i].lease_idx == lease_idx) {
+               if (smb2_lease_equal(&e->client_guid,
+                                    &e->lease_key,
+                                    &e2->client_guid,
+                                    &e2->lease_key)) {
                        break;
                }
        }
@@ -701,30 +694,12 @@ static void remove_share_mode_lease(struct share_mode_data *d,
                return;
        }
 
-       memcpy(&client_guid,
-               &d->leases[lease_idx].client_guid,
-               sizeof(client_guid));
-       lease_key = d->leases[lease_idx].lease_key;
-
-       d->num_leases -= 1;
-       d->leases[lease_idx] = d->leases[d->num_leases];
-
-       /*
-        * We changed the lease array. Fix all references to it.
-        */
-       for (i=0; i<d->num_share_modes; i++) {
-               if (d->share_modes[i].lease_idx == d->num_leases) {
-                       d->share_modes[i].lease_idx = lease_idx;
-                       d->share_modes[i].lease = &d->leases[lease_idx];
-               }
-       }
-
        {
                NTSTATUS status;
 
-               status = leases_db_del(&client_guid,
-                                       &lease_key,
-                                       &e->id);
+               status = leases_db_del(&e->client_guid,
+                                      &e->lease_key,
+                                      &d->id);
 
                DEBUG(10, ("%s: leases_db_del returned %s\n", __func__,
                           nt_errstr(status)));
@@ -742,8 +717,10 @@ bool share_mode_stale_pid(struct share_mode_data *d, uint32_t idx)
        struct share_mode_entry *e;
 
        if (idx > d->num_share_modes) {
-               DEBUG(1, ("Asking for index %u, only %u around\n",
-                         idx, (unsigned)d->num_share_modes));
+               DBG_WARNING("Asking for index %"PRIu32", "
+                           "only %"PRIu32" around\n",
+                           idx,
+                           d->num_share_modes);
                return false;
        }
        e = &d->share_modes[idx];
@@ -754,36 +731,36 @@ bool share_mode_stale_pid(struct share_mode_data *d, uint32_t idx)
                return true;
        }
        if (serverid_exists(&e->pid)) {
-               DEBUG(10, ("PID %s (index %u out of %u) still exists\n",
-                          server_id_str_buf(e->pid, &tmp), idx,
-                          (unsigned)d->num_share_modes));
+               DBG_DEBUG("PID %s (index %"PRIu32" out of %"PRIu32") "
+                         "still exists\n",
+                         server_id_str_buf(e->pid, &tmp),
+                         idx,
+                         d->num_share_modes);
                return false;
        }
-       DEBUG(10, ("PID %s (index %u out of %u) does not exist anymore\n",
-                  server_id_str_buf(e->pid, &tmp), idx,
-                  (unsigned)d->num_share_modes));
+       DBG_DEBUG("PID %s (index %"PRIu32" out of %"PRIu32") "
+                 "does not exist anymore\n",
+                 server_id_str_buf(e->pid, &tmp),
+                 idx,
+                 d->num_share_modes);
 
        e->stale = true;
 
        if (d->num_delete_tokens != 0) {
-               uint32_t i, num_stale;
-
-               /*
-                * We cannot have any delete tokens
-                * if there are no valid share modes.
-                */
-
-               num_stale = 0;
+               uint32_t i;
 
                for (i=0; i<d->num_share_modes; i++) {
-                       if (d->share_modes[i].stale) {
-                               num_stale += 1;
+                       bool valid = !d->share_modes[i].stale;
+                       if (valid) {
+                               break;
                        }
                }
 
-               if (num_stale == d->num_share_modes) {
+               if (i == d->num_share_modes) {
                        /*
-                        * No non-stale share mode found
+                        * No valid (non-stale) share mode found, all
+                        * who might have set the delete token are
+                        * gone.
                         */
                        TALLOC_FREE(d->delete_tokens);
                        d->num_delete_tokens = 0;
@@ -806,27 +783,22 @@ void remove_stale_share_mode_entries(struct share_mode_data *d)
                        struct share_mode_entry *m = d->share_modes;
                        m[i] = m[d->num_share_modes-1];
                        d->num_share_modes -= 1;
-               } else {
-                       i += 1;
+                       continue;
                }
+               i += 1;
        }
 }
 
-bool set_share_mode(struct share_mode_lock *lck, struct files_struct *fsp,
-                   uid_t uid, uint64_t mid, uint16_t op_type,
-                   uint32_t lease_idx)
+bool set_share_mode(struct share_mode_lock *lck,
+                   struct files_struct *fsp,
+                   uid_t uid,
+                   uint64_t mid,
+                   uint16_t op_type,
+                   const struct GUID *client_guid,
+                   const struct smb2_lease_key *lease_key)
 {
        struct share_mode_data *d = lck->data;
        struct share_mode_entry *tmp, *e;
-       struct share_mode_lease *lease = NULL;
-
-       if (lease_idx == UINT32_MAX) {
-               lease = NULL;
-       } else if (lease_idx >= d->num_leases) {
-               return false;
-       } else {
-               lease = &d->leases[lease_idx];
-       }
 
        tmp = talloc_realloc(d, d->share_modes, struct share_mode_entry,
                             d->num_share_modes+1);
@@ -845,11 +817,14 @@ bool set_share_mode(struct share_mode_lock *lck, struct files_struct *fsp,
        e->access_mask = fsp->access_mask;
        e->op_mid = mid;
        e->op_type = op_type;
-       e->lease_idx = lease_idx;
-       e->lease = lease;
+
+       if (op_type == LEASE_OPLOCK) {
+               e->client_guid = *client_guid;
+               e->lease_key = *lease_key;
+       }
+
        e->time.tv_sec = fsp->open_time.tv_sec;
        e->time.tv_usec = fsp->open_time.tv_usec;
-       e->id = fsp->file_id;
        e->share_file_id = fsp->fh->gen_id;
        e->uid = (uint32_t)uid;
        e->flags = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
@@ -864,7 +839,7 @@ struct share_mode_entry *find_share_mode_entry(
 {
        struct share_mode_data *d = lck->data;
        struct server_id pid;
-       int i;
+       uint32_t i;
 
        pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
 
@@ -877,9 +852,6 @@ struct share_mode_entry *find_share_mode_entry(
                if (!serverid_equal(&pid, &e->pid)) {
                        continue;
                }
-               if (!file_id_equal(&fsp->file_id, &e->id)) {
-                       continue;
-               }
                if (fsp->fh->gen_id != e->share_file_id) {
                        continue;
                }
@@ -889,8 +861,7 @@ struct share_mode_entry *find_share_mode_entry(
 }
 
 /*******************************************************************
- Del the share mode of a file for this process. Return the number of
- entries left.
+ Del the share mode of a file for this process.
 ********************************************************************/
 
 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
@@ -976,90 +947,11 @@ bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
        }
 
        e->op_type = LEVEL_II_OPLOCK;
+       lck->data->flags |= SHARE_MODE_HAS_READ_LEASE;
        lck->data->modified = True;
        return True;
 }
 
-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)
-{
-       struct share_mode_data *d = lck->data;
-       struct share_mode_lease *l;
-       uint32_t i;
-
-       *_l = NULL;
-
-       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;
-       }
-
-       l = &d->leases[i];
-
-       if (!l->breaking) {
-               DEBUG(1, ("Attempt to break from %d to %d - but we're not in breaking state\n",
-                          (int)l->current_state, (int)new_lease_state));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /*
-        * Can't upgrade anything: l->breaking_to_requested (and l->current_state)
-        * must be a strict bitwise superset of new_lease_state
-        */
-       if ((new_lease_state & l->breaking_to_requested) != new_lease_state) {
-               DEBUG(1, ("Attempt to upgrade from %d to %d - expected %d\n",
-                          (int)l->current_state, (int)new_lease_state,
-                          (int)l->breaking_to_requested));
-               return NT_STATUS_REQUEST_NOT_ACCEPTED;
-       }
-
-       if (l->current_state != new_lease_state) {
-               l->current_state = new_lease_state;
-               d->modified = true;
-       }
-
-       if ((new_lease_state & ~l->breaking_to_required) != 0) {
-               DEBUG(5, ("lease state %d not fully broken from %d to %d\n",
-                          (int)new_lease_state,
-                          (int)l->current_state,
-                          (int)l->breaking_to_required));
-               l->breaking_to_requested = l->breaking_to_required;
-               if (l->current_state & (~SMB2_LEASE_READ)) {
-                       /*
-                        * Here we break in steps, as windows does
-                        * see the breaking3 and v2_breaking3 tests.
-                        */
-                       l->breaking_to_requested |= SMB2_LEASE_READ;
-               }
-               d->modified = true;
-               *_l = l;
-               return NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
-       }
-
-       DEBUG(10, ("breaking from %d to %d - expected %d\n",
-                  (int)l->current_state, (int)new_lease_state,
-                  (int)l->breaking_to_requested));
-
-       l->breaking_to_requested = 0;
-       l->breaking_to_required = 0;
-       l->breaking = false;
-
-       d->modified = true;
-
-       return NT_STATUS_OK;
-}
-
 /****************************************************************************
  Adds a delete on close token.
 ****************************************************************************/
@@ -1322,3 +1214,108 @@ struct timespec get_share_mode_write_time(struct share_mode_lock *lck)
        }
        return d->old_write_time;
 }
+
+bool file_has_open_streams(files_struct *fsp)
+{
+       struct share_mode_lock *lock = NULL;
+       struct share_mode_data *d = NULL;
+       uint32_t i;
+
+       lock = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
+       if (lock == NULL) {
+               return false;
+       }
+       d = lock->data;
+
+       for (i = 0; i < d->num_share_modes; i++) {
+               struct share_mode_entry *e = &d->share_modes[i];
+
+               if (share_mode_stale_pid(d, i)) {
+                       continue;
+               }
+
+               if (e->private_options &
+                   NTCREATEX_OPTIONS_PRIVATE_STREAM_BASEOPEN)
+               {
+                       TALLOC_FREE(lock);
+                       return true;
+               }
+       }
+
+       TALLOC_FREE(lock);
+       return false;
+}
+
+/*
+ * Walk share mode entries, looking at every lease only once
+ */
+
+bool share_mode_forall_leases(
+       struct share_mode_lock *lck,
+       bool (*fn)(struct share_mode_lock *lck,
+                  struct share_mode_entry *e,
+                  void *private_data),
+       void *private_data)
+{
+       struct share_mode_data *d = lck->data;
+       uint32_t *leases = NULL;
+       uint32_t num_leases = 0;
+       uint32_t i;
+
+       leases = talloc_array(talloc_tos(), uint32_t, d->num_share_modes);
+       if (leases == NULL) {
+               return false;
+       }
+
+       for (i=0; i<d->num_share_modes; i++) {
+               struct share_mode_entry *e = &d->share_modes[i];
+               uint32_t j;
+               bool ok, stop;
+
+               ok = is_valid_share_mode_entry(e);
+               if (!ok) {
+                       continue;
+               }
+
+               if (e->op_type != LEASE_OPLOCK) {
+                       continue;
+               }
+
+               /*
+                * See if we have already seen "e"'s lease. This is
+                * O(n^2). If we sort "leases", we can get this down
+                * to O(n).
+                */
+
+               for (j=0; j<num_leases; j++) {
+                       uint32_t idx = leases[j];
+                       struct share_mode_entry *l = &d->share_modes[idx];
+
+                       if (smb2_lease_equal(&e->client_guid,
+                                            &e->lease_key,
+                                            &l->client_guid,
+                                            &l->lease_key)) {
+                               break;
+                       }
+               }
+               if (j < num_leases) {
+                       /*
+                        * Don't look at "e"'s lease, we've already
+                        * seen it.
+                        */
+                       continue;
+               }
+
+               stop = fn(lck, e, private_data);
+               if (stop) {
+                       TALLOC_FREE(leases);
+                       return true;
+               }
+
+               leases[num_leases] = i;
+               num_leases += 1;
+       }
+
+       TALLOC_FREE(leases);
+       return true;
+}