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 get_lease_type(const struct share_mode_data *d,
154 const struct share_mode_entry *e)
156 if (e->op_type == LEASE_OPLOCK) {
157 return d->leases[e->lease_idx].current_state;
159 return map_oplock_to_lease_type(e->op_type);
162 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
164 struct share_mode_data *d = lck->data;
165 struct byte_range_lock *br_lck;
166 uint32_t num_read_oplocks = 0;
169 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
171 * If we're the only one, we don't need a brlock entry
173 remove_stale_share_mode_entries(d);
174 SMB_ASSERT(d->num_share_modes == 1);
175 SMB_ASSERT(EXCLUSIVE_OPLOCK_TYPE(d->share_modes[0].op_type));
179 for (i=0; i<d->num_share_modes; i++) {
180 struct share_mode_entry *e = &d->share_modes[i];
181 uint32_t e_lease_type = get_lease_type(d, e);
183 if (e_lease_type & SMB2_LEASE_READ) {
184 num_read_oplocks += 1;
188 br_lck = brl_get_locks_readonly(fsp);
189 if (br_lck == NULL) {
192 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
196 br_lck = brl_get_locks(talloc_tos(), fsp);
197 if (br_lck == NULL) {
200 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
205 /****************************************************************************
206 Remove a file oplock with lock already held. Copes with level II and exclusive.
207 ****************************************************************************/
209 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
213 ret = remove_share_oplock(lck, fsp);
215 DBG_ERR("failed to remove share oplock for "
217 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
218 file_id_string_tos(&fsp->file_id));
220 release_file_oplock(fsp);
222 ret = update_num_read_oplocks(fsp, lck);
224 DBG_ERR("update_num_read_oplocks failed for "
226 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
227 file_id_string_tos(&fsp->file_id));
233 /****************************************************************************
234 Remove a file oplock. Copes with level II and exclusive.
235 Locks then unlocks the share mode lock. Client can decide to go directly
236 to none even if a "break-to-level II" was sent.
237 ****************************************************************************/
239 bool remove_oplock(files_struct *fsp)
242 struct share_mode_lock *lck;
244 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
246 /* Remove the oplock flag from the sharemode. */
247 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
249 DBG_ERR("failed to lock share entry for "
250 "file %s\n", fsp_str_dbg(fsp));
254 ret = remove_oplock_under_lock(fsp, lck);
261 * Deal with a reply when a break-to-level II was sent.
263 bool downgrade_oplock(files_struct *fsp)
266 struct share_mode_lock *lck;
268 DEBUG(10, ("downgrade_oplock called for %s\n",
271 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
273 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
274 "file %s\n", fsp_str_dbg(fsp)));
277 ret = downgrade_share_oplock(lck, fsp);
279 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
280 "for file %s, %s, file_id %s\n",
281 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
282 file_id_string_tos(&fsp->file_id)));
284 downgrade_file_oplock(fsp);
286 ret = update_num_read_oplocks(fsp, lck);
288 DEBUG(0, ("%s: update_num_read_oplocks failed for "
290 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
291 file_id_string_tos(&fsp->file_id)));
298 static void lease_timeout_handler(struct tevent_context *ctx,
299 struct tevent_timer *te,
303 struct fsp_lease *lease =
304 talloc_get_type_abort(private_data,
306 struct files_struct *fsp;
307 struct share_mode_lock *lck;
308 uint16_t old_epoch = lease->lease.lease_epoch;
310 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
311 &lease->lease.lease_key);
314 TALLOC_FREE(lease->timeout);
318 lck = get_existing_share_mode_lock(
319 talloc_tos(), fsp->file_id);
322 TALLOC_FREE(lease->timeout);
326 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
328 if (lease->lease.lease_epoch != old_epoch) {
330 * If the epoch changed we need to wait for
331 * the next timeout to happen.
333 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
339 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
341 * If the epoch changed we need to wait for
342 * the next timeout to happen.
344 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
350 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
352 (void)downgrade_lease(lease->sconn->client->connections,
355 &lease->lease.lease_key,
361 bool fsp_lease_update(struct share_mode_lock *lck,
362 const struct GUID *client_guid,
363 struct fsp_lease *lease)
365 struct share_mode_data *d = lck->data;
367 struct share_mode_lease *l = NULL;
369 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
375 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
376 TALLOC_FREE(lease->timeout);
377 lease->lease.lease_state = SMB2_LEASE_NONE;
378 lease->lease.lease_epoch += 1;
379 lease->lease.lease_flags = 0;
383 DEBUG(10,("%s: refresh lease state\n", __func__));
385 /* Ensure we're in sync with current lease state. */
386 if (lease->lease.lease_epoch != l->epoch) {
387 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
388 TALLOC_FREE(lease->timeout);
390 lease->lease.lease_epoch = l->epoch;
391 lease->lease.lease_state = l->current_state;
394 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
396 if (lease->timeout == NULL) {
397 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
399 DEBUG(10,("%s: setup timeout handler\n", __func__));
401 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
403 lease_timeout_handler,
405 if (lease->timeout == NULL) {
406 DEBUG(0, ("%s: Could not add lease timeout handler\n",
411 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
412 TALLOC_FREE(lease->timeout);
418 struct downgrade_lease_additional_state {
419 struct tevent_immediate *im;
420 struct smbXsrv_connection *xconn;
421 uint32_t break_flags;
422 struct smb2_lease_key lease_key;
428 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
429 struct tevent_immediate *im,
432 struct downgrade_lease_additional_state *state =
433 talloc_get_type_abort(private_data,
434 struct downgrade_lease_additional_state);
435 struct smbXsrv_connection *xconn = state->xconn;
438 status = smbd_smb2_send_lease_break(xconn,
445 if (!NT_STATUS_IS_OK(status)) {
446 smbd_server_connection_terminate(xconn,
452 struct downgrade_lease_fsps_state {
454 struct share_mode_lock *lck;
455 const struct smb2_lease_key *key;
458 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
461 struct downgrade_lease_fsps_state *state =
462 (struct downgrade_lease_fsps_state *)private_data;
464 if (fsp->oplock_type != LEASE_OPLOCK) {
467 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
470 if (!file_id_equal(&fsp->file_id, &state->id)) {
474 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
479 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
480 uint32_t num_file_ids,
481 const struct file_id *ids,
482 const struct smb2_lease_key *key,
483 uint32_t lease_state)
485 struct smbd_server_connection *sconn = xconn->client->sconn;
486 struct share_mode_lock *lck;
487 struct share_mode_lease *l = NULL;
488 const struct file_id id = ids[0];
492 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
493 file_id_string_tos(&id), (unsigned)lease_state));
495 lck = get_existing_share_mode_lock(talloc_tos(), id);
497 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
499 status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
501 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
502 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
504 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
505 struct downgrade_lease_additional_state *state;
507 state = talloc_zero(xconn,
508 struct downgrade_lease_additional_state);
511 return NT_STATUS_NO_MEMORY;
514 state->im = tevent_create_immediate(state);
515 if (state->im == NULL) {
518 return NT_STATUS_NO_MEMORY;
521 state->xconn = xconn;
522 if (l->current_state & (~SMB2_LEASE_READ)) {
523 state->break_flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
525 state->lease_key = l->lease_key;
526 state->break_from = l->current_state;
527 state->break_to = l->breaking_to_requested;
528 if (l->lease_version > 1) {
529 state->new_epoch = l->epoch;
532 if (state->break_flags == 0) {
534 * This is an async break without
535 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
537 * we need to store NONE state in the
540 l->current_state = 0;
541 l->breaking_to_requested = 0;
542 l->breaking_to_required = 0;
545 lck->data->modified = true;
548 tevent_schedule_immediate(state->im, xconn->ev_ctx,
549 downgrade_lease_additional_trigger,
554 struct downgrade_lease_fsps_state state = {
555 .id = id, .lck = lck, .key = key,
558 files_forall(sconn, downgrade_lease_fsps, &state);
562 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
563 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
566 * Dynamic share case. Ensure other opens are copies.
567 * This will only be breaking to NONE.
570 for (i = 1; i < num_file_ids; i++) {
571 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
573 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
577 struct downgrade_lease_fsps_state state = {
578 .id = ids[i], .lck = lck, .key = key,
581 files_forall(sconn, downgrade_lease_fsps, &state);
584 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
585 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
593 /****************************************************************************
594 Set up an oplock break message.
595 ****************************************************************************/
597 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
599 static void new_break_message_smb1(files_struct *fsp, int cmd,
600 char result[SMB1_BREAK_MESSAGE_LENGTH])
602 memset(result,'\0',smb_size);
603 srv_set_message(result,8,0,true);
604 SCVAL(result,smb_com,SMBlockingX);
605 SSVAL(result,smb_tid,fsp->conn->cnum);
606 SSVAL(result,smb_pid,0xFFFF);
607 SSVAL(result,smb_uid,0);
608 SSVAL(result,smb_mid,0xFFFF);
609 SCVAL(result,smb_vwv0,0xFF);
610 SSVAL(result,smb_vwv2,fsp->fnum);
611 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
612 SCVAL(result,smb_vwv3+1,cmd);
615 /****************************************************************************
616 Function to do the waiting before sending a local break.
617 ****************************************************************************/
619 static void wait_before_sending_break(void)
621 long wait_time = (long)lp_oplock_break_wait_time();
624 smb_msleep(wait_time);
628 /****************************************************************************
629 Ensure that we have a valid oplock.
630 ****************************************************************************/
632 static files_struct *initial_break_processing(
633 struct smbd_server_connection *sconn, struct file_id id,
634 unsigned long file_id)
636 files_struct *fsp = NULL;
638 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
639 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
640 file_id_string_tos(&id), (int)file_id,
641 sconn->oplocks.exclusive_open,
642 sconn->oplocks.level_II_open));
645 * We need to search the file open table for the
646 * entry containing this dev and inode, and ensure
647 * we have an oplock on it.
650 fsp = file_find_dif(sconn, id, file_id);
653 /* The file could have been closed in the meantime - return success. */
654 DEBUG(3, ("initial_break_processing: cannot find open file "
655 "with file_id %s gen_id = %lu, allowing break to "
656 "succeed.\n", file_id_string_tos(&id), file_id));
660 /* Ensure we have an oplock on the file */
663 * There is a potential race condition in that an oplock could
664 * have been broken due to another udp request, and yet there are
665 * still oplock break messages being sent in the udp message
666 * queue for this file. So return true if we don't have an oplock,
667 * as we may have just freed it.
670 if(fsp->oplock_type == NO_OPLOCK) {
671 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
672 "gen_id = %lu) has no oplock. Allowing break to "
673 "succeed regardless.\n", fsp_str_dbg(fsp),
674 file_id_string_tos(&id), fsp->fh->gen_id));
681 static void oplock_timeout_handler(struct tevent_context *ctx,
682 struct tevent_timer *te,
686 files_struct *fsp = (files_struct *)private_data;
688 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
690 /* Remove the timed event handler. */
691 TALLOC_FREE(fsp->oplock_timeout);
692 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
697 /*******************************************************************
698 Add a timeout handler waiting for the client reply.
699 *******************************************************************/
701 static void add_oplock_timeout_handler(files_struct *fsp)
703 struct smbd_server_connection *sconn = fsp->conn->sconn;
704 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
707 * If kernel oplocks already notifies smbds when an oplock break times
711 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
715 if (fsp->oplock_timeout != NULL) {
716 DEBUG(0, ("Logic problem -- have an oplock event hanging "
720 fsp->oplock_timeout =
721 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
722 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
723 oplock_timeout_handler, fsp);
725 if (fsp->oplock_timeout == NULL) {
726 DEBUG(0, ("Could not add oplock timeout handler\n"));
730 static void send_break_message_smb1(files_struct *fsp, int level)
732 struct smbXsrv_connection *xconn = NULL;
733 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
736 * For SMB1 we only have one connection
738 xconn = fsp->conn->sconn->client->connections;
740 new_break_message_smb1(fsp, level, break_msg);
743 if (!srv_send_smb(xconn,
745 IS_CONN_ENCRYPTED(fsp->conn),
747 exit_server_cleanly("send_break_message_smb1: "
748 "srv_send_smb failed.");
752 /*******************************************************************
753 This handles the generic oplock break message from another smbd.
754 *******************************************************************/
756 static void process_oplock_break_message(struct messaging_context *msg_ctx,
759 struct server_id src,
762 struct share_mode_entry msg;
765 struct smbd_server_connection *sconn =
766 talloc_get_type_abort(private_data,
767 struct smbd_server_connection);
768 struct server_id self = messaging_server_id(sconn->msg_ctx);
769 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
772 bool break_needed = true;
773 struct server_id_buf tmp;
775 if (data->data == NULL) {
776 DEBUG(0, ("Got NULL buffer\n"));
780 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
781 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
785 /* De-linearize incoming message. */
786 message_to_share_mode_entry(&msg, (char *)data->data);
787 break_to = msg.op_type;
789 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
790 (unsigned)break_to, server_id_str_buf(src, &tmp),
791 file_id_string_tos(&msg.id),
792 (unsigned long long)msg.share_file_id));
794 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
797 /* We hit a race here. Break messages are sent, and before we
798 * get to process this message, we have closed the file. */
799 DEBUG(3, ("Did not find fsp\n"));
803 break_from = fsp_lease_type(fsp);
805 if (fsp->oplock_type != LEASE_OPLOCK) {
806 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
808 * Nothing to do anymore
810 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
811 fsp->sent_oplock_break));
816 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
817 DEBUG(10, ("client_caps without level2 oplocks\n"));
818 break_to &= ~SMB2_LEASE_READ;
821 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
822 if (use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
823 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
824 break_to &= ~SMB2_LEASE_READ;
827 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
828 DEBUG(10, ("no level2 oplocks by config\n"));
829 break_to &= ~SMB2_LEASE_READ;
832 if (fsp->oplock_type == LEASE_OPLOCK) {
833 struct share_mode_lock *lck;
836 lck = get_existing_share_mode_lock(
837 talloc_tos(), fsp->file_id);
840 * We hit a race here. Break messages are sent, and
841 * before we get to process this message, we have closed
844 DEBUG(3, ("Did not find share_mode\n"));
848 idx = find_share_mode_lease(
850 fsp_client_guid(fsp),
851 &fsp->lease->lease.lease_key);
853 struct share_mode_lease *l;
854 l = &lck->data->leases[idx];
856 break_from = l->current_state;
857 break_to &= l->current_state;
860 break_to &= l->breaking_to_required;
861 if (l->breaking_to_required != break_to) {
863 * Note we don't increment the epoch
864 * here, which might be a bug in
867 l->breaking_to_required = break_to;
868 lck->data->modified = true;
870 break_needed = false;
871 } else if (l->current_state == break_to) {
872 break_needed = false;
873 } else if (l->current_state == SMB2_LEASE_READ) {
874 l->current_state = SMB2_LEASE_NONE;
875 /* Need to increment the epoch */
877 lck->data->modified = true;
880 l->breaking_to_required = break_to;
881 l->breaking_to_requested = break_to;
882 /* Need to increment the epoch */
884 lck->data->modified = true;
887 /* Ensure we're in sync with current lease state. */
888 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
895 DEBUG(10,("%s: skip break\n", __func__));
899 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
900 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
901 file_id_string_tos(&fsp->file_id),
906 DEBUG(10, ("break_from=%u, break_to=%u\n",
907 (unsigned)break_from, (unsigned)break_to));
909 if ((break_from == break_to) && !break_needed) {
910 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
912 file_id_string_tos(&fsp->file_id),
917 /* Need to wait before sending a break
918 message if we sent ourselves this message. */
919 if (serverid_equal(&self, &src)) {
920 wait_before_sending_break();
923 if (sconn->using_smb2) {
924 send_break_message_smb2(fsp, break_from, break_to);
926 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
927 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
930 if ((break_from == SMB2_LEASE_READ) &&
931 (break_to == SMB2_LEASE_NONE)) {
933 * This is an async break without a reply and thus no timeout
935 * leases are handled above.
937 if (fsp->oplock_type != LEASE_OPLOCK) {
942 if (fsp->oplock_type == LEASE_OPLOCK) {
946 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
947 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
949 add_oplock_timeout_handler(fsp);
952 /*******************************************************************
953 This handles the kernel oplock break message.
954 *******************************************************************/
956 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
959 struct server_id src,
963 unsigned long file_id;
965 struct smbd_server_connection *sconn =
966 talloc_get_type_abort(private_data,
967 struct smbd_server_connection);
968 struct server_id_buf tmp;
970 if (data->data == NULL) {
971 DEBUG(0, ("Got NULL buffer\n"));
975 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
976 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
980 /* Pull the data from the message. */
981 pull_file_id_24((char *)data->data, &id);
982 file_id = (unsigned long)IVAL(data->data, 24);
984 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
985 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
986 (unsigned int)file_id));
988 fsp = initial_break_processing(sconn, id, file_id);
991 DEBUG(3, ("Got a kernel oplock break message for a file "
992 "I don't know about\n"));
996 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
997 /* This is ok, kernel oplocks come in completely async */
998 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1003 if (sconn->using_smb2) {
1004 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1006 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1009 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1011 add_oplock_timeout_handler(fsp);
1014 struct break_to_none_state {
1015 struct smbd_server_connection *sconn;
1017 struct smb2_lease_key lease_key;
1018 struct GUID client_guid;
1020 static void do_break_to_none(struct tevent_context *ctx,
1021 struct tevent_immediate *im,
1022 void *private_data);
1024 /****************************************************************************
1025 This function is called on any file modification or lock request. If a file
1026 is level 2 oplocked then it must tell all other level 2 holders to break to
1028 ****************************************************************************/
1030 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1031 enum level2_contention_type type)
1033 struct smbd_server_connection *sconn = fsp->conn->sconn;
1034 struct tevent_immediate *im;
1035 struct break_to_none_state *state;
1036 struct byte_range_lock *brl;
1037 uint32_t num_read_oplocks = 0;
1040 * If this file is level II oplocked then we need
1041 * to grab the shared memory lock and inform all
1042 * other files with a level II lock that they need
1043 * to flush their read caches. We keep the lock over
1044 * the shared memory area whilst doing this.
1047 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1049 * There can't be any level2 oplocks, we're alone.
1054 brl = brl_get_locks_readonly(fsp);
1056 num_read_oplocks = brl_num_read_oplocks(brl);
1059 DEBUG(10, ("num_read_oplocks = %"PRIu32"\n", num_read_oplocks));
1061 if (num_read_oplocks == 0) {
1062 DEBUG(10, ("No read oplocks around\n"));
1067 * When we get here we might have a brlock entry locked. Also
1068 * locking the share mode entry would violate the locking
1069 * order. Breaking level2 oplocks to none is asynchronous
1070 * anyway, so we postpone this into an immediate event.
1073 state = talloc_zero(sconn, struct break_to_none_state);
1074 if (state == NULL) {
1075 DEBUG(1, ("talloc failed\n"));
1078 state->sconn = sconn;
1079 state->id = fsp->file_id;
1081 if (fsp->oplock_type == LEASE_OPLOCK) {
1082 state->client_guid = *fsp_client_guid(fsp);
1083 state->lease_key = fsp->lease->lease.lease_key;
1084 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1085 state->lease_key.data[0],
1086 state->lease_key.data[1]));
1089 im = tevent_create_immediate(state);
1091 DEBUG(1, ("tevent_create_immediate failed\n"));
1095 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1098 static void send_break_to_none(struct messaging_context *msg_ctx,
1099 const struct share_mode_entry *e)
1101 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1103 share_mode_entry_to_message(msg, e);
1104 /* Overload entry->op_type */
1105 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1107 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1108 (uint8_t *)msg, sizeof(msg));
1111 static void do_break_to_none(struct tevent_context *ctx,
1112 struct tevent_immediate *im,
1115 struct break_to_none_state *state = talloc_get_type_abort(
1116 private_data, struct break_to_none_state);
1118 struct share_mode_lock *lck;
1119 struct share_mode_data *d;
1121 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1123 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1124 __func__, file_id_string_tos(&state->id)));
1130 * Walk leases and oplocks separately: We have to send one break per
1131 * lease. If we have multiple share_mode_entry having a common lease,
1132 * we would break the lease twice if we don't walk the leases list
1136 for (i=0; i<d->num_leases; i++) {
1137 struct share_mode_lease *l = &d->leases[i];
1138 struct share_mode_entry *e = NULL;
1141 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1144 if (smb2_lease_equal(&state->client_guid,
1148 DEBUG(10, ("Don't break our own lease\n"));
1152 for (j=0; j<d->num_share_modes; j++) {
1153 e = &d->share_modes[j];
1155 if (!is_valid_share_mode_entry(e)) {
1158 if (e->lease_idx == i) {
1162 if (j == d->num_share_modes) {
1163 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1168 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1169 "%"PRIu32"\n", i, j));
1171 send_break_to_none(state->sconn->msg_ctx, e);
1174 for(i = 0; i < d->num_share_modes; i++) {
1175 struct share_mode_entry *e = &d->share_modes[i];
1177 if (!is_valid_share_mode_entry(e)) {
1180 if (e->op_type == LEASE_OPLOCK) {
1182 * Took care of those in the loop above
1188 * As there could have been multiple writes waiting at the
1189 * lock_share_entry gate we may not be the first to
1190 * enter. Hence the state of the op_types in the share mode
1191 * entries may be partly NO_OPLOCK and partly LEVEL_II
1192 * oplock. It will do no harm to re-send break messages to
1193 * those smbd's that are still waiting their turn to remove
1194 * their LEVEL_II state, and also no harm to ignore existing
1195 * NO_OPLOCK states. JRA.
1198 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1201 if (e->op_type == NO_OPLOCK) {
1206 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1207 DEBUG(0,("%s: PANIC. "
1208 "share mode entry %d is an exclusive "
1209 "oplock !\n", __func__, i ));
1214 send_break_to_none(state->sconn->msg_ctx, e);
1217 /* We let the message receivers handle removing the oplock state
1218 in the share mode lock db. */
1226 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1227 enum level2_contention_type type)
1229 struct smbd_server_connection *sconn = fsp->conn->sconn;
1230 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1232 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
1233 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
1237 contend_level2_oplocks_begin_default(fsp, type);
1240 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1241 enum level2_contention_type type)
1243 struct smbd_server_connection *sconn = fsp->conn->sconn;
1244 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1246 /* Only kernel oplocks implement this so far */
1247 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
1248 koplocks->ops->contend_level2_oplocks_end(fsp, type);
1252 /****************************************************************************
1253 Linearize a share mode entry struct to an internal oplock break message.
1254 ****************************************************************************/
1256 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
1258 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1259 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1260 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1261 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1262 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1263 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1264 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1265 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1266 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
1267 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1268 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1269 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1270 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1271 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1274 /****************************************************************************
1275 De-linearize an internal oplock break message to a share mode entry struct.
1276 ****************************************************************************/
1278 void message_to_share_mode_entry(struct share_mode_entry *e, const char *msg)
1280 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1281 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1282 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1283 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1284 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1285 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1286 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1287 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1288 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
1289 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1290 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1291 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1292 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1293 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1296 /****************************************************************************
1297 Setup oplocks for this process.
1298 ****************************************************************************/
1300 bool init_oplocks(struct smbd_server_connection *sconn)
1302 DEBUG(3,("init_oplocks: initializing messages.\n"));
1304 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1305 process_oplock_break_message);
1306 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1307 process_kernel_oplock_break);
1311 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1313 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1315 /* only initialize once */
1316 if (koplocks == NULL) {
1317 #if HAVE_KERNEL_OPLOCKS_IRIX
1318 koplocks = irix_init_kernel_oplocks(sconn);
1319 #elif HAVE_KERNEL_OPLOCKS_LINUX
1320 koplocks = linux_init_kernel_oplocks(sconn);
1322 sconn->oplocks.kernel_ops = koplocks;