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 "locking/leases_db.h"
29 #include "../librpc/gen_ndr/ndr_open_files.h"
32 * helper function used by the kernel oplock backends to post the break message
34 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
36 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
38 /* Put the kernel break info into the message. */
39 push_file_id_24((char *)msg, &fsp->file_id);
40 SIVAL(msg,24,fsp->fh->gen_id);
42 /* Don't need to be root here as we're only ever
43 sending to ourselves. */
45 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
47 msg, MSG_SMB_KERNEL_BREAK_SIZE);
50 /****************************************************************************
51 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
52 disabled (just sets flags).
53 ****************************************************************************/
55 NTSTATUS set_file_oplock(files_struct *fsp)
57 struct smbd_server_connection *sconn = fsp->conn->sconn;
58 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
59 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
62 if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
64 "don't support them\n"));
65 return NT_STATUS_NOT_SUPPORTED;
68 if ((fsp->oplock_type != NO_OPLOCK) &&
70 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
72 return map_nt_error_from_unix(errno);
75 fsp->sent_oplock_break = NO_BREAK_SENT;
76 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
77 sconn->oplocks.level_II_open++;
78 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
79 sconn->oplocks.exclusive_open++;
82 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
83 "tv_sec = %x, tv_usec = %x\n",
84 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
85 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
86 (int)fsp->open_time.tv_usec ));
91 static void release_fsp_kernel_oplock(files_struct *fsp)
93 struct smbd_server_connection *sconn = fsp->conn->sconn;
94 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
97 if (koplocks == NULL) {
100 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
104 if (fsp->oplock_type == NO_OPLOCK) {
107 if (fsp->oplock_type == LEASE_OPLOCK) {
109 * For leases we don't touch kernel oplocks at all
114 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
117 /****************************************************************************
118 Attempt to release an oplock on a file. Decrements oplock count.
119 ****************************************************************************/
121 static void release_file_oplock(files_struct *fsp)
123 struct smbd_server_connection *sconn = fsp->conn->sconn;
125 release_fsp_kernel_oplock(fsp);
127 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
128 sconn->oplocks.level_II_open--;
129 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
130 sconn->oplocks.exclusive_open--;
133 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
134 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
136 fsp->oplock_type = NO_OPLOCK;
137 fsp->sent_oplock_break = NO_BREAK_SENT;
139 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
140 delete_write_cache(fsp);
142 TALLOC_FREE(fsp->oplock_timeout);
145 /****************************************************************************
146 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
147 ****************************************************************************/
149 static void downgrade_file_oplock(files_struct *fsp)
151 struct smbd_server_connection *sconn = fsp->conn->sconn;
152 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
153 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
156 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
157 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
162 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
164 fsp->oplock_type = LEVEL_II_OPLOCK;
165 sconn->oplocks.exclusive_open--;
166 sconn->oplocks.level_II_open++;
167 fsp->sent_oplock_break = NO_BREAK_SENT;
169 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
170 delete_write_cache(fsp);
172 TALLOC_FREE(fsp->oplock_timeout);
175 uint32_t get_lease_type(const struct share_mode_data *d,
176 const struct share_mode_entry *e)
178 if (e->op_type == LEASE_OPLOCK) {
180 uint32_t current_state;
182 status = leases_db_get(
188 NULL, /* breaking_to_requested */
189 NULL, /* breaking_to_required */
190 NULL, /* lease_version */
192 SMB_ASSERT(NT_STATUS_IS_OK(status));
193 return current_state;
195 return map_oplock_to_lease_type(e->op_type);
198 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
200 struct share_mode_data *d = lck->data;
201 struct byte_range_lock *br_lck;
202 uint32_t num_read_oplocks = 0;
205 if (fsp_lease_type_is_exclusive(fsp)) {
206 const struct share_mode_entry *e = NULL;
207 uint32_t e_lease_type = 0;
210 * If we're fully exclusive, we don't need a brlock entry
212 remove_stale_share_mode_entries(d);
214 e = find_share_mode_entry(lck, fsp);
216 e_lease_type = get_lease_type(d, e);
219 if (!lease_type_is_exclusive(e_lease_type)) {
220 char *timestr = NULL;
222 timestr = timeval_string(talloc_tos(),
226 NDR_PRINT_DEBUG(share_mode_data, d);
227 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
228 "open_time[%s] lease_type [0x%x] "
229 "oplock_type [0x%x]\n",
231 file_id_string_tos(&fsp->file_id),
232 fsp->fh->gen_id, timestr,
233 e_lease_type, fsp->oplock_type);
235 smb_panic("Found non-exclusive lease");
241 for (i=0; i<d->num_share_modes; i++) {
242 struct share_mode_entry *e = &d->share_modes[i];
243 uint32_t e_lease_type = get_lease_type(d, e);
245 if (e_lease_type & SMB2_LEASE_READ) {
246 num_read_oplocks += 1;
250 br_lck = brl_get_locks_readonly(fsp);
251 if (br_lck == NULL) {
254 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
258 br_lck = brl_get_locks(talloc_tos(), fsp);
259 if (br_lck == NULL) {
262 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
267 /****************************************************************************
268 Remove a file oplock with lock already held. Copes with level II and exclusive.
269 ****************************************************************************/
271 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
275 ret = remove_share_oplock(lck, fsp);
277 DBG_ERR("failed to remove share oplock for "
279 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
280 file_id_string_tos(&fsp->file_id));
282 release_file_oplock(fsp);
284 ret = update_num_read_oplocks(fsp, lck);
286 DBG_ERR("update_num_read_oplocks failed for "
288 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
289 file_id_string_tos(&fsp->file_id));
295 /****************************************************************************
296 Remove a file oplock. Copes with level II and exclusive.
297 Locks then unlocks the share mode lock. Client can decide to go directly
298 to none even if a "break-to-level II" was sent.
299 ****************************************************************************/
301 bool remove_oplock(files_struct *fsp)
304 struct share_mode_lock *lck;
306 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
308 /* Remove the oplock flag from the sharemode. */
309 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
311 DBG_ERR("failed to lock share entry for "
312 "file %s\n", fsp_str_dbg(fsp));
316 ret = remove_oplock_under_lock(fsp, lck);
323 * Deal with a reply when a break-to-level II was sent.
325 bool downgrade_oplock(files_struct *fsp)
328 struct share_mode_lock *lck;
330 DEBUG(10, ("downgrade_oplock called for %s\n",
333 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
335 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
336 "file %s\n", fsp_str_dbg(fsp)));
339 ret = downgrade_share_oplock(lck, fsp);
341 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
342 "for file %s, %s, file_id %s\n",
343 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
344 file_id_string_tos(&fsp->file_id)));
346 downgrade_file_oplock(fsp);
348 ret = update_num_read_oplocks(fsp, lck);
350 DEBUG(0, ("%s: update_num_read_oplocks failed for "
352 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
353 file_id_string_tos(&fsp->file_id)));
360 static void lease_timeout_handler(struct tevent_context *ctx,
361 struct tevent_timer *te,
365 struct fsp_lease *lease =
366 talloc_get_type_abort(private_data,
368 struct files_struct *fsp;
369 struct share_mode_lock *lck;
370 uint16_t old_epoch = lease->lease.lease_epoch;
372 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
373 &lease->lease.lease_key);
376 TALLOC_FREE(lease->timeout);
381 * Paranoia check: There can only be one fsp_lease per lease
384 SMB_ASSERT(fsp->lease == lease);
386 lck = get_existing_share_mode_lock(
387 talloc_tos(), fsp->file_id);
390 TALLOC_FREE(lease->timeout);
394 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
396 if (lease->lease.lease_epoch != old_epoch) {
398 * If the epoch changed we need to wait for
399 * the next timeout to happen.
401 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
407 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
409 * If the epoch changed we need to wait for
410 * the next timeout to happen.
412 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
418 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
420 (void)downgrade_lease(lease->sconn->client->connections,
423 &lease->lease.lease_key,
429 bool fsp_lease_update(struct share_mode_lock *lck,
430 const struct GUID *client_guid,
431 struct fsp_lease *lease)
433 uint32_t current_state;
435 uint16_t lease_version, epoch;
438 status = leases_db_get(client_guid,
439 &lease->lease.lease_key,
443 NULL, /* breaking_to_requested */
444 NULL, /* breaking_to_required */
447 if (!NT_STATUS_IS_OK(status)) {
448 DBG_WARNING("Could not find lease entry: %s\n",
450 TALLOC_FREE(lease->timeout);
451 lease->lease.lease_state = SMB2_LEASE_NONE;
452 lease->lease.lease_epoch += 1;
453 lease->lease.lease_flags = 0;
457 DEBUG(10,("%s: refresh lease state\n", __func__));
459 /* Ensure we're in sync with current lease state. */
460 if (lease->lease.lease_epoch != epoch) {
461 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
462 TALLOC_FREE(lease->timeout);
464 lease->lease.lease_epoch = epoch;
465 lease->lease.lease_state = current_state;
468 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
470 if (lease->timeout == NULL) {
471 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
473 DEBUG(10,("%s: setup timeout handler\n", __func__));
475 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
477 lease_timeout_handler,
479 if (lease->timeout == NULL) {
480 DEBUG(0, ("%s: Could not add lease timeout handler\n",
485 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
486 TALLOC_FREE(lease->timeout);
492 struct downgrade_lease_additional_state {
493 struct tevent_immediate *im;
494 struct smbXsrv_connection *xconn;
495 uint32_t break_flags;
496 struct smb2_lease_key lease_key;
502 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
503 struct tevent_immediate *im,
506 struct downgrade_lease_additional_state *state =
507 talloc_get_type_abort(private_data,
508 struct downgrade_lease_additional_state);
509 struct smbXsrv_connection *xconn = state->xconn;
512 status = smbd_smb2_send_lease_break(xconn,
519 if (!NT_STATUS_IS_OK(status)) {
520 smbd_server_connection_terminate(xconn,
526 struct downgrade_lease_fsps_state {
528 struct share_mode_lock *lck;
529 const struct smb2_lease_key *key;
532 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
535 struct downgrade_lease_fsps_state *state =
536 (struct downgrade_lease_fsps_state *)private_data;
538 if (fsp->oplock_type != LEASE_OPLOCK) {
541 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
544 if (!file_id_equal(&fsp->file_id, &state->id)) {
548 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
553 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
554 uint32_t num_file_ids,
555 const struct file_id *ids,
556 const struct smb2_lease_key *key,
557 uint32_t lease_state)
559 struct smbd_server_connection *sconn = xconn->client->sconn;
560 struct share_mode_lock *lck;
561 struct share_mode_data *d = NULL;
562 struct share_mode_lease *l = NULL;
563 const struct file_id id = ids[0];
568 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
569 file_id_string_tos(&id), (unsigned)lease_state));
571 lck = get_existing_share_mode_lock(talloc_tos(), id);
573 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
577 idx = find_share_mode_lease(
578 d, &sconn->client->connections->smb2.client.guid, key);
580 DEBUG(10, ("lease not found\n"));
581 return NT_STATUS_INVALID_PARAMETER;
586 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
587 "but we're not in breaking state\n",
588 l->current_state, lease_state);
590 return NT_STATUS_UNSUCCESSFUL;
594 * Can't upgrade anything: l->breaking_to_requested (and l->current_state)
595 * must be a strict bitwise superset of new_lease_state
597 if ((lease_state & l->breaking_to_requested) != lease_state) {
598 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
599 "- expected %"PRIu32"\n",
600 l->current_state, lease_state,
601 l->breaking_to_requested);
603 return NT_STATUS_REQUEST_NOT_ACCEPTED;
606 if (l->current_state != lease_state) {
607 l->current_state = lease_state;
611 status = NT_STATUS_OK;
615 if ((lease_state & ~l->breaking_to_required) != 0) {
616 struct downgrade_lease_additional_state *state;
618 DBG_INFO("lease state %"PRIu32" not fully broken from "
619 "%"PRIu32" to %"PRIu32"\n",
622 l->breaking_to_required);
624 l->breaking_to_requested = l->breaking_to_required;
626 if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
628 * Here we break in steps, as windows does
629 * see the breaking3 and v2_breaking3 tests.
631 l->breaking_to_requested |= SMB2_LEASE_READ;
634 state = talloc_zero(xconn,
635 struct downgrade_lease_additional_state);
638 return NT_STATUS_NO_MEMORY;
641 state->im = tevent_create_immediate(state);
642 if (state->im == NULL) {
645 return NT_STATUS_NO_MEMORY;
648 state->xconn = xconn;
649 state->lease_key = l->lease_key;
650 state->break_from = l->current_state;
651 state->break_to = l->breaking_to_requested;
652 if (l->lease_version > 1) {
653 state->new_epoch = l->epoch;
656 if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
658 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
661 * This is an async break without
662 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
664 * we need to store NONE state in the
667 l->current_state = 0;
668 l->breaking_to_requested = 0;
669 l->breaking_to_required = 0;
672 lck->data->modified = true;
677 set_status = leases_db_set(
678 &sconn->client->connections->
683 l->breaking_to_requested,
684 l->breaking_to_required,
688 if (!NT_STATUS_IS_OK(set_status)) {
689 DBG_DEBUG("leases_db_set failed: %s\n",
690 nt_errstr(set_status));
696 tevent_schedule_immediate(state->im,
697 xconn->client->raw_ev_ctx,
698 downgrade_lease_additional_trigger,
701 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
703 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
704 "expected %"PRIu32"\n",
707 l->breaking_to_requested);
709 l->breaking_to_requested = 0;
710 l->breaking_to_required = 0;
719 set_status = leases_db_set(
720 &sconn->client->connections->smb2.client.guid,
724 l->breaking_to_requested,
725 l->breaking_to_required,
729 if (!NT_STATUS_IS_OK(set_status)) {
730 DBG_DEBUG("leases_db_set failed: %s\n",
731 nt_errstr(set_status));
736 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
737 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
740 struct downgrade_lease_fsps_state state = {
741 .id = id, .lck = lck, .key = key,
744 files_forall(sconn, downgrade_lease_fsps, &state);
748 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
749 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
752 * Dynamic share case. Ensure other opens are copies.
753 * This will only be breaking to NONE.
756 for (i = 1; i < num_file_ids; i++) {
757 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
759 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
763 struct downgrade_lease_fsps_state state = {
764 .id = ids[i], .lck = lck, .key = key,
767 files_forall(sconn, downgrade_lease_fsps, &state);
770 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
771 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
779 /****************************************************************************
780 Set up an oplock break message.
781 ****************************************************************************/
783 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
785 static void new_break_message_smb1(files_struct *fsp, int cmd,
786 char result[SMB1_BREAK_MESSAGE_LENGTH])
788 memset(result,'\0',smb_size);
789 srv_set_message(result,8,0,true);
790 SCVAL(result,smb_com,SMBlockingX);
791 SSVAL(result,smb_tid,fsp->conn->cnum);
792 SSVAL(result,smb_pid,0xFFFF);
793 SSVAL(result,smb_uid,0);
794 SSVAL(result,smb_mid,0xFFFF);
795 SCVAL(result,smb_vwv0,0xFF);
796 SSVAL(result,smb_vwv2,fsp->fnum);
797 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
798 SCVAL(result,smb_vwv3+1,cmd);
801 /****************************************************************************
802 Function to do the waiting before sending a local break.
803 ****************************************************************************/
805 static void wait_before_sending_break(void)
807 long wait_time = (long)lp_oplock_break_wait_time();
810 smb_msleep(wait_time);
814 /****************************************************************************
815 Ensure that we have a valid oplock.
816 ****************************************************************************/
818 static files_struct *initial_break_processing(
819 struct smbd_server_connection *sconn, struct file_id id,
820 unsigned long file_id)
822 files_struct *fsp = NULL;
824 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
825 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
826 file_id_string_tos(&id), (int)file_id,
827 sconn->oplocks.exclusive_open,
828 sconn->oplocks.level_II_open));
831 * We need to search the file open table for the
832 * entry containing this dev and inode, and ensure
833 * we have an oplock on it.
836 fsp = file_find_dif(sconn, id, file_id);
839 /* The file could have been closed in the meantime - return success. */
840 DEBUG(3, ("initial_break_processing: cannot find open file "
841 "with file_id %s gen_id = %lu, allowing break to "
842 "succeed.\n", file_id_string_tos(&id), file_id));
846 /* Ensure we have an oplock on the file */
849 * There is a potential race condition in that an oplock could
850 * have been broken due to another udp request, and yet there are
851 * still oplock break messages being sent in the udp message
852 * queue for this file. So return true if we don't have an oplock,
853 * as we may have just freed it.
856 if(fsp->oplock_type == NO_OPLOCK) {
857 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
858 "gen_id = %lu) has no oplock. Allowing break to "
859 "succeed regardless.\n", fsp_str_dbg(fsp),
860 file_id_string_tos(&id), fsp->fh->gen_id));
867 static void oplock_timeout_handler(struct tevent_context *ctx,
868 struct tevent_timer *te,
872 files_struct *fsp = (files_struct *)private_data;
874 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
876 /* Remove the timed event handler. */
877 TALLOC_FREE(fsp->oplock_timeout);
878 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
883 /*******************************************************************
884 Add a timeout handler waiting for the client reply.
885 *******************************************************************/
887 static void add_oplock_timeout_handler(files_struct *fsp)
889 if (fsp->oplock_timeout != NULL) {
890 DEBUG(0, ("Logic problem -- have an oplock event hanging "
894 fsp->oplock_timeout =
895 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
896 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
897 oplock_timeout_handler, fsp);
899 if (fsp->oplock_timeout == NULL) {
900 DEBUG(0, ("Could not add oplock timeout handler\n"));
904 static void send_break_message_smb1(files_struct *fsp, int level)
906 struct smbXsrv_connection *xconn = NULL;
907 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
910 * For SMB1 we only have one connection
912 xconn = fsp->conn->sconn->client->connections;
914 new_break_message_smb1(fsp, level, break_msg);
917 if (!srv_send_smb(xconn,
919 IS_CONN_ENCRYPTED(fsp->conn),
921 exit_server_cleanly("send_break_message_smb1: "
922 "srv_send_smb failed.");
926 /*******************************************************************
927 This handles the generic oplock break message from another smbd.
928 *******************************************************************/
930 static void process_oplock_break_message(struct messaging_context *msg_ctx,
933 struct server_id src,
937 struct share_mode_entry msg;
940 struct smbd_server_connection *sconn =
941 talloc_get_type_abort(private_data,
942 struct smbd_server_connection);
943 struct server_id self = messaging_server_id(sconn->msg_ctx);
944 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
947 bool break_needed = true;
948 struct server_id_buf tmp;
950 if (data->data == NULL) {
951 DEBUG(0, ("Got NULL buffer\n"));
955 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
956 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
960 /* De-linearize incoming message. */
961 message_to_share_mode_entry(&id, &msg, (char *)data->data);
962 break_to = msg.op_type;
964 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
965 (unsigned)break_to, server_id_str_buf(src, &tmp),
966 file_id_string_tos(&id),
967 (unsigned long long)msg.share_file_id));
969 fsp = initial_break_processing(sconn, id, msg.share_file_id);
972 /* We hit a race here. Break messages are sent, and before we
973 * get to process this message, we have closed the file. */
974 DEBUG(3, ("Did not find fsp\n"));
978 break_from = fsp_lease_type(fsp);
980 if (fsp->oplock_type != LEASE_OPLOCK) {
981 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
983 * Nothing to do anymore
985 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
986 fsp->sent_oplock_break));
991 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
992 DEBUG(10, ("client_caps without level2 oplocks\n"));
993 break_to &= ~SMB2_LEASE_READ;
996 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
999 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
1000 break_to &= ~SMB2_LEASE_READ;
1003 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
1004 DEBUG(10, ("no level2 oplocks by config\n"));
1005 break_to &= ~SMB2_LEASE_READ;
1008 if (fsp->oplock_type == LEASE_OPLOCK) {
1009 struct share_mode_lock *lck;
1012 lck = get_existing_share_mode_lock(
1013 talloc_tos(), fsp->file_id);
1016 * We hit a race here. Break messages are sent, and
1017 * before we get to process this message, we have closed
1020 DEBUG(3, ("Did not find share_mode\n"));
1024 idx = find_share_mode_lease(
1026 fsp_client_guid(fsp),
1027 &fsp->lease->lease.lease_key);
1029 struct share_mode_lease *l;
1030 l = &lck->data->leases[idx];
1032 break_from = l->current_state;
1033 break_to &= l->current_state;
1036 break_to &= l->breaking_to_required;
1037 if (l->breaking_to_required != break_to) {
1039 * Note we don't increment the epoch
1040 * here, which might be a bug in
1043 l->breaking_to_required = break_to;
1044 lck->data->modified = true;
1046 break_needed = false;
1047 } else if (l->current_state == break_to) {
1048 break_needed = false;
1049 } else if (l->current_state == SMB2_LEASE_READ) {
1050 l->current_state = SMB2_LEASE_NONE;
1051 /* Need to increment the epoch */
1053 lck->data->modified = true;
1056 l->breaking_to_required = break_to;
1057 l->breaking_to_requested = break_to;
1058 /* Need to increment the epoch */
1060 lck->data->modified = true;
1064 NTSTATUS set_status;
1066 set_status = leases_db_set(
1067 &sconn->client->connections->
1069 &fsp->lease->lease.lease_key,
1072 l->breaking_to_requested,
1073 l->breaking_to_required,
1077 if (!NT_STATUS_IS_OK(set_status)) {
1078 DBG_DEBUG("leases_db_set failed: %s\n",
1079 nt_errstr(set_status));
1084 /* Ensure we're in sync with current lease state. */
1085 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
1091 if (!break_needed) {
1092 DEBUG(10,("%s: skip break\n", __func__));
1096 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1097 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1098 file_id_string_tos(&fsp->file_id),
1103 DEBUG(10, ("break_from=%u, break_to=%u\n",
1104 (unsigned)break_from, (unsigned)break_to));
1106 if ((break_from == break_to) && !break_needed) {
1107 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1109 file_id_string_tos(&fsp->file_id),
1114 /* Need to wait before sending a break
1115 message if we sent ourselves this message. */
1116 if (serverid_equal(&self, &src)) {
1117 wait_before_sending_break();
1120 if (sconn->using_smb2) {
1121 send_break_message_smb2(fsp, break_from, break_to);
1123 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1124 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1127 if ((break_from == SMB2_LEASE_READ) &&
1128 (break_to == SMB2_LEASE_NONE)) {
1130 * This is an async break without a reply and thus no timeout
1132 * leases are handled above.
1134 if (fsp->oplock_type != LEASE_OPLOCK) {
1139 if (fsp->oplock_type == LEASE_OPLOCK) {
1143 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1144 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1146 add_oplock_timeout_handler(fsp);
1149 /*******************************************************************
1150 This handles the kernel oplock break message.
1151 *******************************************************************/
1153 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1156 struct server_id src,
1160 unsigned long file_id;
1162 struct smbd_server_connection *sconn =
1163 talloc_get_type_abort(private_data,
1164 struct smbd_server_connection);
1165 struct server_id_buf tmp;
1167 if (data->data == NULL) {
1168 DEBUG(0, ("Got NULL buffer\n"));
1172 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1173 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1177 /* Pull the data from the message. */
1178 pull_file_id_24((char *)data->data, &id);
1179 file_id = (unsigned long)IVAL(data->data, 24);
1181 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1182 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1183 (unsigned int)file_id));
1185 fsp = initial_break_processing(sconn, id, file_id);
1188 DEBUG(3, ("Got a kernel oplock break message for a file "
1189 "I don't know about\n"));
1193 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1194 /* This is ok, kernel oplocks come in completely async */
1195 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1200 if (sconn->using_smb2) {
1201 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1203 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1206 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1208 add_oplock_timeout_handler(fsp);
1211 static bool file_has_read_oplocks(struct files_struct *fsp)
1213 struct byte_range_lock *brl;
1214 uint32_t num_read_oplocks = 0;
1216 brl = brl_get_locks_readonly(fsp);
1221 num_read_oplocks = brl_num_read_oplocks(brl);
1223 DBG_DEBUG("num_read_oplocks = %"PRIu32"\n", num_read_oplocks);
1225 return (num_read_oplocks != 0);
1228 struct break_to_none_state {
1229 struct smbd_server_connection *sconn;
1231 struct smb2_lease_key lease_key;
1232 struct GUID client_guid;
1234 static void do_break_to_none(struct tevent_context *ctx,
1235 struct tevent_immediate *im,
1236 void *private_data);
1238 /****************************************************************************
1239 This function is called on any file modification or lock request. If a file
1240 is level 2 oplocked then it must tell all other level 2 holders to break to
1242 ****************************************************************************/
1244 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1245 enum level2_contention_type type)
1247 struct smbd_server_connection *sconn = fsp->conn->sconn;
1248 struct tevent_immediate *im;
1249 struct break_to_none_state *state;
1250 bool has_read_oplocks;
1253 * If this file is level II oplocked then we need
1254 * to grab the shared memory lock and inform all
1255 * other files with a level II lock that they need
1256 * to flush their read caches. We keep the lock over
1257 * the shared memory area whilst doing this.
1260 if (fsp_lease_type_is_exclusive(fsp)) {
1262 * There can't be any level2 oplocks, we're alone.
1267 has_read_oplocks = file_has_read_oplocks(fsp);
1268 if (!has_read_oplocks) {
1269 DEBUG(10, ("No read oplocks around\n"));
1274 * When we get here we might have a brlock entry locked. Also
1275 * locking the share mode entry would violate the locking
1276 * order. Breaking level2 oplocks to none is asynchronous
1277 * anyway, so we postpone this into an immediate event.
1280 state = talloc(sconn, struct break_to_none_state);
1281 if (state == NULL) {
1282 DEBUG(1, ("talloc failed\n"));
1285 *state = (struct break_to_none_state) {
1286 .sconn = sconn, .id = fsp->file_id,
1289 if (fsp->oplock_type == LEASE_OPLOCK) {
1290 state->client_guid = *fsp_client_guid(fsp);
1291 state->lease_key = fsp->lease->lease.lease_key;
1292 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1293 state->lease_key.data[0],
1294 state->lease_key.data[1]));
1297 im = tevent_create_immediate(state);
1299 DEBUG(1, ("tevent_create_immediate failed\n"));
1303 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1306 static void send_break_to_none(struct messaging_context *msg_ctx,
1307 const struct file_id *id,
1308 const struct share_mode_entry *e)
1310 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1312 share_mode_entry_to_message(msg, id, e);
1313 /* Overload entry->op_type */
1314 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1316 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1317 (uint8_t *)msg, sizeof(msg));
1320 static void do_break_to_none(struct tevent_context *ctx,
1321 struct tevent_immediate *im,
1324 struct break_to_none_state *state = talloc_get_type_abort(
1325 private_data, struct break_to_none_state);
1327 struct share_mode_lock *lck;
1328 struct share_mode_data *d;
1330 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1332 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1333 __func__, file_id_string_tos(&state->id)));
1339 * Walk leases and oplocks separately: We have to send one break per
1340 * lease. If we have multiple share_mode_entry having a common lease,
1341 * we would break the lease twice if we don't walk the leases list
1345 for (i=0; i<d->num_leases; i++) {
1346 struct share_mode_lease *l = &d->leases[i];
1347 struct share_mode_entry *e = NULL;
1350 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1353 if (smb2_lease_equal(&state->client_guid,
1357 DEBUG(10, ("Don't break our own lease\n"));
1361 for (j=0; j<d->num_share_modes; j++) {
1362 e = &d->share_modes[j];
1364 if (!is_valid_share_mode_entry(e)) {
1367 if (e->lease_idx == i) {
1371 if (j == d->num_share_modes) {
1372 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1377 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1378 "%"PRIu32"\n", i, j));
1380 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1383 for(i = 0; i < d->num_share_modes; i++) {
1384 struct share_mode_entry *e = &d->share_modes[i];
1386 if (!is_valid_share_mode_entry(e)) {
1389 if (e->op_type == LEASE_OPLOCK) {
1391 * Took care of those in the loop above
1397 * As there could have been multiple writes waiting at the
1398 * lock_share_entry gate we may not be the first to
1399 * enter. Hence the state of the op_types in the share mode
1400 * entries may be partly NO_OPLOCK and partly LEVEL_II
1401 * oplock. It will do no harm to re-send break messages to
1402 * those smbd's that are still waiting their turn to remove
1403 * their LEVEL_II state, and also no harm to ignore existing
1404 * NO_OPLOCK states. JRA.
1407 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1410 if (e->op_type == NO_OPLOCK) {
1415 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1416 DEBUG(0,("%s: PANIC. "
1417 "share mode entry %d is an exclusive "
1418 "oplock !\n", __func__, i ));
1423 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1426 /* We let the message receivers handle removing the oplock state
1427 in the share mode lock db. */
1435 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1436 enum level2_contention_type type)
1438 contend_level2_oplocks_begin_default(fsp, type);
1441 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1442 enum level2_contention_type type)
1447 /****************************************************************************
1448 Linearize a share mode entry struct to an internal oplock break message.
1449 ****************************************************************************/
1451 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1452 const struct share_mode_entry *e)
1454 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1455 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1456 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1457 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1458 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1459 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1460 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1461 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1463 * "id" used to be part of share_mode_entry, thus the strange
1464 * place to put this. Feel free to move somewhere else :-)
1466 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1467 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1468 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1469 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1470 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1471 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1474 /****************************************************************************
1475 De-linearize an internal oplock break message to a share mode entry struct.
1476 ****************************************************************************/
1478 void message_to_share_mode_entry(struct file_id *id,
1479 struct share_mode_entry *e,
1482 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1483 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1484 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1485 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1486 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1487 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1488 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1489 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1491 * "id" used to be part of share_mode_entry, thus the strange
1492 * place to put this. Feel free to move somewhere else :-)
1494 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1495 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1496 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1497 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1498 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1499 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1502 /****************************************************************************
1503 Setup oplocks for this process.
1504 ****************************************************************************/
1506 bool init_oplocks(struct smbd_server_connection *sconn)
1508 DEBUG(3,("init_oplocks: initializing messages.\n"));
1510 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1511 process_oplock_break_message);
1512 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1513 process_kernel_oplock_break);
1517 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1519 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1521 /* only initialize once */
1522 if (koplocks == NULL) {
1523 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1524 koplocks = linux_init_kernel_oplocks(sconn);
1526 sconn->oplocks.kernel_ops = koplocks;