2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998 - 2001
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define DBGC_CLASS DBGC_LOCKING
24 #include "lib/util/server_id.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
28 #include "../librpc/gen_ndr/ndr_open_files.h"
31 * helper function used by the kernel oplock backends to post the break message
33 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
35 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
37 /* Put the kernel break info into the message. */
38 push_file_id_24((char *)msg, &fsp->file_id);
39 SIVAL(msg,24,fsp->fh->gen_id);
41 /* Don't need to be root here as we're only ever
42 sending to ourselves. */
44 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
46 msg, MSG_SMB_KERNEL_BREAK_SIZE);
49 /****************************************************************************
50 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
51 disabled (just sets flags).
52 ****************************************************************************/
54 NTSTATUS set_file_oplock(files_struct *fsp)
56 struct smbd_server_connection *sconn = fsp->conn->sconn;
57 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
58 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
61 if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
62 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
63 "don't support them\n"));
64 return NT_STATUS_NOT_SUPPORTED;
67 if ((fsp->oplock_type != NO_OPLOCK) &&
69 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
71 return map_nt_error_from_unix(errno);
74 fsp->sent_oplock_break = NO_BREAK_SENT;
75 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
76 sconn->oplocks.level_II_open++;
77 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
78 sconn->oplocks.exclusive_open++;
81 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
82 "tv_sec = %x, tv_usec = %x\n",
83 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
84 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
85 (int)fsp->open_time.tv_usec ));
90 static void release_fsp_kernel_oplock(files_struct *fsp)
92 struct smbd_server_connection *sconn = fsp->conn->sconn;
93 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
96 if (koplocks == NULL) {
99 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
103 if (fsp->oplock_type == NO_OPLOCK) {
106 if (fsp->oplock_type == LEASE_OPLOCK) {
108 * For leases we don't touch kernel oplocks at all
113 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
116 /****************************************************************************
117 Attempt to release an oplock on a file. Decrements oplock count.
118 ****************************************************************************/
120 static void release_file_oplock(files_struct *fsp)
122 struct smbd_server_connection *sconn = fsp->conn->sconn;
124 release_fsp_kernel_oplock(fsp);
126 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
127 sconn->oplocks.level_II_open--;
128 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
129 sconn->oplocks.exclusive_open--;
132 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
133 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
135 fsp->oplock_type = NO_OPLOCK;
136 fsp->sent_oplock_break = NO_BREAK_SENT;
138 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
139 delete_write_cache(fsp);
141 TALLOC_FREE(fsp->oplock_timeout);
144 /****************************************************************************
145 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
146 ****************************************************************************/
148 static void downgrade_file_oplock(files_struct *fsp)
150 struct smbd_server_connection *sconn = fsp->conn->sconn;
151 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
152 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
155 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
156 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
161 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
163 fsp->oplock_type = LEVEL_II_OPLOCK;
164 sconn->oplocks.exclusive_open--;
165 sconn->oplocks.level_II_open++;
166 fsp->sent_oplock_break = NO_BREAK_SENT;
168 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
169 delete_write_cache(fsp);
171 TALLOC_FREE(fsp->oplock_timeout);
174 uint32_t get_lease_type(const struct share_mode_data *d,
175 const struct share_mode_entry *e)
177 if (e->op_type == LEASE_OPLOCK) {
178 return d->leases[e->lease_idx].current_state;
180 return map_oplock_to_lease_type(e->op_type);
183 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
185 struct share_mode_data *d = lck->data;
186 struct byte_range_lock *br_lck;
187 uint32_t num_read_oplocks = 0;
190 if (fsp_lease_type_is_exclusive(fsp)) {
191 const struct share_mode_entry *e = NULL;
192 uint32_t e_lease_type = 0;
195 * If we're fully exclusive, we don't need a brlock entry
197 remove_stale_share_mode_entries(d);
199 e = find_share_mode_entry(lck, fsp);
201 e_lease_type = get_lease_type(d, e);
204 if (!lease_type_is_exclusive(e_lease_type)) {
205 char *timestr = NULL;
207 timestr = timeval_string(talloc_tos(),
211 NDR_PRINT_DEBUG(share_mode_data, d);
212 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
213 "open_time[%s] lease_type [0x%x] "
214 "oplock_type [0x%x]\n",
216 file_id_string_tos(&fsp->file_id),
217 fsp->fh->gen_id, timestr,
218 e_lease_type, fsp->oplock_type);
220 smb_panic("Found non-exclusive lease");
226 for (i=0; i<d->num_share_modes; i++) {
227 struct share_mode_entry *e = &d->share_modes[i];
228 uint32_t e_lease_type = get_lease_type(d, e);
230 if (e_lease_type & SMB2_LEASE_READ) {
231 num_read_oplocks += 1;
235 br_lck = brl_get_locks_readonly(fsp);
236 if (br_lck == NULL) {
239 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
243 br_lck = brl_get_locks(talloc_tos(), fsp);
244 if (br_lck == NULL) {
247 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
252 /****************************************************************************
253 Remove a file oplock with lock already held. Copes with level II and exclusive.
254 ****************************************************************************/
256 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
260 ret = remove_share_oplock(lck, fsp);
262 DBG_ERR("failed to remove share oplock for "
264 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
265 file_id_string_tos(&fsp->file_id));
267 release_file_oplock(fsp);
269 ret = update_num_read_oplocks(fsp, lck);
271 DBG_ERR("update_num_read_oplocks failed for "
273 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
274 file_id_string_tos(&fsp->file_id));
280 /****************************************************************************
281 Remove a file oplock. Copes with level II and exclusive.
282 Locks then unlocks the share mode lock. Client can decide to go directly
283 to none even if a "break-to-level II" was sent.
284 ****************************************************************************/
286 bool remove_oplock(files_struct *fsp)
289 struct share_mode_lock *lck;
291 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
293 /* Remove the oplock flag from the sharemode. */
294 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
296 DBG_ERR("failed to lock share entry for "
297 "file %s\n", fsp_str_dbg(fsp));
301 ret = remove_oplock_under_lock(fsp, lck);
308 * Deal with a reply when a break-to-level II was sent.
310 bool downgrade_oplock(files_struct *fsp)
313 struct share_mode_lock *lck;
315 DEBUG(10, ("downgrade_oplock called for %s\n",
318 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
320 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
321 "file %s\n", fsp_str_dbg(fsp)));
324 ret = downgrade_share_oplock(lck, fsp);
326 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
327 "for file %s, %s, file_id %s\n",
328 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
329 file_id_string_tos(&fsp->file_id)));
331 downgrade_file_oplock(fsp);
333 ret = update_num_read_oplocks(fsp, lck);
335 DEBUG(0, ("%s: update_num_read_oplocks failed for "
337 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
338 file_id_string_tos(&fsp->file_id)));
345 static void lease_timeout_handler(struct tevent_context *ctx,
346 struct tevent_timer *te,
350 struct fsp_lease *lease =
351 talloc_get_type_abort(private_data,
353 struct files_struct *fsp;
354 struct share_mode_lock *lck;
355 uint16_t old_epoch = lease->lease.lease_epoch;
357 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
358 &lease->lease.lease_key);
361 TALLOC_FREE(lease->timeout);
366 * Paranoia check: There can only be one fsp_lease per lease
369 SMB_ASSERT(fsp->lease == lease);
371 lck = get_existing_share_mode_lock(
372 talloc_tos(), fsp->file_id);
375 TALLOC_FREE(lease->timeout);
379 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
381 if (lease->lease.lease_epoch != old_epoch) {
383 * If the epoch changed we need to wait for
384 * the next timeout to happen.
386 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
392 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
394 * If the epoch changed we need to wait for
395 * the next timeout to happen.
397 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
403 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
405 (void)downgrade_lease(lease->sconn->client->connections,
408 &lease->lease.lease_key,
414 bool fsp_lease_update(struct share_mode_lock *lck,
415 const struct GUID *client_guid,
416 struct fsp_lease *lease)
418 struct share_mode_data *d = lck->data;
420 struct share_mode_lease *l = NULL;
422 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
424 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
425 TALLOC_FREE(lease->timeout);
426 lease->lease.lease_state = SMB2_LEASE_NONE;
427 lease->lease.lease_epoch += 1;
428 lease->lease.lease_flags = 0;
434 DEBUG(10,("%s: refresh lease state\n", __func__));
436 /* Ensure we're in sync with current lease state. */
437 if (lease->lease.lease_epoch != l->epoch) {
438 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
439 TALLOC_FREE(lease->timeout);
441 lease->lease.lease_epoch = l->epoch;
442 lease->lease.lease_state = l->current_state;
445 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
447 if (lease->timeout == NULL) {
448 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
450 DEBUG(10,("%s: setup timeout handler\n", __func__));
452 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
454 lease_timeout_handler,
456 if (lease->timeout == NULL) {
457 DEBUG(0, ("%s: Could not add lease timeout handler\n",
462 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
463 TALLOC_FREE(lease->timeout);
469 struct downgrade_lease_additional_state {
470 struct tevent_immediate *im;
471 struct smbXsrv_connection *xconn;
472 uint32_t break_flags;
473 struct smb2_lease_key lease_key;
479 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
480 struct tevent_immediate *im,
483 struct downgrade_lease_additional_state *state =
484 talloc_get_type_abort(private_data,
485 struct downgrade_lease_additional_state);
486 struct smbXsrv_connection *xconn = state->xconn;
489 status = smbd_smb2_send_lease_break(xconn,
496 if (!NT_STATUS_IS_OK(status)) {
497 smbd_server_connection_terminate(xconn,
503 struct downgrade_lease_fsps_state {
505 struct share_mode_lock *lck;
506 const struct smb2_lease_key *key;
509 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
512 struct downgrade_lease_fsps_state *state =
513 (struct downgrade_lease_fsps_state *)private_data;
515 if (fsp->oplock_type != LEASE_OPLOCK) {
518 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
521 if (!file_id_equal(&fsp->file_id, &state->id)) {
525 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
530 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
531 uint32_t num_file_ids,
532 const struct file_id *ids,
533 const struct smb2_lease_key *key,
534 uint32_t lease_state)
536 struct smbd_server_connection *sconn = xconn->client->sconn;
537 struct share_mode_lock *lck;
538 struct share_mode_data *d = NULL;
539 struct share_mode_lease *l = NULL;
540 const struct file_id id = ids[0];
545 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
546 file_id_string_tos(&id), (unsigned)lease_state));
548 lck = get_existing_share_mode_lock(talloc_tos(), id);
550 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
554 idx = find_share_mode_lease(
555 d, &sconn->client->connections->smb2.client.guid, key);
557 DEBUG(10, ("lease not found\n"));
558 return NT_STATUS_INVALID_PARAMETER;
563 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
564 "but we're not in breaking state\n",
565 l->current_state, lease_state);
567 return NT_STATUS_UNSUCCESSFUL;
571 * Can't upgrade anything: l->breaking_to_requested (and l->current_state)
572 * must be a strict bitwise superset of new_lease_state
574 if ((lease_state & l->breaking_to_requested) != lease_state) {
575 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
576 "- expected %"PRIu32"\n",
577 l->current_state, lease_state,
578 l->breaking_to_requested);
580 return NT_STATUS_REQUEST_NOT_ACCEPTED;
583 if (l->current_state != lease_state) {
584 l->current_state = lease_state;
588 status = NT_STATUS_OK;
592 if ((lease_state & ~l->breaking_to_required) != 0) {
593 struct downgrade_lease_additional_state *state;
595 DBG_INFO("lease state %"PRIu32" not fully broken from "
596 "%"PRIu32" to %"PRIu32"\n",
599 l->breaking_to_required);
601 l->breaking_to_requested = l->breaking_to_required;
603 if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
605 * Here we break in steps, as windows does
606 * see the breaking3 and v2_breaking3 tests.
608 l->breaking_to_requested |= SMB2_LEASE_READ;
611 state = talloc_zero(xconn,
612 struct downgrade_lease_additional_state);
615 return NT_STATUS_NO_MEMORY;
618 state->im = tevent_create_immediate(state);
619 if (state->im == NULL) {
622 return NT_STATUS_NO_MEMORY;
625 state->xconn = xconn;
626 state->lease_key = l->lease_key;
627 state->break_from = l->current_state;
628 state->break_to = l->breaking_to_requested;
629 if (l->lease_version > 1) {
630 state->new_epoch = l->epoch;
633 if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
635 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
638 * This is an async break without
639 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
641 * we need to store NONE state in the
644 l->current_state = 0;
645 l->breaking_to_requested = 0;
646 l->breaking_to_required = 0;
649 lck->data->modified = true;
652 tevent_schedule_immediate(state->im,
653 xconn->client->raw_ev_ctx,
654 downgrade_lease_additional_trigger,
657 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
659 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
660 "expected %"PRIu32"\n",
663 l->breaking_to_requested);
665 l->breaking_to_requested = 0;
666 l->breaking_to_required = 0;
672 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
673 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
676 struct downgrade_lease_fsps_state state = {
677 .id = id, .lck = lck, .key = key,
680 files_forall(sconn, downgrade_lease_fsps, &state);
684 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
685 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
688 * Dynamic share case. Ensure other opens are copies.
689 * This will only be breaking to NONE.
692 for (i = 1; i < num_file_ids; i++) {
693 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
695 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
699 struct downgrade_lease_fsps_state state = {
700 .id = ids[i], .lck = lck, .key = key,
703 files_forall(sconn, downgrade_lease_fsps, &state);
706 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
707 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
715 /****************************************************************************
716 Set up an oplock break message.
717 ****************************************************************************/
719 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
721 static void new_break_message_smb1(files_struct *fsp, int cmd,
722 char result[SMB1_BREAK_MESSAGE_LENGTH])
724 memset(result,'\0',smb_size);
725 srv_set_message(result,8,0,true);
726 SCVAL(result,smb_com,SMBlockingX);
727 SSVAL(result,smb_tid,fsp->conn->cnum);
728 SSVAL(result,smb_pid,0xFFFF);
729 SSVAL(result,smb_uid,0);
730 SSVAL(result,smb_mid,0xFFFF);
731 SCVAL(result,smb_vwv0,0xFF);
732 SSVAL(result,smb_vwv2,fsp->fnum);
733 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
734 SCVAL(result,smb_vwv3+1,cmd);
737 /****************************************************************************
738 Function to do the waiting before sending a local break.
739 ****************************************************************************/
741 static void wait_before_sending_break(void)
743 long wait_time = (long)lp_oplock_break_wait_time();
746 smb_msleep(wait_time);
750 /****************************************************************************
751 Ensure that we have a valid oplock.
752 ****************************************************************************/
754 static files_struct *initial_break_processing(
755 struct smbd_server_connection *sconn, struct file_id id,
756 unsigned long file_id)
758 files_struct *fsp = NULL;
760 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
761 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
762 file_id_string_tos(&id), (int)file_id,
763 sconn->oplocks.exclusive_open,
764 sconn->oplocks.level_II_open));
767 * We need to search the file open table for the
768 * entry containing this dev and inode, and ensure
769 * we have an oplock on it.
772 fsp = file_find_dif(sconn, id, file_id);
775 /* The file could have been closed in the meantime - return success. */
776 DEBUG(3, ("initial_break_processing: cannot find open file "
777 "with file_id %s gen_id = %lu, allowing break to "
778 "succeed.\n", file_id_string_tos(&id), file_id));
782 /* Ensure we have an oplock on the file */
785 * There is a potential race condition in that an oplock could
786 * have been broken due to another udp request, and yet there are
787 * still oplock break messages being sent in the udp message
788 * queue for this file. So return true if we don't have an oplock,
789 * as we may have just freed it.
792 if(fsp->oplock_type == NO_OPLOCK) {
793 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
794 "gen_id = %lu) has no oplock. Allowing break to "
795 "succeed regardless.\n", fsp_str_dbg(fsp),
796 file_id_string_tos(&id), fsp->fh->gen_id));
803 static void oplock_timeout_handler(struct tevent_context *ctx,
804 struct tevent_timer *te,
808 files_struct *fsp = (files_struct *)private_data;
810 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
812 /* Remove the timed event handler. */
813 TALLOC_FREE(fsp->oplock_timeout);
814 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
819 /*******************************************************************
820 Add a timeout handler waiting for the client reply.
821 *******************************************************************/
823 static void add_oplock_timeout_handler(files_struct *fsp)
825 if (fsp->oplock_timeout != NULL) {
826 DEBUG(0, ("Logic problem -- have an oplock event hanging "
830 fsp->oplock_timeout =
831 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
832 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
833 oplock_timeout_handler, fsp);
835 if (fsp->oplock_timeout == NULL) {
836 DEBUG(0, ("Could not add oplock timeout handler\n"));
840 static void send_break_message_smb1(files_struct *fsp, int level)
842 struct smbXsrv_connection *xconn = NULL;
843 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
846 * For SMB1 we only have one connection
848 xconn = fsp->conn->sconn->client->connections;
850 new_break_message_smb1(fsp, level, break_msg);
853 if (!srv_send_smb(xconn,
855 IS_CONN_ENCRYPTED(fsp->conn),
857 exit_server_cleanly("send_break_message_smb1: "
858 "srv_send_smb failed.");
862 /*******************************************************************
863 This handles the generic oplock break message from another smbd.
864 *******************************************************************/
866 static void process_oplock_break_message(struct messaging_context *msg_ctx,
869 struct server_id src,
873 struct share_mode_entry msg;
876 struct smbd_server_connection *sconn =
877 talloc_get_type_abort(private_data,
878 struct smbd_server_connection);
879 struct server_id self = messaging_server_id(sconn->msg_ctx);
880 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
883 bool break_needed = true;
884 struct server_id_buf tmp;
886 if (data->data == NULL) {
887 DEBUG(0, ("Got NULL buffer\n"));
891 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
892 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
896 /* De-linearize incoming message. */
897 message_to_share_mode_entry(&id, &msg, (char *)data->data);
898 break_to = msg.op_type;
900 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
901 (unsigned)break_to, server_id_str_buf(src, &tmp),
902 file_id_string_tos(&id),
903 (unsigned long long)msg.share_file_id));
905 fsp = initial_break_processing(sconn, id, msg.share_file_id);
908 /* We hit a race here. Break messages are sent, and before we
909 * get to process this message, we have closed the file. */
910 DEBUG(3, ("Did not find fsp\n"));
914 break_from = fsp_lease_type(fsp);
916 if (fsp->oplock_type != LEASE_OPLOCK) {
917 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
919 * Nothing to do anymore
921 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
922 fsp->sent_oplock_break));
927 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
928 DEBUG(10, ("client_caps without level2 oplocks\n"));
929 break_to &= ~SMB2_LEASE_READ;
932 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
935 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
936 break_to &= ~SMB2_LEASE_READ;
939 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
940 DEBUG(10, ("no level2 oplocks by config\n"));
941 break_to &= ~SMB2_LEASE_READ;
944 if (fsp->oplock_type == LEASE_OPLOCK) {
945 struct share_mode_lock *lck;
948 lck = get_existing_share_mode_lock(
949 talloc_tos(), fsp->file_id);
952 * We hit a race here. Break messages are sent, and
953 * before we get to process this message, we have closed
956 DEBUG(3, ("Did not find share_mode\n"));
960 idx = find_share_mode_lease(
962 fsp_client_guid(fsp),
963 &fsp->lease->lease.lease_key);
965 struct share_mode_lease *l;
966 l = &lck->data->leases[idx];
968 break_from = l->current_state;
969 break_to &= l->current_state;
972 break_to &= l->breaking_to_required;
973 if (l->breaking_to_required != break_to) {
975 * Note we don't increment the epoch
976 * here, which might be a bug in
979 l->breaking_to_required = break_to;
980 lck->data->modified = true;
982 break_needed = false;
983 } else if (l->current_state == break_to) {
984 break_needed = false;
985 } else if (l->current_state == SMB2_LEASE_READ) {
986 l->current_state = SMB2_LEASE_NONE;
987 /* Need to increment the epoch */
989 lck->data->modified = true;
992 l->breaking_to_required = break_to;
993 l->breaking_to_requested = break_to;
994 /* Need to increment the epoch */
996 lck->data->modified = true;
999 /* Ensure we're in sync with current lease state. */
1000 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
1006 if (!break_needed) {
1007 DEBUG(10,("%s: skip break\n", __func__));
1011 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1012 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1013 file_id_string_tos(&fsp->file_id),
1018 DEBUG(10, ("break_from=%u, break_to=%u\n",
1019 (unsigned)break_from, (unsigned)break_to));
1021 if ((break_from == break_to) && !break_needed) {
1022 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1024 file_id_string_tos(&fsp->file_id),
1029 /* Need to wait before sending a break
1030 message if we sent ourselves this message. */
1031 if (serverid_equal(&self, &src)) {
1032 wait_before_sending_break();
1035 if (sconn->using_smb2) {
1036 send_break_message_smb2(fsp, break_from, break_to);
1038 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1039 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1042 if ((break_from == SMB2_LEASE_READ) &&
1043 (break_to == SMB2_LEASE_NONE)) {
1045 * This is an async break without a reply and thus no timeout
1047 * leases are handled above.
1049 if (fsp->oplock_type != LEASE_OPLOCK) {
1054 if (fsp->oplock_type == LEASE_OPLOCK) {
1058 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1059 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1061 add_oplock_timeout_handler(fsp);
1064 /*******************************************************************
1065 This handles the kernel oplock break message.
1066 *******************************************************************/
1068 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1071 struct server_id src,
1075 unsigned long file_id;
1077 struct smbd_server_connection *sconn =
1078 talloc_get_type_abort(private_data,
1079 struct smbd_server_connection);
1080 struct server_id_buf tmp;
1082 if (data->data == NULL) {
1083 DEBUG(0, ("Got NULL buffer\n"));
1087 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1088 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1092 /* Pull the data from the message. */
1093 pull_file_id_24((char *)data->data, &id);
1094 file_id = (unsigned long)IVAL(data->data, 24);
1096 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1097 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1098 (unsigned int)file_id));
1100 fsp = initial_break_processing(sconn, id, file_id);
1103 DEBUG(3, ("Got a kernel oplock break message for a file "
1104 "I don't know about\n"));
1108 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1109 /* This is ok, kernel oplocks come in completely async */
1110 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1115 if (sconn->using_smb2) {
1116 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1118 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1121 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1123 add_oplock_timeout_handler(fsp);
1126 static bool file_has_read_oplocks(struct files_struct *fsp)
1128 struct byte_range_lock *brl;
1129 uint32_t num_read_oplocks = 0;
1131 brl = brl_get_locks_readonly(fsp);
1136 num_read_oplocks = brl_num_read_oplocks(brl);
1138 DBG_DEBUG("num_read_oplocks = %"PRIu32"\n", num_read_oplocks);
1140 return (num_read_oplocks != 0);
1143 struct break_to_none_state {
1144 struct smbd_server_connection *sconn;
1146 struct smb2_lease_key lease_key;
1147 struct GUID client_guid;
1149 static void do_break_to_none(struct tevent_context *ctx,
1150 struct tevent_immediate *im,
1151 void *private_data);
1153 /****************************************************************************
1154 This function is called on any file modification or lock request. If a file
1155 is level 2 oplocked then it must tell all other level 2 holders to break to
1157 ****************************************************************************/
1159 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1160 enum level2_contention_type type)
1162 struct smbd_server_connection *sconn = fsp->conn->sconn;
1163 struct tevent_immediate *im;
1164 struct break_to_none_state *state;
1165 bool has_read_oplocks;
1168 * If this file is level II oplocked then we need
1169 * to grab the shared memory lock and inform all
1170 * other files with a level II lock that they need
1171 * to flush their read caches. We keep the lock over
1172 * the shared memory area whilst doing this.
1175 if (fsp_lease_type_is_exclusive(fsp)) {
1177 * There can't be any level2 oplocks, we're alone.
1182 has_read_oplocks = file_has_read_oplocks(fsp);
1183 if (!has_read_oplocks) {
1184 DEBUG(10, ("No read oplocks around\n"));
1189 * When we get here we might have a brlock entry locked. Also
1190 * locking the share mode entry would violate the locking
1191 * order. Breaking level2 oplocks to none is asynchronous
1192 * anyway, so we postpone this into an immediate event.
1195 state = talloc(sconn, struct break_to_none_state);
1196 if (state == NULL) {
1197 DEBUG(1, ("talloc failed\n"));
1200 *state = (struct break_to_none_state) {
1201 .sconn = sconn, .id = fsp->file_id,
1204 if (fsp->oplock_type == LEASE_OPLOCK) {
1205 state->client_guid = *fsp_client_guid(fsp);
1206 state->lease_key = fsp->lease->lease.lease_key;
1207 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1208 state->lease_key.data[0],
1209 state->lease_key.data[1]));
1212 im = tevent_create_immediate(state);
1214 DEBUG(1, ("tevent_create_immediate failed\n"));
1218 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1221 static void send_break_to_none(struct messaging_context *msg_ctx,
1222 const struct file_id *id,
1223 const struct share_mode_entry *e)
1225 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1227 share_mode_entry_to_message(msg, id, e);
1228 /* Overload entry->op_type */
1229 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1231 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1232 (uint8_t *)msg, sizeof(msg));
1235 static void do_break_to_none(struct tevent_context *ctx,
1236 struct tevent_immediate *im,
1239 struct break_to_none_state *state = talloc_get_type_abort(
1240 private_data, struct break_to_none_state);
1242 struct share_mode_lock *lck;
1243 struct share_mode_data *d;
1245 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1247 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1248 __func__, file_id_string_tos(&state->id)));
1254 * Walk leases and oplocks separately: We have to send one break per
1255 * lease. If we have multiple share_mode_entry having a common lease,
1256 * we would break the lease twice if we don't walk the leases list
1260 for (i=0; i<d->num_leases; i++) {
1261 struct share_mode_lease *l = &d->leases[i];
1262 struct share_mode_entry *e = NULL;
1265 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1268 if (smb2_lease_equal(&state->client_guid,
1272 DEBUG(10, ("Don't break our own lease\n"));
1276 for (j=0; j<d->num_share_modes; j++) {
1277 e = &d->share_modes[j];
1279 if (!is_valid_share_mode_entry(e)) {
1282 if (e->lease_idx == i) {
1286 if (j == d->num_share_modes) {
1287 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1292 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1293 "%"PRIu32"\n", i, j));
1295 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1298 for(i = 0; i < d->num_share_modes; i++) {
1299 struct share_mode_entry *e = &d->share_modes[i];
1301 if (!is_valid_share_mode_entry(e)) {
1304 if (e->op_type == LEASE_OPLOCK) {
1306 * Took care of those in the loop above
1312 * As there could have been multiple writes waiting at the
1313 * lock_share_entry gate we may not be the first to
1314 * enter. Hence the state of the op_types in the share mode
1315 * entries may be partly NO_OPLOCK and partly LEVEL_II
1316 * oplock. It will do no harm to re-send break messages to
1317 * those smbd's that are still waiting their turn to remove
1318 * their LEVEL_II state, and also no harm to ignore existing
1319 * NO_OPLOCK states. JRA.
1322 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1325 if (e->op_type == NO_OPLOCK) {
1330 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1331 DEBUG(0,("%s: PANIC. "
1332 "share mode entry %d is an exclusive "
1333 "oplock !\n", __func__, i ));
1338 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1341 /* We let the message receivers handle removing the oplock state
1342 in the share mode lock db. */
1350 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1351 enum level2_contention_type type)
1353 contend_level2_oplocks_begin_default(fsp, type);
1356 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1357 enum level2_contention_type type)
1362 /****************************************************************************
1363 Linearize a share mode entry struct to an internal oplock break message.
1364 ****************************************************************************/
1366 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1367 const struct share_mode_entry *e)
1369 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1370 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1371 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1372 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1373 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1374 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1375 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1376 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1378 * "id" used to be part of share_mode_entry, thus the strange
1379 * place to put this. Feel free to move somewhere else :-)
1381 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1382 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1383 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1384 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1385 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1386 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1389 /****************************************************************************
1390 De-linearize an internal oplock break message to a share mode entry struct.
1391 ****************************************************************************/
1393 void message_to_share_mode_entry(struct file_id *id,
1394 struct share_mode_entry *e,
1397 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1398 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1399 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1400 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1401 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1402 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1403 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1404 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1406 * "id" used to be part of share_mode_entry, thus the strange
1407 * place to put this. Feel free to move somewhere else :-)
1409 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1410 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1411 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1412 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1413 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1414 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1417 /****************************************************************************
1418 Setup oplocks for this process.
1419 ****************************************************************************/
1421 bool init_oplocks(struct smbd_server_connection *sconn)
1423 DEBUG(3,("init_oplocks: initializing messages.\n"));
1425 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1426 process_oplock_break_message);
1427 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1428 process_kernel_oplock_break);
1432 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1434 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1436 /* only initialize once */
1437 if (koplocks == NULL) {
1438 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1439 koplocks = linux_init_kernel_oplocks(sconn);
1441 sconn->oplocks.kernel_ops = koplocks;