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/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)) && koplocks;
60 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
62 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
64 "don't support them\n"));
65 return NT_STATUS_NOT_SUPPORTED;
69 if ((fsp->oplock_type != NO_OPLOCK) &&
71 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
73 return map_nt_error_from_unix(errno);
76 fsp->sent_oplock_break = NO_BREAK_SENT;
77 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
78 sconn->oplocks.level_II_open++;
79 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
80 sconn->oplocks.exclusive_open++;
83 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
84 "tv_sec = %x, tv_usec = %x\n",
85 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
86 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
87 (int)fsp->open_time.tv_usec ));
92 /****************************************************************************
93 Attempt to release an oplock on a file. Decrements oplock count.
94 ****************************************************************************/
96 static void release_file_oplock(files_struct *fsp)
98 struct smbd_server_connection *sconn = fsp->conn->sconn;
99 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
100 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
102 if ((fsp->oplock_type != NO_OPLOCK) &&
104 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
107 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
108 sconn->oplocks.level_II_open--;
109 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
110 sconn->oplocks.exclusive_open--;
113 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
114 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
116 fsp->oplock_type = NO_OPLOCK;
117 fsp->sent_oplock_break = NO_BREAK_SENT;
119 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
120 delete_write_cache(fsp);
122 TALLOC_FREE(fsp->oplock_timeout);
125 /****************************************************************************
126 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
127 ****************************************************************************/
129 static void downgrade_file_oplock(files_struct *fsp)
131 struct smbd_server_connection *sconn = fsp->conn->sconn;
132 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
134 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
135 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
140 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
142 fsp->oplock_type = LEVEL_II_OPLOCK;
143 sconn->oplocks.exclusive_open--;
144 sconn->oplocks.level_II_open++;
145 fsp->sent_oplock_break = NO_BREAK_SENT;
147 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
148 delete_write_cache(fsp);
150 TALLOC_FREE(fsp->oplock_timeout);
153 uint32_t map_oplock_to_lease_type(uint16_t op_type)
159 case BATCH_OPLOCK|EXCLUSIVE_OPLOCK:
160 ret = SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE;
162 case EXCLUSIVE_OPLOCK:
163 ret = SMB2_LEASE_READ|SMB2_LEASE_WRITE;
165 case LEVEL_II_OPLOCK:
166 ret = SMB2_LEASE_READ;
169 ret = SMB2_LEASE_NONE;
175 uint32_t get_lease_type(struct share_mode_data *d, 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 (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
192 * If we're the only one, we don't need a brlock entry
194 remove_stale_share_mode_entries(d);
195 SMB_ASSERT(d->num_share_modes == 1);
196 SMB_ASSERT(EXCLUSIVE_OPLOCK_TYPE(d->share_modes[0].op_type));
200 for (i=0; i<d->num_share_modes; i++) {
201 struct share_mode_entry *e = &d->share_modes[i];
202 uint32_t e_lease_type = get_lease_type(d, e);
204 if (e_lease_type & SMB2_LEASE_READ) {
205 num_read_oplocks += 1;
209 br_lck = brl_get_locks_readonly(fsp);
210 if (br_lck == NULL) {
213 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
217 br_lck = brl_get_locks(talloc_tos(), fsp);
218 if (br_lck == NULL) {
221 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
226 /****************************************************************************
227 Remove a file oplock with lock already held. Copes with level II and exclusive.
228 ****************************************************************************/
230 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
234 ret = remove_share_oplock(lck, fsp);
236 DBG_ERR("failed to remove share oplock for "
238 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
239 file_id_string_tos(&fsp->file_id));
241 release_file_oplock(fsp);
243 ret = update_num_read_oplocks(fsp, lck);
245 DBG_ERR("update_num_read_oplocks failed for "
247 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
248 file_id_string_tos(&fsp->file_id));
254 /****************************************************************************
255 Remove a file oplock. Copes with level II and exclusive.
256 Locks then unlocks the share mode lock. Client can decide to go directly
257 to none even if a "break-to-level II" was sent.
258 ****************************************************************************/
260 bool remove_oplock(files_struct *fsp)
263 struct share_mode_lock *lck;
265 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
267 /* Remove the oplock flag from the sharemode. */
268 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
270 DBG_ERR("failed to lock share entry for "
271 "file %s\n", fsp_str_dbg(fsp));
275 ret = remove_oplock_under_lock(fsp, lck);
282 * Deal with a reply when a break-to-level II was sent.
284 bool downgrade_oplock(files_struct *fsp)
287 struct share_mode_lock *lck;
289 DEBUG(10, ("downgrade_oplock called for %s\n",
292 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
294 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
295 "file %s\n", fsp_str_dbg(fsp)));
298 ret = downgrade_share_oplock(lck, fsp);
300 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
301 "for file %s, %s, file_id %s\n",
302 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
303 file_id_string_tos(&fsp->file_id)));
305 downgrade_file_oplock(fsp);
307 ret = update_num_read_oplocks(fsp, lck);
309 DEBUG(0, ("%s: update_num_read_oplocks failed for "
311 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
312 file_id_string_tos(&fsp->file_id)));
319 static void lease_timeout_handler(struct tevent_context *ctx,
320 struct tevent_timer *te,
324 struct fsp_lease *lease =
325 talloc_get_type_abort(private_data,
327 struct files_struct *fsp;
328 struct share_mode_lock *lck;
329 uint16_t old_epoch = lease->lease.lease_epoch;
331 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
332 &lease->lease.lease_key);
335 TALLOC_FREE(lease->timeout);
339 lck = get_existing_share_mode_lock(
340 talloc_tos(), fsp->file_id);
343 TALLOC_FREE(lease->timeout);
347 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
349 if (lease->lease.lease_epoch != old_epoch) {
351 * If the epoch changed we need to wait for
352 * the next timeout to happen.
354 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
360 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
362 * If the epoch changed we need to wait for
363 * the next timeout to happen.
365 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
371 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
373 (void)downgrade_lease(lease->sconn->client->connections,
376 &lease->lease.lease_key,
382 bool fsp_lease_update(struct share_mode_lock *lck,
383 const struct GUID *client_guid,
384 struct fsp_lease *lease)
386 struct share_mode_data *d = lck->data;
388 struct share_mode_lease *l = NULL;
390 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
396 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
397 TALLOC_FREE(lease->timeout);
398 lease->lease.lease_state = SMB2_LEASE_NONE;
399 lease->lease.lease_epoch += 1;
400 lease->lease.lease_flags = 0;
404 DEBUG(10,("%s: refresh lease state\n", __func__));
406 /* Ensure we're in sync with current lease state. */
407 if (lease->lease.lease_epoch != l->epoch) {
408 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
409 TALLOC_FREE(lease->timeout);
411 lease->lease.lease_epoch = l->epoch;
412 lease->lease.lease_state = l->current_state;
415 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
417 if (lease->timeout == NULL) {
418 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
420 DEBUG(10,("%s: setup timeout handler\n", __func__));
422 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
424 lease_timeout_handler,
426 if (lease->timeout == NULL) {
427 DEBUG(0, ("%s: Could not add lease timeout handler\n",
432 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
433 TALLOC_FREE(lease->timeout);
439 struct downgrade_lease_additional_state {
440 struct tevent_immediate *im;
441 struct smbXsrv_connection *xconn;
442 uint32_t break_flags;
443 struct smb2_lease_key lease_key;
449 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
450 struct tevent_immediate *im,
453 struct downgrade_lease_additional_state *state =
454 talloc_get_type_abort(private_data,
455 struct downgrade_lease_additional_state);
456 struct smbXsrv_connection *xconn = state->xconn;
459 status = smbd_smb2_send_lease_break(xconn,
466 if (!NT_STATUS_IS_OK(status)) {
467 smbd_server_connection_terminate(xconn,
473 struct downgrade_lease_fsps_state {
475 struct share_mode_lock *lck;
476 const struct smb2_lease_key *key;
479 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
482 struct downgrade_lease_fsps_state *state =
483 (struct downgrade_lease_fsps_state *)private_data;
485 if (fsp->oplock_type != LEASE_OPLOCK) {
488 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
491 if (!file_id_equal(&fsp->file_id, &state->id)) {
495 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
500 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
501 uint32_t num_file_ids,
502 const struct file_id *ids,
503 const struct smb2_lease_key *key,
504 uint32_t lease_state)
506 struct smbd_server_connection *sconn = xconn->client->sconn;
507 struct share_mode_lock *lck;
508 struct share_mode_lease *l = NULL;
509 const struct file_id id = ids[0];
513 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
514 file_id_string_tos(&id), (unsigned)lease_state));
516 lck = get_existing_share_mode_lock(talloc_tos(), id);
518 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
520 status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
522 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
523 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
525 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
526 struct downgrade_lease_additional_state *state;
528 state = talloc_zero(xconn,
529 struct downgrade_lease_additional_state);
532 return NT_STATUS_NO_MEMORY;
535 state->im = tevent_create_immediate(state);
536 if (state->im == NULL) {
539 return NT_STATUS_NO_MEMORY;
542 state->xconn = xconn;
543 if (l->current_state & (~SMB2_LEASE_READ)) {
544 state->break_flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
546 state->lease_key = l->lease_key;
547 state->break_from = l->current_state;
548 state->break_to = l->breaking_to_requested;
549 if (l->lease_version > 1) {
550 state->new_epoch = l->epoch;
553 if (state->break_flags == 0) {
555 * This is an async break without
556 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
558 * we need to store NONE state in the
561 l->current_state = 0;
562 l->breaking_to_requested = 0;
563 l->breaking_to_required = 0;
566 lck->data->modified = true;
569 tevent_schedule_immediate(state->im, xconn->ev_ctx,
570 downgrade_lease_additional_trigger,
575 struct downgrade_lease_fsps_state state = {
576 .id = id, .lck = lck, .key = key,
579 files_forall(sconn, downgrade_lease_fsps, &state);
583 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
584 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
587 * Dynamic share case. Ensure other opens are copies.
588 * This will only be breaking to NONE.
591 for (i = 1; i < num_file_ids; i++) {
592 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
594 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
598 struct downgrade_lease_fsps_state state = {
599 .id = ids[i], .lck = lck, .key = key,
602 files_forall(sconn, downgrade_lease_fsps, &state);
605 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
606 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
614 /****************************************************************************
615 Set up an oplock break message.
616 ****************************************************************************/
618 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
620 static void new_break_message_smb1(files_struct *fsp, int cmd,
621 char result[SMB1_BREAK_MESSAGE_LENGTH])
623 memset(result,'\0',smb_size);
624 srv_set_message(result,8,0,true);
625 SCVAL(result,smb_com,SMBlockingX);
626 SSVAL(result,smb_tid,fsp->conn->cnum);
627 SSVAL(result,smb_pid,0xFFFF);
628 SSVAL(result,smb_uid,0);
629 SSVAL(result,smb_mid,0xFFFF);
630 SCVAL(result,smb_vwv0,0xFF);
631 SSVAL(result,smb_vwv2,fsp->fnum);
632 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
633 SCVAL(result,smb_vwv3+1,cmd);
636 /****************************************************************************
637 Function to do the waiting before sending a local break.
638 ****************************************************************************/
640 static void wait_before_sending_break(void)
642 long wait_time = (long)lp_oplock_break_wait_time();
645 smb_msleep(wait_time);
649 /****************************************************************************
650 Ensure that we have a valid oplock.
651 ****************************************************************************/
653 static files_struct *initial_break_processing(
654 struct smbd_server_connection *sconn, struct file_id id,
655 unsigned long file_id)
657 files_struct *fsp = NULL;
659 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
660 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
661 file_id_string_tos(&id), (int)file_id,
662 sconn->oplocks.exclusive_open,
663 sconn->oplocks.level_II_open));
666 * We need to search the file open table for the
667 * entry containing this dev and inode, and ensure
668 * we have an oplock on it.
671 fsp = file_find_dif(sconn, id, file_id);
674 /* The file could have been closed in the meantime - return success. */
675 DEBUG(3, ("initial_break_processing: cannot find open file "
676 "with file_id %s gen_id = %lu, allowing break to "
677 "succeed.\n", file_id_string_tos(&id), file_id));
681 /* Ensure we have an oplock on the file */
684 * There is a potential race condition in that an oplock could
685 * have been broken due to another udp request, and yet there are
686 * still oplock break messages being sent in the udp message
687 * queue for this file. So return true if we don't have an oplock,
688 * as we may have just freed it.
691 if(fsp->oplock_type == NO_OPLOCK) {
692 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
693 "gen_id = %lu) has no oplock. Allowing break to "
694 "succeed regardless.\n", fsp_str_dbg(fsp),
695 file_id_string_tos(&id), fsp->fh->gen_id));
702 static void oplock_timeout_handler(struct tevent_context *ctx,
703 struct tevent_timer *te,
707 files_struct *fsp = (files_struct *)private_data;
709 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
711 /* Remove the timed event handler. */
712 TALLOC_FREE(fsp->oplock_timeout);
713 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
718 /*******************************************************************
719 Add a timeout handler waiting for the client reply.
720 *******************************************************************/
722 static void add_oplock_timeout_handler(files_struct *fsp)
724 struct smbd_server_connection *sconn = fsp->conn->sconn;
725 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
728 * If kernel oplocks already notifies smbds when an oplock break times
732 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
736 if (fsp->oplock_timeout != NULL) {
737 DEBUG(0, ("Logic problem -- have an oplock event hanging "
741 fsp->oplock_timeout =
742 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
743 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
744 oplock_timeout_handler, fsp);
746 if (fsp->oplock_timeout == NULL) {
747 DEBUG(0, ("Could not add oplock timeout handler\n"));
751 static void send_break_message_smb1(files_struct *fsp, int level)
753 struct smbXsrv_connection *xconn = NULL;
754 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
757 * For SMB1 we only have one connection
759 xconn = fsp->conn->sconn->client->connections;
761 new_break_message_smb1(fsp, level, break_msg);
764 if (!srv_send_smb(xconn,
766 IS_CONN_ENCRYPTED(fsp->conn),
768 exit_server_cleanly("send_break_message_smb1: "
769 "srv_send_smb failed.");
773 /*******************************************************************
774 This handles the generic oplock break message from another smbd.
775 *******************************************************************/
777 static void process_oplock_break_message(struct messaging_context *msg_ctx,
780 struct server_id src,
783 struct share_mode_entry msg;
786 struct smbd_server_connection *sconn =
787 talloc_get_type_abort(private_data,
788 struct smbd_server_connection);
789 struct server_id self = messaging_server_id(sconn->msg_ctx);
790 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
793 bool break_needed = true;
794 struct server_id_buf tmp;
796 if (data->data == NULL) {
797 DEBUG(0, ("Got NULL buffer\n"));
801 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
802 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
806 /* De-linearize incoming message. */
807 message_to_share_mode_entry(&msg, (char *)data->data);
808 break_to = msg.op_type;
810 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
811 (unsigned)break_to, server_id_str_buf(src, &tmp),
812 file_id_string_tos(&msg.id),
813 (unsigned long long)msg.share_file_id));
815 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
818 /* We hit a race here. Break messages are sent, and before we
819 * get to process this message, we have closed the file. */
820 DEBUG(3, ("Did not find fsp\n"));
824 break_from = fsp_lease_type(fsp);
826 if (fsp->oplock_type != LEASE_OPLOCK) {
827 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
829 * Nothing to do anymore
831 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
832 fsp->sent_oplock_break));
837 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
838 DEBUG(10, ("client_caps without level2 oplocks\n"));
839 break_to &= ~SMB2_LEASE_READ;
842 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
843 if (use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
844 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
845 break_to &= ~SMB2_LEASE_READ;
848 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
849 DEBUG(10, ("no level2 oplocks by config\n"));
850 break_to &= ~SMB2_LEASE_READ;
853 if (fsp->oplock_type == LEASE_OPLOCK) {
854 struct share_mode_lock *lck;
857 lck = get_existing_share_mode_lock(
858 talloc_tos(), fsp->file_id);
861 * We hit a race here. Break messages are sent, and
862 * before we get to process this message, we have closed
865 DEBUG(3, ("Did not find share_mode\n"));
869 idx = find_share_mode_lease(
871 fsp_client_guid(fsp),
872 &fsp->lease->lease.lease_key);
874 struct share_mode_lease *l;
875 l = &lck->data->leases[idx];
877 break_from = l->current_state;
878 break_to &= l->current_state;
881 break_to &= l->breaking_to_required;
882 if (l->breaking_to_required != break_to) {
884 * Note we don't increment the epoch
885 * here, which might be a bug in
888 l->breaking_to_required = break_to;
889 lck->data->modified = true;
891 break_needed = false;
892 } else if (l->current_state == break_to) {
893 break_needed = false;
894 } else if (l->current_state == SMB2_LEASE_READ) {
895 l->current_state = SMB2_LEASE_NONE;
896 /* Need to increment the epoch */
898 lck->data->modified = true;
901 l->breaking_to_required = break_to;
902 l->breaking_to_requested = break_to;
903 /* Need to increment the epoch */
905 lck->data->modified = true;
908 /* Ensure we're in sync with current lease state. */
909 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
916 DEBUG(10,("%s: skip break\n", __func__));
920 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
921 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
922 file_id_string_tos(&fsp->file_id),
927 DEBUG(10, ("break_from=%u, break_to=%u\n",
928 (unsigned)break_from, (unsigned)break_to));
930 if ((break_from == break_to) && !break_needed) {
931 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
933 file_id_string_tos(&fsp->file_id),
938 /* Need to wait before sending a break
939 message if we sent ourselves this message. */
940 if (serverid_equal(&self, &src)) {
941 wait_before_sending_break();
944 if (sconn->using_smb2) {
945 send_break_message_smb2(fsp, break_from, break_to);
947 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
948 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
951 if ((break_from == SMB2_LEASE_READ) &&
952 (break_to == SMB2_LEASE_NONE)) {
954 * This is an async break without a reply and thus no timeout
956 * leases are handled above.
958 if (fsp->oplock_type != LEASE_OPLOCK) {
963 if (fsp->oplock_type == LEASE_OPLOCK) {
967 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
968 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
970 add_oplock_timeout_handler(fsp);
973 /*******************************************************************
974 This handles the kernel oplock break message.
975 *******************************************************************/
977 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
980 struct server_id src,
984 unsigned long file_id;
986 struct smbd_server_connection *sconn =
987 talloc_get_type_abort(private_data,
988 struct smbd_server_connection);
989 struct server_id_buf tmp;
991 if (data->data == NULL) {
992 DEBUG(0, ("Got NULL buffer\n"));
996 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
997 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1001 /* Pull the data from the message. */
1002 pull_file_id_24((char *)data->data, &id);
1003 file_id = (unsigned long)IVAL(data->data, 24);
1005 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1006 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1007 (unsigned int)file_id));
1009 fsp = initial_break_processing(sconn, id, file_id);
1012 DEBUG(3, ("Got a kernel oplock break message for a file "
1013 "I don't know about\n"));
1017 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1018 /* This is ok, kernel oplocks come in completely async */
1019 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1024 if (sconn->using_smb2) {
1025 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1027 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1030 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1032 add_oplock_timeout_handler(fsp);
1035 struct break_to_none_state {
1036 struct smbd_server_connection *sconn;
1038 struct smb2_lease_key lease_key;
1039 struct GUID client_guid;
1041 static void do_break_to_none(struct tevent_context *ctx,
1042 struct tevent_immediate *im,
1043 void *private_data);
1045 /****************************************************************************
1046 This function is called on any file modification or lock request. If a file
1047 is level 2 oplocked then it must tell all other level 2 holders to break to
1049 ****************************************************************************/
1051 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1052 enum level2_contention_type type)
1054 struct smbd_server_connection *sconn = fsp->conn->sconn;
1055 struct tevent_immediate *im;
1056 struct break_to_none_state *state;
1057 struct byte_range_lock *brl;
1058 uint32_t num_read_oplocks = 0;
1061 * If this file is level II oplocked then we need
1062 * to grab the shared memory lock and inform all
1063 * other files with a level II lock that they need
1064 * to flush their read caches. We keep the lock over
1065 * the shared memory area whilst doing this.
1068 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1070 * There can't be any level2 oplocks, we're alone.
1075 brl = brl_get_locks_readonly(fsp);
1077 num_read_oplocks = brl_num_read_oplocks(brl);
1080 DEBUG(10, ("num_read_oplocks = %"PRIu32"\n", num_read_oplocks));
1082 if (num_read_oplocks == 0) {
1083 DEBUG(10, ("No read oplocks around\n"));
1088 * When we get here we might have a brlock entry locked. Also
1089 * locking the share mode entry would violate the locking
1090 * order. Breaking level2 oplocks to none is asynchronous
1091 * anyway, so we postpone this into an immediate event.
1094 state = talloc_zero(sconn, struct break_to_none_state);
1095 if (state == NULL) {
1096 DEBUG(1, ("talloc failed\n"));
1099 state->sconn = sconn;
1100 state->id = fsp->file_id;
1102 if (fsp->oplock_type == LEASE_OPLOCK) {
1103 state->client_guid = *fsp_client_guid(fsp);
1104 state->lease_key = fsp->lease->lease.lease_key;
1105 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1106 state->lease_key.data[0],
1107 state->lease_key.data[1]));
1110 im = tevent_create_immediate(state);
1112 DEBUG(1, ("tevent_create_immediate failed\n"));
1116 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1119 static void send_break_to_none(struct messaging_context *msg_ctx,
1120 const struct share_mode_entry *e)
1122 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1124 share_mode_entry_to_message(msg, e);
1125 /* Overload entry->op_type */
1126 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1128 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1129 (uint8_t *)msg, sizeof(msg));
1132 static void do_break_to_none(struct tevent_context *ctx,
1133 struct tevent_immediate *im,
1136 struct break_to_none_state *state = talloc_get_type_abort(
1137 private_data, struct break_to_none_state);
1139 struct share_mode_lock *lck;
1140 struct share_mode_data *d;
1142 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1144 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1145 __func__, file_id_string_tos(&state->id)));
1151 * Walk leases and oplocks separately: We have to send one break per
1152 * lease. If we have multiple share_mode_entry having a common lease,
1153 * we would break the lease twice if we don't walk the leases list
1157 for (i=0; i<d->num_leases; i++) {
1158 struct share_mode_lease *l = &d->leases[i];
1159 struct share_mode_entry *e = NULL;
1162 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1165 if (smb2_lease_equal(&state->client_guid,
1169 DEBUG(10, ("Don't break our own lease\n"));
1173 for (j=0; j<d->num_share_modes; j++) {
1174 e = &d->share_modes[j];
1176 if (!is_valid_share_mode_entry(e)) {
1179 if (e->lease_idx == i) {
1183 if (j == d->num_share_modes) {
1184 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1189 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1190 "%"PRIu32"\n", i, j));
1192 send_break_to_none(state->sconn->msg_ctx, e);
1195 for(i = 0; i < d->num_share_modes; i++) {
1196 struct share_mode_entry *e = &d->share_modes[i];
1198 if (!is_valid_share_mode_entry(e)) {
1201 if (e->op_type == LEASE_OPLOCK) {
1203 * Took care of those in the loop above
1209 * As there could have been multiple writes waiting at the
1210 * lock_share_entry gate we may not be the first to
1211 * enter. Hence the state of the op_types in the share mode
1212 * entries may be partly NO_OPLOCK and partly LEVEL_II
1213 * oplock. It will do no harm to re-send break messages to
1214 * those smbd's that are still waiting their turn to remove
1215 * their LEVEL_II state, and also no harm to ignore existing
1216 * NO_OPLOCK states. JRA.
1219 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1222 if (e->op_type == NO_OPLOCK) {
1227 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1228 DEBUG(0,("%s: PANIC. "
1229 "share mode entry %d is an exclusive "
1230 "oplock !\n", __func__, i ));
1235 send_break_to_none(state->sconn->msg_ctx, e);
1238 /* We let the message receivers handle removing the oplock state
1239 in the share mode lock db. */
1247 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1248 enum level2_contention_type type)
1250 struct smbd_server_connection *sconn = fsp->conn->sconn;
1251 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1253 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
1254 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
1258 contend_level2_oplocks_begin_default(fsp, type);
1261 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1262 enum level2_contention_type type)
1264 struct smbd_server_connection *sconn = fsp->conn->sconn;
1265 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1267 /* Only kernel oplocks implement this so far */
1268 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
1269 koplocks->ops->contend_level2_oplocks_end(fsp, type);
1273 /****************************************************************************
1274 Linearize a share mode entry struct to an internal oplock break message.
1275 ****************************************************************************/
1277 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
1279 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1280 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1281 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1282 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1283 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1284 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1285 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1286 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1287 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
1288 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1289 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1290 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1291 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1292 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1295 /****************************************************************************
1296 De-linearize an internal oplock break message to a share mode entry struct.
1297 ****************************************************************************/
1299 void message_to_share_mode_entry(struct share_mode_entry *e, const char *msg)
1301 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1302 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1303 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1304 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1305 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1306 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1307 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1308 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1309 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
1310 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1311 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1312 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1313 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1314 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1317 /****************************************************************************
1318 Setup oplocks for this process.
1319 ****************************************************************************/
1321 bool init_oplocks(struct smbd_server_connection *sconn)
1323 DEBUG(3,("init_oplocks: initializing messages.\n"));
1325 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1326 process_oplock_break_message);
1327 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1328 process_kernel_oplock_break);
1332 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1334 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1336 /* only initialize once */
1337 if (koplocks == NULL) {
1338 #if HAVE_KERNEL_OPLOCKS_IRIX
1339 koplocks = irix_init_kernel_oplocks(sconn);
1340 #elif HAVE_KERNEL_OPLOCKS_LINUX
1341 koplocks = linux_init_kernel_oplocks(sconn);
1343 sconn->oplocks.kernel_ops = koplocks;